postgresai 0.14.0-dev.53 → 0.14.0-dev.54
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/README.md +34 -35
- package/bin/postgres-ai.ts +436 -4
- package/bun.lock +3 -1
- package/bunfig.toml +11 -0
- package/dist/bin/postgres-ai.js +2184 -218
- package/lib/auth-server.ts +52 -5
- package/lib/checkup-api.ts +386 -0
- package/lib/checkup.ts +1327 -0
- package/lib/config.ts +3 -0
- package/lib/issues.ts +5 -41
- package/lib/metrics-embedded.ts +79 -0
- package/lib/metrics-loader.ts +127 -0
- package/lib/util.ts +61 -0
- package/package.json +12 -6
- package/packages/postgres-ai/README.md +26 -0
- package/packages/postgres-ai/bin/postgres-ai.js +27 -0
- package/packages/postgres-ai/package.json +27 -0
- package/scripts/embed-metrics.ts +154 -0
- package/test/checkup.integration.test.ts +273 -0
- package/test/checkup.test.ts +890 -0
- package/test/init.integration.test.ts +36 -33
- package/test/schema-validation.test.ts +81 -0
- package/test/test-utils.ts +122 -0
- package/dist/sql/01.role.sql +0 -16
- package/dist/sql/02.permissions.sql +0 -37
- package/dist/sql/03.optional_rds.sql +0 -6
- package/dist/sql/04.optional_self_managed.sql +0 -8
- package/dist/sql/05.helpers.sql +0 -415
|
@@ -0,0 +1,890 @@
|
|
|
1
|
+
import { describe, test, expect } from "bun:test";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
|
|
4
|
+
// Import from source directly since we're using Bun
|
|
5
|
+
import * as checkup from "../lib/checkup";
|
|
6
|
+
import * as api from "../lib/checkup-api";
|
|
7
|
+
import { createMockClient } from "./test-utils";
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
function runCli(args: string[], env: Record<string, string> = {}) {
|
|
11
|
+
const cliPath = resolve(import.meta.dir, "..", "bin", "postgres-ai.ts");
|
|
12
|
+
const bunBin = typeof process.execPath === "string" && process.execPath.length > 0 ? process.execPath : "bun";
|
|
13
|
+
const result = Bun.spawnSync([bunBin, cliPath, ...args], {
|
|
14
|
+
env: { ...process.env, ...env },
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
status: result.exitCode,
|
|
18
|
+
stdout: new TextDecoder().decode(result.stdout),
|
|
19
|
+
stderr: new TextDecoder().decode(result.stderr),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Unit tests for parseVersionNum
|
|
24
|
+
describe("parseVersionNum", () => {
|
|
25
|
+
test("parses PG 16.3 version number", () => {
|
|
26
|
+
const result = checkup.parseVersionNum("160003");
|
|
27
|
+
expect(result.major).toBe("16");
|
|
28
|
+
expect(result.minor).toBe("3");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("parses PG 15.7 version number", () => {
|
|
32
|
+
const result = checkup.parseVersionNum("150007");
|
|
33
|
+
expect(result.major).toBe("15");
|
|
34
|
+
expect(result.minor).toBe("7");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("parses PG 14.12 version number", () => {
|
|
38
|
+
const result = checkup.parseVersionNum("140012");
|
|
39
|
+
expect(result.major).toBe("14");
|
|
40
|
+
expect(result.minor).toBe("12");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("handles empty string", () => {
|
|
44
|
+
const result = checkup.parseVersionNum("");
|
|
45
|
+
expect(result.major).toBe("");
|
|
46
|
+
expect(result.minor).toBe("");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("handles null/undefined", () => {
|
|
50
|
+
const result = checkup.parseVersionNum(null as any);
|
|
51
|
+
expect(result.major).toBe("");
|
|
52
|
+
expect(result.minor).toBe("");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("handles short string", () => {
|
|
56
|
+
const result = checkup.parseVersionNum("123");
|
|
57
|
+
expect(result.major).toBe("");
|
|
58
|
+
expect(result.minor).toBe("");
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Unit tests for createBaseReport
|
|
63
|
+
describe("createBaseReport", () => {
|
|
64
|
+
test("creates correct structure", () => {
|
|
65
|
+
const report = checkup.createBaseReport("A002", "Postgres major version", "test-node");
|
|
66
|
+
|
|
67
|
+
expect(report.checkId).toBe("A002");
|
|
68
|
+
expect(report.checkTitle).toBe("Postgres major version");
|
|
69
|
+
expect(typeof report.version).toBe("string");
|
|
70
|
+
expect(report.version!.length).toBeGreaterThan(0);
|
|
71
|
+
expect(typeof report.build_ts).toBe("string");
|
|
72
|
+
expect(report.nodes.primary).toBe("test-node");
|
|
73
|
+
expect(report.nodes.standbys).toEqual([]);
|
|
74
|
+
expect(report.results).toEqual({});
|
|
75
|
+
expect(typeof report.timestamptz).toBe("string");
|
|
76
|
+
// Verify timestamp is ISO format
|
|
77
|
+
expect(new Date(report.timestamptz).toISOString()).toBe(report.timestamptz);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("uses provided node name", () => {
|
|
81
|
+
const report = checkup.createBaseReport("A003", "Postgres settings", "my-custom-node");
|
|
82
|
+
expect(report.nodes.primary).toBe("my-custom-node");
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Tests for CHECK_INFO
|
|
87
|
+
describe("CHECK_INFO and REPORT_GENERATORS", () => {
|
|
88
|
+
const expectedChecks: Record<string, string> = {
|
|
89
|
+
A002: "Postgres major version",
|
|
90
|
+
A003: "Postgres settings",
|
|
91
|
+
A004: "Cluster information",
|
|
92
|
+
A007: "Altered settings",
|
|
93
|
+
A013: "Postgres minor version",
|
|
94
|
+
D004: "pg_stat_statements and pg_stat_kcache settings",
|
|
95
|
+
F001: "Autovacuum: current settings",
|
|
96
|
+
G001: "Memory-related settings",
|
|
97
|
+
H001: "Invalid indexes",
|
|
98
|
+
H002: "Unused indexes",
|
|
99
|
+
H004: "Redundant indexes",
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
test("CHECK_INFO contains all expected checks with correct descriptions", () => {
|
|
103
|
+
for (const [checkId, description] of Object.entries(expectedChecks)) {
|
|
104
|
+
expect(checkup.CHECK_INFO[checkId]).toBe(description);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("REPORT_GENERATORS has function for each check", () => {
|
|
109
|
+
for (const checkId of Object.keys(expectedChecks)) {
|
|
110
|
+
expect(typeof checkup.REPORT_GENERATORS[checkId]).toBe("function");
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("REPORT_GENERATORS and CHECK_INFO have same keys", () => {
|
|
115
|
+
const generatorKeys = Object.keys(checkup.REPORT_GENERATORS).sort();
|
|
116
|
+
const infoKeys = Object.keys(checkup.CHECK_INFO).sort();
|
|
117
|
+
expect(generatorKeys).toEqual(infoKeys);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Tests for formatBytes
|
|
122
|
+
describe("formatBytes", () => {
|
|
123
|
+
test("formats zero bytes", () => {
|
|
124
|
+
expect(checkup.formatBytes(0)).toBe("0 B");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("formats bytes", () => {
|
|
128
|
+
expect(checkup.formatBytes(500)).toBe("500.00 B");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("formats kibibytes", () => {
|
|
132
|
+
expect(checkup.formatBytes(1024)).toBe("1.00 KiB");
|
|
133
|
+
expect(checkup.formatBytes(1536)).toBe("1.50 KiB");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("formats mebibytes", () => {
|
|
137
|
+
expect(checkup.formatBytes(1048576)).toBe("1.00 MiB");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("formats gibibytes", () => {
|
|
141
|
+
expect(checkup.formatBytes(1073741824)).toBe("1.00 GiB");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("handles negative bytes", () => {
|
|
145
|
+
expect(checkup.formatBytes(-1024)).toBe("-1.00 KiB");
|
|
146
|
+
expect(checkup.formatBytes(-1048576)).toBe("-1.00 MiB");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("handles edge cases", () => {
|
|
150
|
+
expect(checkup.formatBytes(NaN)).toBe("NaN B");
|
|
151
|
+
expect(checkup.formatBytes(Infinity)).toBe("Infinity B");
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Mock client tests for report generators
|
|
156
|
+
describe("Report generators with mock client", () => {
|
|
157
|
+
test("getPostgresVersion extracts version info", async () => {
|
|
158
|
+
const mockClient = createMockClient({
|
|
159
|
+
versionRows: [
|
|
160
|
+
{ name: "server_version", setting: "16.3" },
|
|
161
|
+
{ name: "server_version_num", setting: "160003" },
|
|
162
|
+
],
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const version = await checkup.getPostgresVersion(mockClient as any);
|
|
166
|
+
expect(version.version).toBe("16.3");
|
|
167
|
+
expect(version.server_version_num).toBe("160003");
|
|
168
|
+
expect(version.server_major_ver).toBe("16");
|
|
169
|
+
expect(version.server_minor_ver).toBe("3");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("getSettings transforms rows to keyed object", async () => {
|
|
173
|
+
const mockClient = createMockClient({
|
|
174
|
+
settingsRows: [
|
|
175
|
+
{
|
|
176
|
+
tag_setting_name: "shared_buffers",
|
|
177
|
+
tag_setting_value: "16384",
|
|
178
|
+
tag_unit: "8kB",
|
|
179
|
+
tag_category: "Resource Usage / Memory",
|
|
180
|
+
tag_vartype: "integer",
|
|
181
|
+
is_default: 1,
|
|
182
|
+
setting_normalized: "134217728", // 16384 * 8192
|
|
183
|
+
unit_normalized: "bytes",
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
tag_setting_name: "work_mem",
|
|
187
|
+
tag_setting_value: "4096",
|
|
188
|
+
tag_unit: "kB",
|
|
189
|
+
tag_category: "Resource Usage / Memory",
|
|
190
|
+
tag_vartype: "integer",
|
|
191
|
+
is_default: 1,
|
|
192
|
+
setting_normalized: "4194304", // 4096 * 1024
|
|
193
|
+
unit_normalized: "bytes",
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const settings = await checkup.getSettings(mockClient as any);
|
|
199
|
+
expect("shared_buffers" in settings).toBe(true);
|
|
200
|
+
expect("work_mem" in settings).toBe(true);
|
|
201
|
+
expect(settings.shared_buffers.setting).toBe("16384");
|
|
202
|
+
expect(settings.shared_buffers.unit).toBe("8kB");
|
|
203
|
+
// pretty_value is now computed from setting_normalized
|
|
204
|
+
expect(settings.shared_buffers.pretty_value).toBe("128.00 MiB");
|
|
205
|
+
expect(settings.work_mem.pretty_value).toBe("4.00 MiB");
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("generateA002 creates report with version data", async () => {
|
|
209
|
+
const mockClient = createMockClient({
|
|
210
|
+
versionRows: [
|
|
211
|
+
{ name: "server_version", setting: "16.3" },
|
|
212
|
+
{ name: "server_version_num", setting: "160003" },
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const report = await checkup.generateA002(mockClient as any, "test-node");
|
|
217
|
+
expect(report.checkId).toBe("A002");
|
|
218
|
+
expect(report.checkTitle).toBe("Postgres major version");
|
|
219
|
+
expect(report.nodes.primary).toBe("test-node");
|
|
220
|
+
expect("test-node" in report.results).toBe(true);
|
|
221
|
+
expect("version" in report.results["test-node"].data).toBe(true);
|
|
222
|
+
expect(report.results["test-node"].data.version.version).toBe("16.3");
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("generateA003 creates report with settings and version", async () => {
|
|
226
|
+
const mockClient = createMockClient({
|
|
227
|
+
versionRows: [
|
|
228
|
+
{ name: "server_version", setting: "16.3" },
|
|
229
|
+
{ name: "server_version_num", setting: "160003" },
|
|
230
|
+
],
|
|
231
|
+
settingsRows: [
|
|
232
|
+
{
|
|
233
|
+
tag_setting_name: "shared_buffers",
|
|
234
|
+
tag_setting_value: "16384",
|
|
235
|
+
tag_unit: "8kB",
|
|
236
|
+
tag_category: "Resource Usage / Memory",
|
|
237
|
+
tag_vartype: "integer",
|
|
238
|
+
is_default: 1,
|
|
239
|
+
setting_normalized: "134217728",
|
|
240
|
+
unit_normalized: "bytes",
|
|
241
|
+
},
|
|
242
|
+
],
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
const report = await checkup.generateA003(mockClient as any, "test-node");
|
|
246
|
+
expect(report.checkId).toBe("A003");
|
|
247
|
+
expect(report.checkTitle).toBe("Postgres settings");
|
|
248
|
+
expect("test-node" in report.results).toBe(true);
|
|
249
|
+
expect("shared_buffers" in report.results["test-node"].data).toBe(true);
|
|
250
|
+
expect(report.results["test-node"].postgres_version).toBeTruthy();
|
|
251
|
+
expect(report.results["test-node"].postgres_version!.version).toBe("16.3");
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test("generateA013 creates report with minor version data", async () => {
|
|
255
|
+
const mockClient = createMockClient({
|
|
256
|
+
versionRows: [
|
|
257
|
+
{ name: "server_version", setting: "16.3" },
|
|
258
|
+
{ name: "server_version_num", setting: "160003" },
|
|
259
|
+
],
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
const report = await checkup.generateA013(mockClient as any, "test-node");
|
|
263
|
+
expect(report.checkId).toBe("A013");
|
|
264
|
+
expect(report.checkTitle).toBe("Postgres minor version");
|
|
265
|
+
expect(report.nodes.primary).toBe("test-node");
|
|
266
|
+
expect("test-node" in report.results).toBe(true);
|
|
267
|
+
expect("version" in report.results["test-node"].data).toBe(true);
|
|
268
|
+
expect(report.results["test-node"].data.version.server_minor_ver).toBe("3");
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test("generateAllReports returns reports for all checks", async () => {
|
|
272
|
+
const mockClient = createMockClient({
|
|
273
|
+
versionRows: [
|
|
274
|
+
{ name: "server_version", setting: "16.3" },
|
|
275
|
+
{ name: "server_version_num", setting: "160003" },
|
|
276
|
+
],
|
|
277
|
+
settingsRows: [
|
|
278
|
+
{
|
|
279
|
+
tag_setting_name: "shared_buffers",
|
|
280
|
+
tag_setting_value: "16384",
|
|
281
|
+
tag_unit: "8kB",
|
|
282
|
+
tag_category: "Resource Usage / Memory",
|
|
283
|
+
tag_vartype: "integer",
|
|
284
|
+
is_default: 0, // Non-default for A007
|
|
285
|
+
setting_normalized: "134217728",
|
|
286
|
+
unit_normalized: "bytes",
|
|
287
|
+
},
|
|
288
|
+
],
|
|
289
|
+
databaseSizesRows: [{ datname: "postgres", size_bytes: "1073741824" }],
|
|
290
|
+
dbStatsRows: [{
|
|
291
|
+
numbackends: 5,
|
|
292
|
+
xact_commit: 100,
|
|
293
|
+
xact_rollback: 1,
|
|
294
|
+
blks_read: 1000,
|
|
295
|
+
blks_hit: 9000,
|
|
296
|
+
tup_returned: 500,
|
|
297
|
+
tup_fetched: 400,
|
|
298
|
+
tup_inserted: 50,
|
|
299
|
+
tup_updated: 30,
|
|
300
|
+
tup_deleted: 10,
|
|
301
|
+
deadlocks: 0,
|
|
302
|
+
temp_files: 0,
|
|
303
|
+
temp_bytes: 0,
|
|
304
|
+
postmaster_uptime_s: 864000
|
|
305
|
+
}],
|
|
306
|
+
connectionStatesRows: [{ state: "active", count: 2 }, { state: "idle", count: 3 }],
|
|
307
|
+
uptimeRows: [{ start_time: new Date("2024-01-01T00:00:00Z"), uptime: "10 days" }],
|
|
308
|
+
invalidIndexesRows: [],
|
|
309
|
+
unusedIndexesRows: [],
|
|
310
|
+
redundantIndexesRows: [],
|
|
311
|
+
}
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const reports = await checkup.generateAllReports(mockClient as any, "test-node");
|
|
315
|
+
expect("A002" in reports).toBe(true);
|
|
316
|
+
expect("A003" in reports).toBe(true);
|
|
317
|
+
expect("A004" in reports).toBe(true);
|
|
318
|
+
expect("A007" in reports).toBe(true);
|
|
319
|
+
expect("A013" in reports).toBe(true);
|
|
320
|
+
expect("H001" in reports).toBe(true);
|
|
321
|
+
expect("H002" in reports).toBe(true);
|
|
322
|
+
expect("H004" in reports).toBe(true);
|
|
323
|
+
expect(reports.A002.checkId).toBe("A002");
|
|
324
|
+
expect(reports.A003.checkId).toBe("A003");
|
|
325
|
+
expect(reports.A004.checkId).toBe("A004");
|
|
326
|
+
expect(reports.A007.checkId).toBe("A007");
|
|
327
|
+
expect(reports.A013.checkId).toBe("A013");
|
|
328
|
+
expect(reports.H001.checkId).toBe("H001");
|
|
329
|
+
expect(reports.H002.checkId).toBe("H002");
|
|
330
|
+
expect(reports.H004.checkId).toBe("H004");
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// Tests for A007 (Altered settings)
|
|
335
|
+
describe("A007 - Altered settings", () => {
|
|
336
|
+
test("getAlteredSettings returns non-default settings", async () => {
|
|
337
|
+
const mockClient = createMockClient({
|
|
338
|
+
settingsRows: [
|
|
339
|
+
{ tag_setting_name: "shared_buffers", tag_setting_value: "256MB", tag_unit: "", tag_category: "Resource Usage / Memory", tag_vartype: "string", is_default: 0, setting_normalized: null, unit_normalized: null },
|
|
340
|
+
{ tag_setting_name: "work_mem", tag_setting_value: "64MB", tag_unit: "", tag_category: "Resource Usage / Memory", tag_vartype: "string", is_default: 0, setting_normalized: null, unit_normalized: null },
|
|
341
|
+
{ tag_setting_name: "default_setting", tag_setting_value: "on", tag_unit: "", tag_category: "Other", tag_vartype: "bool", is_default: 1, setting_normalized: null, unit_normalized: null },
|
|
342
|
+
],
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
const settings = await checkup.getAlteredSettings(mockClient as any);
|
|
346
|
+
expect("shared_buffers" in settings).toBe(true);
|
|
347
|
+
expect("work_mem" in settings).toBe(true);
|
|
348
|
+
expect("default_setting" in settings).toBe(false); // Should be filtered out
|
|
349
|
+
expect(settings.shared_buffers.value).toBe("256MB");
|
|
350
|
+
expect(settings.work_mem.value).toBe("64MB");
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
test("generateA007 creates report with altered settings", async () => {
|
|
354
|
+
const mockClient = createMockClient({
|
|
355
|
+
versionRows: [
|
|
356
|
+
{ name: "server_version", setting: "16.3" },
|
|
357
|
+
{ name: "server_version_num", setting: "160003" },
|
|
358
|
+
],
|
|
359
|
+
settingsRows: [
|
|
360
|
+
{ tag_setting_name: "max_connections", tag_setting_value: "200", tag_unit: "", tag_category: "Connections and Authentication", tag_vartype: "integer", is_default: 0, setting_normalized: null, unit_normalized: null },
|
|
361
|
+
],
|
|
362
|
+
}
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
const report = await checkup.generateA007(mockClient as any, "test-node");
|
|
366
|
+
expect(report.checkId).toBe("A007");
|
|
367
|
+
expect(report.checkTitle).toBe("Altered settings");
|
|
368
|
+
expect(report.nodes.primary).toBe("test-node");
|
|
369
|
+
expect("test-node" in report.results).toBe(true);
|
|
370
|
+
expect("max_connections" in report.results["test-node"].data).toBe(true);
|
|
371
|
+
expect(report.results["test-node"].data.max_connections.value).toBe("200");
|
|
372
|
+
expect(report.results["test-node"].postgres_version).toBeTruthy();
|
|
373
|
+
});
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
// Tests for A004 (Cluster information)
|
|
377
|
+
describe("A004 - Cluster information", () => {
|
|
378
|
+
test("getDatabaseSizes returns database sizes", async () => {
|
|
379
|
+
const mockClient = createMockClient({
|
|
380
|
+
databaseSizesRows: [
|
|
381
|
+
{ datname: "postgres", size_bytes: "1073741824" },
|
|
382
|
+
{ datname: "mydb", size_bytes: "536870912" },
|
|
383
|
+
],
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
const sizes = await checkup.getDatabaseSizes(mockClient as any);
|
|
387
|
+
expect("postgres" in sizes).toBe(true);
|
|
388
|
+
expect("mydb" in sizes).toBe(true);
|
|
389
|
+
expect(sizes.postgres).toBe(1073741824);
|
|
390
|
+
expect(sizes.mydb).toBe(536870912);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test("getClusterInfo returns cluster metrics", async () => {
|
|
394
|
+
const mockClient = createMockClient({
|
|
395
|
+
dbStatsRows: [{
|
|
396
|
+
numbackends: 10,
|
|
397
|
+
xact_commit: 1000,
|
|
398
|
+
xact_rollback: 5,
|
|
399
|
+
blks_read: 500,
|
|
400
|
+
blks_hit: 9500,
|
|
401
|
+
tup_returned: 5000,
|
|
402
|
+
tup_fetched: 4000,
|
|
403
|
+
tup_inserted: 100,
|
|
404
|
+
tup_updated: 50,
|
|
405
|
+
tup_deleted: 25,
|
|
406
|
+
deadlocks: 0,
|
|
407
|
+
temp_files: 2,
|
|
408
|
+
temp_bytes: 1048576,
|
|
409
|
+
postmaster_uptime_s: 2592000, // 30 days
|
|
410
|
+
}],
|
|
411
|
+
connectionStatesRows: [
|
|
412
|
+
{ state: "active", count: 3 },
|
|
413
|
+
{ state: "idle", count: 7 },
|
|
414
|
+
],
|
|
415
|
+
uptimeRows: [{
|
|
416
|
+
start_time: new Date("2024-01-01T00:00:00Z"),
|
|
417
|
+
uptime: "30 days",
|
|
418
|
+
}],
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
const info = await checkup.getClusterInfo(mockClient as any);
|
|
422
|
+
expect("total_connections" in info).toBe(true);
|
|
423
|
+
expect("cache_hit_ratio" in info).toBe(true);
|
|
424
|
+
expect("connections_active" in info).toBe(true);
|
|
425
|
+
expect("connections_idle" in info).toBe(true);
|
|
426
|
+
expect("start_time" in info).toBe(true);
|
|
427
|
+
expect(info.total_connections.value).toBe("10");
|
|
428
|
+
expect(info.cache_hit_ratio.value).toBe("95.00");
|
|
429
|
+
expect(info.connections_active.value).toBe("3");
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
test("generateA004 creates report with cluster info and database sizes", async () => {
|
|
433
|
+
const mockClient = createMockClient({
|
|
434
|
+
versionRows: [
|
|
435
|
+
{ name: "server_version", setting: "16.3" },
|
|
436
|
+
{ name: "server_version_num", setting: "160003" },
|
|
437
|
+
],
|
|
438
|
+
databaseSizesRows: [
|
|
439
|
+
{ datname: "postgres", size_bytes: "1073741824" },
|
|
440
|
+
],
|
|
441
|
+
dbStatsRows: [{
|
|
442
|
+
numbackends: 5,
|
|
443
|
+
xact_commit: 100,
|
|
444
|
+
xact_rollback: 1,
|
|
445
|
+
blks_read: 100,
|
|
446
|
+
blks_hit: 900,
|
|
447
|
+
tup_returned: 500,
|
|
448
|
+
tup_fetched: 400,
|
|
449
|
+
tup_inserted: 50,
|
|
450
|
+
tup_updated: 30,
|
|
451
|
+
tup_deleted: 10,
|
|
452
|
+
deadlocks: 0,
|
|
453
|
+
temp_files: 0,
|
|
454
|
+
temp_bytes: 0,
|
|
455
|
+
postmaster_uptime_s: 864000,
|
|
456
|
+
}],
|
|
457
|
+
connectionStatesRows: [{ state: "active", count: 2 }],
|
|
458
|
+
uptimeRows: [{ start_time: new Date("2024-01-01T00:00:00Z"), uptime: "10 days" }],
|
|
459
|
+
}
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
const report = await checkup.generateA004(mockClient as any, "test-node");
|
|
463
|
+
expect(report.checkId).toBe("A004");
|
|
464
|
+
expect(report.checkTitle).toBe("Cluster information");
|
|
465
|
+
expect(report.nodes.primary).toBe("test-node");
|
|
466
|
+
expect("test-node" in report.results).toBe(true);
|
|
467
|
+
|
|
468
|
+
const data = report.results["test-node"].data;
|
|
469
|
+
expect("general_info" in data).toBe(true);
|
|
470
|
+
expect("database_sizes" in data).toBe(true);
|
|
471
|
+
expect("total_connections" in data.general_info).toBe(true);
|
|
472
|
+
expect("postgres" in data.database_sizes).toBe(true);
|
|
473
|
+
expect(data.database_sizes.postgres).toBe(1073741824);
|
|
474
|
+
expect(report.results["test-node"].postgres_version).toBeTruthy();
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
// Tests for H001 (Invalid indexes)
|
|
479
|
+
describe("H001 - Invalid indexes", () => {
|
|
480
|
+
test("getInvalidIndexes returns invalid indexes", async () => {
|
|
481
|
+
const mockClient = createMockClient({
|
|
482
|
+
invalidIndexesRows: [
|
|
483
|
+
{ schema_name: "public", table_name: "users", index_name: "users_email_idx", relation_name: "users", index_size_bytes: "1048576", supports_fk: false },
|
|
484
|
+
],
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
const indexes = await checkup.getInvalidIndexes(mockClient as any);
|
|
488
|
+
expect(indexes.length).toBe(1);
|
|
489
|
+
expect(indexes[0].schema_name).toBe("public");
|
|
490
|
+
expect(indexes[0].table_name).toBe("users");
|
|
491
|
+
expect(indexes[0].index_name).toBe("users_email_idx");
|
|
492
|
+
expect(indexes[0].index_size_bytes).toBe(1048576);
|
|
493
|
+
expect(indexes[0].index_size_pretty).toBeTruthy();
|
|
494
|
+
expect(indexes[0].relation_name).toBe("users");
|
|
495
|
+
expect(indexes[0].supports_fk).toBe(false);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
test("generateH001 creates report with invalid indexes", async () => {
|
|
499
|
+
const mockClient = createMockClient({
|
|
500
|
+
versionRows: [
|
|
501
|
+
{ name: "server_version", setting: "16.3" },
|
|
502
|
+
{ name: "server_version_num", setting: "160003" },
|
|
503
|
+
],
|
|
504
|
+
invalidIndexesRows: [
|
|
505
|
+
{ schema_name: "public", table_name: "orders", index_name: "orders_status_idx", relation_name: "orders", index_size_bytes: "2097152", supports_fk: false },
|
|
506
|
+
],
|
|
507
|
+
}
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
const report = await checkup.generateH001(mockClient as any, "test-node");
|
|
511
|
+
expect(report.checkId).toBe("H001");
|
|
512
|
+
expect(report.checkTitle).toBe("Invalid indexes");
|
|
513
|
+
expect("test-node" in report.results).toBe(true);
|
|
514
|
+
|
|
515
|
+
// Data is now keyed by database name
|
|
516
|
+
const data = report.results["test-node"].data;
|
|
517
|
+
expect("testdb" in data).toBe(true);
|
|
518
|
+
const dbData = data["testdb"] as any;
|
|
519
|
+
expect(dbData.invalid_indexes).toBeTruthy();
|
|
520
|
+
expect(dbData.total_count).toBe(1);
|
|
521
|
+
expect(dbData.total_size_bytes).toBe(2097152);
|
|
522
|
+
expect(dbData.total_size_pretty).toBeTruthy();
|
|
523
|
+
expect(dbData.database_size_bytes).toBeTruthy();
|
|
524
|
+
expect(dbData.database_size_pretty).toBeTruthy();
|
|
525
|
+
expect(report.results["test-node"].postgres_version).toBeTruthy();
|
|
526
|
+
});
|
|
527
|
+
// Top-level structure tests removed - covered by schema-validation.test.ts
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
// Tests for H002 (Unused indexes)
|
|
531
|
+
describe("H002 - Unused indexes", () => {
|
|
532
|
+
test("getUnusedIndexes returns unused indexes", async () => {
|
|
533
|
+
const mockClient = createMockClient({
|
|
534
|
+
unusedIndexesRows: [
|
|
535
|
+
{
|
|
536
|
+
schema_name: "public",
|
|
537
|
+
table_name: "products",
|
|
538
|
+
index_name: "products_old_idx",
|
|
539
|
+
index_definition: "CREATE INDEX products_old_idx ON public.products USING btree (old_column)",
|
|
540
|
+
reason: "Never Used Indexes",
|
|
541
|
+
index_size_bytes: "4194304",
|
|
542
|
+
idx_scan: "0",
|
|
543
|
+
idx_is_btree: true,
|
|
544
|
+
supports_fk: false,
|
|
545
|
+
},
|
|
546
|
+
],
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
const indexes = await checkup.getUnusedIndexes(mockClient as any);
|
|
550
|
+
expect(indexes.length).toBe(1);
|
|
551
|
+
expect(indexes[0].schema_name).toBe("public");
|
|
552
|
+
expect(indexes[0].index_name).toBe("products_old_idx");
|
|
553
|
+
expect(indexes[0].index_size_bytes).toBe(4194304);
|
|
554
|
+
expect(indexes[0].idx_scan).toBe(0);
|
|
555
|
+
expect(indexes[0].supports_fk).toBe(false);
|
|
556
|
+
expect(indexes[0].index_definition).toBeTruthy();
|
|
557
|
+
expect(indexes[0].idx_is_btree).toBe(true);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
test("generateH002 creates report with unused indexes", async () => {
|
|
561
|
+
const mockClient = createMockClient({
|
|
562
|
+
versionRows: [
|
|
563
|
+
{ name: "server_version", setting: "16.3" },
|
|
564
|
+
{ name: "server_version_num", setting: "160003" },
|
|
565
|
+
],
|
|
566
|
+
unusedIndexesRows: [
|
|
567
|
+
{
|
|
568
|
+
schema_name: "public",
|
|
569
|
+
table_name: "logs",
|
|
570
|
+
index_name: "logs_created_idx",
|
|
571
|
+
index_definition: "CREATE INDEX logs_created_idx ON public.logs USING btree (created_at)",
|
|
572
|
+
reason: "Never Used Indexes",
|
|
573
|
+
index_size_bytes: "8388608",
|
|
574
|
+
idx_scan: "0",
|
|
575
|
+
idx_is_btree: true,
|
|
576
|
+
supports_fk: false,
|
|
577
|
+
},
|
|
578
|
+
],
|
|
579
|
+
}
|
|
580
|
+
);
|
|
581
|
+
|
|
582
|
+
const report = await checkup.generateH002(mockClient as any, "test-node");
|
|
583
|
+
expect(report.checkId).toBe("H002");
|
|
584
|
+
expect(report.checkTitle).toBe("Unused indexes");
|
|
585
|
+
expect("test-node" in report.results).toBe(true);
|
|
586
|
+
|
|
587
|
+
// Data is now keyed by database name
|
|
588
|
+
const data = report.results["test-node"].data;
|
|
589
|
+
expect("testdb" in data).toBe(true);
|
|
590
|
+
const dbData = data["testdb"] as any;
|
|
591
|
+
expect(dbData.unused_indexes).toBeTruthy();
|
|
592
|
+
expect(dbData.total_count).toBe(1);
|
|
593
|
+
expect(dbData.total_size_bytes).toBe(8388608);
|
|
594
|
+
expect(dbData.total_size_pretty).toBeTruthy();
|
|
595
|
+
expect(dbData.stats_reset).toBeTruthy();
|
|
596
|
+
expect(report.results["test-node"].postgres_version).toBeTruthy();
|
|
597
|
+
});
|
|
598
|
+
// Top-level structure tests removed - covered by schema-validation.test.ts
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
// Tests for H004 (Redundant indexes)
|
|
602
|
+
describe("H004 - Redundant indexes", () => {
|
|
603
|
+
test("getRedundantIndexes returns redundant indexes", async () => {
|
|
604
|
+
const mockClient = createMockClient({
|
|
605
|
+
redundantIndexesRows: [
|
|
606
|
+
{
|
|
607
|
+
schema_name: "public",
|
|
608
|
+
table_name: "orders",
|
|
609
|
+
index_name: "orders_user_id_idx",
|
|
610
|
+
relation_name: "orders",
|
|
611
|
+
access_method: "btree",
|
|
612
|
+
reason: "public.orders_user_id_created_idx",
|
|
613
|
+
index_size_bytes: "2097152",
|
|
614
|
+
table_size_bytes: "16777216",
|
|
615
|
+
index_usage: "0",
|
|
616
|
+
supports_fk: false,
|
|
617
|
+
index_definition: "CREATE INDEX orders_user_id_idx ON public.orders USING btree (user_id)",
|
|
618
|
+
redundant_to_json: JSON.stringify([
|
|
619
|
+
{ index_name: "public.orders_user_id_created_idx", index_definition: "CREATE INDEX orders_user_id_created_idx ON public.orders USING btree (user_id, created_at)", index_size_bytes: 1048576 }
|
|
620
|
+
]),
|
|
621
|
+
},
|
|
622
|
+
],
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
const indexes = await checkup.getRedundantIndexes(mockClient as any);
|
|
626
|
+
expect(indexes.length).toBe(1);
|
|
627
|
+
expect(indexes[0].schema_name).toBe("public");
|
|
628
|
+
expect(indexes[0].index_name).toBe("orders_user_id_idx");
|
|
629
|
+
expect(indexes[0].reason).toBe("public.orders_user_id_created_idx");
|
|
630
|
+
expect(indexes[0].index_size_bytes).toBe(2097152);
|
|
631
|
+
expect(indexes[0].supports_fk).toBe(false);
|
|
632
|
+
expect(indexes[0].index_definition).toBeTruthy();
|
|
633
|
+
expect(indexes[0].relation_name).toBe("orders");
|
|
634
|
+
// Verify redundant_to is populated with definitions and sizes
|
|
635
|
+
expect(indexes[0].redundant_to).toBeInstanceOf(Array);
|
|
636
|
+
expect(indexes[0].redundant_to.length).toBe(1);
|
|
637
|
+
expect(indexes[0].redundant_to[0].index_name).toBe("public.orders_user_id_created_idx");
|
|
638
|
+
expect(indexes[0].redundant_to[0].index_definition).toContain("CREATE INDEX");
|
|
639
|
+
expect(indexes[0].redundant_to[0].index_size_bytes).toBe(1048576);
|
|
640
|
+
expect(indexes[0].redundant_to[0].index_size_pretty).toBe("1.00 MiB");
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
test("generateH004 creates report with redundant indexes", async () => {
|
|
644
|
+
const mockClient = createMockClient({
|
|
645
|
+
versionRows: [
|
|
646
|
+
{ name: "server_version", setting: "16.3" },
|
|
647
|
+
{ name: "server_version_num", setting: "160003" },
|
|
648
|
+
],
|
|
649
|
+
redundantIndexesRows: [
|
|
650
|
+
{
|
|
651
|
+
schema_name: "public",
|
|
652
|
+
table_name: "products",
|
|
653
|
+
index_name: "products_category_idx",
|
|
654
|
+
relation_name: "products",
|
|
655
|
+
access_method: "btree",
|
|
656
|
+
reason: "public.products_category_name_idx",
|
|
657
|
+
index_size_bytes: "4194304",
|
|
658
|
+
table_size_bytes: "33554432",
|
|
659
|
+
index_usage: "5",
|
|
660
|
+
supports_fk: false,
|
|
661
|
+
index_definition: "CREATE INDEX products_category_idx ON public.products USING btree (category)",
|
|
662
|
+
redundant_to_json: JSON.stringify([
|
|
663
|
+
{ index_name: "public.products_category_name_idx", index_definition: "CREATE INDEX products_category_name_idx ON public.products USING btree (category, name)", index_size_bytes: 2097152 }
|
|
664
|
+
]),
|
|
665
|
+
},
|
|
666
|
+
],
|
|
667
|
+
}
|
|
668
|
+
);
|
|
669
|
+
|
|
670
|
+
const report = await checkup.generateH004(mockClient as any, "test-node");
|
|
671
|
+
expect(report.checkId).toBe("H004");
|
|
672
|
+
expect(report.checkTitle).toBe("Redundant indexes");
|
|
673
|
+
expect("test-node" in report.results).toBe(true);
|
|
674
|
+
|
|
675
|
+
// Data is now keyed by database name
|
|
676
|
+
const data = report.results["test-node"].data;
|
|
677
|
+
expect("testdb" in data).toBe(true);
|
|
678
|
+
const dbData = data["testdb"] as any;
|
|
679
|
+
expect(dbData.redundant_indexes).toBeTruthy();
|
|
680
|
+
expect(dbData.total_count).toBe(1);
|
|
681
|
+
expect(dbData.total_size_bytes).toBe(4194304);
|
|
682
|
+
expect(dbData.total_size_pretty).toBeTruthy();
|
|
683
|
+
expect(dbData.database_size_bytes).toBeTruthy();
|
|
684
|
+
expect(report.results["test-node"].postgres_version).toBeTruthy();
|
|
685
|
+
});
|
|
686
|
+
// Top-level structure tests removed - covered by schema-validation.test.ts
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
// CLI tests
|
|
690
|
+
describe("CLI tests", () => {
|
|
691
|
+
test("checkup command exists and shows help", () => {
|
|
692
|
+
const r = runCli(["checkup", "--help"]);
|
|
693
|
+
expect(r.status).toBe(0);
|
|
694
|
+
expect(r.stdout).toMatch(/express mode/i);
|
|
695
|
+
expect(r.stdout).toMatch(/--check-id/);
|
|
696
|
+
expect(r.stdout).toMatch(/--node-name/);
|
|
697
|
+
expect(r.stdout).toMatch(/--output/);
|
|
698
|
+
expect(r.stdout).toMatch(/upload/);
|
|
699
|
+
expect(r.stdout).toMatch(/--json/);
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
test("checkup --help shows available check IDs", () => {
|
|
703
|
+
const r = runCli(["checkup", "--help"]);
|
|
704
|
+
expect(r.status).toBe(0);
|
|
705
|
+
expect(r.stdout).toMatch(/A002/);
|
|
706
|
+
expect(r.stdout).toMatch(/A003/);
|
|
707
|
+
expect(r.stdout).toMatch(/A004/);
|
|
708
|
+
expect(r.stdout).toMatch(/A007/);
|
|
709
|
+
expect(r.stdout).toMatch(/A013/);
|
|
710
|
+
expect(r.stdout).toMatch(/H001/);
|
|
711
|
+
expect(r.stdout).toMatch(/H002/);
|
|
712
|
+
expect(r.stdout).toMatch(/H004/);
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
test("checkup without connection shows help", () => {
|
|
716
|
+
const r = runCli(["checkup"]);
|
|
717
|
+
expect(r.status).not.toBe(0);
|
|
718
|
+
// Should show full help (options + examples), like `checkup --help`
|
|
719
|
+
expect(r.stdout).toMatch(/generate health check reports/i);
|
|
720
|
+
expect(r.stdout).toMatch(/--check-id/);
|
|
721
|
+
expect(r.stdout).toMatch(/available checks/i);
|
|
722
|
+
expect(r.stdout).toMatch(/A002/);
|
|
723
|
+
});
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
// Tests for checkup-api module
|
|
727
|
+
describe("checkup-api", () => {
|
|
728
|
+
test("formatRpcErrorForDisplay formats details/hint nicely", () => {
|
|
729
|
+
const err = new api.RpcError({
|
|
730
|
+
rpcName: "checkup_report_file_post",
|
|
731
|
+
statusCode: 402,
|
|
732
|
+
payloadText: JSON.stringify({
|
|
733
|
+
hint: "Start an express checkup subscription for the organization or contact support.",
|
|
734
|
+
details: "Checkup report uploads require an active checkup subscription",
|
|
735
|
+
}),
|
|
736
|
+
payloadJson: {
|
|
737
|
+
hint: "Start an express checkup subscription for the organization or contact support.",
|
|
738
|
+
details: "Checkup report uploads require an active checkup subscription.",
|
|
739
|
+
},
|
|
740
|
+
});
|
|
741
|
+
const lines = api.formatRpcErrorForDisplay(err);
|
|
742
|
+
const text = lines.join("\n");
|
|
743
|
+
expect(text).toMatch(/RPC checkup_report_file_post failed: HTTP 402/);
|
|
744
|
+
expect(text).toMatch(/Details:/);
|
|
745
|
+
expect(text).toMatch(/Hint:/);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
test("withRetry succeeds on first attempt", async () => {
|
|
749
|
+
let attempts = 0;
|
|
750
|
+
const result = await api.withRetry(async () => {
|
|
751
|
+
attempts++;
|
|
752
|
+
return "success";
|
|
753
|
+
});
|
|
754
|
+
expect(result).toBe("success");
|
|
755
|
+
expect(attempts).toBe(1);
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
test("withRetry retries on retryable errors and succeeds", async () => {
|
|
759
|
+
let attempts = 0;
|
|
760
|
+
const result = await api.withRetry(
|
|
761
|
+
async () => {
|
|
762
|
+
attempts++;
|
|
763
|
+
if (attempts < 3) {
|
|
764
|
+
throw new Error("connection timeout");
|
|
765
|
+
}
|
|
766
|
+
return "success after retry";
|
|
767
|
+
},
|
|
768
|
+
{ maxAttempts: 3, initialDelayMs: 10 }
|
|
769
|
+
);
|
|
770
|
+
expect(result).toBe("success after retry");
|
|
771
|
+
expect(attempts).toBe(3);
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
test("withRetry calls onRetry callback", async () => {
|
|
775
|
+
let attempts = 0;
|
|
776
|
+
const retryLogs: string[] = [];
|
|
777
|
+
await api.withRetry(
|
|
778
|
+
async () => {
|
|
779
|
+
attempts++;
|
|
780
|
+
if (attempts < 2) {
|
|
781
|
+
throw new Error("socket hang up");
|
|
782
|
+
}
|
|
783
|
+
return "ok";
|
|
784
|
+
},
|
|
785
|
+
{ maxAttempts: 3, initialDelayMs: 10 },
|
|
786
|
+
(attempt, err, delayMs) => {
|
|
787
|
+
retryLogs.push(`attempt ${attempt}, delay ${delayMs}ms`);
|
|
788
|
+
}
|
|
789
|
+
);
|
|
790
|
+
expect(retryLogs.length).toBe(1);
|
|
791
|
+
expect(retryLogs[0]).toMatch(/attempt 1/);
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
test("withRetry does not retry on non-retryable errors", async () => {
|
|
795
|
+
let attempts = 0;
|
|
796
|
+
try {
|
|
797
|
+
await api.withRetry(
|
|
798
|
+
async () => {
|
|
799
|
+
attempts++;
|
|
800
|
+
throw new Error("invalid input");
|
|
801
|
+
},
|
|
802
|
+
{ maxAttempts: 3, initialDelayMs: 10 }
|
|
803
|
+
);
|
|
804
|
+
} catch (err) {
|
|
805
|
+
expect((err as Error).message).toBe("invalid input");
|
|
806
|
+
}
|
|
807
|
+
expect(attempts).toBe(1);
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
test("withRetry does not retry on 4xx RpcError", async () => {
|
|
811
|
+
let attempts = 0;
|
|
812
|
+
try {
|
|
813
|
+
await api.withRetry(
|
|
814
|
+
async () => {
|
|
815
|
+
attempts++;
|
|
816
|
+
throw new api.RpcError({
|
|
817
|
+
rpcName: "test",
|
|
818
|
+
statusCode: 400,
|
|
819
|
+
payloadText: "bad request",
|
|
820
|
+
payloadJson: null,
|
|
821
|
+
});
|
|
822
|
+
},
|
|
823
|
+
{ maxAttempts: 3, initialDelayMs: 10 }
|
|
824
|
+
);
|
|
825
|
+
} catch (err) {
|
|
826
|
+
expect(err).toBeInstanceOf(api.RpcError);
|
|
827
|
+
}
|
|
828
|
+
expect(attempts).toBe(1);
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
test("withRetry retries on 5xx RpcError", async () => {
|
|
832
|
+
let attempts = 0;
|
|
833
|
+
try {
|
|
834
|
+
await api.withRetry(
|
|
835
|
+
async () => {
|
|
836
|
+
attempts++;
|
|
837
|
+
throw new api.RpcError({
|
|
838
|
+
rpcName: "test",
|
|
839
|
+
statusCode: 503,
|
|
840
|
+
payloadText: "service unavailable",
|
|
841
|
+
payloadJson: null,
|
|
842
|
+
});
|
|
843
|
+
},
|
|
844
|
+
{ maxAttempts: 2, initialDelayMs: 10 }
|
|
845
|
+
);
|
|
846
|
+
} catch (err) {
|
|
847
|
+
expect(err).toBeInstanceOf(api.RpcError);
|
|
848
|
+
}
|
|
849
|
+
expect(attempts).toBe(2);
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
test("withRetry retries on timeout errors", async () => {
|
|
853
|
+
// Tests that timeout-like error messages are considered retryable
|
|
854
|
+
let attempts = 0;
|
|
855
|
+
try {
|
|
856
|
+
await api.withRetry(
|
|
857
|
+
async () => {
|
|
858
|
+
attempts++;
|
|
859
|
+
throw new Error("RPC test timed out after 30000ms (no response)");
|
|
860
|
+
},
|
|
861
|
+
{ maxAttempts: 3, initialDelayMs: 10 }
|
|
862
|
+
);
|
|
863
|
+
} catch (err) {
|
|
864
|
+
expect(err).toBeInstanceOf(Error);
|
|
865
|
+
expect((err as Error).message).toContain("timed out");
|
|
866
|
+
}
|
|
867
|
+
expect(attempts).toBe(3); // Should retry on timeout
|
|
868
|
+
});
|
|
869
|
+
|
|
870
|
+
test("withRetry retries on ECONNRESET errors", async () => {
|
|
871
|
+
// Tests that connection reset errors are considered retryable
|
|
872
|
+
let attempts = 0;
|
|
873
|
+
try {
|
|
874
|
+
await api.withRetry(
|
|
875
|
+
async () => {
|
|
876
|
+
attempts++;
|
|
877
|
+
const err = new Error("connection reset") as Error & { code: string };
|
|
878
|
+
err.code = "ECONNRESET";
|
|
879
|
+
throw err;
|
|
880
|
+
},
|
|
881
|
+
{ maxAttempts: 2, initialDelayMs: 10 }
|
|
882
|
+
);
|
|
883
|
+
} catch (err) {
|
|
884
|
+
expect(err).toBeInstanceOf(Error);
|
|
885
|
+
}
|
|
886
|
+
expect(attempts).toBe(2); // Should retry on ECONNRESET
|
|
887
|
+
});
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
|