openclaw-nostr 2026.7.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +183 -0
- package/README.md +845 -0
- package/api.ts +6 -0
- package/index.ts +69 -0
- package/node_modules/cascadia-ts/cascadia.ts +1187 -0
- package/node_modules/cascadia-ts/dist/cascadia.d.ts +607 -0
- package/node_modules/cascadia-ts/dist/cascadia.js +705 -0
- package/node_modules/cascadia-ts/package.json +21 -0
- package/openclaw.plugin.json +153 -0
- package/package.json +81 -0
- package/runtime-api.ts +12 -0
- package/setup-api.ts +1 -0
- package/setup-entry.ts +4 -0
- package/src/channel-actions.test.ts +304 -0
- package/src/channel-actions.ts +199 -0
- package/src/channel.dm-access.test.ts +387 -0
- package/src/channel.inbound.test.ts +476 -0
- package/src/channel.lifecycle.test.ts +768 -0
- package/src/channel.outbound.test.ts +1143 -0
- package/src/channel.test.ts +175 -0
- package/src/channel.ts +2827 -0
- package/src/config-schema.test.ts +486 -0
- package/src/config-schema.ts +356 -0
- package/src/default-relays.ts +1 -0
- package/src/fleet-agent.test.ts +423 -0
- package/src/fleet-agent.ts +549 -0
- package/src/metrics.ts +466 -0
- package/src/nip46-cutover.test.ts +324 -0
- package/src/nip46-cutover.ts +461 -0
- package/src/nip46-doctor.test.ts +191 -0
- package/src/nip46-doctor.ts +329 -0
- package/src/nip46-enroll.test.ts +346 -0
- package/src/nip46-enroll.ts +567 -0
- package/src/nip46-signer.test.ts +809 -0
- package/src/nip46-signer.ts +1263 -0
- package/src/nip46-status.test.ts +186 -0
- package/src/nip46-status.ts +250 -0
- package/src/nip51-lists.test.ts +404 -0
- package/src/nip51-lists.ts +777 -0
- package/src/nostr-access.test.ts +175 -0
- package/src/nostr-access.ts +134 -0
- package/src/nostr-bus-nip29.ts +998 -0
- package/src/nostr-bus-relay.ts +477 -0
- package/src/nostr-bus-types.ts +392 -0
- package/src/nostr-bus.fuzz.test.ts +533 -0
- package/src/nostr-bus.integration.test.ts +505 -0
- package/src/nostr-bus.protocol.test.ts +2057 -0
- package/src/nostr-bus.test.ts +235 -0
- package/src/nostr-bus.ts +1868 -0
- package/src/nostr-capabilities-extended.test.ts +612 -0
- package/src/nostr-capabilities.ts +719 -0
- package/src/nostr-discovery.test.ts +568 -0
- package/src/nostr-discovery.ts +336 -0
- package/src/nostr-extras.test.ts +88 -0
- package/src/nostr-extras.ts +136 -0
- package/src/nostr-profile-http.test.ts +662 -0
- package/src/nostr-profile-http.ts +836 -0
- package/src/nostr-profile-import.test.ts +274 -0
- package/src/nostr-profile-import.ts +295 -0
- package/src/nostr-profile.fuzz.test.ts +480 -0
- package/src/nostr-profile.test.ts +410 -0
- package/src/nostr-profile.ts +320 -0
- package/src/nostr-pubkey.ts +56 -0
- package/src/nostr-state-store.test.ts +790 -0
- package/src/nostr-state-store.ts +1284 -0
- package/src/runtime.ts +9 -0
- package/src/seen-tracker.test.ts +25 -0
- package/src/seen-tracker.ts +292 -0
- package/src/session-route.ts +25 -0
- package/src/setup-surface.ts +320 -0
- package/src/types.test.ts +783 -0
- package/src/types.ts +508 -0
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createMetrics, type MetricName } from "./metrics.js";
|
|
3
|
+
import { validatePrivateKey, isValidPubkey, normalizePubkey } from "./nostr-bus.js";
|
|
4
|
+
import { createSeenTracker } from "./seen-tracker.js";
|
|
5
|
+
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// Fuzz Tests for validatePrivateKey
|
|
8
|
+
// ============================================================================
|
|
9
|
+
|
|
10
|
+
describe("validatePrivateKey fuzz", () => {
|
|
11
|
+
describe("type confusion", () => {
|
|
12
|
+
it("rejects null input", () => {
|
|
13
|
+
expect(() => validatePrivateKey(null as unknown as string)).toThrow();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("rejects undefined input", () => {
|
|
17
|
+
expect(() => validatePrivateKey(undefined as unknown as string)).toThrow();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("rejects number input", () => {
|
|
21
|
+
expect(() => validatePrivateKey(123 as unknown as string)).toThrow();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("rejects boolean input", () => {
|
|
25
|
+
expect(() => validatePrivateKey(true as unknown as string)).toThrow();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("rejects object input", () => {
|
|
29
|
+
expect(() => validatePrivateKey({} as unknown as string)).toThrow();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("rejects array input", () => {
|
|
33
|
+
expect(() => validatePrivateKey([] as unknown as string)).toThrow();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("rejects function input", () => {
|
|
37
|
+
expect(() => validatePrivateKey((() => {}) as unknown as string)).toThrow();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("unicode attacks", () => {
|
|
42
|
+
it("rejects unicode lookalike characters", () => {
|
|
43
|
+
// Using zero-width characters
|
|
44
|
+
const withZeroWidth =
|
|
45
|
+
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\u200Bf";
|
|
46
|
+
expect(() => validatePrivateKey(withZeroWidth)).toThrow();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("rejects RTL override", () => {
|
|
50
|
+
const withRtl = "\u202E0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
|
51
|
+
expect(() => validatePrivateKey(withRtl)).toThrow();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("rejects homoglyph 'a' (Cyrillic а)", () => {
|
|
55
|
+
// Using Cyrillic 'а' (U+0430) instead of Latin 'a'
|
|
56
|
+
const withCyrillicA = "0123456789\u0430bcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
|
57
|
+
expect(() => validatePrivateKey(withCyrillicA)).toThrow();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("rejects emoji", () => {
|
|
61
|
+
const withEmoji = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789ab😀";
|
|
62
|
+
expect(() => validatePrivateKey(withEmoji)).toThrow();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("rejects combining characters", () => {
|
|
66
|
+
// 'a' followed by combining acute accent
|
|
67
|
+
const withCombining = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\u0301";
|
|
68
|
+
expect(() => validatePrivateKey(withCombining)).toThrow();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe("injection attempts", () => {
|
|
73
|
+
it("rejects null byte injection", () => {
|
|
74
|
+
const withNullByte = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\x00f";
|
|
75
|
+
expect(() => validatePrivateKey(withNullByte)).toThrow();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("rejects newline injection", () => {
|
|
79
|
+
const withNewline = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\nf";
|
|
80
|
+
expect(() => validatePrivateKey(withNewline)).toThrow();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("rejects carriage return injection", () => {
|
|
84
|
+
const withCR = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\rf";
|
|
85
|
+
expect(() => validatePrivateKey(withCR)).toThrow();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("rejects tab injection", () => {
|
|
89
|
+
const withTab = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\tf";
|
|
90
|
+
expect(() => validatePrivateKey(withTab)).toThrow();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("rejects form feed injection", () => {
|
|
94
|
+
const withFormFeed = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde\ff";
|
|
95
|
+
expect(() => validatePrivateKey(withFormFeed)).toThrow();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("edge cases", () => {
|
|
100
|
+
it("rejects very long string", () => {
|
|
101
|
+
const veryLong = "a".repeat(10000);
|
|
102
|
+
expect(() => validatePrivateKey(veryLong)).toThrow();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("rejects string of spaces matching length", () => {
|
|
106
|
+
const spaces = " ".repeat(64);
|
|
107
|
+
expect(() => validatePrivateKey(spaces)).toThrow();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("rejects hex with spaces between characters", () => {
|
|
111
|
+
const withSpaces =
|
|
112
|
+
"01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef";
|
|
113
|
+
expect(() => validatePrivateKey(withSpaces)).toThrow();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("nsec format edge cases", () => {
|
|
118
|
+
it("rejects nsec with invalid bech32 characters", () => {
|
|
119
|
+
// 'b', 'i', 'o' are not valid bech32 characters
|
|
120
|
+
const invalidBech32 = "nsec1qypqxpq9qtpqscx7peytbfwtdjmcv0mrz5rjpej8vjppfkqfqy8skqfv3l";
|
|
121
|
+
expect(() => validatePrivateKey(invalidBech32)).toThrow();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("rejects nsec with wrong prefix", () => {
|
|
125
|
+
expect(() => validatePrivateKey("nsec0aaaa")).toThrow();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("rejects partial nsec", () => {
|
|
129
|
+
expect(() => validatePrivateKey("nsec1")).toThrow();
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// ============================================================================
|
|
135
|
+
// Fuzz Tests for isValidPubkey
|
|
136
|
+
// ============================================================================
|
|
137
|
+
|
|
138
|
+
describe("isValidPubkey fuzz", () => {
|
|
139
|
+
describe("type confusion", () => {
|
|
140
|
+
it("handles null gracefully", () => {
|
|
141
|
+
expect(isValidPubkey(null as unknown as string)).toBe(false);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("handles undefined gracefully", () => {
|
|
145
|
+
expect(isValidPubkey(undefined as unknown as string)).toBe(false);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("handles number gracefully", () => {
|
|
149
|
+
expect(isValidPubkey(123 as unknown as string)).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("handles object gracefully", () => {
|
|
153
|
+
expect(isValidPubkey({} as unknown as string)).toBe(false);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("malicious inputs", () => {
|
|
158
|
+
it("rejects __proto__ key", () => {
|
|
159
|
+
expect(isValidPubkey("__proto__")).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("rejects constructor key", () => {
|
|
163
|
+
expect(isValidPubkey("constructor")).toBe(false);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("rejects toString key", () => {
|
|
167
|
+
expect(isValidPubkey("toString")).toBe(false);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// ============================================================================
|
|
173
|
+
// Fuzz Tests for normalizePubkey
|
|
174
|
+
// ============================================================================
|
|
175
|
+
|
|
176
|
+
describe("normalizePubkey fuzz", () => {
|
|
177
|
+
describe("prototype pollution attempts", () => {
|
|
178
|
+
it("throws for __proto__", () => {
|
|
179
|
+
expect(() => normalizePubkey("__proto__")).toThrow();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("throws for constructor", () => {
|
|
183
|
+
expect(() => normalizePubkey("constructor")).toThrow();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("throws for prototype", () => {
|
|
187
|
+
expect(() => normalizePubkey("prototype")).toThrow();
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe("case sensitivity", () => {
|
|
192
|
+
it("normalizes uppercase to lowercase", () => {
|
|
193
|
+
const upper = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
|
|
194
|
+
const lower = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
|
195
|
+
expect(normalizePubkey(upper)).toBe(lower);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("normalizes mixed case to lowercase", () => {
|
|
199
|
+
const mixed = "0123456789AbCdEf0123456789AbCdEf0123456789AbCdEf0123456789AbCdEf";
|
|
200
|
+
const lower = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
|
201
|
+
expect(normalizePubkey(mixed)).toBe(lower);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// ============================================================================
|
|
207
|
+
// Fuzz Tests for SeenTracker
|
|
208
|
+
// ============================================================================
|
|
209
|
+
|
|
210
|
+
describe("SeenTracker fuzz", () => {
|
|
211
|
+
describe("malformed IDs", () => {
|
|
212
|
+
it("handles empty string IDs", () => {
|
|
213
|
+
const tracker = createSeenTracker({ maxEntries: 100 });
|
|
214
|
+
expect(() => tracker.add("")).not.toThrow();
|
|
215
|
+
expect(tracker.peek("")).toBe(true);
|
|
216
|
+
tracker.stop();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("handles very long IDs", () => {
|
|
220
|
+
const tracker = createSeenTracker({ maxEntries: 100 });
|
|
221
|
+
const longId = "a".repeat(100000);
|
|
222
|
+
expect(() => tracker.add(longId)).not.toThrow();
|
|
223
|
+
expect(tracker.peek(longId)).toBe(true);
|
|
224
|
+
tracker.stop();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("handles unicode IDs", () => {
|
|
228
|
+
const tracker = createSeenTracker({ maxEntries: 100 });
|
|
229
|
+
const unicodeId = "事件ID_🎉_тест";
|
|
230
|
+
expect(() => tracker.add(unicodeId)).not.toThrow();
|
|
231
|
+
expect(tracker.peek(unicodeId)).toBe(true);
|
|
232
|
+
tracker.stop();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("handles IDs with null bytes", () => {
|
|
236
|
+
const tracker = createSeenTracker({ maxEntries: 100 });
|
|
237
|
+
const idWithNull = "event\x00id";
|
|
238
|
+
expect(() => tracker.add(idWithNull)).not.toThrow();
|
|
239
|
+
expect(tracker.peek(idWithNull)).toBe(true);
|
|
240
|
+
tracker.stop();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("handles prototype property names as IDs", () => {
|
|
244
|
+
const tracker = createSeenTracker({ maxEntries: 100 });
|
|
245
|
+
|
|
246
|
+
// These should not affect the tracker's internal operation
|
|
247
|
+
expect(() => tracker.add("__proto__")).not.toThrow();
|
|
248
|
+
expect(() => tracker.add("constructor")).not.toThrow();
|
|
249
|
+
expect(() => tracker.add("toString")).not.toThrow();
|
|
250
|
+
expect(() => tracker.add("hasOwnProperty")).not.toThrow();
|
|
251
|
+
|
|
252
|
+
expect(tracker.peek("__proto__")).toBe(true);
|
|
253
|
+
expect(tracker.peek("constructor")).toBe(true);
|
|
254
|
+
expect(tracker.peek("toString")).toBe(true);
|
|
255
|
+
expect(tracker.peek("hasOwnProperty")).toBe(true);
|
|
256
|
+
|
|
257
|
+
tracker.stop();
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
describe("rapid operations", () => {
|
|
262
|
+
it("handles rapid add/check cycles", () => {
|
|
263
|
+
const tracker = createSeenTracker({ maxEntries: 1000 });
|
|
264
|
+
|
|
265
|
+
for (let i = 0; i < 10000; i++) {
|
|
266
|
+
const id = `event-${i}`;
|
|
267
|
+
tracker.add(id);
|
|
268
|
+
// Recently added should be findable
|
|
269
|
+
if (i < 1000) {
|
|
270
|
+
tracker.peek(id);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Size should be capped at maxEntries
|
|
275
|
+
expect(tracker.size()).toBeLessThanOrEqual(1000);
|
|
276
|
+
tracker.stop();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("handles concurrent-style operations", () => {
|
|
280
|
+
const tracker = createSeenTracker({ maxEntries: 100 });
|
|
281
|
+
|
|
282
|
+
// Simulate interleaved operations
|
|
283
|
+
for (let i = 0; i < 100; i++) {
|
|
284
|
+
tracker.add(`add-${i}`);
|
|
285
|
+
tracker.peek(`peek-${i}`);
|
|
286
|
+
tracker.has(`has-${i}`);
|
|
287
|
+
if (i % 10 === 0) {
|
|
288
|
+
tracker.delete(`add-${i - 5}`);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
expect(() => tracker.size()).not.toThrow();
|
|
293
|
+
tracker.stop();
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
describe("seed edge cases", () => {
|
|
298
|
+
it("handles empty seed array", () => {
|
|
299
|
+
const tracker = createSeenTracker({ maxEntries: 100 });
|
|
300
|
+
expect(() => tracker.seed([])).not.toThrow();
|
|
301
|
+
expect(tracker.size()).toBe(0);
|
|
302
|
+
tracker.stop();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("handles seed with duplicate IDs", () => {
|
|
306
|
+
const tracker = createSeenTracker({ maxEntries: 100 });
|
|
307
|
+
tracker.seed(["id1", "id1", "id1", "id2", "id2"]);
|
|
308
|
+
expect(tracker.size()).toBe(2);
|
|
309
|
+
tracker.stop();
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("handles seed larger than maxEntries", () => {
|
|
313
|
+
const tracker = createSeenTracker({ maxEntries: 5 });
|
|
314
|
+
const ids = Array.from({ length: 100 }, (_, i) => `id-${i}`);
|
|
315
|
+
tracker.seed(ids);
|
|
316
|
+
expect(tracker.size()).toBeLessThanOrEqual(5);
|
|
317
|
+
tracker.stop();
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
// ============================================================================
|
|
323
|
+
// Fuzz Tests for Metrics
|
|
324
|
+
// ============================================================================
|
|
325
|
+
|
|
326
|
+
describe("Metrics fuzz", () => {
|
|
327
|
+
describe("invalid metric names", () => {
|
|
328
|
+
it("handles unknown metric names gracefully", () => {
|
|
329
|
+
const metrics = createMetrics();
|
|
330
|
+
|
|
331
|
+
// Cast to bypass type checking - testing runtime behavior
|
|
332
|
+
expect(() => {
|
|
333
|
+
metrics.emit("invalid.metric.name" as MetricName);
|
|
334
|
+
}).not.toThrow();
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
describe("invalid label values", () => {
|
|
339
|
+
it("handles null relay label", () => {
|
|
340
|
+
const metrics = createMetrics();
|
|
341
|
+
expect(() => {
|
|
342
|
+
metrics.emit("relay.connect", 1, { relay: null as unknown as string });
|
|
343
|
+
}).not.toThrow();
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("handles undefined relay label", () => {
|
|
347
|
+
const metrics = createMetrics();
|
|
348
|
+
expect(() => {
|
|
349
|
+
metrics.emit("relay.connect", 1, { relay: undefined as unknown as string });
|
|
350
|
+
}).not.toThrow();
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it("handles very long relay URL", () => {
|
|
354
|
+
const metrics = createMetrics();
|
|
355
|
+
const longUrl = "wss://" + "a".repeat(10000) + ".com";
|
|
356
|
+
expect(() => {
|
|
357
|
+
metrics.emit("relay.connect", 1, { relay: longUrl });
|
|
358
|
+
}).not.toThrow();
|
|
359
|
+
|
|
360
|
+
const snapshot = metrics.getSnapshot();
|
|
361
|
+
expect(snapshot.relays[longUrl]).toBeDefined();
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
describe("extreme values", () => {
|
|
366
|
+
it("handles NaN value", () => {
|
|
367
|
+
const metrics = createMetrics();
|
|
368
|
+
expect(() => metrics.emit("event.received", NaN)).not.toThrow();
|
|
369
|
+
|
|
370
|
+
const snapshot = metrics.getSnapshot();
|
|
371
|
+
expect(isNaN(snapshot.eventsReceived)).toBe(true);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("handles Infinity value", () => {
|
|
375
|
+
const metrics = createMetrics();
|
|
376
|
+
expect(() => metrics.emit("event.received", Infinity)).not.toThrow();
|
|
377
|
+
|
|
378
|
+
const snapshot = metrics.getSnapshot();
|
|
379
|
+
expect(snapshot.eventsReceived).toBe(Infinity);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("handles negative value", () => {
|
|
383
|
+
const metrics = createMetrics();
|
|
384
|
+
metrics.emit("event.received", -1);
|
|
385
|
+
|
|
386
|
+
const snapshot = metrics.getSnapshot();
|
|
387
|
+
expect(snapshot.eventsReceived).toBe(-1);
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it("handles very large value", () => {
|
|
391
|
+
const metrics = createMetrics();
|
|
392
|
+
metrics.emit("event.received", Number.MAX_SAFE_INTEGER);
|
|
393
|
+
|
|
394
|
+
const snapshot = metrics.getSnapshot();
|
|
395
|
+
expect(snapshot.eventsReceived).toBe(Number.MAX_SAFE_INTEGER);
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
describe("rapid emissions", () => {
|
|
400
|
+
it("handles many rapid emissions", () => {
|
|
401
|
+
const events: unknown[] = [];
|
|
402
|
+
const metrics = createMetrics((e) => events.push(e));
|
|
403
|
+
|
|
404
|
+
for (let i = 0; i < 10000; i++) {
|
|
405
|
+
metrics.emit("event.received");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
expect(events).toHaveLength(10000);
|
|
409
|
+
const snapshot = metrics.getSnapshot();
|
|
410
|
+
expect(snapshot.eventsReceived).toBe(10000);
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
describe("reset during operation", () => {
|
|
415
|
+
it("handles reset mid-operation safely", () => {
|
|
416
|
+
const metrics = createMetrics();
|
|
417
|
+
|
|
418
|
+
metrics.emit("event.received");
|
|
419
|
+
metrics.emit("event.received");
|
|
420
|
+
metrics.reset();
|
|
421
|
+
metrics.emit("event.received");
|
|
422
|
+
|
|
423
|
+
const snapshot = metrics.getSnapshot();
|
|
424
|
+
expect(snapshot.eventsReceived).toBe(1);
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
// ============================================================================
|
|
430
|
+
// Event Shape Validation (simulating malformed events)
|
|
431
|
+
// ============================================================================
|
|
432
|
+
|
|
433
|
+
describe("Event shape validation", () => {
|
|
434
|
+
describe("malformed event structures", () => {
|
|
435
|
+
// These test what happens if malformed data somehow gets through
|
|
436
|
+
|
|
437
|
+
it("identifies missing required fields", () => {
|
|
438
|
+
const malformedEvents = [
|
|
439
|
+
{}, // empty
|
|
440
|
+
{ id: "abc" }, // missing pubkey, created_at, etc.
|
|
441
|
+
{ id: null, pubkey: null }, // null values
|
|
442
|
+
{ id: 123, pubkey: 456 }, // wrong types
|
|
443
|
+
{ tags: "not-an-array" }, // wrong type for tags
|
|
444
|
+
{ tags: [[1, 2, 3]] }, // wrong type for tag elements
|
|
445
|
+
];
|
|
446
|
+
|
|
447
|
+
for (const event of malformedEvents) {
|
|
448
|
+
// These should be caught by shape validation before processing
|
|
449
|
+
const hasId = typeof event?.id === "string";
|
|
450
|
+
const hasPubkey = typeof (event as { pubkey?: unknown })?.pubkey === "string";
|
|
451
|
+
const hasTags = Array.isArray((event as { tags?: unknown })?.tags);
|
|
452
|
+
|
|
453
|
+
// At least one should be invalid
|
|
454
|
+
expect(hasId && hasPubkey && hasTags).toBe(false);
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
describe("timestamp edge cases", () => {
|
|
460
|
+
const testTimestamps = [
|
|
461
|
+
{ value: NaN, desc: "NaN" },
|
|
462
|
+
{ value: Infinity, desc: "Infinity" },
|
|
463
|
+
{ value: -Infinity, desc: "-Infinity" },
|
|
464
|
+
{ value: -1, desc: "negative" },
|
|
465
|
+
{ value: 0, desc: "zero" },
|
|
466
|
+
{ value: 253402300800, desc: "year 10000" }, // Far future
|
|
467
|
+
{ value: -62135596800, desc: "year 0001" }, // Far past
|
|
468
|
+
{ value: 1.5, desc: "float" },
|
|
469
|
+
];
|
|
470
|
+
|
|
471
|
+
for (const { value, desc } of testTimestamps) {
|
|
472
|
+
it(`handles ${desc} timestamp`, () => {
|
|
473
|
+
const isValidTimestamp =
|
|
474
|
+
typeof value === "number" &&
|
|
475
|
+
!isNaN(value) &&
|
|
476
|
+
isFinite(value) &&
|
|
477
|
+
value >= 0 &&
|
|
478
|
+
Number.isInteger(value);
|
|
479
|
+
|
|
480
|
+
// Timestamps should be validated as positive integers
|
|
481
|
+
if (["NaN", "Infinity", "-Infinity", "negative", "float"].includes(desc)) {
|
|
482
|
+
expect(isValidTimestamp).toBe(false);
|
|
483
|
+
}
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
// ============================================================================
|
|
490
|
+
// JSON parsing edge cases (simulating relay responses)
|
|
491
|
+
// ============================================================================
|
|
492
|
+
|
|
493
|
+
describe("JSON parsing edge cases", () => {
|
|
494
|
+
const malformedJsonCases = [
|
|
495
|
+
{ input: "", desc: "empty string" },
|
|
496
|
+
{ input: "null", desc: "null literal" },
|
|
497
|
+
{ input: "undefined", desc: "undefined literal" },
|
|
498
|
+
{ input: "{", desc: "incomplete object" },
|
|
499
|
+
{ input: "[", desc: "incomplete array" },
|
|
500
|
+
{ input: '{"key": undefined}', desc: "undefined value" },
|
|
501
|
+
{ input: "{'key': 'value'}", desc: "single quotes" },
|
|
502
|
+
{ input: '{"key": NaN}', desc: "NaN value" },
|
|
503
|
+
{ input: '{"key": Infinity}', desc: "Infinity value" },
|
|
504
|
+
{ input: "\x00", desc: "null byte" },
|
|
505
|
+
{ input: "abc", desc: "plain string" },
|
|
506
|
+
{ input: "123", desc: "plain number" },
|
|
507
|
+
];
|
|
508
|
+
|
|
509
|
+
for (const { input, desc } of malformedJsonCases) {
|
|
510
|
+
it(`handles malformed JSON: ${desc}`, () => {
|
|
511
|
+
let parsed: unknown;
|
|
512
|
+
let parseError = false;
|
|
513
|
+
|
|
514
|
+
try {
|
|
515
|
+
parsed = JSON.parse(input);
|
|
516
|
+
} catch {
|
|
517
|
+
parseError = true;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// Either it throws or produces something that needs validation
|
|
521
|
+
if (!parseError) {
|
|
522
|
+
// If it parsed, we need to validate the structure
|
|
523
|
+
const isValidRelayMessage =
|
|
524
|
+
Array.isArray(parsed) && parsed.length >= 2 && typeof parsed[0] === "string";
|
|
525
|
+
|
|
526
|
+
// Most malformed cases won't produce valid relay messages
|
|
527
|
+
if (["null literal", "plain number", "plain string"].includes(desc)) {
|
|
528
|
+
expect(isValidRelayMessage).toBe(false);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
});
|