Files
OmniRoute/tests/unit/antigravity-client-identity-paths.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

146 lines
5.1 KiB
TypeScript

import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-ag-identity-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = "test-antigravity-client-identity";
const { AntigravityExecutor } = await import("../../open-sse/executors/antigravity.ts");
const { setCliCompatProviders } = await import("../../open-sse/config/cliFingerprints.ts");
const { antigravityCliUserAgent } = await import("../../open-sse/services/antigravityHeaders.ts");
const { clearAntigravityVersionCaches, seedAntigravityCliVersionCache } =
await import("../../open-sse/services/antigravityVersion.ts");
const { handleImageGeneration } = await import("../../open-sse/handlers/imageGeneration.ts");
const core = await import("../../src/lib/db/core.ts");
const originalFetch = globalThis.fetch;
const originalCreditsMode = process.env.ANTIGRAVITY_CREDITS;
test.afterEach(() => {
globalThis.fetch = originalFetch;
setCliCompatProviders([]);
clearAntigravityVersionCaches();
if (originalCreditsMode === undefined) {
delete process.env.ANTIGRAVITY_CREDITS;
} else {
process.env.ANTIGRAVITY_CREDITS = originalCreditsMode;
}
});
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("executor token refresh uses the selected CLI identity", async () => {
seedAntigravityCliVersionCache("1.1.1");
globalThis.fetch = async (url, init) => {
assert.match(String(url), /oauth2\.googleapis\.com\/token$/);
assert.equal(new Headers(init?.headers).get("User-Agent"), antigravityCliUserAgent("1.1.1"));
return Response.json({
access_token: "new-token",
refresh_token: "new-refresh",
expires_in: 3600,
});
};
const result = await new AntigravityExecutor().refreshCredentials(
{
refreshToken: "refresh",
projectId: "project-1",
providerSpecificData: { clientProfile: "cli" },
},
null
);
assert.equal(result?.accessToken, "new-token");
assert.deepEqual(result?.providerSpecificData, { clientProfile: "cli" });
});
test("credits retry keeps the selected CLI identity after fingerprint serialization", async () => {
const calls: Array<{ body: Record<string, unknown>; headers: Headers }> = [];
seedAntigravityCliVersionCache("1.1.1");
setCliCompatProviders(["antigravity"]);
process.env.ANTIGRAVITY_CREDITS = "retry";
globalThis.fetch = async (_url, init) => {
calls.push({ body: JSON.parse(String(init?.body)), headers: new Headers(init?.headers) });
if (calls.length === 1) {
return Response.json(
{ error: { message: "RESOURCE_EXHAUSTED: quota exhausted" } },
{ status: 429 }
);
}
return new Response(
'data: {"response":{"candidates":[{"content":{"parts":[{"text":"credits"}]},"finishReason":"STOP"}]}}\n\n',
{ status: 200, headers: { "Content-Type": "text/event-stream" } }
);
};
const result = await new AntigravityExecutor().execute({
model: "antigravity/gemini-2.5-flash",
body: { request: { contents: [] } },
stream: false,
credentials: {
accessToken: "cli-credit-token",
projectId: "project-1",
providerSpecificData: { clientProfile: "cli" },
},
log: { debug() {}, warn() {}, info() {} },
});
assert.equal(result.response.status, 200);
assert.equal(calls.length, 2);
assert.deepEqual(calls[1].body.enabledCreditTypes, ["GOOGLE_ONE_AI"]);
for (const call of calls) {
assert.equal(call.headers.get("User-Agent"), antigravityCliUserAgent("1.1.1"));
assert.equal(call.headers.get("x-client-name"), null);
assert.equal(call.headers.get("X-Goog-Api-Client"), null);
}
});
test("image generation forwards the selected CLI identity and public envelope", async () => {
seedAntigravityCliVersionCache("1.1.1");
let capturedHeaders = new Headers();
let capturedBody: Record<string, unknown> = {};
globalThis.fetch = async (_url, init) => {
capturedHeaders = new Headers(init?.headers);
capturedBody = JSON.parse(String(init?.body));
return Response.json({
response: {
candidates: [
{
content: {
parts: [{ inlineData: { mimeType: "image/png", data: "aW1hZ2U=" } }],
},
},
],
},
});
};
const result = await handleImageGeneration({
body: {
model: "antigravity/gemini-3.1-flash-image",
prompt: "painted beach",
size: "1024x1024",
},
credentials: {
accessToken: "image-token",
projectId: "image-project",
providerSpecificData: { clientProfile: "cli" },
},
log: null,
});
assert.equal(result.success, true);
assert.equal(capturedHeaders.get("User-Agent"), antigravityCliUserAgent("1.1.1"));
assert.equal(capturedHeaders.get("x-client-name"), null);
assert.equal(capturedHeaders.get("X-Goog-Api-Client"), null);
assert.equal(capturedBody.userAgent, "antigravity");
assert.equal(capturedBody.requestType, "image_gen");
});