Files
OmniRoute/tests/integration/traffic-inspector-session-requests.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

134 lines
4.6 KiB
TypeScript

/**
* Integration tests: POST /api/tools/traffic-inspector/sessions/[id]/requests
*
* Tests snapshot persistence: seq increments, request_count sync, validation, 404.
*/
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-ti-session-requests-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const { resetDbInstance, getDbInstance } = await import("../../src/lib/db/core.ts");
async function resetStorage() {
resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
getDbInstance();
}
const sessionsRoute = await import("../../src/app/api/tools/traffic-inspector/sessions/route.ts");
const sessionDetailRoute =
await import("../../src/app/api/tools/traffic-inspector/sessions/[id]/route.ts");
const sessionRequestsRoute =
await import("../../src/app/api/tools/traffic-inspector/sessions/[id]/requests/route.ts");
async function createSession(name?: string): Promise<string> {
const res = await sessionsRoute.POST(
new Request("http://localhost/api/tools/traffic-inspector/sessions", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(name !== undefined ? { name } : {}),
})
);
const body = (await res.json()) as { id: string };
return body.id;
}
async function postRequest(sessionId: string, payload: string): Promise<Response> {
return sessionRequestsRoute.POST(
new Request("http://localhost/", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ payload }),
}),
{ params: Promise.resolve({ id: sessionId }) }
);
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(() => {
resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("POST /sessions/[id]/requests: seq increments 1, 2, 3", async () => {
const id = await createSession("seq-test");
const r1 = await postRequest(id, JSON.stringify({ note: "first" }));
assert.equal(r1.status, 201);
const b1 = (await r1.json()) as { seq: number };
assert.equal(b1.seq, 1);
const r2 = await postRequest(id, JSON.stringify({ note: "second" }));
assert.equal(r2.status, 201);
const b2 = (await r2.json()) as { seq: number };
assert.equal(b2.seq, 2);
const r3 = await postRequest(id, JSON.stringify({ note: "third" }));
assert.equal(r3.status, 201);
const b3 = (await r3.json()) as { seq: number };
assert.equal(b3.seq, 3);
});
test("POST /sessions/[id]/requests: GET session reflects requestCount === 3", async () => {
const id = await createSession("count-test");
await postRequest(id, "payload-1");
await postRequest(id, "payload-2");
await postRequest(id, "payload-3");
const getRes = await sessionDetailRoute.GET(new Request("http://localhost/"), {
params: Promise.resolve({ id }),
});
assert.equal(getRes.status, 200);
const body = (await getRes.json()) as { session: { request_count: number } };
assert.equal(body.session.request_count, 3);
});
test("POST /sessions/[id]/requests: invalid body returns 400", async () => {
const id = await createSession("validation-test");
// Missing `payload` field
const res = await sessionRequestsRoute.POST(
new Request("http://localhost/", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ not_payload: "oops" }),
}),
{ params: Promise.resolve({ id }) }
);
assert.equal(res.status, 400);
});
test("POST /sessions/[id]/requests: payload exceeding 1MB cap returns 400", async () => {
const id = await createSession("size-cap-test");
// Exceed 1_048_576 bytes
const oversized = "x".repeat(1_048_577);
const res = await postRequest(id, oversized);
assert.equal(res.status, 400);
});
test("POST /sessions/[id]/requests: non-existent session id returns 404", async () => {
const res = await postRequest("00000000-0000-4000-8000-000000000000", "some-payload");
assert.equal(res.status, 404);
});
test("POST /sessions/[id]/requests: error response does not leak stack trace", async () => {
// POST to non-existent session — exercises the 404 path error body
const res = await postRequest("00000000-0000-4000-8000-000000000099", "data");
assert.equal(res.status, 404);
const body = (await res.json()) as { error?: { message?: string } };
const msg = body?.error?.message ?? "";
assert.ok(!msg.includes("at /"), "should not contain stack trace");
});