blowater/database.test.ts

66 lines
2.1 KiB
TypeScript
Raw Normal View History

import { testEventsAdapter } from "./UI/_setup.test.ts";
2023-09-12 14:51:27 +00:00
import { Database_Contextual_View } from "./database.ts";
import { prepareNormalNostrEvent } from "./lib/nostr-ts/event.ts";
import { PrivateKey } from "./lib/nostr-ts/key.ts";
import { InMemoryAccountContext, NostrEvent, NostrKind } from "./lib/nostr-ts/nostr.ts";
2023-09-12 14:51:27 +00:00
import { assertEquals, fail } from "https://deno.land/std@0.176.0/testing/asserts.ts";
const ctx = InMemoryAccountContext.New(PrivateKey.Generate());
2023-09-08 13:09:14 +00:00
Deno.test("Database", async () => {
const db = await Database_Contextual_View.New(testEventsAdapter, ctx);
2023-09-12 14:51:27 +00:00
if (db instanceof Error) fail(db.message);
2023-09-08 13:09:14 +00:00
const stream = db.subscribe();
const event_to_add = await prepareNormalNostrEvent(ctx, NostrKind.TEXT_NOTE, [], "1");
await db.addEvent(event_to_add);
2023-09-08 13:09:14 +00:00
assertEquals(
db.events.map((e): NostrEvent => {
return {
content: e.content,
created_at: e.created_at,
id: e.id,
kind: e.kind,
pubkey: e.pubkey,
sig: e.sig,
tags: e.tags,
};
}),
[event_to_add],
2023-09-08 13:09:14 +00:00
);
const e = await stream.pop() as NostrEvent;
assertEquals(
{
content: e.content,
created_at: e.created_at,
id: e.id,
kind: e.kind,
pubkey: e.pubkey,
sig: e.sig,
tags: e.tags,
},
event_to_add,
);
const stream2 = db.subscribe();
await db.addEvent(event_to_add); // add a duplicated event
assertEquals(db.events.length, 1); // no changes
const event_to_add2 = await prepareNormalNostrEvent(ctx, NostrKind.TEXT_NOTE, [], "2");
// console.log(event_to_add2.id, event_to_add.id)
await db.addEvent(event_to_add2);
const e2 = await stream.pop() as NostrEvent;
assertEquals(e2, await stream2.pop() as NostrEvent);
assertEquals({
content: e2.content,
created_at: e2.created_at,
id: e2.id,
kind: e2.kind,
pubkey: e2.pubkey,
sig: e2.sig,
tags: e2.tags,
}, event_to_add2);
2023-09-08 13:09:14 +00:00
});