Files
OmniRoute/tests/unit/quota-groups-migration.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

213 lines
7.9 KiB
TypeScript

/**
* tests/unit/quota-groups-migration.test.ts
*
* Task B1 — first-class quota Group entity.
*
* Coverage:
* - Migration file 088_quota_groups.sql exists and contains the expected SQL.
* - After migrations run (fresh DB), quota_groups has a 'group-demo' row.
* - createPool without groupId → pool.groupId === 'group-demo'.
* - createPool with groupId: 'g1' (group pre-inserted) → pool.groupId === 'g1'.
* - getPool and listPools both surface groupId.
* - updatePool with groupId updates the group assignment.
*/
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";
// ── DB harness (mirrors quota-pool-connections.test.ts) ─────────────────────
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-quota-groups-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const poolsDb = await import("../../src/lib/db/quotaPools.ts");
async function resetStorage() {
core.resetDbInstance();
for (let attempt = 0; attempt < 10; attempt++) {
try {
if (fs.existsSync(TEST_DATA_DIR)) {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
break;
} catch (err: any) {
if ((err?.code === "EBUSY" || err?.code === "EPERM") && attempt < 9) {
await new Promise((resolve) => setTimeout(resolve, 50 * (attempt + 1)));
} else {
throw err;
}
}
}
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(async () => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
// Helper to get a raw DB handle for inspection / seeding.
function getDb() {
return core.getDbInstance() as unknown as {
prepare: <TRow = unknown>(
sql: string
) => {
all: (...params: unknown[]) => TRow[];
get: (...params: unknown[]) => TRow | undefined;
run: (...params: unknown[]) => { changes: number };
};
};
}
// ── B1.1: Migration file content ─────────────────────────────────────────────
test("migration 087 file exists", () => {
const migrationPath = path.resolve("src/lib/db/migrations/088_quota_groups.sql");
assert.ok(fs.existsSync(migrationPath), `migration file not found: ${migrationPath}`);
});
test("migration 087 contains quota_groups CREATE TABLE", () => {
const sql = fs.readFileSync(path.resolve("src/lib/db/migrations/088_quota_groups.sql"), "utf8");
assert.ok(sql.includes("quota_groups"), "migration SQL should reference quota_groups");
assert.ok(
sql.includes("CREATE TABLE IF NOT EXISTS quota_groups"),
"migration SQL should create quota_groups with IF NOT EXISTS"
);
});
test("migration 087 seeds group-demo", () => {
const sql = fs.readFileSync(path.resolve("src/lib/db/migrations/088_quota_groups.sql"), "utf8");
assert.ok(sql.includes("group-demo"), "migration SQL should insert the 'group-demo' seed row");
assert.ok(
sql.includes("INSERT OR IGNORE INTO quota_groups"),
"migration SQL should use INSERT OR IGNORE for idempotency"
);
});
test("migration 087 adds group_id column to quota_pools", () => {
const sql = fs.readFileSync(path.resolve("src/lib/db/migrations/088_quota_groups.sql"), "utf8");
assert.ok(
sql.includes("ALTER TABLE quota_pools ADD COLUMN group_id"),
"migration SQL should ALTER TABLE quota_pools to add group_id"
);
});
test("migration 087 contains backfill UPDATE for existing pools", () => {
const sql = fs.readFileSync(path.resolve("src/lib/db/migrations/088_quota_groups.sql"), "utf8");
assert.ok(
sql.includes("UPDATE quota_pools SET group_id = 'group-demo'"),
"migration SQL should backfill existing pools to group-demo"
);
assert.ok(
sql.includes("group_id IS NULL OR group_id = ''"),
"backfill should only touch pools without a group"
);
});
// ── B1.2: Schema after migration ──────────────────────────────────────────────
test("after migrations run, quota_groups has a group-demo row named GroupDemo", () => {
// Trigger DB initialisation (runs all migrations including 087).
const db = getDb();
const row = db
.prepare<{ id: string; name: string }>(
"SELECT id, name FROM quota_groups WHERE id = 'group-demo'"
)
.get();
assert.ok(row, "group-demo row should exist in quota_groups");
assert.equal(row!.id, "group-demo");
assert.equal(row!.name, "GroupDemo");
});
test("quota_pools has a group_id column after migration", () => {
const db = getDb();
const cols = db
.prepare<{ name: string }>("PRAGMA table_info(quota_pools)")
.all()
.map((r) => r.name);
assert.ok(cols.includes("group_id"), "quota_pools should have a group_id column");
});
// ── B1.3: createPool defaults ─────────────────────────────────────────────────
test("createPool without groupId → pool.groupId === 'group-demo'", () => {
// Ensure migrations have run.
getDb();
const pool = poolsDb.createPool({
connectionId: "conn-1",
name: "Default Group Pool",
});
assert.equal(pool.groupId, "group-demo", "groupId should default to 'group-demo'");
// Re-read from DB to confirm persistence.
const reread = poolsDb.getPool(pool.id);
assert.ok(reread, "pool should be findable after creation");
assert.equal(reread!.groupId, "group-demo", "persisted groupId should be 'group-demo'");
});
test("createPool with explicit groupId persists the given group", () => {
const db = getDb();
// Seed a custom group first (raw SQL, as quotaGroups module is not yet implemented).
db.prepare("INSERT OR IGNORE INTO quota_groups (id, name) VALUES ('g1', 'Group One')").run();
const pool = poolsDb.createPool({
connectionId: "conn-g1",
name: "G1 Pool",
groupId: "g1",
});
assert.equal(pool.groupId, "g1", "groupId should be 'g1'");
const reread = poolsDb.getPool(pool.id);
assert.ok(reread, "pool should be findable after creation");
assert.equal(reread!.groupId, "g1", "persisted groupId should be 'g1'");
});
// ── B1.4: listPools surfaces groupId ─────────────────────────────────────────
test("listPools returns groupId on every pool", () => {
const db = getDb();
db.prepare("INSERT OR IGNORE INTO quota_groups (id, name) VALUES ('g2', 'Group Two')").run();
poolsDb.createPool({ connectionId: "lp-1", name: "Pool Default" });
poolsDb.createPool({ connectionId: "lp-2", name: "Pool G2", groupId: "g2" });
const { items: pools } = poolsDb.listPools();
assert.equal(pools.length, 2);
const pDef = pools.find((p) => p.name === "Pool Default")!;
assert.equal(pDef.groupId, "group-demo");
const pG2 = pools.find((p) => p.name === "Pool G2")!;
assert.equal(pG2.groupId, "g2");
});
// ── B1.5: updatePool groupId ──────────────────────────────────────────────────
test("updatePool with groupId updates the group assignment", () => {
const db = getDb();
db.prepare("INSERT OR IGNORE INTO quota_groups (id, name) VALUES ('g3', 'Group Three')").run();
const pool = poolsDb.createPool({ connectionId: "up-1", name: "Update Group Pool" });
assert.equal(pool.groupId, "group-demo");
const updated = poolsDb.updatePool(pool.id, { groupId: "g3" });
assert.ok(updated, "updatePool should return the updated pool");
assert.equal(updated!.groupId, "g3", "groupId should be updated to 'g3'");
const reread = poolsDb.getPool(pool.id);
assert.equal(reread!.groupId, "g3", "persisted groupId should be 'g3'");
});