Skip to content

Commit 97044f7

Browse files
committed
Harden smoke test HTTP requests across restarts
1 parent 71973ab commit 97044f7

1 file changed

Lines changed: 56 additions & 44 deletions

File tree

scripts/smoke-test.js

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,40 @@ function waitForServer(server, { rejectOnError = true } = {}) {
204204
});
205205
}
206206

207-
async function fetchIndexPayload(port, query = "") {
208-
const response = await fetch(`http://127.0.0.1:${port}/api/index${query}`);
209-
assert.equal(response.status, 200, "Expected /api/index to return 200");
210-
return response.json();
211-
}
212-
213-
function requestStatus(url) {
207+
function request(url) {
214208
return new Promise((resolve, reject) => {
215-
const request = http.get(url, (response) => {
216-
response.resume();
209+
const req = http.get(url, {
210+
agent: false,
211+
headers: {
212+
Connection: "close",
213+
},
214+
}, (response) => {
215+
const chunks = [];
216+
response.on("data", (chunk) => {
217+
chunks.push(chunk);
218+
});
217219
response.on("end", () => {
218-
resolve(response.statusCode);
220+
resolve({
221+
status: response.statusCode,
222+
body: Buffer.concat(chunks).toString("utf8"),
223+
});
219224
});
220225
});
221226

222-
request.on("error", reject);
227+
req.on("error", reject);
223228
});
224229
}
225230

231+
async function requestJson(url, message) {
232+
const response = await request(url);
233+
assert.equal(response.status, 200, message);
234+
return JSON.parse(response.body);
235+
}
236+
237+
async function fetchIndexPayload(port, query = "") {
238+
return requestJson(`http://127.0.0.1:${port}/api/index${query}`, "Expected /api/index to return 200");
239+
}
240+
226241
async function waitForCondition(check, { timeoutMs = 15000, intervalMs = 150 } = {}) {
227242
const startedAt = Date.now();
228243
while (Date.now() - startedAt < timeoutMs) {
@@ -312,9 +327,7 @@ async function run() {
312327
try {
313328
await waitForServer(server);
314329

315-
const indexResponse = await fetch(`http://127.0.0.1:${PORT}/api/index`);
316-
assert.equal(indexResponse.status, 200, "Expected /api/index to return 200");
317-
const indexPayload = await indexResponse.json();
330+
const indexPayload = await requestJson(`http://127.0.0.1:${PORT}/api/index`, "Expected /api/index to return 200");
318331
assert.equal(indexPayload.items.length, 6, "Expected all manifest items to remain indexed");
319332
assert.equal(indexPayload.stats.totalItems, 6, "Expected stats to report all indexed items");
320333
assert.equal(indexPayload.stats.withLocalMedia, 1, "Expected only one item to match the local media pair");
@@ -333,67 +346,66 @@ async function run() {
333346
assert.equal(item.id.includes("undefined"), false, "Expected fallback manifest IDs to be stable");
334347
}
335348

336-
const manifestSearchResponse = await fetch(`http://127.0.0.1:${PORT}/api/index?query=caption`);
337-
assert.equal(manifestSearchResponse.status, 200, "Expected manifest search to return 200");
338-
const manifestSearchPayload = await manifestSearchResponse.json();
349+
const manifestSearchPayload = await requestJson(
350+
`http://127.0.0.1:${PORT}/api/index?query=caption`,
351+
"Expected manifest search to return 200",
352+
);
339353
assert.equal(manifestSearchPayload.items.length, 1, "Expected manifest-only metadata to be searchable");
340354

341-
const usernamePrefixSearchResponse = await fetch(`http://127.0.0.1:${PORT}/api/index?query=%40smoke`);
342-
assert.equal(usernamePrefixSearchResponse.status, 200, "Expected @username prefix search to return 200");
343-
const usernamePrefixSearchPayload = await usernamePrefixSearchResponse.json();
355+
const usernamePrefixSearchPayload = await requestJson(
356+
`http://127.0.0.1:${PORT}/api/index?query=%40smoke`,
357+
"Expected @username prefix search to return 200",
358+
);
344359
assert.equal(usernamePrefixSearchPayload.items.length, 1, "Expected @username prefix search to match manifest usernames");
345360

346-
const usernameNonPrefixSearchResponse = await fetch(`http://127.0.0.1:${PORT}/api/index?query=%40user`);
347-
assert.equal(usernameNonPrefixSearchResponse.status, 200, "Expected non-prefix @username search to return 200");
348-
const usernameNonPrefixSearchPayload = await usernameNonPrefixSearchResponse.json();
361+
const usernameNonPrefixSearchPayload = await requestJson(
362+
`http://127.0.0.1:${PORT}/api/index?query=%40user`,
363+
"Expected non-prefix @username search to return 200",
364+
);
349365
assert.equal(usernameNonPrefixSearchPayload.items.length, 0, "Expected non-prefix @username search not to match manifest usernames");
350366

351-
const literalAtSearchResponse = await fetch(
367+
const literalAtSearchPayload = await requestJson(
352368
`http://127.0.0.1:${PORT}/api/index?query=${encodeURIComponent("@smoke marker")}`,
369+
"Expected spaced @ query to return 200",
353370
);
354-
assert.equal(literalAtSearchResponse.status, 200, "Expected spaced @ query to return 200");
355-
const literalAtSearchPayload = await literalAtSearchResponse.json();
356371
assert.equal(literalAtSearchPayload.items.length, 1, "Expected spaced @ query to use simple text search");
357372
assert.equal(literalAtSearchPayload.items[0].prompt, "Literal @smoke marker");
358373

359-
const fallbackSearchResponse = await fetch(`http://127.0.0.1:${PORT}/api/index?query=${encodeURIComponent("Fallback manifest")}`);
360-
assert.equal(fallbackSearchResponse.status, 200, "Expected fallback prompt search to return 200");
361-
const fallbackSearchPayload = await fallbackSearchResponse.json();
374+
const fallbackSearchPayload = await requestJson(
375+
`http://127.0.0.1:${PORT}/api/index?query=${encodeURIComponent("Fallback manifest")}`,
376+
"Expected fallback prompt search to return 200",
377+
);
362378
assert.equal(fallbackSearchPayload.items.length, 2, "Expected both identifier-less manifest items to be searchable");
363379

364-
const dateRangeHitResponse = await fetch(
380+
const dateRangeHitPayload = await requestJson(
365381
`http://127.0.0.1:${PORT}/api/index?dateFrom=${encodeURIComponent("2026-04-15")}&dateTo=${encodeURIComponent("2026-04-15")}`,
382+
"Expected date range search to return 200",
366383
);
367-
assert.equal(dateRangeHitResponse.status, 200, "Expected date range search to return 200");
368-
const dateRangeHitPayload = await dateRangeHitResponse.json();
369384
assert.equal(dateRangeHitPayload.items.length, 1, "Expected the fixture item to match its own date range");
370385

371-
const dateRangeMissResponse = await fetch(
386+
const dateRangeMissPayload = await requestJson(
372387
`http://127.0.0.1:${PORT}/api/index?dateFrom=${encodeURIComponent("2026-04-18")}`,
388+
"Expected out-of-range date search to return 200",
373389
);
374-
assert.equal(dateRangeMissResponse.status, 200, "Expected out-of-range date search to return 200");
375-
const dateRangeMissPayload = await dateRangeMissResponse.json();
376390
assert.equal(dateRangeMissPayload.items.length, 0, "Expected out-of-range date filtering to exclude the fixture item");
377391

378-
const detailResponse = await fetch(
392+
const detailPayload = await requestJson(
379393
`http://127.0.0.1:${PORT}/api/item/${encodeURIComponent(mainItem.id)}`,
394+
"Expected /api/item to return 200",
380395
);
381-
assert.equal(detailResponse.status, 200, "Expected /api/item to return 200");
382-
const detailPayload = await detailResponse.json();
383396
assert.equal(detailPayload.mediaUrl, "/media?id=v2_profile%3Agen_smoke123&kind=media");
384397
assert.equal(detailPayload.debug, null, "Expected debug payloads to be hidden by default");
385398
assert.equal(detailPayload.local.txtRaw.includes("Smoke test prompt"), true);
386399
assert.equal(fs.existsSync(path.join(APP_DATA_DIR, "txt-record-cache.json")), true, "Expected TXT cache file to be created");
387400

388-
const ambiguousDetailResponse = await fetch(
401+
const ambiguousDetailPayload = await requestJson(
389402
`http://127.0.0.1:${PORT}/api/item/${encodeURIComponent(ambiguousItem.id)}`,
403+
"Expected the ambiguous task fixture detail to load",
390404
);
391-
assert.equal(ambiguousDetailResponse.status, 200, "Expected the ambiguous task fixture detail to load");
392-
const ambiguousDetailPayload = await ambiguousDetailResponse.json();
393405
assert.equal(ambiguousDetailPayload.mediaUrl, null, "Expected the ambiguous task fixture not to inherit the local media URL");
394406

395-
const mediaStatus = await requestStatus(`http://127.0.0.1:${PORT}${detailPayload.mediaUrl}`);
396-
assert.equal(mediaStatus, 200, "Expected /media to return 200");
407+
const mediaResponse = await request(`http://127.0.0.1:${PORT}${detailPayload.mediaUrl}`);
408+
assert.equal(mediaResponse.status, 200, "Expected /media to return 200");
397409
} finally {
398410
await new Promise((resolve) => server.close(resolve));
399411
try {
@@ -403,6 +415,6 @@ async function run() {
403415
}
404416

405417
run().catch((error) => {
406-
console.error(error.message || error);
418+
console.error(error?.stack || error?.message || error);
407419
process.exitCode = 1;
408420
});

0 commit comments

Comments
 (0)