mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-15 19:32:20 +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.
115 lines
4.9 KiB
TypeScript
115 lines
4.9 KiB
TypeScript
/**
|
||
* TDD for the RTK learn/discover API routes (F2.1 / Item 2).
|
||
*
|
||
* GET /api/context/rtk/discover → ranked noise candidates mined from the
|
||
* opt-in rtk raw-output sample store.
|
||
* GET /api/context/rtk/learn?command=X → a suggested RTK filter draft for command X.
|
||
*
|
||
* Auth: requireManagementAuth — not required in the unconfigured/first-run state
|
||
* (fresh temp DATA_DIR, no INITIAL_PASSWORD), so these exercise the 200 happy path.
|
||
* The pure miners (discoverRepeatedNoise/suggestFilter) and the sample adapter
|
||
* (listRtkCommandSamples) are unit-tested separately; this asserts the wiring.
|
||
*
|
||
* Run: node --import tsx/esm --test tests/unit/api/compression/rtk-learn-discover-routes.test.ts
|
||
*/
|
||
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-rtk-ld-routes-"));
|
||
const ORIGINAL_DATA_DIR = process.env.DATA_DIR;
|
||
const ORIGINAL_INITIAL_PASSWORD = process.env.INITIAL_PASSWORD;
|
||
process.env.DATA_DIR = TEST_DATA_DIR;
|
||
delete process.env.INITIAL_PASSWORD;
|
||
|
||
const { maybePersistRtkRawOutput } =
|
||
await import("../../../../open-sse/services/compression/engines/rtk/index.ts");
|
||
const discoverRoute = await import("../../../../src/app/api/context/rtk/discover/route.ts");
|
||
const learnRoute = await import("../../../../src/app/api/context/rtk/learn/route.ts");
|
||
|
||
function seedSamples() {
|
||
// Three runs of the same command with a shared noise line + a unique line each.
|
||
for (let i = 0; i < 3; i++) {
|
||
maybePersistRtkRawOutput(
|
||
`Resolving dependencies...\nnpm warn deprecated foo@1.0.0\nadded ${i} packages in ${i}s\n`,
|
||
{ retention: "always", command: "npm install" }
|
||
);
|
||
}
|
||
}
|
||
|
||
function get(url: string): Request {
|
||
return new Request(url, { method: "GET" });
|
||
}
|
||
|
||
test.beforeEach(() => {
|
||
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
||
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
||
});
|
||
|
||
test.after(() => {
|
||
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
||
if (ORIGINAL_DATA_DIR === undefined) delete process.env.DATA_DIR;
|
||
else process.env.DATA_DIR = ORIGINAL_DATA_DIR;
|
||
if (ORIGINAL_INITIAL_PASSWORD !== undefined)
|
||
process.env.INITIAL_PASSWORD = ORIGINAL_INITIAL_PASSWORD;
|
||
});
|
||
|
||
test("GET /discover — returns ranked noise candidates + sampleCount", async () => {
|
||
seedSamples();
|
||
const res = await discoverRoute.GET(get("http://localhost/api/context/rtk/discover"));
|
||
assert.equal(res.status, 200);
|
||
const body = (await res.json()) as { sampleCount: number; candidates: Array<{ hits: number }> };
|
||
assert.equal(body.sampleCount, 3);
|
||
assert.ok(Array.isArray(body.candidates));
|
||
assert.ok(body.candidates.length > 0, "the shared 'Resolving dependencies' line is a candidate");
|
||
});
|
||
|
||
test("GET /discover — empty store → 200 with no candidates", async () => {
|
||
const res = await discoverRoute.GET(get("http://localhost/api/context/rtk/discover"));
|
||
assert.equal(res.status, 200);
|
||
const body = (await res.json()) as { sampleCount: number; candidates: unknown[] };
|
||
assert.equal(body.sampleCount, 0);
|
||
assert.equal(body.candidates.length, 0);
|
||
});
|
||
|
||
test("GET /learn?command=npm install — returns a suggested filter learned from matching samples", async () => {
|
||
seedSamples();
|
||
const res = await learnRoute.GET(
|
||
get("http://localhost/api/context/rtk/learn?command=" + encodeURIComponent("npm install"))
|
||
);
|
||
assert.equal(res.status, 200);
|
||
const body = (await res.json()) as {
|
||
command: string;
|
||
sampleCount: number;
|
||
filter: { id: string; match: { commands: string[] } };
|
||
};
|
||
assert.equal(body.command, "npm install");
|
||
assert.equal(body.sampleCount, 3, "only the matching command's samples are learned from");
|
||
assert.ok(body.filter.id.length > 0);
|
||
assert.ok(body.filter.match.commands.length > 0);
|
||
});
|
||
|
||
test("GET /learn without a command → 400", async () => {
|
||
const res = await learnRoute.GET(get("http://localhost/api/context/rtk/learn"));
|
||
assert.equal(res.status, 400);
|
||
const body = (await res.json()) as { error: { message?: string } | string };
|
||
// Error must not leak a stack trace (Hard Rule #12).
|
||
const msg = typeof body.error === "string" ? body.error : (body.error?.message ?? "");
|
||
assert.ok(!msg.includes("at /"), "no stack trace in error body");
|
||
});
|
||
|
||
test("GET /learn filters out other commands' samples", async () => {
|
||
seedSamples(); // 3× "npm install"
|
||
maybePersistRtkRawOutput("Compiling cargo...\nFinished in 2s\n", {
|
||
retention: "always",
|
||
command: "cargo build",
|
||
});
|
||
const res = await learnRoute.GET(
|
||
get("http://localhost/api/context/rtk/learn?command=" + encodeURIComponent("npm install"))
|
||
);
|
||
const body = (await res.json()) as { sampleCount: number };
|
||
assert.equal(body.sampleCount, 3, "the cargo sample is excluded");
|
||
});
|