Files
OmniRoute/tests/unit/usage-cache-health-route.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

124 lines
4.9 KiB
TypeScript

/**
* Route contract for GET /api/usage/cache-health.
*
* Uses a real on-disk SQLite so the SQL itself is exercised — a summary that
* silently returns zeros because the WHERE clause is wrong is worse than no
* endpoint at all, and only a real query catches that.
*/
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 tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-cache-health-"));
process.env.DATA_DIR = tmpDir;
// Cleanup on exit, not in test.after(): a root after-hook fires as soon as the
// first top-level test settles and would delete the directory out from under
// the later ones.
process.on("exit", () => {
try {
fs.rmSync(tmpDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} catch {
/* best effort */
}
});
const { getDbInstance, resetDbInstance } = await import("@/lib/db/core");
const { buildCacheHealthResponse } = await import("@/lib/usage/cacheHealth");
const { GET } = await import("@/app/api/usage/cache-health/route");
const NOW = Date.parse("2026-07-28T03:00:00.000Z");
function seed() {
const db = getDbInstance();
db.prepare("DELETE FROM call_logs").run();
const insert = db.prepare(
`INSERT INTO call_logs (id, timestamp, status, model, requested_model,
tokens_cache_read, tokens_cache_creation)
VALUES (?, ?, ?, ?, ?, ?, ?)`
);
const iso = (minsAgo: number) => new Date(NOW - minsAgo * 60_000).toISOString();
// dentro da janela de 1h: um write inicial + dois reads (padrão saudável)
insert.run("a1", iso(10), 200, "claude-sonnet-5", "claude-sonnet-5", 0, 11660);
insert.run("a2", iso(9), 200, "claude-sonnet-5", "claude-sonnet-5", 11660, 14);
insert.run("a3", iso(8), 200, "claude-sonnet-5", "claude-sonnet-5", 11674, 15);
// uma chamada cara do mesmo período, modelo diferente
insert.run("a4", iso(7), 200, "claude-opus-4-8", "claude-opus-4-8", 37509, 91878);
// ruído que NÃO pode entrar: erro, fora da janela, e linha sem contabilidade de cache
insert.run("b1", iso(5), 500, "claude-sonnet-5", "claude-sonnet-5", 999999, 999999);
insert.run("b2", iso(600), 200, "claude-sonnet-5", "claude-sonnet-5", 888888, 888888);
insert.run("b3", iso(4), 200, "claude-sonnet-5", "claude-sonnet-5", null, null);
}
test("resume a janela ignorando erro, fora-da-janela e linhas sem contabilidade", () => {
seed();
const r = buildCacheHealthResponse({ range: "1h", now: NOW });
assert.equal(r.totalCalls, 4, "só as 4 chamadas 200 dentro de 1h com colunas de cache");
assert.equal(r.cacheReadTotal, 0 + 11660 + 11674 + 37509);
assert.equal(r.cacheWriteTotal, 11660 + 14 + 15 + 91878);
assert.equal(r.timeRange, "1h");
assert.equal(r.truncated, false);
assert.ok(
!JSON.stringify(r).includes("999999") && !JSON.stringify(r).includes("888888"),
"o 500 e a linha antiga não podem vazar para o resumo"
);
});
test("filtra por modelo quando pedido", () => {
seed();
const r = buildCacheHealthResponse({ range: "1h", model: "claude-opus-4-8", now: NOW });
assert.equal(r.totalCalls, 1);
assert.equal(r.byModel.length, 1);
assert.equal(r.byModel[0].model, "claude-opus-4-8");
});
test("GET responde 200 com o resumo", async () => {
seed();
const res = await GET(new Request("http://localhost/api/usage/cache-health?range=1h"));
assert.equal(res.status, 200);
const body = await res.json();
assert.equal(body.timeRange, "1h");
assert.equal(typeof body.writeReadRatio, "number");
assert.ok(Array.isArray(body.byModel));
assert.ok(["healthy", "degraded", "thrash", "no-data"].includes(body.verdict));
});
test("GET rejeita range inválido com 400", async () => {
const res = await GET(new Request("http://localhost/api/usage/cache-health?range=99y"));
assert.equal(res.status, 400);
});
test("GET assume 24h quando o range é omitido", async () => {
seed();
const res = await GET(new Request("http://localhost/api/usage/cache-health"));
assert.equal(res.status, 200);
assert.equal((await res.json()).timeRange, "24h");
});
test("erro interno responde 500 sem vazar stack trace nem SQL", async () => {
// Gatilho determinístico: sem a tabela, o prepare() lança SqliteError. Um
// DATA_DIR inválido NÃO serve — o driver fica preso tentando criar o arquivo
// em vez de falhar, e o teste passaria sem exercitar o catch.
seed();
const db = getDbInstance();
db.prepare("DROP TABLE call_logs").run();
try {
const res = await GET(new Request("http://localhost/api/usage/cache-health?range=1h"));
assert.equal(res.status, 500, "o caminho de erro precisa ser realmente exercitado");
const text = JSON.stringify(await res.json());
assert.ok(!text.includes("at /"), "sem stack trace no corpo");
assert.ok(!/\.ts:\d+/.test(text), "sem caminho de arquivo no corpo");
assert.ok(!/SELECT|call_logs/i.test(text), "sem o texto do SQL nem nome de tabela no corpo");
} finally {
resetDbInstance();
}
});