2023-09-10 17:56:37 +00:00
|
|
|
import { testEventsAdapter } from "./UI/_setup.test.ts";
|
2023-09-12 14:51:27 +00:00
|
|
|
import { Database_Contextual_View } from "./database.ts";
|
2023-09-07 14:58:53 +00:00
|
|
|
import { prepareNormalNostrEvent } from "./lib/nostr-ts/event.ts";
|
|
|
|
import { PrivateKey } from "./lib/nostr-ts/key.ts";
|
2023-09-16 20:04:48 +00:00
|
|
|
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";
|
2023-09-07 14:58:53 +00:00
|
|
|
|
|
|
|
const ctx = InMemoryAccountContext.New(PrivateKey.Generate());
|
2023-09-08 13:09:14 +00:00
|
|
|
|
|
|
|
Deno.test("Database", async () => {
|
2023-09-10 17:56:37 +00:00
|
|
|
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
|
|
|
|
2023-09-08 18:35:46 +00:00
|
|
|
const stream = db.subscribe();
|
2023-09-16 20:04:48 +00:00
|
|
|
const event_to_add = await prepareNormalNostrEvent(ctx, NostrKind.TEXT_NOTE, [], "1");
|
2023-09-08 18:35:46 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}),
|
2023-09-08 18:35:46 +00:00
|
|
|
[event_to_add],
|
2023-09-08 13:09:14 +00:00
|
|
|
);
|
2023-09-08 18:35:46 +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
|
|
|
|
|
2023-09-16 20:04:48 +00:00
|
|
|
const event_to_add2 = await prepareNormalNostrEvent(ctx, NostrKind.TEXT_NOTE, [], "2");
|
2023-09-08 18:35:46 +00:00
|
|
|
// 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
|
|
|
});
|