Files
OmniRoute/tests/unit/radar-export.test.mjs
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

86 lines
3.5 KiB
JavaScript

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 { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";
// Gera o export estável do catálogo (scripts/release/radar-export.mjs) e valida
// o contrato consumido pelo OmniRoute Radar + a proveniência (D16: desconhecido
// permanece `null`, nunca inventado).
const DIR = path.dirname(fileURLToPath(import.meta.url));
const REPO = path.resolve(DIR, "../.."); // …/OmniRoute
const SCRIPT = path.join(REPO, "scripts/release/radar-export.mjs");
function runExport(extraEnv = {}) {
const outDir = fs.mkdtempSync(path.join(os.tmpdir(), "radar-export-"));
const outPath = path.join(outDir, "export-omniroute.json");
execFileSync("node", ["--import", "tsx/esm", SCRIPT, outPath], {
cwd: REPO,
stdio: ["ignore", "ignore", "inherit"],
// Base limpa: sem herdar GITHUB_* do ambiente do CI que roda os testes.
env: {
PATH: process.env.PATH,
HOME: process.env.HOME,
GITHUB_SHA: undefined,
GITHUB_REF_NAME: undefined,
GITHUB_REF: undefined,
GITHUB_ACTIONS: undefined,
GITHUB_SERVER_URL: undefined,
GITHUB_REPOSITORY: undefined,
GITHUB_RUN_ID: undefined,
...extraEnv,
},
});
const parsed = JSON.parse(fs.readFileSync(outPath, "utf8"));
fs.rmSync(outDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
return parsed;
}
test("radar export satisfies the Radar consumer contract with a fresh catalog", () => {
const data = runExport();
// Contrato mínimo de src/feed/exportSource.ts: budgets[] não-vazio + geradoEm.
assert.ok(Array.isArray(data.budgets) && data.budgets.length > 0, "budgets não-vazio");
assert.ok(Array.isArray(data.registry) && data.registry.length > 0, "registry não-vazio");
assert.ok(
typeof data.geradoEm === "string" && !Number.isNaN(Date.parse(data.geradoEm)),
"geradoEm ISO válido"
);
assert.ok(data.totais && typeof data.totais === "object", "totais presente");
// registry ordenado e sem duplicatas (chaves de provider).
assert.deepEqual(data.registry, [...data.registry].sort());
});
test("radar export provenance never fabricates unknown fields", () => {
const data = runExport();
const p = data.provenance;
assert.ok(p && typeof p === "object", "provenance presente");
assert.equal(p.generatedAt, data.geradoEm);
assert.equal(p.generator, "scripts/release/radar-export.mjs");
// Fora de um runner do GitHub Actions: manual, e ref/runUrl desconhecidos = null.
assert.equal(p.generatedBy, "manual");
assert.equal(p.sourceRef, null);
assert.equal(p.runUrl, null);
// sourceCommit: SHA de 40 hex (via git no checkout) ou null se indisponível.
assert.ok(p.sourceCommit === null || /^[0-9a-f]{40}$/.test(p.sourceCommit), "sourceCommit sha|null");
});
test("radar export provenance reflects the GitHub Actions environment when present", () => {
const sha = "0123456789abcdef0123456789abcdef01234567";
const data = runExport({
GITHUB_ACTIONS: "true",
GITHUB_SHA: sha,
GITHUB_REF_NAME: "release/v9.9.9",
GITHUB_SERVER_URL: "https://github.com",
GITHUB_REPOSITORY: "diegosouzapw/OmniRoute",
GITHUB_RUN_ID: "42",
});
const p = data.provenance;
assert.equal(p.generatedBy, "github-actions");
assert.equal(p.sourceCommit, sha);
assert.equal(p.sourceRef, "release/v9.9.9");
assert.equal(p.runUrl, "https://github.com/diegosouzapw/OmniRoute/actions/runs/42");
});