From 9e39c066ea991fc206dc918229c63c1db3b56ef8 Mon Sep 17 00:00:00 2001 From: luvs01 <27862058+luvs01@users.noreply.github.com> Date: Sun, 27 Sep 2026 13:54:59 +0900 Subject: [PATCH] test(compaction): drain fixture state and close routing history --- tests/helpers/compaction-routing-fixtures.ts | 38 ++++++++++++++++++- .../responses-compaction-routing.test.ts | 24 ++++++------ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/tests/helpers/compaction-routing-fixtures.ts b/tests/helpers/compaction-routing-fixtures.ts index fb4db7b627b..1c2688692a0 100644 --- a/tests/helpers/compaction-routing-fixtures.ts +++ b/tests/helpers/compaction-routing-fixtures.ts @@ -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 { + await flushResponseState(); + clearResponseStateForTests(); + closeRequestHistoryIndex(); +} + +export async function removeCompactionFixture(path: string): Promise { + await drainCompactionResponseState(); + removeTreeWithRetry(path); +} + export function keyProviderConfig(overrides: Partial = {}): OcxConfig { return { defaultProvider: "gw", diff --git a/tests/responses/responses-compaction-routing.test.ts b/tests/responses/responses-compaction-routing.test.ts index 52540f1dd4a..587159b58dd 100644 --- a/tests/responses/responses-compaction-routing.test.ts +++ b/tests/responses/responses-compaction-routing.test.ts @@ -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. @@ -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)", () => { @@ -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; @@ -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; @@ -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; @@ -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; @@ -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;