-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathevent-cache-test.ts
104 lines (90 loc) · 3.33 KB
/
event-cache-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* eslint-env jest */
import {Kind, type Event} from "nostr-tools";
import {EventCache} from "./event-cache";
describe("EventCache", () => {
let eventCache: EventCache;
let event: Event;
beforeEach(() => {
eventCache = new EventCache();
event = {
id: "1",
pubkey: "pk",
kind: Kind.Metadata,
created_at: 0,
tags: [],
content: "",
sig: "",
};
});
test("addEvent should add event to eventsById", () => {
eventCache.addEvent(event);
expect(eventCache.eventsById.get(event.id)).toBe(event);
});
test("addEvent should add event to metadataByPubKey if event is metadata", () => {
eventCache.addEvent(event);
expect(eventCache.metadataByPubKey.get(event.pubkey)).toBe(event);
});
test("addEvent should add event to contactsByPubKey if event is contact", () => {
event.kind = Kind.Contacts;
eventCache.addEvent(event);
expect(eventCache.contactsByPubKey.get(event.pubkey)).toBe(event);
});
test("getEventById should return event for given id", () => {
eventCache.addEvent(event);
expect(eventCache.getEventById(event.id)).toBe(event);
});
test("hasEventById should return true if event with given id exists", () => {
eventCache.addEvent(event);
expect(eventCache.hasEventById(event.id)).toBe(true);
});
test("getCachedEventsByPubKeyWithUpdatedFilter should return events and filter if all conditions met", () => {
eventCache.addEvent(event);
const filter = {
authors: [event.pubkey],
kinds: [Kind.Metadata],
noCache: false,
};
const result = eventCache.getCachedEventsWithUpdatedFilters([filter], []);
expect(result).toEqual({
filters: [filter],
events: [event],
});
});
test("getCachedEventsByPubKeyWithUpdatedFilter should return undefined if noCache is true", () => {
const filter = {
authors: [event.pubkey],
kinds: [Kind.Metadata],
noCache: true,
};
const result = eventCache.getCachedEventsWithUpdatedFilters([filter], []);
expect(result).toStrictEqual({filters: [filter], events: []});
});
test("getCachedEventsByPubKeyWithUpdatedFilter should return undefined if authors not specified", () => {
const filter = {kinds: [Kind.Metadata], noCache: false};
const result = eventCache.getCachedEventsWithUpdatedFilters([filter], []);
expect(result).toStrictEqual({filters: [filter], events: []});
});
// test('getCachedEventsByPubKeyWithUpdatedFilter should return undefined if kinds not specified', () => {
// const filter = { authors: [event.pubkey], noCache: false };
// const result = eventCache.#getCachedEventsByPubKey
test("getCachedEventsByPubKeyWithUpdatedFilter should return events by pubkey", () => {
eventCache.addEvent(event);
const filter = {
authors: ["pk"],
kinds: [Kind.Metadata, Kind.Contacts],
noCache: false,
};
const result = eventCache.getCachedEventsWithUpdatedFilters([filter], []);
expect(result).toEqual({events: [event], filters: [filter]});
});
test("tags", () => {
event.tags = [["p", "pk2"]];
eventCache.addEvent(event);
const filter = {
"#p": ["pk2"],
};
// console.log("tags", eventCache.eventsByTags);
const result = eventCache.getCachedEventsWithUpdatedFilters([filter], []);
expect(result).toEqual({events: [event], filters: [filter]});
});
});