Files
OmniRoute/tests/unit/strict-random-deck.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

174 lines
6.1 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";
// ── Env vars BEFORE dynamic imports ──────────────────────────────────────────
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-strict-random-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "strict-random-test-secret";
const core = await import("../../src/lib/db/core.ts");
const { fisherYatesShuffle, getNextFromDeck } = await import("../../src/sse/services/auth.ts");
test.after(() => {
core.resetDbInstance();
if (fs.existsSync(TEST_DATA_DIR)) {
for (const entry of fs.readdirSync(TEST_DATA_DIR)) {
fs.rmSync(path.join(TEST_DATA_DIR, entry), { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
}
});
// ─── fisherYatesShuffle ──────────────────────────────────────────────────────
test("fisherYatesShuffle: returns array with same elements", () => {
const input = ["a", "b", "c", "d", "e"];
const result = fisherYatesShuffle(input);
assert.equal(result.length, input.length);
for (const item of input) {
assert.ok(result.includes(item), `Missing item: ${item}`);
}
});
test("fisherYatesShuffle: does not mutate original array", () => {
const input = Object.freeze(["a", "b", "c"]);
const result = fisherYatesShuffle(input);
assert.deepStrictEqual([...input], ["a", "b", "c"]);
assert.equal(result.length, 3);
});
test("fisherYatesShuffle: single element returns same element", () => {
const result = fisherYatesShuffle(["only"]);
assert.deepStrictEqual(result, ["only"]);
});
test("fisherYatesShuffle: empty array returns empty array", () => {
const result = fisherYatesShuffle([]);
assert.deepStrictEqual(result, []);
});
// ─── getNextFromDeck ─────────────────────────────────────────────────────────
test("getNextFromDeck: uses all connections before repeating", () => {
const provider = "test-full-cycle";
const ids = ["c1", "c2", "c3", "c4"];
const seen = new Set();
for (let i = 0; i < ids.length; i++) {
const id = getNextFromDeck(provider, ids);
assert.ok(!seen.has(id), `Duplicate before full cycle: ${id} at step ${i}`);
seen.add(id);
}
assert.equal(seen.size, ids.length, "Should have used every connection exactly once");
});
test("getNextFromDeck: reshuffles after exhausting deck", () => {
const provider = "test-reshuffle";
const ids = ["c1", "c2", "c3"];
// Exhaust first cycle
for (let i = 0; i < ids.length; i++) {
getNextFromDeck(provider, ids);
}
// Next call should start a new cycle (reshuffle)
const firstOfNewCycle = getNextFromDeck(provider, ids);
assert.ok(ids.includes(firstOfNewCycle), "New cycle should return a valid connection");
// Complete the new cycle
const newCycleSeen = new Set([firstOfNewCycle]);
for (let i = 1; i < ids.length; i++) {
const id = getNextFromDeck(provider, ids);
assert.ok(!newCycleSeen.has(id), `Duplicate in new cycle: ${id}`);
newCycleSeen.add(id);
}
assert.equal(newCycleSeen.size, ids.length, "New cycle should use all connections");
});
test("getNextFromDeck: last of previous cycle is not first of next cycle", () => {
const provider = "test-no-repeat-boundary";
const ids = ["c1", "c2", "c3", "c4", "c5"];
// Run multiple full cycles and check the boundary condition
let violations = 0;
const totalCycles = 50;
for (let cycle = 0; cycle < totalCycles; cycle++) {
let lastId = "";
for (let i = 0; i < ids.length; i++) {
lastId = getNextFromDeck(provider, ids);
}
// First of next cycle
const firstOfNext = getNextFromDeck(provider, ids);
if (firstOfNext === lastId) violations++;
// Consume rest of cycle
for (let i = 1; i < ids.length; i++) {
getNextFromDeck(provider, ids);
}
}
assert.equal(
violations,
0,
`Last of cycle matched first of next cycle ${violations}/${totalCycles} times`
);
});
test("getNextFromDeck: connection list change resets deck", () => {
const provider = "test-reset-on-change";
const originalIds = ["c1", "c2", "c3", "c4"];
// Use 2 from original deck
getNextFromDeck(provider, originalIds);
getNextFromDeck(provider, originalIds);
// Now change the connection list (simulates quota exhaustion removing a connection)
const newIds = ["c1", "c2", "c3"]; // c4 removed
const seen = new Set();
for (let i = 0; i < newIds.length; i++) {
const id = getNextFromDeck(provider, newIds);
assert.ok(newIds.includes(id), `Got invalid id ${id} after reset`);
assert.ok(!seen.has(id), `Duplicate after reset: ${id}`);
seen.add(id);
}
assert.equal(seen.size, newIds.length, "Should use all new connections after reset");
});
test("getNextFromDeck: single connection always returns that connection", () => {
const provider = "test-single";
const ids = ["only-one"];
for (let i = 0; i < 10; i++) {
const id = getNextFromDeck(provider, ids);
assert.equal(id, "only-one");
}
});
test("getNextFromDeck: empty array returns empty string", () => {
const provider = "test-empty";
const id = getNextFromDeck(provider, []);
assert.equal(id, "");
});
test("getNextFromDeck: different providers have independent decks", () => {
const idsA = ["a1", "a2", "a3"];
const idsB = ["b1", "b2"];
const firstA = getNextFromDeck("providerA", idsA);
const firstB = getNextFromDeck("providerB", idsB);
assert.ok(idsA.includes(firstA));
assert.ok(idsB.includes(firstB));
// Exhaust providerB deck
getNextFromDeck("providerB", idsB);
// providerA should still have remaining items from its deck
const secondA = getNextFromDeck("providerA", idsA);
assert.ok(idsA.includes(secondA));
assert.notEqual(firstA, secondA, "providerA deck should advance independently");
});