-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathconfig.schema.test.ts
More file actions
249 lines (215 loc) · 8.22 KB
/
config.schema.test.ts
File metadata and controls
249 lines (215 loc) · 8.22 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
import packageJson from "@/../package.json" with { type: "json" };
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
type ENSIndexerPublicConfig,
type EnsDbPublicConfig,
PluginName,
} from "@ensnode/ensnode-sdk";
import type { RpcConfig } from "@ensnode/ensnode-sdk/internal";
vi.mock("@/lib/ensdb/singleton", () => ({
ensDbClient: {
getEnsDbPublicConfig: vi.fn(async () => ENSDB_PUBLIC_CONFIG),
getEnsIndexerPublicConfig: vi.fn(async () => ENSINDEXER_PUBLIC_CONFIG),
},
}));
import { buildConfigFromEnvironment, buildEnsApiPublicConfig } from "@/config/config.schema";
import { ENSApi_DEFAULT_PORT } from "@/config/defaults";
import type { EnsApiEnvironment } from "@/config/environment";
import logger from "@/lib/logger";
vi.mock("@/lib/logger", () => ({
default: {
error: vi.fn(),
info: vi.fn(),
},
}));
const VALID_RPC_URL = "https://eth-sepolia.g.alchemy.com/v2/1234";
const BASE_ENV = {
DATABASE_URL: "postgresql://user:password@localhost:5432/mydb",
RPC_URL_1: VALID_RPC_URL,
} satisfies EnsApiEnvironment;
const ENSDB_PUBLIC_CONFIG = {
postgresVersion: "17.4",
rootSchemaVersion: "1.0.0",
} satisfies EnsDbPublicConfig;
const ENSINDEXER_PUBLIC_CONFIG = {
namespace: "mainnet",
databaseSchemaName: "ensapi",
ensRainbowPublicConfig: {
version: packageJson.version,
labelSet: { labelSetId: "subgraph", highestLabelSetVersion: 0 },
recordsCount: 100,
},
indexedChainIds: new Set([1]),
isSubgraphCompatible: false,
labelSet: { labelSetId: "subgraph", labelSetVersion: 0 },
plugins: [PluginName.Subgraph],
versionInfo: {
ensDb: packageJson.version,
ensIndexer: packageJson.version,
ensNormalize: "1.1.1",
nodejs: "1.1.1",
ponder: "1.1.1",
},
} satisfies ENSIndexerPublicConfig;
describe("buildConfigFromEnvironment", () => {
it("returns a valid config object using environment variables", async () => {
await expect(buildConfigFromEnvironment(BASE_ENV)).resolves.toStrictEqual({
port: ENSApi_DEFAULT_PORT,
databaseUrl: BASE_ENV.DATABASE_URL,
theGraphApiKey: undefined,
ensDbPublicConfig: ENSDB_PUBLIC_CONFIG,
ensIndexerPublicConfig: ENSINDEXER_PUBLIC_CONFIG,
namespace: ENSINDEXER_PUBLIC_CONFIG.namespace,
ensIndexerSchemaName: ENSINDEXER_PUBLIC_CONFIG.databaseSchemaName,
rpcConfigs: new Map([
[
1,
{
httpRPCs: [new URL(BASE_ENV.RPC_URL_1)],
websocketRPC: undefined,
} satisfies RpcConfig,
],
]),
customReferralProgramEditionConfigSetUrl: undefined,
});
});
it("parses CUSTOM_REFERRAL_PROGRAM_EDITIONS as a URL object", async () => {
const customUrl = "https://example.com/editions.json";
const config = await buildConfigFromEnvironment({
...BASE_ENV,
CUSTOM_REFERRAL_PROGRAM_EDITIONS: customUrl,
});
expect(config.customReferralProgramEditionConfigSetUrl).toEqual(new URL(customUrl));
});
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();
});
const TEST_ENV: EnsApiEnvironment = {
DATABASE_URL: BASE_ENV.DATABASE_URL,
};
it("logs error and exits when CUSTOM_REFERRAL_PROGRAM_EDITIONS is not a valid URL", async () => {
await buildConfigFromEnvironment({
...TEST_ENV,
CUSTOM_REFERRAL_PROGRAM_EDITIONS: "not-a-url",
});
expect(logger.error).toHaveBeenCalledWith(
expect.stringContaining("CUSTOM_REFERRAL_PROGRAM_EDITIONS is not a valid URL: not-a-url"),
);
expect(process.exit).toHaveBeenCalledWith(1);
});
it("logs error message when QuickNode RPC config was partially configured (missing endpoint name)", async () => {
await buildConfigFromEnvironment({
...TEST_ENV,
QUICKNODE_API_KEY: "my-api-key",
});
expect(logger.error).toHaveBeenCalledWith(
new Error(
"Use of the QUICKNODE_API_KEY environment variable requires use of the QUICKNODE_ENDPOINT_NAME environment variable as well.",
),
"Failed to build EnsApiConfig",
);
expect(process.exit).toHaveBeenCalledWith(1);
});
it("logs error message when QuickNode RPC config was partially configured (missing API key)", async () => {
await buildConfigFromEnvironment({
...TEST_ENV,
QUICKNODE_ENDPOINT_NAME: "my-endpoint-name",
});
expect(logger.error).toHaveBeenCalledWith(
new Error(
"Use of the QUICKNODE_ENDPOINT_NAME environment variable requires use of the QUICKNODE_API_KEY environment variable as well.",
),
"Failed to build EnsApiConfig",
);
expect(process.exit).toHaveBeenCalledWith(1);
});
});
});
describe("buildEnsApiPublicConfig", () => {
it("returns a valid ENSApi public config with correct structure", () => {
const mockConfig = {
port: ENSApi_DEFAULT_PORT,
databaseUrl: BASE_ENV.DATABASE_URL,
ensDbPublicConfig: ENSDB_PUBLIC_CONFIG,
ensIndexerPublicConfig: ENSINDEXER_PUBLIC_CONFIG,
namespace: ENSINDEXER_PUBLIC_CONFIG.namespace,
ensIndexerSchemaName: ENSINDEXER_PUBLIC_CONFIG.databaseSchemaName,
rpcConfigs: new Map([
[
1,
{
httpRPCs: [new URL(VALID_RPC_URL)],
websocketRPC: undefined,
} satisfies RpcConfig,
],
]),
customReferralProgramEditionConfigSetUrl: undefined,
};
const result = buildEnsApiPublicConfig(mockConfig);
expect(result).toStrictEqual({
version: packageJson.version,
theGraphFallback: {
canFallback: false,
reason: "not-subgraph-compatible",
},
ensDbPublicConfig: ENSDB_PUBLIC_CONFIG,
ensIndexerPublicConfig: ENSINDEXER_PUBLIC_CONFIG,
});
});
it("preserves the complete ENSIndexer public config structure", () => {
const mockConfig = {
port: ENSApi_DEFAULT_PORT,
databaseUrl: BASE_ENV.DATABASE_URL,
ensDbPublicConfig: ENSDB_PUBLIC_CONFIG,
ensIndexerPublicConfig: ENSINDEXER_PUBLIC_CONFIG,
namespace: ENSINDEXER_PUBLIC_CONFIG.namespace,
ensIndexerSchemaName: ENSINDEXER_PUBLIC_CONFIG.databaseSchemaName,
rpcConfigs: new Map(),
customReferralProgramEditionConfigSetUrl: undefined,
};
const result = buildEnsApiPublicConfig(mockConfig);
// Verify that all ENSIndexer public config fields are preserved
expect(result.ensIndexerPublicConfig.namespace).toBe(ENSINDEXER_PUBLIC_CONFIG.namespace);
expect(result.ensIndexerPublicConfig.plugins).toEqual(ENSINDEXER_PUBLIC_CONFIG.plugins);
expect(result.ensIndexerPublicConfig.versionInfo).toEqual(ENSINDEXER_PUBLIC_CONFIG.versionInfo);
expect(result.ensIndexerPublicConfig.indexedChainIds).toEqual(
ENSINDEXER_PUBLIC_CONFIG.indexedChainIds,
);
expect(result.ensIndexerPublicConfig.isSubgraphCompatible).toBe(
ENSINDEXER_PUBLIC_CONFIG.isSubgraphCompatible,
);
expect(result.ensIndexerPublicConfig.labelSet).toEqual(ENSINDEXER_PUBLIC_CONFIG.labelSet);
expect(result.ensIndexerPublicConfig.databaseSchemaName).toBe(
ENSINDEXER_PUBLIC_CONFIG.databaseSchemaName,
);
});
it("includes the theGraphFallback and redacts api key", () => {
const mockConfig = {
port: ENSApi_DEFAULT_PORT,
databaseUrl: BASE_ENV.DATABASE_URL,
ensDbPublicConfig: ENSDB_PUBLIC_CONFIG,
ensIndexerPublicConfig: {
...ENSINDEXER_PUBLIC_CONFIG,
plugins: ["subgraph"],
isSubgraphCompatible: true,
},
namespace: ENSINDEXER_PUBLIC_CONFIG.namespace,
ensIndexerSchemaName: ENSINDEXER_PUBLIC_CONFIG.databaseSchemaName,
rpcConfigs: new Map(),
customReferralProgramEditionConfigSetUrl: undefined,
theGraphApiKey: "secret-api-key",
};
const result = buildEnsApiPublicConfig(mockConfig);
expect(result.theGraphFallback.canFallback).toBe(true);
// discriminate the type...
if (!result.theGraphFallback.canFallback) throw new Error("never");
// shouldn't have the secret-api-key in the url
expect(result.theGraphFallback.url).not.toMatch(/secret-api-key/gi);
});
});