forked from adamritter/nostr-relaypool-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-demultiplexer-test.ts
128 lines (119 loc) · 3.54 KB
/
event-demultiplexer-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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-env jest */
import {Filter, matchFilter} from "nostr-tools";
import {EventDemultiplexer} from "./event-demultiplexer";
import {Event, NostrToolsEventWithId} from "./event";
import {RelayPool} from "./relay-pool";
let eventFrom = (event: NostrToolsEventWithId) =>
new Event(event, new RelayPool(), []);
describe("EventDemultiplexer", () => {
let demultiplexer: EventDemultiplexer;
beforeEach(() => {
demultiplexer = new EventDemultiplexer();
});
test("subscribe method should add filter and OnEvent to filterAndOnEventByEvent map", () => {
const filters = [{ids: ["123"]}];
const onEvent = jest.fn();
demultiplexer.subscribe(filters, onEvent);
expect(demultiplexer.filterAndOnEventByEvent.get("ids:123")).toEqual([
[filters[0], onEvent],
]);
});
test("onEvent method should call OnEvent callback for event that matches subscribed filter", () => {
const filters = [{ids: ["123"]}];
const onEvent = jest.fn();
demultiplexer.subscribe(filters, onEvent);
const event: Event = eventFrom({
id: "123",
kind: 1,
pubkey: "abc",
tags: [],
content: "",
created_at: 0,
});
demultiplexer.onEvent(event, true, "https://example.com");
expect(onEvent).toHaveBeenCalledWith(event, true, "https://example.com");
});
test("onEvent method should not call OnEvent callback for event that does not match any subscribed filters", () => {
const filters = [{ids: ["456"]}];
const onEvent = jest.fn();
demultiplexer.subscribe(filters, onEvent);
const event: Event = eventFrom({
id: "123",
kind: 1,
pubkey: "abc",
tags: [],
content: "",
created_at: 0,
});
demultiplexer.onEvent(event, true, "https://example.com");
expect(onEvent).not.toHaveBeenCalled();
});
test("subscribe method should handle edge cases", () => {
const filters = [{ids: [""]}];
const onEvent = jest.fn();
demultiplexer.subscribe(filters, onEvent);
const event: Event = eventFrom({
id: "",
kind: 0,
pubkey: "",
tags: [],
content: "",
created_at: 0,
});
demultiplexer.onEvent(event, true, "");
expect(onEvent).toHaveBeenCalledWith(event, true, "");
});
test.skip("many filters", () => {
const time = new Date().getTime();
let counter = 0;
const onEvent = () => {
counter++;
};
for (let i = 0; i < 2000; i++) {
demultiplexer.subscribe([{ids: ["" + i * 3]}], onEvent);
}
const event: Event = eventFrom({
id: "123",
kind: 1,
pubkey: "abc",
tags: [],
content: "",
created_at: 0,
});
for (let i = 0; i < 2000; i++) {
demultiplexer.onEvent(
eventFrom({...event, id: "" + i * 2}),
true,
"https://example.com"
);
}
expect(new Date().getTime() - time).toBeLessThan(5);
expect(counter).toBe(667);
});
test.skip("many filters using matchFilter", () => {
const time = new Date().getTime();
let counter = 0;
const filters: Filter[] = [];
for (let i = 0; i < 2000; i++) {
filters.push({ids: ["" + i * 3]});
}
const event: Event = eventFrom({
id: "123",
kind: 1,
pubkey: "abc",
tags: [],
content: "",
created_at: 0,
});
for (let i = 0; i < 2000; i++) {
const e = {...event, id: "" + i * 2};
for (const filter of filters) {
if (matchFilter(filter, e)) {
counter++;
}
}
}
expect(new Date().getTime() - time).toBeGreaterThan(20);
expect(counter).toBe(667);
});
});