From d59604b15ede07bdfc236d6c9810099806e1be12 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 16 Apr 2026 22:55:14 +0100 Subject: [PATCH] test: speed up hotspot boundaries --- .../src/host/embeddings-gemini.test.ts | 16 +- src/acp/control-plane/manager.test.ts | 106 +++++--- .../host/embeddings-gemini.test.ts | 70 +---- src/pairing/pairing-store.test.ts | 242 +++++++++--------- src/plugin-activation-boundary.test.ts | 34 +-- 5 files changed, 211 insertions(+), 257 deletions(-) diff --git a/packages/memory-host-sdk/src/host/embeddings-gemini.test.ts b/packages/memory-host-sdk/src/host/embeddings-gemini.test.ts index af6ea7f14f9..358c3ccd315 100644 --- a/packages/memory-host-sdk/src/host/embeddings-gemini.test.ts +++ b/packages/memory-host-sdk/src/host/embeddings-gemini.test.ts @@ -246,6 +246,8 @@ describe("gemini embedding provider", () => { outputDimensionality: 768, taskType: "SEMANTIC_SIMILARITY", }); + await expect(v2QueryProvider.embedQuery(" ")).resolves.toEqual([]); + await expect(v2QueryProvider.embedBatch([])).resolves.toEqual([]); expectNormalizedThreeFourVector(await v2QueryProvider.embedQuery("test query")); const v2BatchFetch = createGeminiBatchFetchMock(2, [3, 4]); @@ -286,18 +288,4 @@ describe("gemini model normalization", () => { ); expect(normalizeGeminiModel("")).toBe(DEFAULT_GEMINI_EMBEDDING_MODEL); }); - - it("returns empty arrays without fetching for blank query and empty batch", async () => { - mockResolvedProviderKey(); - - const { provider } = await createGeminiEmbeddingProvider({ - config: {} as never, - provider: "gemini", - model: "gemini-embedding-2-preview", - fallback: "none", - }); - - await expect(provider.embedQuery(" ")).resolves.toEqual([]); - await expect(provider.embedBatch([])).resolves.toEqual([]); - }); }); diff --git a/src/acp/control-plane/manager.test.ts b/src/acp/control-plane/manager.test.ts index c9d44a4502d..192def6c4ca 100644 --- a/src/acp/control-plane/manager.test.ts +++ b/src/acp/control-plane/manager.test.ts @@ -90,6 +90,14 @@ async function flushMicrotasks(rounds = 3): Promise { } } +function createDeferred(): { promise: Promise; resolve: () => void } { + let resolve!: () => void; + const promise = new Promise((next) => { + resolve = next; + }); + return { promise, resolve }; +} + function createRuntime(): { runtime: AcpRuntime; ensureSession: ReturnType; @@ -391,11 +399,14 @@ describe("AcpSessionManager", () => { let inFlight = 0; let maxInFlight = 0; + const releaseFirstTurn = createDeferred(); runtimeState.runTurn.mockImplementation(async function* (_input: { requestId: string }) { inFlight += 1; maxInFlight = Math.max(maxInFlight, inFlight); try { - await sleep(10); + if (_input.requestId === "r1") { + await releaseFirstTurn.promise; + } yield { type: "done" }; } finally { inFlight -= 1; @@ -410,6 +421,12 @@ describe("AcpSessionManager", () => { mode: "prompt", requestId: "r1", }); + await vi.waitFor( + () => { + expect(runtimeState.runTurn).toHaveBeenCalledTimes(1); + }, + { interval: 1 }, + ); const second = manager.runTurn({ cfg: baseCfg, sessionKey: "agent:codex:acp:session-1", @@ -417,6 +434,9 @@ describe("AcpSessionManager", () => { mode: "prompt", requestId: "r2", }); + await flushMicrotasks(); + expect(runtimeState.runTurn).toHaveBeenCalledTimes(1); + releaseFirstTurn.resolve(); await Promise.all([first, second]); expect(maxInFlight).toBe(1); @@ -455,9 +475,12 @@ describe("AcpSessionManager", () => { mode: "prompt", requestId: "r1", }); - await vi.waitFor(() => { - expect(firstTurnStarted).toBe(true); - }); + await vi.waitFor( + () => { + expect(firstTurnStarted).toBe(true); + }, + { interval: 1 }, + ); const abortController = new AbortController(); const second = manager.runTurn({ @@ -482,9 +505,12 @@ describe("AcpSessionManager", () => { releaseFirstTurn?.(); await first; - await vi.waitFor(() => { - expect(manager.getObservabilitySnapshot(baseCfg).turns.queueDepth).toBe(0); - }); + await vi.waitFor( + () => { + expect(manager.getObservabilitySnapshot(baseCfg).turns.queueDepth).toBe(0); + }, + { interval: 1 }, + ); expect(secondOutcome.status).toBe("rejected"); if (secondOutcome.status !== "rejected") { @@ -539,9 +565,12 @@ describe("AcpSessionManager", () => { requestId: "r1", }); void first.catch(() => undefined); - await vi.waitFor(() => { - expect(firstTurnStarted).toBe(true); - }); + await vi.waitFor( + () => { + expect(firstTurnStarted).toBe(true); + }, + { interval: 1 }, + ); const second = manager.runTurn({ cfg, @@ -639,9 +668,12 @@ describe("AcpSessionManager", () => { requestId: "r1", }); void first.catch(() => undefined); - await vi.waitFor(() => { - expect(firstTurnStarted).toBe(true); - }); + await vi.waitFor( + () => { + expect(firstTurnStarted).toBe(true); + }, + { interval: 1 }, + ); await vi.advanceTimersByTimeAsync(4_500); @@ -689,11 +721,15 @@ describe("AcpSessionManager", () => { let inFlight = 0; let maxInFlight = 0; + const bothTurnsEntered = createDeferred(); runtimeState.runTurn.mockImplementation(async function* () { inFlight += 1; maxInFlight = Math.max(maxInFlight, inFlight); + if (inFlight === 2) { + bothTurnsEntered.resolve(); + } try { - await sleep(15); + await bothTurnsEntered.promise; yield { type: "done" as const }; } finally { inFlight -= 1; @@ -701,20 +737,25 @@ describe("AcpSessionManager", () => { }); const manager = new AcpSessionManager(); - await Promise.all([ - manager.runTurn({ - cfg: baseCfg, - sessionKey: "agent:codex:acp:session-a", - text: "first", - mode: "prompt", - requestId: "r1", - }), - manager.runTurn({ - cfg: baseCfg, - sessionKey: "agent:codex:acp:session-b", - text: "second", - mode: "prompt", - requestId: "r2", + await Promise.race([ + Promise.all([ + manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-a", + text: "first", + mode: "prompt", + requestId: "r1", + }), + manager.runTurn({ + cfg: baseCfg, + sessionKey: "agent:codex:acp:session-b", + text: "second", + mode: "prompt", + requestId: "r2", + }), + ]), + sleep(100).then(() => { + throw new Error("ACP sessions did not run in parallel"); }), ]); @@ -1784,9 +1825,12 @@ describe("AcpSessionManager", () => { mode: "prompt", requestId: "run-1", }); - await vi.waitFor(() => { - expect(enteredRun).toBe(true); - }); + await vi.waitFor( + () => { + expect(enteredRun).toBe(true); + }, + { interval: 1 }, + ); await manager.cancelSession({ cfg: baseCfg, diff --git a/src/memory-host-sdk/host/embeddings-gemini.test.ts b/src/memory-host-sdk/host/embeddings-gemini.test.ts index 1d6c0e061c5..585c02604f8 100644 --- a/src/memory-host-sdk/host/embeddings-gemini.test.ts +++ b/src/memory-host-sdk/host/embeddings-gemini.test.ts @@ -16,10 +16,6 @@ vi.mock("../../agents/model-auth.js", async () => { return createModelAuthMockModule(); }); -function magnitude(values: number[]) { - return Math.sqrt(values.reduce((sum, value) => sum + value * value, 0)); -} - let buildGeminiEmbeddingRequest: typeof import("./embeddings-gemini.js").buildGeminiEmbeddingRequest; let buildGeminiTextEmbeddingRequest: typeof import("./embeddings-gemini.js").buildGeminiTextEmbeddingRequest; let createGeminiEmbeddingProvider: typeof import("./embeddings-gemini.js").createGeminiEmbeddingProvider; @@ -70,12 +66,6 @@ async function createProviderWithFetch( return provider; } -function expectNormalizedThreeFourVector(embedding: number[]) { - expect(embedding[0]).toBeCloseTo(0.6, 5); - expect(embedding[1]).toBeCloseTo(0.8, 5); - expect(magnitude(embedding)).toBeCloseTo(1, 5); -} - describe("buildGeminiTextEmbeddingRequest", () => { it("builds a text embedding request with optional model and dimensions", () => { expect( @@ -192,21 +182,23 @@ describe("gemini embedding provider", () => { expect(parseFetchBody(legacyFetch, 0)).not.toHaveProperty("outputDimensionality"); expect(parseFetchBody(legacyFetch, 1)).not.toHaveProperty("outputDimensionality"); - const v2QueryFetch = createGeminiFetchMock([3, 4]); + const v2QueryFetch = createGeminiFetchMock([3, 4, Number.NaN]); const v2QueryProvider = await createProviderWithFetch(v2QueryFetch, { model: "gemini-embedding-2-preview", }); - expectNormalizedThreeFourVector(await v2QueryProvider.embedQuery("test query")); + await expect(v2QueryProvider.embedQuery(" ")).resolves.toEqual([]); + await expect(v2QueryProvider.embedBatch([])).resolves.toEqual([]); + await expect(v2QueryProvider.embedQuery("test query")).resolves.toEqual([0.6, 0.8, 0]); - const v2BatchFetch = createGeminiBatchFetchMock(2, [3, 4]); + const v2BatchFetch = createGeminiBatchFetchMock(2, [0, Number.POSITIVE_INFINITY, 5]); const v2BatchProvider = await createProviderWithFetch(v2BatchFetch, { model: "gemini-embedding-2-preview", }); const batch = await v2BatchProvider.embedBatch(["text1", "text2"]); - expect(batch).toHaveLength(2); - for (const embedding of batch) { - expectNormalizedThreeFourVector(embedding); - } + expect(batch).toEqual([ + [0, 0, 1], + [0, 0, 1], + ]); expect(parseFetchBody(v2QueryFetch)).toMatchObject({ outputDimensionality: 3072, @@ -238,7 +230,7 @@ describe("gemini embedding provider", () => { }); await provider.embedQuery("test"); - await provider.embedBatchInputs?.([ + const structuredBatch = await provider.embedBatchInputs?.([ { text: "Image file: diagram.png", parts: [ @@ -254,6 +246,10 @@ describe("gemini embedding provider", () => { ], }, ]); + expect(structuredBatch).toEqual([ + [0.2672612419124244, 0.5345224838248488, 0.8017837257372732], + [0.2672612419124244, 0.5345224838248488, 0.8017837257372732], + ]); const { url } = readFirstFetchRequest(fetchMock); expect(url).toBe( @@ -288,30 +284,6 @@ describe("gemini embedding provider", () => { }, ]); }); - - it("sanitizes non-finite query and structured batch responses", async () => { - const queryFetch = createGeminiFetchMock([3, 4, Number.NaN]); - const queryProvider = await createProviderWithFetch(queryFetch, { - model: "gemini-embedding-2-preview", - }); - await expect(queryProvider.embedQuery("test")).resolves.toEqual([0.6, 0.8, 0]); - - const batchFetch = createGeminiBatchFetchMock(1, [0, Number.POSITIVE_INFINITY, 5]); - const batchProvider = await createProviderWithFetch(batchFetch, { - model: "gemini-embedding-2-preview", - }); - await expect( - batchProvider.embedBatchInputs?.([ - { - text: "Image file: diagram.png", - parts: [ - { type: "text", text: "Image file: diagram.png" }, - { type: "inline-data", mimeType: "image/png", data: "img" }, - ], - }, - ]), - ).resolves.toEqual([[0, 0, 1]]); - }); }); // ---------- Model normalization ---------- @@ -329,18 +301,4 @@ describe("gemini model normalization", () => { ); expect(normalizeGeminiModel("")).toBe(DEFAULT_GEMINI_EMBEDDING_MODEL); }); - - it("returns empty arrays without fetching for blank query and empty batch", async () => { - mockResolvedProviderKey(authModule.resolveApiKeyForProvider); - - const { provider } = await createGeminiEmbeddingProvider({ - config: {} as never, - provider: "gemini", - model: "gemini-embedding-2-preview", - fallback: "none", - }); - - await expect(provider.embedQuery(" ")).resolves.toEqual([]); - await expect(provider.embedBatch([])).resolves.toEqual([]); - }); }); diff --git a/src/pairing/pairing-store.test.ts b/src/pairing/pairing-store.test.ts index e8f3af564f5..94ca03f9479 100644 --- a/src/pairing/pairing-store.test.ts +++ b/src/pairing/pairing-store.test.ts @@ -57,6 +57,11 @@ function resolveAllowFromFilePath(stateDir: string, channel: string, accountId?: return path.join(resolveOAuthDir(process.env, stateDir), `${channel}${suffix}-allowFrom.json`); } +async function clearOAuthFixtures(stateDir: string) { + clearPairingAllowFromReadCacheForTest(); + await fs.rm(resolveOAuthDir(process.env, stateDir), { recursive: true, force: true }); +} + async function writeAllowFromFixture(params: { stateDir: string; channel: string; @@ -441,140 +446,127 @@ describe("pairing store", () => { }); }); - it.each([ - { - name: "does not read legacy channel-scoped allowFrom for non-default account ids", - setup: async (stateDir: string) => { - await seedTelegramAllowFromFixtures({ - stateDir, - scopedAccountId: "yy", - scopedAllowFrom: ["1003"], - legacyAllowFrom: ["1001", "*", "1002", "1001"], - }); - }, - accountId: "yy", - expected: ["1003"], - }, - { - name: "does not fall back to legacy allowFrom when scoped file exists but is empty", - setup: async (stateDir: string) => { - await seedTelegramAllowFromFixtures({ - stateDir, - scopedAccountId: "yy", - scopedAllowFrom: [], - }); - }, - accountId: "yy", - expected: [], - }, - { - name: "keeps async and sync reads aligned for malformed scoped allowFrom files", - setup: async (stateDir: string) => { - await writeAllowFromFixture({ - stateDir, - channel: "telegram", - allowFrom: ["1001"], - }); - const malformedScopedPath = resolveAllowFromFilePath(stateDir, "telegram", "yy"); - await fs.mkdir(path.dirname(malformedScopedPath), { recursive: true }); - await fs.writeFile(malformedScopedPath, "{ this is not json\n", "utf8"); - }, - accountId: "yy", - expected: [], - }, - { - name: "reads legacy channel-scoped allowFrom for default account", - setup: async (stateDir: string) => { - await seedDefaultAccountAllowFromFixture(stateDir); - }, - accountId: DEFAULT_ACCOUNT_ID, - expected: ["1002", "1001"], - }, - { - name: "uses default-account allowFrom when account id is omitted", - setup: async (stateDir: string) => { - await seedDefaultAccountAllowFromFixture(stateDir); - }, - accountId: undefined, - expected: ["1002", "1001"], - }, - ] as const)("$name", async ({ setup, accountId, expected }) => { + it("reads allowFrom variants with account-scoped isolation", async () => { await withTempStateDir(async (stateDir) => { - await setup(stateDir); - await expectAllowFromReadConsistencyCase({ - ...(accountId !== undefined ? { accountId } : {}), - expected, - }); + for (const { setup, accountId, expected } of [ + { + setup: async () => { + await seedTelegramAllowFromFixtures({ + stateDir, + scopedAccountId: "yy", + scopedAllowFrom: ["1003"], + legacyAllowFrom: ["1001", "*", "1002", "1001"], + }); + }, + accountId: "yy", + expected: ["1003"], + }, + { + setup: async () => { + await seedTelegramAllowFromFixtures({ + stateDir, + scopedAccountId: "yy", + scopedAllowFrom: [], + }); + }, + accountId: "yy", + expected: [], + }, + { + setup: async () => { + await writeAllowFromFixture({ + stateDir, + channel: "telegram", + allowFrom: ["1001"], + }); + const malformedScopedPath = resolveAllowFromFilePath(stateDir, "telegram", "yy"); + await fs.mkdir(path.dirname(malformedScopedPath), { recursive: true }); + await fs.writeFile(malformedScopedPath, "{ this is not json\n", "utf8"); + }, + accountId: "yy", + expected: [], + }, + { + setup: async () => { + await seedDefaultAccountAllowFromFixture(stateDir); + }, + accountId: DEFAULT_ACCOUNT_ID, + expected: ["1002", "1001"], + }, + { + setup: async () => { + await seedDefaultAccountAllowFromFixture(stateDir); + }, + accountId: undefined, + expected: ["1002", "1001"], + }, + ] as const) { + await clearOAuthFixtures(stateDir); + await setup(); + await expectAllowFromReadConsistencyCase({ + ...(accountId !== undefined ? { accountId } : {}), + expected, + }); + } }); }); - it.each([ - { - name: "does not reuse pairing requests across accounts for the same sender id", - run: async () => { - await withTempStateDir(async () => { - await expectPendingPairingRequestsIsolatedByAccount({ - sharedId: "12345", - firstAccountId: "alpha", - secondAccountId: "beta", - }); - }); - }, - }, - { - name: "does not block a new account when other accounts already filled their own pending slots", - run: async () => { - await withTempStateDir(async () => { - for (const accountId of ["alpha", "beta", "gamma"]) { - const created = await upsertChannelPairingRequest({ - channel: "telegram", - accountId, - id: `pending-${accountId}`, - }); - expect(created.created).toBe(true); - } + it("keeps pending pairing requests isolated by account", async () => { + await withTempStateDir(async (stateDir) => { + await expectPendingPairingRequestsIsolatedByAccount({ + sharedId: "12345", + firstAccountId: "alpha", + secondAccountId: "beta", + }); - const delta = await upsertChannelPairingRequest({ - channel: "telegram", - accountId: "delta", - id: "pending-delta", - }); - expect(delta.created).toBe(true); - - const deltaList = await listChannelPairingRequests("telegram", process.env, "delta"); - const allPending = await listChannelPairingRequests("telegram"); - expect(deltaList.map((entry) => entry.id)).toEqual(["pending-delta"]); - expect(allPending.map((entry) => entry.id)).toEqual([ - "pending-alpha", - "pending-beta", - "pending-gamma", - "pending-delta", - ]); + await clearOAuthFixtures(stateDir); + for (const accountId of ["alpha", "beta", "gamma"]) { + const created = await upsertChannelPairingRequest({ + channel: "telegram", + accountId, + id: `pending-${accountId}`, }); - }, - }, - ] as const)("$name", async ({ run }) => { - await run(); + expect(created.created).toBe(true); + } + + const delta = await upsertChannelPairingRequest({ + channel: "telegram", + accountId: "delta", + id: "pending-delta", + }); + expect(delta.created).toBe(true); + + const deltaList = await listChannelPairingRequests("telegram", process.env, "delta"); + const allPending = await listChannelPairingRequests("telegram"); + expect(deltaList.map((entry) => entry.id)).toEqual(["pending-delta"]); + expect(allPending.map((entry) => entry.id)).toEqual([ + "pending-alpha", + "pending-beta", + "pending-gamma", + "pending-delta", + ]); + }); }); - it.each([ - { - label: "async", - createReadSpy: () => vi.spyOn(fs, "readFile"), - readAllowFrom: () => readChannelAllowFromStore("telegram", process.env, "yy"), - }, - { - label: "sync", - createReadSpy: () => vi.spyOn(fsSync, "readFileSync"), - readAllowFrom: async () => readChannelAllowFromStoreSync("telegram", process.env, "yy"), - }, - ])("reuses cached $label allowFrom reads and invalidates on file updates", async (variant) => { + it("reuses cached allowFrom reads and invalidates on file updates", async () => { await withTempStateDir(async (stateDir) => { - await withAllowFromCacheReadSpy({ - stateDir, - createReadSpy: variant.createReadSpy, - readAllowFrom: variant.readAllowFrom, - }); + for (const variant of [ + { + createReadSpy: () => vi.spyOn(fs, "readFile"), + readAllowFrom: () => readChannelAllowFromStore("telegram", process.env, "yy"), + }, + { + createReadSpy: () => vi.spyOn(fsSync, "readFileSync"), + readAllowFrom: async () => readChannelAllowFromStoreSync("telegram", process.env, "yy"), + }, + ]) { + await clearOAuthFixtures(stateDir); + await withAllowFromCacheReadSpy({ + stateDir, + createReadSpy: variant.createReadSpy, + readAllowFrom: variant.readAllowFrom, + }); + } }); }); }); diff --git a/src/plugin-activation-boundary.test.ts b/src/plugin-activation-boundary.test.ts index 8ee96fc81e2..b2fb255fef6 100644 --- a/src/plugin-activation-boundary.test.ts +++ b/src/plugin-activation-boundary.test.ts @@ -105,7 +105,6 @@ describe("plugin activation boundary", () => { loadBundledPluginPublicSurfaceModuleSync.mockReset(); }); - let ambientImportsPromise: Promise | undefined; let configHelpersPromise: | Promise<{ isStaticallyChannelConfigured: typeof import("./config/channel-configured-shared.js").isStaticallyChannelConfigured; @@ -134,16 +133,6 @@ describe("plugin activation boundary", () => { resolveProfile: typeof import("./plugin-sdk/browser-config.js").resolveProfile; }> | undefined; - let browserAmbientImportsPromise: Promise | undefined; - function importAmbientModules() { - ambientImportsPromise ??= Promise.all([ - import("./commands/onboard-custom.js"), - import("./plugins/provider-model-defaults.js"), - import("./plugins/provider-model-primary.js"), - ]).then(() => undefined); - return ambientImportsPromise; - } - function importConfigHelpers() { configHelpersPromise ??= Promise.all([ import("./config/channel-configured-shared.js"), @@ -185,26 +174,9 @@ describe("plugin activation boundary", () => { return browserHelpersPromise; } - function importBrowserAmbientModules() { - browserAmbientImportsPromise ??= Promise.all([ - import("./agents/sandbox/browser.js"), - import("./agents/sandbox/context.js"), - import("./commands/doctor-browser.js"), - import("./node-host/runner.js"), - import("./security/audit.js"), - import("./security/audit-extra.sync.js"), - ]).then(() => undefined); - return browserAmbientImportsPromise; - } - - it("keeps ambient core imports cold", async () => { - const [, { isStaticallyChannelConfigured, resolveEnvApiKey }, { normalizeModelRef }] = - await Promise.all([ - importAmbientModules(), - importConfigHelpers(), - importModelSelection(), - importBrowserAmbientModules(), - ]); + it("keeps config and model boundary helpers cold", async () => { + const [{ isStaticallyChannelConfigured, resolveEnvApiKey }, { normalizeModelRef }] = + await Promise.all([importConfigHelpers(), importModelSelection()]); expect(isStaticallyChannelConfigured({}, "telegram", { TELEGRAM_BOT_TOKEN: "token" })).toBe( true,