-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathconfig.test.ts
More file actions
849 lines (710 loc) · 29.9 KB
/
config.test.ts
File metadata and controls
849 lines (710 loc) · 29.9 KB
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
type ENSNamespaceId,
ensTestEnvChain,
getENSNamespace,
maybeGetDatasource,
} from "@ensnode/datasources";
import { buildBlockNumberRange, ENSNamespaceIds, PluginName } from "@ensnode/ensnode-sdk";
import type { RpcConfig } from "@ensnode/ensnode-sdk/internal";
import { buildConfigFromEnvironment } from "@/config/config.schema";
import { getPlugin } from "@/plugins";
import type { ENSIndexerEnvironment } from "./environment";
import { EnvironmentDefaults } from "./environment-defaults";
const VALID_RPC_URL = "https://eth-mainnet.g.alchemy.com/v2/1234";
const VALID_RPC_URL_ALT = "https://lb.drpc.org/ethereum/987";
const VALID_RPC_WS_URL = "wss://eth-mainnet.g.alchemy.com/v2/1234";
const VALID_RPC_WS_URL_ALT = "wss://lb.drpc.org/ethereum/987";
const BASE_ENV: ENSIndexerEnvironment = {
NAMESPACE: "mainnet",
PLUGINS: "subgraph",
DATABASE_SCHEMA: "ensnode",
DATABASE_URL: "postgresql://user:password@localhost:5432/mydb",
ENSRAINBOW_URL: "http://localhost:3223",
LABEL_SET_ID: "ens-test-env",
LABEL_SET_VERSION: "0",
RPC_URL_1: VALID_RPC_URL,
};
async function getConfig() {
vi.resetModules(); // Reset module cache
const configModule = await import("@/config");
return configModule.default;
}
async function stubEnv(env: ENSIndexerEnvironment) {
Object.entries(env).forEach(([key, value]) => vi.stubEnv(key, value));
}
/**
* Stubs RPC_URL env vars for all chains defined in the given namespace's datasources.
*/
function stubRpcUrlsForNamespace(namespace: ENSNamespaceId) {
const datasources = getENSNamespace(namespace);
const chainIds = new Set(Object.values(datasources).map((ds) => ds.chain.id));
for (const chainId of chainIds) {
vi.stubEnv(`RPC_URL_${chainId}`, VALID_RPC_URL);
}
}
describe("config (with base env)", () => {
beforeEach(() => {
stubEnv(BASE_ENV);
});
afterEach(() => {
vi.unstubAllEnvs();
});
describe("general behavior", () => {
it("returns a valid config object using environment variables", async () => {
const config = await getConfig();
expect(config.namespace).toBe("mainnet");
expect(config.globalBlockrange).toEqual(buildBlockNumberRange(undefined, undefined));
expect(config.databaseSchemaName).toBe("ensnode");
expect(config.plugins).toEqual(["subgraph"]);
expect(config.ensRainbowUrl).toStrictEqual(new URL("http://localhost:3223"));
});
it("refreshes config when module is re-imported with new environment variables", async () => {
const initialConfig = await getConfig();
vi.stubEnv("LABEL_SET_ID", "subgraph");
const newConfig = await getConfig();
expect(newConfig.labelSet.labelSetId).toBe("subgraph");
expect(newConfig).not.toBe(initialConfig);
});
});
describe(".globalBlockrange", () => {
it("returns both startBlock and endBlock as numbers when both are set", async () => {
vi.stubEnv("START_BLOCK", "10");
vi.stubEnv("END_BLOCK", "20");
const config = await getConfig();
expect(config.globalBlockrange).toEqual(buildBlockNumberRange(10, 20));
});
it("returns only startBlock when only START_BLOCK is set", async () => {
vi.stubEnv("START_BLOCK", "5");
const config = await getConfig();
expect(config.globalBlockrange).toEqual(buildBlockNumberRange(5, undefined));
});
it("returns only endBlock when only END_BLOCK is set", async () => {
vi.stubEnv("END_BLOCK", "15");
const config = await getConfig();
expect(config.globalBlockrange).toEqual(buildBlockNumberRange(undefined, 15));
});
it("returns both as undefined when neither is set", async () => {
const config = await getConfig();
expect(config.globalBlockrange).toEqual(buildBlockNumberRange(undefined, undefined));
});
it("throws if START_BLOCK is negative", async () => {
vi.stubEnv("START_BLOCK", "-1");
await expect(getConfig()).rejects.toThrow(/START_BLOCK must be a positive integer/i);
});
it("throws if END_BLOCK is negative", async () => {
vi.stubEnv("END_BLOCK", "-5");
await expect(getConfig()).rejects.toThrow(/END_BLOCK must be a positive integer/i);
});
it("throws if START_BLOCK is not a number", async () => {
vi.stubEnv("START_BLOCK", "foo");
await expect(getConfig()).rejects.toThrow(/START_BLOCK must be a positive integer/i);
});
it("throws if END_BLOCK is not a number", async () => {
vi.stubEnv("END_BLOCK", "bar");
await expect(getConfig()).rejects.toThrow(/END_BLOCK must be a positive integer/i);
});
it("throws if START_BLOCK > END_BLOCK", async () => {
vi.stubEnv("START_BLOCK", "100");
vi.stubEnv("END_BLOCK", "50");
await expect(getConfig()).rejects.toThrow(
/START_BLOCK must be less than or equal to END_BLOCK/i,
);
});
it("does not throw if START_BLOCK == END_BLOCK", async () => {
vi.stubEnv("START_BLOCK", "100");
vi.stubEnv("END_BLOCK", "100");
const config = await getConfig();
expect(config.globalBlockrange).toEqual(buildBlockNumberRange(100, 100));
});
});
describe(".ensRainbowUrl", () => {
it("throws an error if ENSRAINBOW_URL is not a valid URL", async () => {
vi.stubEnv("ENSRAINBOW_URL", "invalid url");
await expect(getConfig()).rejects.toThrow(/ENSRAINBOW_URL must be a valid URL string/i);
});
it("returns the ENSRAINBOW_URL if it is a valid URL", async () => {
vi.stubEnv("ENSRAINBOW_URL", "https://customrainbow.com");
const config = await getConfig();
expect(config.ensRainbowUrl).toStrictEqual(new URL("https://customrainbow.com"));
});
it("throws an error if ENSRAINBOW_URL is not set", async () => {
vi.stubEnv("ENSRAINBOW_URL", undefined);
await expect(getConfig()).rejects.toThrow(/ENSRAINBOW_URL must be a valid URL string/i);
});
});
describe(".databaseSchemaName", () => {
it("returns the DATABASE_SCHEMA if set", async () => {
vi.stubEnv("DATABASE_SCHEMA", "someschema");
const config = await getConfig();
expect(config.databaseSchemaName).toBe("someschema");
});
it("throws an error when DATABASE_SCHEMA is not set", async () => {
vi.stubEnv("DATABASE_SCHEMA", undefined);
await expect(getConfig()).rejects.toThrow(/DATABASE_SCHEMA is required/);
});
it("throws an error when DATABASE_SCHEMA is empty", async () => {
vi.stubEnv("DATABASE_SCHEMA", "");
await expect(getConfig()).rejects.toThrow(
/DATABASE_SCHEMA is required and cannot be an empty string/,
);
});
it("throws an error when DATABASE_SCHEMA is only whitespace", async () => {
vi.stubEnv("DATABASE_SCHEMA", " ");
await expect(getConfig()).rejects.toThrow(
/DATABASE_SCHEMA is required and cannot be an empty string/,
);
});
});
describe(".namespace", () => {
it("returns the NAMESPACE if set", async () => {
vi.stubEnv("NAMESPACE", "sepolia");
stubRpcUrlsForNamespace("sepolia");
const config = await getConfig();
expect(config.namespace).toBe("sepolia");
});
it("throws if NAMESPACE is not set", async () => {
vi.stubEnv("NAMESPACE", undefined);
await expect(getConfig()).rejects.toThrow(/NAMESPACE/);
});
it("throws if NAMESPACE is an invalid string value", async () => {
vi.stubEnv("NAMESPACE", "not-a-chain");
await expect(getConfig()).rejects.toThrow(/Invalid NAMESPACE/i);
});
});
describe(".plugins", () => {
describe("SUBGRAPH_COMPAT=true", () => {
beforeEach(() => {
vi.stubEnv("SUBGRAPH_COMPAT", "true");
vi.stubEnv("LABEL_SET_ID", "subgraph");
vi.stubEnv("LABEL_SET_VERSION", "0");
});
it("has default plugins", async () => {
vi.stubEnv("PLUGINS", undefined);
await expect(getConfig()).resolves.toMatchObject({
plugins: EnvironmentDefaults.subgraphCompatible.PLUGINS.split(","),
});
});
});
describe("SUBGRAPH_COMPAT=false", () => {
beforeEach(() => {
vi.stubEnv("SUBGRAPH_COMPAT", "false");
});
it("has default plugins", async () => {
vi.stubEnv("PLUGINS", undefined);
stubRpcUrlsForNamespace("mainnet");
await expect(getConfig()).resolves.toMatchObject({
plugins: EnvironmentDefaults.alpha.PLUGINS.split(","),
});
});
});
it("returns the PLUGINS if it is a valid array", async () => {
vi.stubEnv("PLUGINS", "subgraph,basenames");
stubRpcUrlsForNamespace("mainnet");
const config = await getConfig();
expect(config.plugins).toEqual(["subgraph", "basenames"]);
});
it("returns a single plugin if only one is provided", async () => {
vi.stubEnv("PLUGINS", "basenames");
stubRpcUrlsForNamespace("mainnet");
const config = await getConfig();
expect(config.plugins).toEqual(["basenames"]);
});
it("throws if PLUGINS is an empty string", async () => {
vi.stubEnv("PLUGINS", "");
await expect(getConfig()).rejects.toThrow(
/PLUGINS must be a comma separated list with at least one valid plugin name/i,
);
});
it("throws if PLUGINS consists only of commas or whitespace", async () => {
vi.stubEnv("PLUGINS", " ,, ,");
await expect(getConfig()).rejects.toThrow(
/PLUGINS must be a comma separated list with at least one valid plugin name/i,
);
});
it("throws if PLUGINS consists of non-existent plugins", async () => {
vi.stubEnv("PLUGINS", "some,nonexistent,plugins");
await expect(getConfig()).rejects.toThrow(
/PLUGINS must be a comma separated list with at least one valid plugin name/i,
);
});
it("throws if PLUGINS contains duplicate values", async () => {
vi.stubEnv("PLUGINS", "subgraph,basenames,subgraph");
await expect(getConfig()).rejects.toThrow(/PLUGINS cannot contain duplicate values/i);
});
});
describe(".chains", () => {
it("returns the chains if it is a valid object (one HTTP protocol URL)", async () => {
vi.stubEnv("RPC_URL_1", VALID_RPC_URL);
const config = await getConfig();
expect(config.rpcConfigs).toStrictEqual(
new Map([
[
1,
{
httpRPCs: [new URL(VALID_RPC_URL)],
websocketRPC: undefined,
} satisfies RpcConfig,
],
]),
);
});
it("returns the chains if it is a valid object (multiple HTTP protocol URLs)", async () => {
vi.stubEnv("RPC_URL_1", `${VALID_RPC_URL_ALT},${VALID_RPC_URL}`);
const config = await getConfig();
expect(config.rpcConfigs).toStrictEqual(
new Map([
[
1,
{
httpRPCs: [new URL(VALID_RPC_URL_ALT), new URL(VALID_RPC_URL)],
websocketRPC: undefined,
} satisfies RpcConfig,
],
]),
);
});
it("returns the chains if it is a valid object (multiple HTTP protocol URLs, and one WebSocket protocol URL)", async () => {
vi.stubEnv("RPC_URL_1", `${VALID_RPC_URL},${VALID_RPC_WS_URL},${VALID_RPC_URL_ALT}`);
const config = await getConfig();
expect(config.rpcConfigs).toStrictEqual(
new Map([
[
1,
{
httpRPCs: [new URL(VALID_RPC_URL), new URL(VALID_RPC_URL_ALT)],
websocketRPC: new URL(VALID_RPC_WS_URL),
} satisfies RpcConfig,
],
]),
);
});
it("throws an error if RPC_URL_1 is not a valid URL", async () => {
vi.stubEnv("RPC_URL_1", "invalid url");
await expect(getConfig()).rejects.toThrow(/must be a valid URL string/i);
});
it("throws an error if RPC_URL_1 includes less than one HTTP protocol URL", async () => {
vi.stubEnv("RPC_URL_1", `${VALID_RPC_WS_URL},${VALID_RPC_WS_URL_ALT}`);
await expect(getConfig()).rejects.toThrow(
/RPC endpoint configuration for a chain must include at least one http\/https protocol URL/i,
);
});
it("throws an error if RPC_URL_1 includes more than one WebSockets protocol URL", async () => {
vi.stubEnv("RPC_URL_1", `${VALID_RPC_URL},${VALID_RPC_WS_URL},${VALID_RPC_WS_URL_ALT}`);
await expect(getConfig()).rejects.toThrow(
/RPC endpoint configuration for a chain must include at most one websocket \(ws\/wss\) protocol URL./i,
);
});
describe("Useful error messages", () => {
// Mock process.exit to prevent actual exit
const mockExit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
mockExit.mockClear();
});
it("logs error message when QuickNode RPC config was partially configured (missing endpoint name)", async () => {
expect(() =>
buildConfigFromEnvironment({
...BASE_ENV,
QUICKNODE_API_KEY: "my-api-key",
}),
).toThrowError(
/Use of the QUICKNODE_API_KEY environment variable requires use of the QUICKNODE_ENDPOINT_NAME environment variable as well/i,
);
});
it("logs error message when QuickNode RPC config was partially configured (missing API key)", async () => {
expect(() =>
buildConfigFromEnvironment({
...BASE_ENV,
QUICKNODE_ENDPOINT_NAME: "my-endpoint-name",
}),
).toThrowError(
/Use of the QUICKNODE_ENDPOINT_NAME environment variable requires use of the QUICKNODE_API_KEY environment variable as well/i,
);
});
});
});
describe(".databaseUrl", () => {
it("accepts a valid PostgreSQL connection string", async () => {
vi.stubEnv("DATABASE_URL", "postgresql://user:password@localhost:5432/mydb");
const config = await getConfig();
expect(config.databaseUrl).toBe("postgresql://user:password@localhost:5432/mydb");
});
it("accepts a connection string with additional parameters", async () => {
vi.stubEnv("DATABASE_URL", "postgresql://user:password@localhost:5432/mydb?sslmode=require");
const config = await getConfig();
expect(config.databaseUrl).toBe(
"postgresql://user:password@localhost:5432/mydb?sslmode=require",
);
});
it("throws an error if DATABASE_URL is not set", async () => {
vi.stubEnv("DATABASE_URL", undefined);
await expect(getConfig()).rejects.toThrow(/Invalid input/);
});
it("throws an error if DATABASE_URL is empty", async () => {
vi.stubEnv("DATABASE_URL", "");
await expect(getConfig()).rejects.toThrow(/Invalid PostgreSQL connection string/);
});
it("throws an error if DATABASE_URL is not a valid postgres connection string", async () => {
vi.stubEnv("DATABASE_URL", "not-a-postgres-connection-string");
await expect(getConfig()).rejects.toThrow(/Invalid PostgreSQL connection string/);
});
it("throws an error if DATABASE_URL uses the wrong protocol", async () => {
vi.stubEnv("DATABASE_URL", "mysql://user:password@localhost:3306/mydb");
await expect(getConfig()).rejects.toThrow(/Invalid PostgreSQL connection string/);
});
it("throws an error if DATABASE_URL is missing required components", async () => {
vi.stubEnv("DATABASE_URL", "postgresql://localhost:5432");
await expect(getConfig()).rejects.toThrow(/Invalid PostgreSQL connection string/);
});
it("accepts postgres:// protocol", async () => {
vi.stubEnv("DATABASE_URL", "postgres://user:password@localhost:5432/mydb");
const config = await getConfig();
expect(config.databaseUrl).toBe("postgres://user:password@localhost:5432/mydb");
});
it("accepts postgresql:// protocol", async () => {
vi.stubEnv("DATABASE_URL", "postgresql://user:password@localhost:5432/mydb");
const config = await getConfig();
expect(config.databaseUrl).toBe("postgresql://user:password@localhost:5432/mydb");
});
});
describe("SUBGRAPH_COMPAT", () => {
// start in subgraph-compatible state
beforeEach(() => {
vi.stubEnv("SUBGRAPH_COMPAT", "true");
vi.stubEnv("LABEL_SET_ID", undefined);
vi.stubEnv("LABEL_SET_VERSION", undefined);
});
it("is true when compatible", async () => {
await expect(getConfig()).resolves.toMatchObject({ isSubgraphCompatible: true });
});
it("throws when PLUGINS does not include subgraph", async () => {
vi.stubEnv("PLUGINS", "basenames");
stubRpcUrlsForNamespace("mainnet");
await expect(getConfig()).rejects.toThrow(/isSubgraphCompatible/);
});
it("throws when PLUGINS includes subgraph along with other plugins", async () => {
vi.stubEnv("PLUGINS", "subgraph,basenames");
stubRpcUrlsForNamespace("mainnet");
await expect(getConfig()).rejects.toThrow(/isSubgraphCompatible/);
});
});
describe(".indexedChainIds", () => {
it("derives chain id 1 for subgraph plugin on mainnet", async () => {
vi.stubEnv("PLUGINS", "subgraph");
const config = await getConfig();
expect(config.indexedChainIds).toEqual(new Set([1]));
});
it("derives chain ids 1 and 8453 for subgraph,basenames on mainnet", async () => {
vi.stubEnv("PLUGINS", "subgraph,basenames");
stubRpcUrlsForNamespace("mainnet");
const config = await getConfig();
expect(config.indexedChainIds).toEqual(new Set([1, 8453]));
});
it("derives all expected chain ids for protocol-acceleration on mainnet", async () => {
vi.stubEnv("PLUGINS", "protocol-acceleration");
stubRpcUrlsForNamespace("mainnet");
const config = await getConfig();
const plugin = getPlugin(PluginName.ProtocolAcceleration);
const expected = new Set(
plugin.allDatasourceNames
.map((name) => maybeGetDatasource(ENSNamespaceIds.Mainnet, name)?.chain.id)
.filter((id) => id !== undefined),
);
expect(config.indexedChainIds).toEqual(expected);
});
// This test asserts that protocol-acceleration derives different chain ids per namespace,
// because available datasources differ (e.g. sepolia has no ThreeDNS datasources).
// If this test fails after updating datasources for a namespace, update the expected
// chain ids or remove this test if the distinction no longer holds.
it("derives different chain ids for protocol-acceleration on sepolia vs mainnet", async () => {
vi.stubEnv("PLUGINS", "protocol-acceleration");
stubRpcUrlsForNamespace("mainnet");
const mainnetConfig = await getConfig();
vi.stubEnv("NAMESPACE", "sepolia");
stubRpcUrlsForNamespace("sepolia");
const sepoliaConfig = await getConfig();
expect(mainnetConfig.indexedChainIds).not.toEqual(sepoliaConfig.indexedChainIds);
});
});
describe("additional checks", () => {
it("all plugins have requiredDatasourceNames as a subset of allDatasourceNames", () => {
for (const pluginName of Object.values(PluginName)) {
const plugin = getPlugin(pluginName);
const allSet = new Set(plugin.allDatasourceNames);
for (const required of plugin.requiredDatasourceNames) {
expect(
allSet.has(required),
`${pluginName}: requiredDatasourceName '${required}' missing from allDatasourceNames`,
).toBe(true);
}
}
});
it("requires available datasources", async () => {
vi.stubEnv("NAMESPACE", "ens-test-env");
vi.stubEnv("PLUGINS", "basenames");
await expect(getConfig()).rejects.toThrow(/specifies dependent datasources/i);
});
it("requires rpc url for indexed chains", async () => {
vi.stubEnv("PLUGINS", "subgraph,basenames");
await expect(getConfig()).rejects.toThrow(/RPC_URL_\d+/i);
});
it("cannot constrain blockrange with multiple chains", async () => {
vi.stubEnv("PLUGINS", "subgraph,basenames");
stubRpcUrlsForNamespace("mainnet");
vi.stubEnv("END_BLOCK", "1");
await expect(getConfig()).rejects.toThrow(/multiple chains/i);
});
});
describe(".labelSet", () => {
it("returns the labelSet configuration if both LABEL_SET_ID and LABEL_SET_VERSION are valid", async () => {
vi.stubEnv("LABEL_SET_ID", "subgraph");
vi.stubEnv("LABEL_SET_VERSION", "5");
const config = await getConfig();
expect(config.labelSet).toEqual({
labelSetId: "subgraph",
labelSetVersion: 5,
});
});
describe("SUBGRAPH_COMPAT=true", () => {
beforeEach(() => {
vi.stubEnv("SUBGRAPH_COMPAT", "true");
});
it("has default label set", async () => {
vi.stubEnv("LABEL_SET_ID", undefined);
vi.stubEnv("LABEL_SET_VERSION", undefined);
await expect(getConfig()).resolves.toMatchObject({
labelSet: { labelSetId: "subgraph", labelSetVersion: 0 },
});
});
});
describe("SUBGRAPH_COMPAT=false", () => {
beforeEach(() => {
vi.stubEnv("SUBGRAPH_COMPAT", "false");
});
it("has default label set", async () => {
vi.stubEnv("LABEL_SET_ID", undefined);
vi.stubEnv("LABEL_SET_VERSION", undefined);
await expect(getConfig()).resolves.toMatchObject({
labelSet: { labelSetId: "subgraph", labelSetVersion: 0 },
});
});
});
it("throws an error when LABEL_SET_ID is empty", async () => {
vi.stubEnv("LABEL_SET_ID", "");
await expect(getConfig()).rejects.toThrow(/LABEL_SET_ID must be 1-50 characters long/);
});
it("throws an error when LABEL_SET_ID is only whitespace", async () => {
vi.stubEnv("LABEL_SET_ID", " ");
await expect(getConfig()).rejects.toThrow(/LABEL_SET_ID can only contain lowercase letters/);
});
it("throws an error when LABEL_SET_ID is too long", async () => {
vi.stubEnv("LABEL_SET_ID", "a".repeat(51));
await expect(getConfig()).rejects.toThrow(/LABEL_SET_ID must be 1-50 characters long/);
});
it("throws an error when LABEL_SET_ID contains invalid characters", async () => {
vi.stubEnv("LABEL_SET_ID", "invalid-id_with_underscores");
await expect(getConfig()).rejects.toThrow(/LABEL_SET_ID can only contain lowercase letters/);
});
it("throws an error when LABEL_SET_ID contains uppercase letters", async () => {
vi.stubEnv("LABEL_SET_ID", "InvalidId");
await expect(getConfig()).rejects.toThrow(/LABEL_SET_ID can only contain lowercase letters/);
});
it("accepts valid LABEL_SET_ID with hyphens", async () => {
vi.stubEnv("LABEL_SET_ID", "ens-test-env");
const config = await getConfig();
expect(config.labelSet.labelSetId).toBe("ens-test-env");
});
it("throws an error when LABEL_SET_VERSION is negative", async () => {
vi.stubEnv("LABEL_SET_VERSION", "-1");
await expect(getConfig()).rejects.toThrow(/LABEL_SET_VERSION must be a non-negative integer/);
});
it("throws an error when LABEL_SET_VERSION is not an integer", async () => {
vi.stubEnv("LABEL_SET_VERSION", "5.5");
await expect(getConfig()).rejects.toThrow(/LABEL_SET_VERSION must be an integer/);
});
it("throws an error when LABEL_SET_VERSION is not a number", async () => {
vi.stubEnv("LABEL_SET_VERSION", "not-a-number");
await expect(getConfig()).rejects.toThrow(/LABEL_SET_VERSION must be an integer/);
});
it("accepts zero as a valid LABEL_SET_VERSION", async () => {
vi.stubEnv("LABEL_SET_VERSION", "0");
const config = await getConfig();
expect(config.labelSet.labelSetVersion).toBe(0);
});
});
});
/**
* The following test block defines the minimal environment, so each test case is more readable.
*/
describe("config (minimal base env)", () => {
beforeEach(() => {
const { NAMESPACE, ENSRAINBOW_URL, DATABASE_URL, DATABASE_SCHEMA, RPC_URL_1 } = BASE_ENV;
stubEnv({
NAMESPACE,
ENSRAINBOW_URL,
DATABASE_URL,
DATABASE_SCHEMA,
RPC_URL_1,
});
});
afterEach(() => {
vi.unstubAllEnvs();
});
describe("SUBGAPH_COMPAT=false", () => {
beforeEach(() => {
stubEnv({ SUBGRAPH_COMPAT: "false" });
});
it("requires default plugins rpc urls", async () => {
await expect(getConfig()).rejects.toThrow(/RPC_URL_/);
});
it("provides default plugins", async () => {
stubRpcUrlsForNamespace("mainnet");
await expect(getConfig()).resolves.toMatchObject({
plugins: EnvironmentDefaults.alpha.PLUGINS.split(","),
});
});
it("allows override of default plugins", async () => {
stubEnv({ PLUGINS: "tokenscope" });
stubRpcUrlsForNamespace("mainnet");
await expect(getConfig()).resolves.toMatchObject({ plugins: [PluginName.TokenScope] });
});
describe("ens-test-env rpcs", () => {
it("should provide ens-test-env rpc defaults", async () => {
stubEnv({ NAMESPACE: "ens-test-env", PLUGINS: "subgraph" });
const config = await getConfig();
expect(config.rpcConfigs.has(ensTestEnvChain.id)).toBe(true);
});
});
describe("with ALCHEMY_API_KEY", () => {
beforeEach(() => {
stubEnv({ ALCHEMY_API_KEY: "anything", RPC_URL_1: undefined });
});
it("should provide rpcConfigs for all mainnet chains", async () => {
const config = await getConfig();
const rpcConfigs = [...config.rpcConfigs.values()];
expect(rpcConfigs.length, "should have some configs").toBeGreaterThan(0);
expect(
rpcConfigs.every((rpcConfig) => rpcConfig.httpRPCs.length >= 1),
"must have http rpc url",
).toBe(true);
});
it("should not generate WS RPCs by default (http-only mode)", async () => {
const config = await getConfig();
const rpcConfigs = [...config.rpcConfigs.values()];
expect(rpcConfigs.length, "should have some configs").toBeGreaterThan(0);
expect(
rpcConfigs.every((rpcConfig) => rpcConfig.websocketRPC === undefined),
"must not have ws rpc url",
).toBe(true);
});
});
describe("with ALCHEMY_API_KEY and RPC_AUTO_GEN_MODE=http-and-ws", () => {
beforeEach(() => {
stubEnv({
ALCHEMY_API_KEY: "anything",
RPC_URL_1: undefined,
RPC_AUTO_GEN_MODE: "http-and-ws",
});
});
it("should generate WS RPCs for chains that support them", async () => {
const config = await getConfig();
const rpcConfigs = [...config.rpcConfigs.values()];
expect(rpcConfigs.length, "should have some configs").toBeGreaterThan(0);
expect(
rpcConfigs.some((rpcConfig) => rpcConfig.websocketRPC !== undefined),
"should have at least one ws rpc url",
).toBe(true);
});
});
describe("with ALCHEMY_API_KEY and RPC_AUTO_GEN_MODE=http-only", () => {
beforeEach(() => {
stubEnv({
ALCHEMY_API_KEY: "anything",
RPC_URL_1: undefined,
RPC_AUTO_GEN_MODE: "http-only",
});
});
it("should not generate WS RPCs", async () => {
const config = await getConfig();
const rpcConfigs = [...config.rpcConfigs.values()];
expect(rpcConfigs.length, "should have some configs").toBeGreaterThan(0);
expect(
rpcConfigs.every((rpcConfig) => rpcConfig.websocketRPC === undefined),
"must not have ws rpc url",
).toBe(true);
});
});
describe("with ALCHEMY_API_KEY and RPC_AUTO_GEN_MODE=invalid", () => {
beforeEach(() => {
stubEnv({
ALCHEMY_API_KEY: "anything",
RPC_URL_1: undefined,
RPC_AUTO_GEN_MODE: "invalid",
});
});
it("throws an error for invalid RPC_AUTO_GEN_MODE", async () => {
await expect(getConfig()).rejects.toThrow(/Invalid RPC_AUTO_GEN_MODE env var/i);
});
});
describe("with DRPC_API_KEY", async () => {
beforeEach(() => {
stubEnv({ DRPC_API_KEY: "anything", RPC_URL_1: undefined });
});
it("should provide rpcConfigs for all mainnet chains", async () => {
const config = await getConfig();
const rpcConfigs = [...config.rpcConfigs.values()];
// should provide some rpcConfigs
expect(rpcConfigs.length, "should have some configs").toBeGreaterThan(0);
expect(
rpcConfigs.every((rpcConfig) => rpcConfig.httpRPCs.length >= 1),
"must have http rpc url",
).toBe(true);
expect(
rpcConfigs.every((rpcConfig) => rpcConfig.websocketRPC === undefined),
"must not have ws rpc url",
).toBe(true);
});
});
describe("with ALCHEMY_API_KEY, DRPC_API_KEY, and RPC_URL_1", async () => {
beforeEach(() => {
stubEnv({ ALCHEMY_API_KEY: "anything" });
stubEnv({ DRPC_API_KEY: "anything" });
});
it("should provide rpcConfigs for all mainnet chains with order", async () => {
const config = await getConfig();
const rpcConfigs = [...config.rpcConfigs.values()];
// should provide some rpcConfigs
expect(rpcConfigs.length, "should have some configs").toBeGreaterThan(0);
expect(config.rpcConfigs.get(1)!.httpRPCs.length).toBe(1); // with RPC_URL_1
expect(config.rpcConfigs.get(1)!.httpRPCs[0]!.href).toBe(VALID_RPC_URL);
expect(config.rpcConfigs.get(10)!.httpRPCs.length).toBe(2);
expect(config.rpcConfigs.get(10)!.httpRPCs[0]!.href).toContain("alchemy");
expect(config.rpcConfigs.get(10)!.httpRPCs[1]!.href).toContain("drpc");
});
});
});
describe("SUBGAPH_COMPAT=true", () => {
beforeEach(() => {
stubEnv({ SUBGRAPH_COMPAT: "true" });
});
it("ens-test-env namespace/labelset is subgraph-compatible", async () => {
stubEnv({
NAMESPACE: "ens-test-env",
LABEL_SET_ID: "ens-test-env",
LABEL_SET_VERSION: "0",
[`RPC_URL_${ensTestEnvChain.id}`]: VALID_RPC_URL,
});
await expect(getConfig()).resolves.toMatchObject({
namespace: ENSNamespaceIds.EnsTestEnv,
labelSet: {
labelSetId: "ens-test-env",
labelSetVersion: 0,
},
isSubgraphCompatible: true,
});
});
});
});