mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 02:42:24 +03:00
* 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.
124 lines
4.4 KiB
TypeScript
124 lines
4.4 KiB
TypeScript
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";
|
|
import { createServer, type Server } from "node:http";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-conductor-route-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const fleetRoute = await import("../../src/app/api/conductor/fleet/route.ts");
|
|
const detailRoute = await import("../../src/app/api/conductor/tasks/[id]/route.ts");
|
|
const cancelRoute = await import("../../src/app/api/conductor/tasks/[id]/cancel/route.ts");
|
|
|
|
const servers: Server[] = [];
|
|
|
|
function fakeHub(routes: Record<string, { status: number; body: unknown }>): Promise<string> {
|
|
const server = createServer((req, res) => {
|
|
const hit = Object.entries(routes).find(([p]) => (req.url ?? "").startsWith(p));
|
|
res.writeHead(hit ? hit[1].status : 404, { "content-type": "application/json" });
|
|
res.end(
|
|
JSON.stringify(hit ? hit[1].body : { error: "hub: segredo interno que NÃO pode vazar" })
|
|
);
|
|
});
|
|
servers.push(server);
|
|
return new Promise((resolve) => {
|
|
server.listen(0, "127.0.0.1", () => {
|
|
const addr = server.address();
|
|
resolve(`http://127.0.0.1:${typeof addr === "object" && addr ? addr.port : 0}`);
|
|
});
|
|
});
|
|
}
|
|
|
|
test.beforeEach(() => {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
delete process.env.CONDUCTOR_HUB_URL;
|
|
delete process.env.CONDUCTOR_HUB_TOKEN;
|
|
});
|
|
|
|
test.after(async () => {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
delete process.env.CONDUCTOR_HUB_URL;
|
|
while (servers.length > 0) {
|
|
const s = servers.pop();
|
|
await new Promise((resolve) => s?.close(resolve));
|
|
}
|
|
});
|
|
|
|
test("GET /api/conductor/fleet devolve snapshot whitelisted; sem hub → degradado 200", async () => {
|
|
process.env.CONDUCTOR_HUB_URL = await fakeHub({
|
|
"/v1/runners": {
|
|
status: 200,
|
|
body: [
|
|
{
|
|
id: "r_1",
|
|
token: "VAZOU?",
|
|
online: true,
|
|
capabilities: { name: "devbox", clis: [{ profile: "claude" }] },
|
|
},
|
|
],
|
|
},
|
|
"/v1/tasks": {
|
|
status: 200,
|
|
body: [
|
|
{
|
|
id: "t_1",
|
|
status: "working",
|
|
mode: "solo",
|
|
repo: { url: "https://x/r" },
|
|
assigned_runner: "r_1",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
process.env.CONDUCTOR_HUB_TOKEN = "tok";
|
|
const res = await fleetRoute.GET(new Request("http://localhost/api/conductor/fleet"));
|
|
assert.equal(res.status, 200);
|
|
const body = await res.json();
|
|
assert.equal(body.offline, false);
|
|
assert.equal(body.runners[0].name, "devbox");
|
|
assert.equal(body.tasks[0].status, "working");
|
|
assert.ok(!JSON.stringify(body).includes("VAZOU?"), "token de runner não vaza pela rota");
|
|
|
|
delete process.env.CONDUCTOR_HUB_URL; // sem hub: degradado, nunca 500
|
|
const down = await fleetRoute.GET(new Request("http://localhost/api/conductor/fleet"));
|
|
assert.equal(down.status, 200);
|
|
assert.equal((await down.json()).offline, true);
|
|
});
|
|
|
|
test("GET /api/conductor/tasks/[id] → 404 sanitizado quando o hub não conhece a task", async () => {
|
|
process.env.CONDUCTOR_HUB_URL = await fakeHub({});
|
|
const res = await detailRoute.GET(new Request("http://localhost/api/conductor/tasks/t_x"), {
|
|
params: Promise.resolve({ id: "t_x" }),
|
|
});
|
|
assert.equal(res.status, 404);
|
|
const text = await res.text();
|
|
assert.ok(!text.includes("segredo interno"), "corpo do hub NUNCA repassado");
|
|
});
|
|
|
|
test("POST cancel repassa recusa do hub com status, sem corpo upstream", async () => {
|
|
process.env.CONDUCTOR_HUB_URL = await fakeHub({
|
|
"/v1/tasks/t_done/cancel": {
|
|
status: 409,
|
|
body: { error: "segredo interno que NÃO pode vazar" },
|
|
},
|
|
"/v1/tasks/t_ok/cancel": { status: 200, body: { ok: true } },
|
|
});
|
|
const denied = await cancelRoute.POST(new Request("http://localhost/x", { method: "POST" }), {
|
|
params: Promise.resolve({ id: "t_done" }),
|
|
});
|
|
assert.equal(denied.status, 409);
|
|
assert.ok(!(await denied.text()).includes("segredo interno"));
|
|
|
|
const ok = await cancelRoute.POST(new Request("http://localhost/x", { method: "POST" }), {
|
|
params: Promise.resolve({ id: "t_ok" }),
|
|
});
|
|
assert.equal(ok.status, 200);
|
|
assert.deepEqual(await ok.json(), { ok: true });
|
|
});
|