From f8131c8a19a7a7a921884d3ebf0151a2a2c2b09a Mon Sep 17 00:00:00 2001 From: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com> Date: Mon, 15 Jun 2026 14:10:55 -0300 Subject: [PATCH] fix(test): de-flake pipeline-payloads via per-test re-seed + honest reset (#3893) Integrated into release/v3.8.26 --- .../provider-request-failure-pipeline.test.ts | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/tests/unit/provider-request-failure-pipeline.test.ts b/tests/unit/provider-request-failure-pipeline.test.ts index d58b8ed15d..c9681b09f1 100644 --- a/tests/unit/provider-request-failure-pipeline.test.ts +++ b/tests/unit/provider-request-failure-pipeline.test.ts @@ -10,6 +10,7 @@ process.env.DATA_DIR = TEST_DATA_DIR; const core = await import("../../src/lib/db/core.ts"); const settingsDb = await import("../../src/lib/db/settings.ts"); +const { invalidateDbCache } = await import("../../src/lib/db/readCache.ts"); const { invalidateCacheControlSettingsCache } = await import("../../src/lib/cacheControlSettings.ts"); const { clearCache } = await import("../../src/lib/semanticCache.ts"); @@ -17,14 +18,12 @@ const { clearIdempotency } = await import("../../src/lib/idempotencyLayer.ts"); const { getPendingRequests, clearPendingRequests } = await import("../../src/lib/usage/usageHistory.ts"); const { clearInflight } = await import("../../open-sse/services/requestDedup.ts"); -const { - resetAll: resetAccountSemaphores, -} = await import("../../open-sse/services/accountSemaphore.ts"); +const { resetAll: resetAccountSemaphores } = + await import("../../open-sse/services/accountSemaphore.ts"); const { clearModelLock } = await import("../../open-sse/services/accountFallback.ts"); const { getCallLogs, getCallLogById } = await import("../../src/lib/usage/callLogs.ts"); const { handleChatCore } = await import("../../open-sse/handlers/chatCore.ts"); -const { resetPayloadRulesConfigForTests } = - await import("../../open-sse/services/payloadRules.ts"); +const { resetPayloadRulesConfigForTests } = await import("../../open-sse/services/payloadRules.ts"); const originalFetch = globalThis.fetch; @@ -61,11 +60,21 @@ async function resetStorage() { clearInflight(); clearModelLock(); core.resetDbInstance(); + // A full reset must also drop the settings read-cache. Otherwise the cached + // value (e.g. call_log_pipeline_enabled=true seeded earlier) survives the DB + // wipe and silently masks the fact that the fresh DB has the default. In CI + // under load this cache is evicted at unpredictable times, so tests that rely + // on the stale cache flake. Make the reset honest and deterministic here. + invalidateDbCache("settings"); fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); fs.mkdirSync(TEST_DATA_DIR, { recursive: true }); } -test.before(async () => { +// Re-seed per test, NOT once: every `afterEach` runs resetStorage(), which wipes +// the DB and drops the settings cache. A run-once `before` only guaranteed the +// flag for the first test; later tests depended on a stale cache surviving the +// wipe, which flakes under CI load. beforeEach re-establishes it deterministically. +test.beforeEach(async () => { await settingsDb.updateSettings({ call_log_pipeline_enabled: true }); }); @@ -117,22 +126,23 @@ test("network failure persisted call log includes providerRequest in pipeline pa const detail = await waitFor(getLatestCallLog); assert.ok(detail, "expected a call log to be persisted"); - assert.ok(detail.pipelinePayloads, "expected pipeline payloads when call_log_pipeline_enabled is true"); + assert.ok( + detail.pipelinePayloads, + "expected pipeline payloads when call_log_pipeline_enabled is true" + ); assert.ok( detail.pipelinePayloads.providerRequest, "providerRequest must be present in pipeline payloads even on network failure" ); const providerReqBody = - detail.pipelinePayloads.providerRequest.body ?? - detail.pipelinePayloads.providerRequest; + detail.pipelinePayloads.providerRequest.body ?? detail.pipelinePayloads.providerRequest; assert.equal( providerReqBody.model, "gpt-4o-mini", "providerRequest should contain the translated model" ); const messages = - providerReqBody.messages ?? - (Array.isArray(providerReqBody) ? providerReqBody : null); + providerReqBody.messages ?? (Array.isArray(providerReqBody) ? providerReqBody : null); if (messages) { assert.equal(messages[0]?.content, "hello"); } @@ -141,10 +151,7 @@ test("network failure persisted call log includes providerRequest in pipeline pa null, "providerResponse should be null/absent on network failure (no response received)" ); - assert.ok( - detail.pipelinePayloads.error, - "pipeline payloads should include the error details" - ); + assert.ok(detail.pipelinePayloads.error, "pipeline payloads should include the error details"); }); test("network timeout persisted call log includes providerRequest in pipeline payloads", async () => { @@ -181,10 +188,7 @@ test("network timeout persisted call log includes providerRequest in pipeline pa await waitForAsyncSideEffects(); assert.equal(result.success, false); - assert.ok( - result.status === 504, - `expected 504 timeout, got ${result.status}` - ); + assert.ok(result.status === 504, `expected 504 timeout, got ${result.status}`); const detail = await waitFor(getLatestCallLog); assert.ok(detail, "expected a call log to be persisted"); @@ -195,12 +199,8 @@ test("network timeout persisted call log includes providerRequest in pipeline pa "providerRequest must be present in pipeline payloads on timeout" ); const providerReqBody = - detail.pipelinePayloads?.providerRequest?.body ?? - detail.pipelinePayloads?.providerRequest; - assert.equal( - providerReqBody?.model, - "gpt-4o-mini" - ); + detail.pipelinePayloads?.providerRequest?.body ?? detail.pipelinePayloads?.providerRequest; + assert.equal(providerReqBody?.model, "gpt-4o-mini"); } finally { if (originalGetTimeoutMs) executor.getTimeoutMs = originalGetTimeoutMs; } @@ -311,10 +311,7 @@ test("successful response includes both providerRequest and providerResponse in assert.ok(detail, "expected a call log to be persisted"); assert.ok(detail.pipelinePayloads, "expected pipeline payloads"); - assert.ok( - detail.pipelinePayloads.providerRequest, - "providerRequest must be present on success" - ); + assert.ok(detail.pipelinePayloads.providerRequest, "providerRequest must be present on success"); assert.ok( detail.pipelinePayloads.providerResponse, "providerResponse must be present on success"