Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions tests/helpers/compaction-routing-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
import type { OcxConfig, OcxProviderConfig } from "../../src/types";
import { afterAll, beforeAll } from "bun:test";
import { clearResponseStateForTests, flushResponseState } from "../../src/responses/state";
import { flushConfigDirHardeningForTests } from "../../src/config/paths";
import { setAsyncIcaclsRunnerForTests, setIcaclsRunnerForTests } from "../../src/lib/windows-secret-acl";
import { closeRequestHistoryIndex } from "../../src/routing/history/indexer";
import { removeTreeWithRetry } from "./remove-tree";

/**
* Config, request and upstream-response fixtures for the compaction-routing suite.
*
* Moved verbatim out of tests/responses/responses-compaction-routing.test.ts: that file sits at
* its file-size cap, and the repository answer to a cap is a sibling helper rather than
* compressed control flow. Nothing here decides anything; every value is the one its callers
* were already building inline.
* compressed control flow. Fixture teardown also owns the continuation writes started by
* direct handler calls, so they cannot survive a fixture-home switch.
*/
export function installCompactionRoutingAclFixture(): void {
// These cases prove routing and replay with synthetic credentials, not Windows DACLs.
// Actual ACL contracts have their own subprocess tests; incidental spawns here can
// outlive a case timeout and mutate the next fixture's continuation state.
beforeAll(() => {
const ok = { success: true, exitCode: 0, timedOut: false, stdout: "" };
setIcaclsRunnerForTests(() => ok);
setAsyncIcaclsRunnerForTests(async () => ok);
});
afterAll(async () => {
try { await flushConfigDirHardeningForTests(); } finally {
setIcaclsRunnerForTests(null);
setAsyncIcaclsRunnerForTests(null);
}
});
}

export async function drainCompactionResponseState(): Promise<void> {
await flushResponseState();
clearResponseStateForTests();
closeRequestHistoryIndex();
}

export async function removeCompactionFixture(path: string): Promise<void> {
await drainCompactionResponseState();
removeTreeWithRetry(path);
}

export function keyProviderConfig(overrides: Partial<OcxProviderConfig> = {}): OcxConfig {
return {
defaultProvider: "gw",
Expand Down
24 changes: 13 additions & 11 deletions tests/responses/responses-compaction-routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ import { acquireNativeMainProfileDrain, tryAdmitTurn } from "../../src/server/li
import type { OcxConfig, OcxProviderConfig } from "../../src/types";
import { clearComboRecallForTests, recallComboForLane, rememberComboForLane } from "../../src/server/responses/combo-session-recall";
import { captureConfigGeneration } from "../../src/lib/state-store-sweeper";
import { removeTreeWithRetry } from "../helpers/remove-tree";
import { baseCompactionBody, compactionRequest, completedPayload, jsonResponse, keyProviderConfig, nativePoolConfig, sseResponse, twoAccountPoolConfig } from "../helpers/compaction-routing-fixtures";
import { baseCompactionBody, compactionRequest, completedPayload, drainCompactionResponseState, installCompactionRoutingAclFixture, jsonResponse, keyProviderConfig, nativePoolConfig, removeCompactionFixture, sseResponse, twoAccountPoolConfig } from "../helpers/compaction-routing-fixtures";
import { acquireOwnedSpendHome } from "../helpers/owned-spend-home";
import { SERVER_BUDGET_MS } from "../helpers/test-budget";

const originalFetch = globalThis.fetch;
installCompactionRoutingAclFixture();

// A case that calls a handler directly never runs startServer, so it never takes the
// spend-journal writer lease and its dispatch is refused before it reaches its own contract.
Expand All @@ -56,9 +56,11 @@ let releaseSpendHome: (() => void) | undefined;
const takeSpendHome = (): void => { releaseSpendHome ??= acquireOwnedSpendHome(); };
const dropSpendHome = (): void => { releaseSpendHome?.(); releaseSpendHome = undefined; };

afterEach(() => {
dropSpendHome();
globalThis.fetch = originalFetch;
afterEach(async () => {
try { await drainCompactionResponseState(); } finally {
dropSpendHome();
globalThis.fetch = originalFetch;
}
});

describe("supportsNativeResponsesCompactEndpoint (#422)", () => {
Expand Down Expand Up @@ -323,7 +325,7 @@ describe("native compact usage reporting", () => {
clearAccountQuota();
// Released before the directory holding it is removed.
dropSpendHome();
removeTreeWithRetry(testDir);
await removeCompactionFixture(testDir);
if (previousOpencodexHome === undefined) delete process.env.OPENCODEX_HOME;
else process.env.OPENCODEX_HOME = previousOpencodexHome;
if (previousCodexHome === undefined) delete process.env.CODEX_HOME;
Expand Down Expand Up @@ -398,7 +400,7 @@ describe("native Codex pool compaction", () => {
clearCodexUpstreamHealth();
// Released before the directory holding it is removed.
dropSpendHome();
removeTreeWithRetry(testDir);
await removeCompactionFixture(testDir);
if (previousOpencodexHome === undefined) delete process.env.OPENCODEX_HOME;
else process.env.OPENCODEX_HOME = previousOpencodexHome;
if (previousCodexHome === undefined) delete process.env.CODEX_HOME;
Expand Down Expand Up @@ -473,7 +475,7 @@ describe("native Codex pool compaction", () => {
clearCodexUpstreamHealth();
// Released before the directory holding it is removed.
dropSpendHome();
removeTreeWithRetry(testDir);
await removeCompactionFixture(testDir);
if (previousOpencodexHome === undefined) delete process.env.OPENCODEX_HOME;
else process.env.OPENCODEX_HOME = previousOpencodexHome;
if (previousCodexHome === undefined) delete process.env.CODEX_HOME;
Expand Down Expand Up @@ -543,7 +545,7 @@ describe("native Codex pool compaction", () => {
clearCodexUpstreamHealth();
// Released before the directory holding it is removed.
dropSpendHome();
removeTreeWithRetry(testDir);
await removeCompactionFixture(testDir);
if (previousOpencodexHome === undefined) delete process.env.OPENCODEX_HOME;
else process.env.OPENCODEX_HOME = previousOpencodexHome;
if (previousCodexHome === undefined) delete process.env.CODEX_HOME;
Expand Down Expand Up @@ -886,14 +888,14 @@ describe("compact alternate-account attempt (#913)", () => {
});
updateAccountQuota(id, id === "pool-a" ? 10 : 20);
}
return run(twoAccountPoolConfig()).finally(() => {
return run(twoAccountPoolConfig()).finally(async () => {
globalThis.fetch = originalFetch;
clearCodexUpstreamHealth();
clearUpstreamHostHealth();
clearAccountQuota();
// Released before the directory holding it is removed.
dropSpendHome();
removeTreeWithRetry(testDir);
await removeCompactionFixture(testDir);
if (previousOpencodexHome === undefined) delete process.env.OPENCODEX_HOME;
else process.env.OPENCODEX_HOME = previousOpencodexHome;
if (previousCodexHome === undefined) delete process.env.CODEX_HOME;
Expand Down
Loading