Skip to content

fix(mcp): give the audit tests a loader seam createRequire cannot hide from - #9559

Merged
diegosouzapw merged 2 commits into
release/v3.8.50from
fix/mcp-audit-vitest-seam
Aug 6, 2026
Merged

diegosouzapw merged 2 commits into
release/v3.8.50from
fix/mcp-audit-vitest-seam

Conversation

@diegosouzapw

Copy link
Copy Markdown
Collaborator

Problem

Vitest (fast-path) reds the entire PR queue on the 3 audit.test.ts shutdown/fallback tests. This has been misdiagnosed as a flake (the #9095 merge notes call it a "pre-existing audit.test.ts flake") — it is deterministic: reproduced 3/3 locally on the clean tip.

Root cause

#8959 switched the audit DB loader to createRequire("better-sqlite3") so Electron/global-install resolution works. vi.doMock only patches Vitest's ESM module graph — a CommonJS createRequire load is invisible to it. The test's better-sqlite3 mock never engaged: the code opened a REAL empty sqlite file in the temp DATA_DIR (no such table: mcp_tool_audit on stderr) and every mock assertion counted 0 calls.

Fix

  • Shutdown tests inject the mock through the audit connection cache (globalThis.__omnirouteMcpAuditDb) — the module's own seam, no module mocking needed.
  • Fallback test drives __setBetterSqliteLoaderForTests, a new test-only loader override in audit.ts; the production createRequire path is unchanged (node:sqlite itself is import()'d, so its doMock still works and the fallback is exercised end-to-end).

Validation (TDD)

  • Before: 3/3 fail deterministically on the clean tip.
  • After: 3/3 green; full open-sse/mcp-server vitest suite 88/88; lint clean.

Refs #8959 #9095

…e from

Since #8959 the audit DB loads better-sqlite3 via createRequire() (so
Electron/global-install resolution works) — which vi.doMock cannot
intercept: it only patches Vitest's ESM module graph. The audit.test.ts
better-sqlite3 mock therefore never engaged; the tests opened a REAL
empty sqlite file in the temp DATA_DIR ('no such table: mcp_tool_audit'
on stderr) and every mock assertion counted zero calls. The 3 failures
are deterministic (reproduced 3/3 locally), redding Vitest (fast-path)
for the entire PR queue — long misdiagnosed as a flake (#9095 merge
notes call it 'pre-existing audit.test.ts flake').

- Shutdown tests inject the mock through the audit connection cache
  (globalThis.__omnirouteMcpAuditDb) — the module's own seam.
- The node:sqlite fallback test drives __setBetterSqliteLoaderForTests,
  a test-only loader override; the production createRequire path is
  untouched (node:sqlite itself is import()'d, so its doMock still
  works).

3/3 red -> 3/3 green; full open-sse/mcp-server vitest suite 88/88.
@diegosouzapw
diegosouzapw merged commit a33fb7c into release/v3.8.50 Aug 6, 2026
9 of 10 checks passed
@diegosouzapw
diegosouzapw deleted the fix/mcp-audit-vitest-seam branch August 7, 2026 14:42
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…e from (cheaper-inference#9559)

* fix(mcp): give the audit tests a loader seam createRequire cannot hide from

Since cheaper-inference#8959 the audit DB loads better-sqlite3 via createRequire() (so
Electron/global-install resolution works) — which vi.doMock cannot
intercept: it only patches Vitest's ESM module graph. The audit.test.ts
better-sqlite3 mock therefore never engaged; the tests opened a REAL
empty sqlite file in the temp DATA_DIR ('no such table: mcp_tool_audit'
on stderr) and every mock assertion counted zero calls. The 3 failures
are deterministic (reproduced 3/3 locally), redding Vitest (fast-path)
for the entire PR queue — long misdiagnosed as a flake (cheaper-inference#9095 merge
notes call it 'pre-existing audit.test.ts flake').

- Shutdown tests inject the mock through the audit connection cache
  (globalThis.__omnirouteMcpAuditDb) — the module's own seam.
- The node:sqlite fallback test drives __setBetterSqliteLoaderForTests,
  a test-only loader override; the production createRequire path is
  untouched (node:sqlite itself is import()'d, so its doMock still
  works).

3/3 red -> 3/3 green; full open-sse/mcp-server vitest suite 88/88.

* chore: align changelog slug with the PR number (9559)

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant