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
|
export interface Check {
name: string;
url: string;
expectStatus: number | number[];
expectKey?: string;
expectContains?: string;
}
export interface CheckResult {
name: string;
url: string;
status: number;
durationMs: number;
ok: boolean;
error?: string;
}
export type FetchLike = (
url: string,
init?: { method?: string; headers?: Record<string, string> }
) => Promise<{
status: number;
text: () => Promise<string>;
}>;
export const CHECKS: readonly Check[] = [
{ name: "healthz", url: "/healthz", expectStatus: 200, expectKey: "ok" },
{ name: "readyz", url: "/readyz", expectStatus: 200 },
{ name: "version", url: "/api/version", expectStatus: 200, expectKey: "sha" },
{
name: "login renders",
url: "/login",
expectStatus: 200,
expectContains: "Sign in",
},
{
name: "register renders",
url: "/register",
expectStatus: 200,
expectContains: "Create account",
},
{ name: "landing renders", url: "/", expectStatus: 200 },
{ name: "explore renders", url: "/explore", expectStatus: 200 },
{ name: "demo renders", url: "/demo", expectStatus: [200, 302] },
{ name: "pricing renders", url: "/pricing", expectStatus: 200 },
{ name: "status renders", url: "/status", expectStatus: 200 },
{ name: "api v2 health", url: "/api/v2/healthz", expectStatus: [200, 404] },
{
name: "mcp discovery",
url: "/mcp",
expectStatus: 200,
expectKey: "serverInfo",
},
{ name: "manifest", url: "/manifest.webmanifest", expectStatus: 200 },
{ name: "sw", url: "/sw.js", expectStatus: 200 },
{ name: "dxt download", url: "/gluecron.dxt", expectStatus: 200 },
];
export function assertStatus(
got: number,
expected: number | number[]
): string | null {
const allowed = Array.isArray(expected) ? expected : [expected];
if (allowed.includes(got)) return null;
return `expected status ${allowed.join("/")}, got ${got}`;
}
export function assertKey(body: string, key: string): string | null {
let parsed: unknown;
try {
parsed = JSON.parse(body);
} catch {
return `expected JSON with key "${key}", got non-JSON body`;
}
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
return `expected JSON object with key "${key}", got ${typeof parsed}`;
}
if (!Object.prototype.hasOwnProperty.call(parsed, key)) {
return `expected JSON key "${key}", not present`;
}
return null;
}
export function assertContains(body: string, needle: string): string | null {
if (body.includes(needle)) return null;
return `expected body to contain ${JSON.stringify(needle)}`;
}
export interface RunOptions {
baseUrl: string;
fetchImpl: FetchLike;
checks?: readonly Check[];
now?: () => number;
log?: (line: string) => void;
}
export interface RunSummary {
results: CheckResult[];
passed: number;
failed: number;
ok: boolean;
}
export async function runChecks(opts: RunOptions): Promise<RunSummary> {
const checks = opts.checks ?? CHECKS;
const now = opts.now ?? (() => Date.now());
const log = opts.log ?? (() => undefined);
const results: CheckResult[] = [];
for (const check of checks) {
const t0 = now();
let status = 0;
let body = "";
let fetchErr: string | undefined;
try {
const res = await opts.fetchImpl(opts.baseUrl + check.url);
status = res.status;
try {
body = await res.text();
} catch (err) {
body = "";
fetchErr = `body read failed: ${(err as Error).message}`;
}
} catch (err) {
fetchErr = `fetch failed: ${(err as Error).message}`;
}
const durationMs = now() - t0;
let error: string | null = fetchErr ?? null;
if (!error) error = assertStatus(status, check.expectStatus);
if (!error && check.expectKey) error = assertKey(body, check.expectKey);
if (!error && check.expectContains)
error = assertContains(body, check.expectContains);
const result: CheckResult = {
name: check.name,
url: check.url,
status,
durationMs,
ok: error === null,
...(error !== null ? { error } : {}),
};
results.push(result);
log(
`[smoke] ${result.ok ? "PASS" : "FAIL"} ${check.name.padEnd(20)} ${String(status).padStart(3)} ${durationMs}ms${error ? " — " + error : ""}`
);
}
const failed = results.filter((r) => !r.ok).length;
return {
results,
passed: results.length - failed,
failed,
ok: failed === 0,
};
}
export function formatTable(results: readonly CheckResult[]): string {
const header = ["name", "status", "duration_ms", "result"];
const rows = results.map((r) => [
r.name,
String(r.status),
String(r.durationMs),
r.ok ? "PASS" : `FAIL: ${r.error ?? "?"}`,
]);
const all = [header, ...rows];
const widths = header.map((_, col) =>
Math.max(...all.map((row) => row[col].length))
);
const fmt = (row: string[]) =>
row.map((cell, i) => cell.padEnd(widths[i])).join(" | ");
const sep = widths.map((w) => "-".repeat(w)).join("-+-");
return [fmt(header), sep, ...rows.map(fmt)].join("\n");
}
export function missingMigrations(
fileNames: readonly string[],
appliedNames: readonly string[]
): string[] {
const applied = new Set(appliedNames);
return fileNames
.filter((name) => name.endsWith(".sql"))
.filter((name) => !applied.has(name))
.slice()
.sort();
}
export function latestMigration(fileNames: readonly string[]): string | null {
const sql = fileNames.filter((n) => n.endsWith(".sql")).slice().sort();
return sql.length === 0 ? null : sql[sql.length - 1];
}
|