blowater/UI/message-panel.test.tsx

103 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-06-30 14:05:57 +00:00
/** @jsx h */
import { h, render } from "https://esm.sh/preact@10.17.1";
2023-06-30 14:05:57 +00:00
import { MessagePanel } from "./message-panel.tsx";
2023-09-14 12:02:56 +00:00
import { InvalidKey, PrivateKey } from "../lib/nostr-ts/key.ts";
2023-09-11 15:27:38 +00:00
import { InMemoryAccountContext, NostrKind } from "../lib/nostr-ts/nostr.ts";
import { Datebase_View } from "../database.ts";
2023-11-17 07:06:11 +00:00
import { testEventBus, testEventMarker, testEventsAdapter, testRelayAdapter } from "./_setup.test.ts";
2023-09-11 15:27:38 +00:00
import { prepareNormalNostrEvent } from "../lib/nostr-ts/event.ts";
2023-10-15 22:39:21 +00:00
import { DM_List } from "./conversation-list.ts";
2023-09-11 15:27:38 +00:00
import { EventSyncer } from "./event_syncer.ts";
2023-11-03 13:09:13 +00:00
import { ConnectionPool } from "../lib/nostr-ts/relay-pool.ts";
2023-10-01 20:59:07 +00:00
import { ProfileSyncer } from "../features/profile.ts";
import { handle_SendMessage } from "./app_update.tsx";
import { LamportTime } from "../time.ts";
import { initialModel } from "./app_model.ts";
import { relays } from "../lib/nostr-ts/relay-list.test.ts";
2023-09-14 12:02:56 +00:00
import { fail } from "https://deno.land/std@0.176.0/testing/asserts.ts";
import { GroupChatSyncer, GroupMessageController } from "../features/gm.ts";
2023-09-11 15:27:38 +00:00
const ctx = InMemoryAccountContext.New(PrivateKey.Generate());
const database = await test_db_view();
const lamport = new LamportTime(0);
2023-09-11 15:27:38 +00:00
await database.addEvent(
await prepareNormalNostrEvent(ctx, {
content: "hi",
kind: NostrKind.TEXT_NOTE,
}),
);
await database.addEvent(
await prepareNormalNostrEvent(ctx, {
content: "hi 2",
kind: NostrKind.TEXT_NOTE,
}),
);
await database.addEvent(
await prepareNormalNostrEvent(ctx, {
content: "hi 3",
kind: NostrKind.TEXT_NOTE,
}),
);
2023-09-11 15:27:38 +00:00
const pool = new ConnectionPool();
const model = initialModel();
pool.addRelayURL(relays[0]);
const groupMessageController = new GroupMessageController(
ctx,
new GroupChatSyncer(database, pool),
new ProfileSyncer(database, pool),
);
2023-06-30 14:05:57 +00:00
2023-10-21 11:29:47 +00:00
const editor = model.dmEditors.get(ctx.publicKey.hex);
2023-10-04 21:34:43 +00:00
const view = () => {
2023-10-04 21:34:43 +00:00
if (editor == undefined) {
return undefined;
}
return (
<MessagePanel
profileGetter={database}
2023-10-04 21:34:43 +00:00
/**
* If we use a map to store all editor models,
* need to distinguish editor models for DMs and GMs
*/
editorModel={editor}
eventSyncer={new EventSyncer(pool, database)}
focusedContent={undefined}
myPublicKey={ctx.publicKey}
2023-10-01 20:59:07 +00:00
profilesSyncer={new ProfileSyncer(database, pool)}
2023-09-14 12:02:56 +00:00
emit={testEventBus.emit}
2023-10-04 21:34:43 +00:00
messages={[]}
rightPanelModel={{
show: true,
}}
isGroupChat={false}
/>
);
};
2023-06-30 14:05:57 +00:00
render(view(), document.body);
for await (const e of testEventBus.onChange()) {
console.log(e);
if (e.type == "SendMessage") {
const err = await handle_SendMessage(
e,
ctx,
lamport,
pool,
2023-10-21 11:29:47 +00:00
model.dmEditors,
model.gmEditors,
database,
groupMessageController,
);
if (err instanceof Error) {
console.error("update:SendMessage", err);
continue; // todo: global error toast
}
} else if (e.type == "UpdateEditorText") {
}
render(view(), document.body);
}