blowater/app/UI/_setup.test.ts

79 lines
2.4 KiB
TypeScript
Raw Normal View History

import { Database_View, EventMark, EventMarker, EventsAdapter, Indices, RelayRecorder } from "../database.ts";
import { EventBus } from "../event-bus.ts";
2024-08-29 08:49:13 +00:00
import { NostrAccountContext, NostrEvent, NostrKind, prepareNostrEvent } from "@blowater/nostr-sdk";
import { UI_Interaction_Event } from "./app_update.tsx";
import { Profile_Nostr_Event } from "../nostr.ts";
import { ProfileData } from "../features/profile.ts";
export const testEventBus = new EventBus<UI_Interaction_Event>();
2023-11-17 07:06:11 +00:00
export async function test_db_view() {
const data = new Map();
const testEventsAdapter: EventsAdapter = {
2024-06-17 07:42:51 +00:00
filter: async () => {
const events = [];
2024-06-17 07:42:51 +00:00
for (const v of data.values()) {
events.push(v);
}
return events;
},
get: async (keys: Indices) => {
return data.get(keys.id);
},
put: async (e: NostrEvent) => {
data.set(e.id, e);
},
};
const relays = new Map<string, Set<string>>();
const testRelayRecorder: RelayRecorder = {
setRelayRecord: async (eventID: string, url: string) => {
const records = relays.get(eventID);
if (records) {
records.add(url);
} else {
relays.set(eventID, new Set([url]));
}
},
getAllRelayRecords: async () => {
return relays;
},
};
2023-11-17 07:06:11 +00:00
const marks = new Map<string, EventMark>();
const testEventMarker: EventMarker = {
getMark: async (eventID: string) => {
return marks.get(eventID);
},
async markEvent(eventID: string, reason: "removed") {
marks.set(eventID, {
event_id: eventID,
reason: reason,
});
},
async getAllMarks() {
return Array.from(marks.values());
},
};
return await Database_View.New(testEventsAdapter, testRelayRecorder, testEventMarker);
}
export const prepareProfileEvent = async (
author: NostrAccountContext,
profile: ProfileData,
) => {
2024-08-29 08:49:13 +00:00
const profileEvent = await prepareNostrEvent(author, {
kind: NostrKind.META_DATA,
content: JSON.stringify(profile),
}) as NostrEvent<NostrKind.META_DATA>;
return {
...profileEvent,
profile,
publicKey: author.publicKey,
parsedTags: {
p: [],
e: [],
},
} as Profile_Nostr_Event;
};