Files
OmniRoute/tests/unit/call-log-artifact-worker.test.ts
Diego Rodrigues de Sa e Souza 93265eede3 test(infra): retry recursive temp-dir removal on main (main twin of #11968) (#12246)
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)

`main` has been red since b342c1a361 on the vitest and integration gates:

  ✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
  ✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos

Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).

release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.

This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.

The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.

* chore(scripts): carry the rm-maxretries codemod onto main alongside its output

The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
2026-09-01 01:48:00 -03:00

169 lines
5.5 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";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-call-log-worker-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const { writeCallArtifactAsync, closeCallLogArtifactWriter, resolveCallLogArtifactWorker } =
await import("../../src/lib/usage/callLogArtifactWriter.ts");
test.after(async () => {
await closeCallLogArtifactWriter();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
function buildArtifact(id: string) {
return {
schemaVersion: 5 as const,
summary: {
id,
timestamp: "2026-08-11T12:34:56.789Z",
method: "POST",
path: "/v1/chat/completions",
status: 200,
model: "test-model",
requestedModel: null,
provider: "test-provider",
account: "test-account",
connectionId: null,
duration: 10,
tokens: {
in: 1,
out: 2,
cacheRead: null,
cacheWrite: null,
reasoning: null,
compressed: null,
},
requestType: "chat",
sourceFormat: "openai",
targetFormat: "openai",
apiKeyId: null,
apiKeyName: null,
comboName: null,
comboStepId: null,
comboExecutionKey: null,
},
requestBody: { worker: true },
responseBody: { content: "written" },
error: null,
};
}
test("worker resolution covers npm, standalone, source, and missing layouts", () => {
const layoutRoot = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-worker-layout-"));
const createWorker = (workerFile: string) => {
fs.mkdirSync(path.dirname(workerFile), { recursive: true });
fs.writeFileSync(workerFile, "");
};
try {
const npmRoot = path.join(layoutRoot, "package", "dist");
const npmWorker = path.join(npmRoot, "src", "lib", "usage", "callLogArtifactWorker.js");
createWorker(npmWorker);
assert.deepEqual(
resolveCallLogArtifactWorker({
moduleDir: path.join(npmRoot, ".next", "server", "chunks"),
cwd: path.join(layoutRoot, "unrelated-caller"),
entryFile: path.join(npmRoot, "server.js"),
fileExists: fs.existsSync,
}),
{ workerFile: npmWorker, execArgv: [] }
);
const standaloneRoot = path.join(layoutRoot, "standalone");
const standaloneWorker = path.join(
standaloneRoot,
"src",
"lib",
"usage",
"callLogArtifactWorker.js"
);
createWorker(standaloneWorker);
assert.deepEqual(
resolveCallLogArtifactWorker({
moduleDir: path.join(standaloneRoot, ".next", "server", "chunks"),
cwd: standaloneRoot,
entryFile: null,
fileExists: fs.existsSync,
}),
{ workerFile: standaloneWorker, execArgv: [] }
);
const sourceDir = path.join(layoutRoot, "source", "src", "lib", "usage");
const sourceWorker = path.join(sourceDir, "callLogArtifactWorker.ts");
createWorker(sourceWorker);
assert.deepEqual(
resolveCallLogArtifactWorker({
moduleDir: sourceDir,
cwd: path.join(layoutRoot, "unrelated-source-caller"),
entryFile: null,
fileExists: fs.existsSync,
}),
{ workerFile: sourceWorker, execArgv: ["--import", "tsx/esm"] }
);
const missingRoot = path.join(layoutRoot, "missing");
assert.deepEqual(
resolveCallLogArtifactWorker({
moduleDir: path.join(missingRoot, ".next", "server", "chunks"),
cwd: path.join(layoutRoot, "unrelated-missing-caller"),
entryFile: path.join(missingRoot, "server.js"),
fileExists: fs.existsSync,
}),
{
workerFile: path.join(missingRoot, "src", "lib", "usage", "callLogArtifactWorker.js"),
execArgv: [],
}
);
} finally {
fs.rmSync(layoutRoot, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
const resolved = resolveCallLogArtifactWorker();
assert.equal(fs.existsSync(resolved.workerFile), true);
assert.equal(path.basename(resolved.workerFile), "callLogArtifactWorker.ts");
assert.deepEqual(resolved.execArgv, ["--import", "tsx/esm"]);
const source = fs.readFileSync("src/lib/usage/callLogArtifactWriter.ts", "utf8");
assert.doesNotMatch(source, /firstAncestorWith|MAX_WALK_UP|runtimeAnchors/);
assert.doesNotMatch(source, /new Worker\(/);
assert.match(source, /Reflect\.construct\(Worker/);
});
test("async worker writes call-log artifact and returns matching metadata", async () => {
const artifact = buildArtifact("worker-write-1");
const result = await writeCallArtifactAsync(artifact);
assert.ok(result);
const artifactPath = path.join(TEST_DATA_DIR, "call_logs", result.relPath);
const serialized = fs.readFileSync(artifactPath, "utf8");
assert.equal(result.sizeBytes, Buffer.byteLength(serialized));
assert.match(result.sha256, /^[0-9a-f]{8}$/);
assert.deepEqual(JSON.parse(serialized), artifact);
});
test("bounded queue fails open and rate-limits saturation warnings", async () => {
const originalWarn = console.warn;
let warningCount = 0;
console.warn = () => {
warningCount++;
};
try {
const writes = Array.from({ length: 131 }, (_, index) =>
writeCallArtifactAsync(buildArtifact(`worker-overflow-${index}`))
);
assert.equal(warningCount, 1);
await closeCallLogArtifactWriter(0);
const results = await Promise.all(writes);
assert.ok(results.every((result) => result === null));
} finally {
console.warn = originalWarn;
}
});