dfklfg/packages/system/tests/note-collection.test.ts

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-08-17 18:54:14 +00:00
import { TaggedNostrEvent } from "../src/nostr";
2023-05-19 14:15:13 +00:00
import { describe, expect } from "@jest/globals";
2023-07-23 22:19:26 +00:00
import { FlatNoteStore, ReplaceableNoteStore } from "../src/note-collection";
2023-03-28 14:34:01 +00:00
describe("NoteStore", () => {
describe("flat", () => {
test("one event", () => {
2023-08-17 18:54:14 +00:00
const ev = { id: "one" } as TaggedNostrEvent;
2023-03-28 14:34:01 +00:00
const c = new FlatNoteStore();
c.add(ev);
expect(c.getSnapshotData()).toEqual([ev]);
});
test("still one event", () => {
2023-08-17 18:54:14 +00:00
const ev = { id: "one" } as TaggedNostrEvent;
2023-03-28 14:34:01 +00:00
const c = new FlatNoteStore();
c.add(ev);
c.add(ev);
expect(c.getSnapshotData()).toEqual([ev]);
});
test("clears", () => {
2023-08-17 18:54:14 +00:00
const ev = { id: "one" } as TaggedNostrEvent;
2023-03-28 14:34:01 +00:00
const c = new FlatNoteStore();
c.add(ev);
expect(c.getSnapshotData()).toEqual([ev]);
c.clear();
expect(c.getSnapshotData()).toEqual([]);
});
});
describe("replacable", () => {
test("one event", () => {
2023-08-17 18:54:14 +00:00
const ev = { id: "test", created_at: 69 } as TaggedNostrEvent;
2023-03-28 14:34:01 +00:00
const c = new ReplaceableNoteStore();
c.add(ev);
expect(c.getSnapshotData()).toEqual(ev);
});
test("dont replace with older", () => {
2023-08-17 18:54:14 +00:00
const ev = { id: "test", created_at: 69 } as TaggedNostrEvent;
const evOlder = { id: "test2", created_at: 68 } as TaggedNostrEvent;
2023-03-28 14:34:01 +00:00
const c = new ReplaceableNoteStore();
c.add(ev);
c.add(evOlder);
expect(c.getSnapshotData()).toEqual(ev);
});
test("replace with newer", () => {
2023-08-17 18:54:14 +00:00
const ev = { id: "test", created_at: 69 } as TaggedNostrEvent;
const evNewer = { id: "test2", created_at: 70 } as TaggedNostrEvent;
2023-03-28 14:34:01 +00:00
const c = new ReplaceableNoteStore();
c.add(ev);
c.add(evNewer);
expect(c.getSnapshotData()).toEqual(evNewer);
});
});
});