-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.test.ts
587 lines (506 loc) · 16.3 KB
/
config.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import { assert, assertArrayIncludes, assertEquals, assertThrows } from "@std/assert";
import { NosdumpConfigRepo, NosdumpConfigSchema, RelayAliasesOps, RelaySetsOps } from "./config.ts";
import { ZodError } from "zod";
Deno.test("NosdumpConfigRepo", async (t) => {
await t.step("resolveRelaySpecifiers()", async (t) => {
await t.step("basic scenario", () => {
const repo = NosdumpConfigRepo.fromConfigObjectForTesting({
relay: {
aliases: {
"foo": "wss://foo.example.com/",
"bar": "wss://bar.example.com/",
},
},
});
const resolved = repo.resolveRelaySpecifiers([
"foo",
"wss://hoge.example.com/",
"bar",
]);
assert(resolved.isOk);
assertArrayIncludes(resolved.val, [
"wss://foo.example.com/",
"wss://hoge.example.com/",
"wss://bar.example.com/",
]);
});
await t.step("normalizes raw relay URLs", () => {
const repo = NosdumpConfigRepo.fromConfigObjectForTesting({
relay: {},
});
const resolved = repo.resolveRelaySpecifiers([
"wss://example.com",
"wss://example.com/",
"wss://example.com/sub",
"wss://example.com/sub/",
]);
assert(resolved.isOk);
assertArrayIncludes(resolved.val, [
"wss://example.com/",
"wss://example.com/sub",
]);
});
await t.step("returns error if an alias is not found", () => {
const repo = NosdumpConfigRepo.fromConfigObjectForTesting({
relay: {
aliases: {
"foo": "wss://foo.example.com/",
},
},
});
const resolved = repo.resolveRelaySpecifiers([
"foo",
"wss://hoge.example.com/",
"bar",
]);
assert(!resolved.isOk);
assert(resolved.err.length === 1);
assert(resolved.err.some((msg) => msg.includes("bar")));
});
});
});
Deno.test("RelayAliasesOps", async (t) => {
await t.step("basic scenario", async (t) => {
const aliases = new RelayAliasesOps({});
await t.step("list the empty aliases", () => {
const l = aliases.list();
assertEquals(l, {});
});
await t.step("set an alias", () => {
// make sure that alias "foo" does not exist
assert(!aliases.has("foo"));
aliases.set("foo", "wss://foo.example.com/");
// make sure that alias "foo" exists and refers to the intended URL
assert(aliases.has("foo"));
assertEquals(aliases.get("foo"), "wss://foo.example.com/");
assertEquals(aliases.list(), { foo: "wss://foo.example.com/" });
});
await t.step("unset an alias", () => {
const unset = aliases.unset("foo");
assert(unset);
// make sure that alias "foo" is correctly unset
assert(!aliases.has("foo"));
assertEquals(aliases.get("foo"), undefined);
assertEquals(aliases.list(), {});
});
});
await t.step("set()", async (t) => {
await t.step("normalizes relay URL", () => {
const aliases = new RelayAliasesOps({});
aliases.set("root-no-trailing-slash", "wss://example.com");
aliases.set("sub-trailing-slash", "wss://example.com/sub/");
assertEquals(aliases.list(), {
"root-no-trailing-slash": "wss://example.com/",
"sub-trailing-slash": "wss://example.com/sub",
});
});
await t.step("throws if alias contains illegal characters", () => {
const aliases = new RelayAliasesOps({});
assertThrows(() => {
aliases.set("a/b", "wss://example.com");
});
});
await t.step("throws if relay URL is not a valid URL", () => {
const aliases = new RelayAliasesOps({});
assertThrows(() => {
aliases.set("foo", "not-a-url");
});
});
await t.step("throws if relay URL is not a Relay URL", () => {
const aliases = new RelayAliasesOps({});
assertThrows(() => {
aliases.set("foo", "https://example.com");
});
});
});
await t.step("setting duplicated alias overwrites existing one", () => {
const aliases = new RelayAliasesOps({ foo: "wss://foo.example.com/" });
assertEquals(aliases.get("foo"), "wss://foo.example.com/");
aliases.set("foo", "wss://new.foo.example.com/");
assertEquals(aliases.get("foo"), "wss://new.foo.example.com/");
});
await t.step("unsetting non-existing alias is no-op", () => {
const aliases = new RelayAliasesOps({ foo: "wss://foo.example.com/" });
const unset = aliases.unset("bar");
assert(!unset);
assert(aliases.has("foo"));
assert(!aliases.has("bar"));
assertEquals(aliases.get("bar"), undefined);
});
await t.step(
"modifying result of list() does not affect the internal state",
() => {
const aliases = new RelayAliasesOps({});
const l = aliases.list();
l.foo = "wss://foo.example.com/";
assert(!aliases.has("foo"));
assertEquals(aliases.get("foo"), undefined);
assertEquals(aliases.list(), {});
},
);
});
Deno.test("RelaySetsOps", async (t) => {
await t.step("basic scenario", async (t) => {
const sets = new RelaySetsOps({});
await t.step(
"adding a relay to a relay set that does not exist crates new one",
() => {
// make sure that relay set "foo" does not exist
assert(!sets.has("foo"));
sets.addRelayUrlsTo("foo", ["wss://foo-1.example.com/"]);
// make sure that set "foo" now exists and contains the relay added
assert(sets.has("foo"));
assertEquals(sets.listRelaysOf("foo"), ["wss://foo-1.example.com/"]);
assertEquals(sets.listAll(), { foo: ["wss://foo-1.example.com/"] });
},
);
await t.step("add more relays to a set", () => {
sets.addRelayUrlsTo("foo", [
"wss://foo-2.example.com/",
"wss://foo-3.example.com/",
"wss://foo-4.example.com/",
]);
assert(sets.has("foo"));
assertEquals(sets.listRelaysOf("foo"), [
"wss://foo-1.example.com/",
"wss://foo-2.example.com/",
"wss://foo-3.example.com/",
"wss://foo-4.example.com/",
]);
});
await t.step("remove relays from a set", () => {
const removed = sets.removeRelayUrlsFrom("foo", [
"wss://foo-2.example.com/",
"wss://foo-4.example.com/",
]);
assert(removed);
assert(sets.has("foo"));
assertEquals(sets.listRelaysOf("foo"), [
"wss://foo-1.example.com/",
"wss://foo-3.example.com/",
]);
});
await t.step("copy a set", () => {
sets.copy("foo", "bar");
assert(sets.has("foo") && sets.has("bar"));
assertEquals(sets.listRelaysOf("bar"), [
"wss://foo-1.example.com/",
"wss://foo-3.example.com/",
]);
// adding relays to the copy should not affect the original
sets.addRelayUrlsTo("bar", ["wss://bar.example.com/"]);
assertEquals(sets.listRelaysOf("bar"), [
"wss://foo-1.example.com/",
"wss://foo-3.example.com/",
"wss://bar.example.com/",
]);
assertEquals(sets.listRelaysOf("foo"), [
"wss://foo-1.example.com/",
"wss://foo-3.example.com/",
]);
});
await t.step("rename a set", () => {
sets.rename("bar", "bar2");
assert(!sets.has("bar") && sets.has("bar2"));
assertEquals(sets.listRelaysOf("bar2"), [
"wss://foo-1.example.com/",
"wss://foo-3.example.com/",
"wss://bar.example.com/",
]);
});
await t.step("delete a set", () => {
const deleted = sets.delete("foo");
assert(deleted);
// make sure that the set is deleted
assert(!sets.has("foo"));
assertEquals(sets.listRelaysOf("foo"), undefined);
});
});
await t.step("addRelayUrlsTo()", async (t) => {
await t.step("dedupes relay URLs", () => {
const sets = new RelaySetsOps({
foo: ["wss://foo-1.example.com/"],
});
sets.addRelayUrlsTo("foo", [
"wss://foo-2.example.com/",
"wss://foo-1.example.com/",
"wss://foo-2.example.com/",
]);
assertEquals(sets.listRelaysOf("foo"), [
"wss://foo-1.example.com/",
"wss://foo-2.example.com/",
]);
});
await t.step("normalizes relay URLs", () => {
const sets = new RelaySetsOps({});
sets.addRelayUrlsTo("foo", [
"wss://example.com",
"wss://example.com/",
"wss://example.com/sub",
"wss://example.com/sub/",
]);
assertEquals(sets.listRelaysOf("foo"), [
"wss://example.com/",
"wss://example.com/sub",
]);
});
await t.step("throws if relay set name contains illegal characters", () => {
const sets = new RelaySetsOps({});
assertThrows(() => {
sets.addRelayUrlsTo("a/b", ["wss://example.com"]);
});
});
await t.step("throws if relay URLs are not valid URLs", () => {
const sets = new RelaySetsOps({});
assertThrows(() => {
sets.addRelayUrlsTo("foo", ["not-a-url", "invalid-url"]);
});
});
await t.step("throws if relay URLs are not Relay URLs", () => {
const sets = new RelaySetsOps({});
assertThrows(() => {
sets.addRelayUrlsTo("foo", [
"https://example.com",
"http://example.com",
]);
});
});
});
await t.step("removeRelayUrlsFrom()", async (t) => {
await t.step("normalizes relay URLs in arguments before matching", () => {
const sets = new RelaySetsOps({
foo: ["wss://foo-1.example.com/", "wss://foo-2.example.com/"],
});
sets.removeRelayUrlsFrom("foo", [
"wss://foo-1.example.com", // no trailing slash
]);
// URLs with no trailing slash in arguments should be normalized before matching against existing URLs,
// so "wss://foo-1.example.com/" should be removed by the URL without trailing slash.
assertEquals(sets.listRelaysOf("foo"), [
"wss://foo-2.example.com/",
]);
});
await t.step("if removing relays empties a relay set, deletes it", () => {
const sets = new RelaySetsOps({
foo: ["wss://foo-1.example.com/", "wss://foo-2.example.com/"],
});
sets.removeRelayUrlsFrom("foo", ["wss://foo-1.example.com/"]);
sets.removeRelayUrlsFrom("foo", ["wss://foo-2.example.com/"]);
assert(!sets.has("foo"));
assertEquals(sets.listRelaysOf("foo"), undefined);
});
});
await t.step("copy()", async (t) => {
await t.step(
"throws if names of source and destination are the same",
() => {
const sets = new RelaySetsOps({
foo: ["wss://foo.example.com/"],
});
assertThrows(() => {
sets.copy("foo", "foo");
});
},
);
await t.step(
"throws if destination set name contains illegal characters",
() => {
const sets = new RelaySetsOps({
foo: ["wss://foo.example.com/"],
});
assertThrows(() => {
sets.copy("foo", "a/b");
});
},
);
await t.step("throws if source set does not exist", () => {
const sets = new RelaySetsOps({
baz: ["wss://baz.example.com/"],
});
assertThrows(() => {
sets.copy("foo", "bar");
});
});
});
await t.step("rename()", async (t) => {
await t.step(
"throws if names of source and destination are the same",
() => {
const sets = new RelaySetsOps({
foo: ["wss://foo.example.com/"],
});
assertThrows(() => {
sets.rename("foo", "foo");
});
},
);
await t.step(
"throws if destination set name contains illegal characters",
() => {
const sets = new RelaySetsOps({
foo: ["wss://foo.example.com/"],
});
assertThrows(() => {
sets.rename("foo", "a/b");
});
},
);
await t.step("throws if source set does not exist", () => {
const sets = new RelaySetsOps({
baz: ["wss://baz.example.com/"],
});
assertThrows(() => {
sets.rename("foo", "bar");
});
});
});
await t.step("deleting a non-existing set is no-op", () => {
const sets = new RelaySetsOps({ foo: ["wss://foo.example.com/"] });
const unset = sets.delete("bar");
assert(!unset);
assert(sets.has("foo"));
assert(!sets.has("bar"));
assertEquals(sets.listRelaysOf("bar"), undefined);
});
await t.step(
"modifying result of list() does not affect the internal state",
() => {
const sets = new RelaySetsOps({ foo: ["wss://foo.example.com/"] });
const l = sets.listAll();
l.bar = ["wss://bar.example.com/"];
assert(!sets.has("bar"));
assertEquals(sets.listRelaysOf("bar"), undefined);
l.foo.push("wss://unknown.example.com/");
assertEquals(sets.listRelaysOf("foo"), ["wss://foo.example.com/"]);
delete l.foo;
assert(sets.has("foo"));
assertEquals(sets.listRelaysOf("foo"), ["wss://foo.example.com/"]);
},
);
await t.step(
"modifying result of get() does not affect the internal state",
() => {
const sets = new RelaySetsOps({ foo: ["wss://foo.example.com/"] });
const foo = sets.listRelaysOf("foo");
assert(foo !== undefined);
foo.push("wss://unknown.example.com/");
assertEquals(sets.listRelaysOf("foo"), ["wss://foo.example.com/"]);
},
);
});
Deno.test("NosdumpConfigSchema", async (t) => {
await t.step(
"normalizes values (relay URLs) of relay.{aliases,sets} if no validation error",
() => {
const conf = {
relay: {
aliases: {
"root1": "wss://root1.example.com",
"root2": "wss://root2.example.com/",
"sub1": "wss://sub1.example.com/sub",
"sub2": "wss://sub2.example.com/sub/",
},
sets: {
"root": ["wss://root1.example.com", "wss://root2.example.com/"],
"sub": [
"wss://sub1.example.com/sub",
"wss://sub2.example.com/sub/",
],
"dup": ["wss://example.com", "wss://example.com/"],
},
},
};
const parsed = NosdumpConfigSchema.parse(conf);
assertEquals(parsed, {
relay: {
aliases: {
"root1": "wss://root1.example.com/",
"root2": "wss://root2.example.com/",
"sub1": "wss://sub1.example.com/sub",
"sub2": "wss://sub2.example.com/sub",
},
sets: {
"root": ["wss://root1.example.com/", "wss://root2.example.com/"],
"sub": [
"wss://sub1.example.com/sub",
"wss://sub2.example.com/sub",
],
"dup": ["wss://example.com/"], // should be deduped after normalization
},
},
});
},
);
await t.step("reports keys of relay.aliases have illegal letters", () => {
const conf = {
relay: {
aliases: {
"a/b": "wss://a-b.example.com",
},
},
};
assertThrows(() => {
NosdumpConfigSchema.parse(conf);
}, ZodError);
});
await t.step("reports values of relay.aliases have invalid URL", () => {
const conf = {
relay: {
aliases: {
"err": "not-a-url",
},
},
};
assertThrows(() => {
NosdumpConfigSchema.parse(conf);
}, ZodError);
});
await t.step("reports values of relay.aliases have non-Relay URL", () => {
const conf = {
relay: {
aliases: {
"err": "https://example.com",
},
},
};
assertThrows(() => {
NosdumpConfigSchema.parse(conf);
}, ZodError);
});
await t.step("reports keys of relay.sets have illegal letters", () => {
const conf = {
relay: {
sets: {
"a/b": ["wss://a-b.example.com"],
},
},
};
assertThrows(() => {
NosdumpConfigSchema.parse(conf);
}, ZodError);
});
await t.step("reports values of relay.sets have invalid URL", () => {
const conf = {
relay: {
sets: {
"err": ["not-a-url", "invalid-url"],
},
},
};
assertThrows(() => {
NosdumpConfigSchema.parse(conf);
}, ZodError);
});
await t.step("reports values of relay.sets have non-Relay URL", () => {
const conf = {
relay: {
sets: {
"err": ["https://example.com", "http://example.com"],
},
},
};
assertThrows(() => {
NosdumpConfigSchema.parse(conf);
}, ZodError);
});
});