Files
OmniRoute/tests/unit/providers-batch-update.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
6.0 KiB
TypeScript

import { after, beforeEach, describe, it } 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-batch-update-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const { batchUpdateProviderConnectionsSchema, providersBatchTestSchema } = await import(
"../../src/shared/validation/schemas.ts"
);
type Connection = Awaited<ReturnType<typeof providersDb.createProviderConnection>>;
function getConnectionId(connection: Connection): string {
assert.ok(connection);
assert.equal(typeof connection.id, "string");
return connection.id as string;
}
async function resetStorage() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
async function createConnection(isActive: boolean): Promise<Connection> {
// Distinct apiKey per connection — createProviderConnection dedupes by key value (#3023)
const suffix = Math.random().toString(16).slice(2, 10);
return providersDb.createProviderConnection({
provider: "openai",
authType: "apikey",
name: `openai-${suffix}`,
apiKey: `sk-test-${suffix}`,
isActive,
});
}
beforeEach(async () => {
await resetStorage();
});
after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
describe("batchUpdateProviderConnectionsSchema", () => {
it("accepts a valid ids + isActive payload", () => {
const result = batchUpdateProviderConnectionsSchema.safeParse({
ids: ["conn-1", "conn-2"],
isActive: false,
});
assert.equal(result.success, true);
});
it("rejects an empty ids array", () => {
const result = batchUpdateProviderConnectionsSchema.safeParse({ ids: [], isActive: true });
assert.equal(result.success, false);
});
it("rejects more than 100 ids", () => {
const ids = Array.from({ length: 101 }, (_, i) => `conn-${i}`);
const result = batchUpdateProviderConnectionsSchema.safeParse({ ids, isActive: true });
assert.equal(result.success, false);
});
it("rejects a missing isActive flag", () => {
const result = batchUpdateProviderConnectionsSchema.safeParse({ ids: ["conn-1"] });
assert.equal(result.success, false);
});
it("rejects blank ids", () => {
const result = batchUpdateProviderConnectionsSchema.safeParse({
ids: [" "],
isActive: true,
});
assert.equal(result.success, false);
});
});
describe("providersBatchTestSchema mode=selected", () => {
it("accepts mode=selected with connectionIds", () => {
const result = providersBatchTestSchema.safeParse({
mode: "selected",
connectionIds: ["conn-1", "conn-2"],
});
assert.equal(result.success, true);
});
it("rejects mode=selected without connectionIds", () => {
const result = providersBatchTestSchema.safeParse({ mode: "selected" });
assert.equal(result.success, false);
});
it("rejects mode=selected with an empty connectionIds array", () => {
const result = providersBatchTestSchema.safeParse({ mode: "selected", connectionIds: [] });
assert.equal(result.success, false);
});
it("rejects mode=selected with more than 100 connectionIds", () => {
const connectionIds = Array.from({ length: 101 }, (_, i) => `conn-${i}`);
const result = providersBatchTestSchema.safeParse({ mode: "selected", connectionIds });
assert.equal(result.success, false);
});
it("still accepts other modes without connectionIds", () => {
const result = providersBatchTestSchema.safeParse({ mode: "provider", providerId: "openai" });
assert.equal(result.success, true);
});
});
describe("bulk isActive update round-trip", () => {
it("deactivates and reactivates multiple connections, reporting unknown ids", async () => {
const first = getConnectionId(await createConnection(true));
const second = getConnectionId(await createConnection(true));
const ids = [first, second, "missing-id"];
// Mirrors the PATCH /api/providers loop: update each id, partition results
const updatedIds: string[] = [];
const notFoundIds: string[] = [];
for (const id of ids) {
const updated = await providersDb.updateProviderConnection(id, { isActive: false });
if (updated) updatedIds.push(id);
else notFoundIds.push(id);
}
assert.deepEqual(updatedIds, [first, second]);
assert.deepEqual(notFoundIds, ["missing-id"]);
for (const id of [first, second]) {
const stored = await providersDb.getProviderConnectionById(id);
assert.ok(stored);
assert.equal(Boolean(stored.isActive), false);
}
await providersDb.updateProviderConnection(first, { isActive: true });
const reactivated = await providersDb.getProviderConnectionById(first);
assert.ok(reactivated);
assert.equal(Boolean(reactivated.isActive), true);
});
it("getProviderConnections without filter reaches inactive connections (mode=selected)", async () => {
const activeId = getConnectionId(await createConnection(true));
const inactiveId = getConnectionId(await createConnection(false));
const all = (await providersDb.getProviderConnections()) as Array<{ id: string }>;
const allIds = new Set(all.map((c) => c.id));
assert.ok(allIds.has(activeId));
assert.ok(allIds.has(inactiveId));
const activeOnly = (await providersDb.getProviderConnections({ isActive: true })) as Array<{
id: string;
}>;
const activeIds = new Set(activeOnly.map((c) => c.id));
assert.ok(activeIds.has(activeId));
assert.ok(!activeIds.has(inactiveId));
// Mirrors the test-batch mode=selected filter
const idSet = new Set([inactiveId]);
const selected = all.filter((c) => idSet.has(c.id));
assert.equal(selected.length, 1);
assert.equal(selected[0].id, inactiveId);
});
});