-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconfig.tests.ts
More file actions
290 lines (243 loc) · 8.11 KB
/
config.tests.ts
File metadata and controls
290 lines (243 loc) · 8.11 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
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { loadConfig, resolveAccounts, type ConfigAccount } from "./config";
const temporaryTestBaseDir = resolve(tmpdir(), "rendezvous-test-config");
const createTempConfigFile = (dirName: string, content: string): string => {
const dir = join(temporaryTestBaseDir, dirName);
mkdirSync(dir, { recursive: true });
const filePath = join(dir, "rv.config.json");
writeFileSync(filePath, content, "utf-8");
return filePath;
};
afterAll(() => {
rmSync(temporaryTestBaseDir, { recursive: true, force: true });
});
describe("Config loading and validation", () => {
it("throws when the config file does not exist", () => {
// Arrange
const nonExistentPath = "/tmp/nonexistent-rv-config.json";
// Act & Assert
expect(() => loadConfig(nonExistentPath)).toThrow("Config file not found:");
});
it("throws when the config file contains invalid JSON", () => {
// Arrange
const filePath = createTempConfigFile("invalid-json", "not json {{{");
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(
"Failed to parse config file as JSON:",
);
});
it("throws when the config file is not a JSON object", () => {
// Arrange
const filePath = createTempConfigFile("array-root", "[1, 2, 3]");
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(
"Config file must contain a JSON object.",
);
});
it("returns an empty config for an empty JSON object", () => {
// Arrange
const filePath = createTempConfigFile("empty-object", "{}");
// Act
const { config } = loadConfig(filePath);
// Assert
expect(config).toEqual({});
});
it("parses a valid config with all fields", () => {
// Arrange
const content = JSON.stringify({
accounts: [
{
name: "whale_1",
address: "SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE",
},
],
accounts_mode: "overwrite",
seed: 42,
runs: 500,
bail: true,
regr: false,
dial: "./dialers.cjs",
});
const filePath = createTempConfigFile("all-fields", content);
// Act
const { config, unknownKeys } = loadConfig(filePath);
// Assert
expect(config.accounts).toEqual([
{ name: "whale_1", address: "SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE" },
]);
expect(config.accounts_mode).toBe("overwrite");
expect(config.seed).toBe(42);
expect(config.runs).toBe(500);
expect(config.bail).toBe(true);
expect(config.regr).toBe(false);
expect(config.dial).toBe("./dialers.cjs");
expect(unknownKeys).toEqual([]);
});
it("reports unrecognized keys", () => {
// Arrange
const filePath = createTempConfigFile(
"unknown-keys",
JSON.stringify({ sedd: 42, rans: 100, seed: 1 }),
);
// Act
const { config, unknownKeys } = loadConfig(filePath);
// Assert
expect(unknownKeys).toEqual(["sedd", "rans"]);
expect(config.seed).toBe(1);
});
it("throws when accounts is not an array", () => {
// Arrange
const filePath = createTempConfigFile(
"accounts-not-array",
JSON.stringify({ accounts: "not-an-array" }),
);
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(`"accounts" must be an array.`);
});
it("throws when an account entry is missing the name field", () => {
// Arrange
const filePath = createTempConfigFile(
"account-no-name",
JSON.stringify({
accounts: [{ address: "SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE" }],
}),
);
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(
`"accounts[0].name" must be a non-empty string.`,
);
});
it("throws when an account entry is missing the address field", () => {
// Arrange
const filePath = createTempConfigFile(
"account-no-address",
JSON.stringify({ accounts: [{ name: "whale_1" }] }),
);
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(
`"accounts[0].address" must be a non-empty string.`,
);
});
it("throws when accounts_mode is invalid", () => {
// Arrange
const filePath = createTempConfigFile(
"invalid-mode",
JSON.stringify({ accounts_mode: "merge" }),
);
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(
`"accounts_mode" must be "concatenate" or "overwrite".`,
);
});
it("throws when seed is not an integer", () => {
// Arrange
const filePath = createTempConfigFile(
"seed-float",
JSON.stringify({ seed: 1.5 }),
);
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(`"seed" must be an integer.`);
});
it("throws when runs is not a positive integer", () => {
// Arrange
const filePath = createTempConfigFile(
"runs-zero",
JSON.stringify({ runs: 0 }),
);
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(
`"runs" must be a positive integer.`,
);
});
it("throws when bail is not a boolean", () => {
// Arrange
const filePath = createTempConfigFile(
"bail-string",
JSON.stringify({ bail: "yes" }),
);
// Act & Assert
expect(() => loadConfig(filePath)).toThrow(`"bail" must be a boolean.`);
});
});
describe("Account resolution", () => {
const simnetAccounts = new Map([
["deployer", "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM"],
["wallet_1", "ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5"],
["faucet", "STNHKEPYEPJ8ET55ZZ0M5A34J0R3N5FM2CMMMAZ6"],
]);
it("filters out faucet when no config accounts are provided", () => {
// Arrange & Act
const { eligibleAccounts, allAddresses } = resolveAccounts(
simnetAccounts,
undefined,
);
// Assert
expect(eligibleAccounts.has("faucet")).toBe(false);
expect(eligibleAccounts.has("deployer")).toBe(true);
expect(eligibleAccounts.has("wallet_1")).toBe(true);
expect(allAddresses).toContain("STNHKEPYEPJ8ET55ZZ0M5A34J0R3N5FM2CMMMAZ6");
});
it("overwrites simnet accounts with config accounts by default", () => {
// Arrange
const configAccounts: ConfigAccount[] = [
{ name: "whale_1", address: "SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE" },
];
// Act
const { eligibleAccounts, allAddresses } = resolveAccounts(
simnetAccounts,
configAccounts,
);
// Assert
expect(eligibleAccounts.size).toBe(1);
expect(eligibleAccounts.has("whale_1")).toBe(true);
expect(eligibleAccounts.has("deployer")).toBe(false);
expect(allAddresses).toEqual(["SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE"]);
});
it("overwrites simnet accounts when mode is overwrite", () => {
// Arrange
const configAccounts: ConfigAccount[] = [
{ name: "whale_1", address: "SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE" },
];
// Act
const { eligibleAccounts, allAddresses } = resolveAccounts(
simnetAccounts,
configAccounts,
"overwrite",
);
// Assert
expect(eligibleAccounts.size).toBe(1);
expect(eligibleAccounts.has("whale_1")).toBe(true);
expect(eligibleAccounts.has("deployer")).toBe(false);
expect(allAddresses).toEqual(["SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE"]);
});
it("config accounts override simnet accounts with the same name when concatenating", () => {
// Arrange
const configAccounts: ConfigAccount[] = [
{
name: "wallet_1",
address: "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7",
},
];
// Act
const { eligibleAccounts } = resolveAccounts(
simnetAccounts,
configAccounts,
"concatenate",
);
// Assert
expect(eligibleAccounts.get("wallet_1")).toBe(
"SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7",
);
});
it("returns simnet accounts unchanged when config accounts list is empty", () => {
// Arrange & Act
const { eligibleAccounts } = resolveAccounts(simnetAccounts, []);
// Assert
expect(eligibleAccounts.has("deployer")).toBe(true);
expect(eligibleAccounts.has("wallet_1")).toBe(true);
expect(eligibleAccounts.has("faucet")).toBe(false);
expect(eligibleAccounts.size).toBe(2);
});
});