Files
codegraph/__tests__/sqlite-backend.test.ts
T
55daeffe13 fix(db): surface SQLite backend in status + actionable WASM-fallback banner (#148)
Closes the visibility gap behind issues #138 (WASM-on-macOS) and #139
(MCP "database is locked"). `better-sqlite3` is in optionalDependencies,
so when the native build fails npm install still succeeds and the
runtime silently falls back to node-sqlite3-wasm — 5-10x slower and
without WAL, so writers block readers (which is what makes the MCP
server appear to "lock the DB" in #139). The only existing signal was
a one-line `console.warn` to stderr that MCP transports typically
swallow.

This patch does NOT change install behavior — better-sqlite3 stays in
optionalDependencies so cross-platform installs keep working. It just
makes the substitution observable + recoverable.

## Visibility (4 surfaces)

- CLI `codegraph status`: new `Backend:` line under Index Statistics.
  `native` rendered green; `wasm` rendered yellow with an inline
  `npm rebuild better-sqlite3` nudge. Also exposed in `--json` as
  `backend: 'native' | 'wasm'`.
- MCP `codegraph_status`: new `**Backend:**` line. Native form reads
  `native (better-sqlite3)`; wasm form prepends a warning glyph and
  includes the full fix recipe.
- Stderr banner on fallback (`buildWasmFallbackBanner`): replaces the
  bare one-line `console.warn` with a multi-line bordered banner
  covering macOS + Linux fix steps and optionally appending the
  native load error.
- README troubleshooting: new "Indexing is slow / MCP database is
  locked / WASM fallback active" entry that walks users to the
  `Backend:` line and the fix.

## Per-instance backend tracking

`createDatabase` previously set a module-level `activeBackend` global.
MCP can open multiple project DBs in one process via the
`getCodeGraph()` cache, so the global would race / overwrite. Refactor:
`createDatabase` now returns `{db, backend}`, `DatabaseConnection`
carries `private backend` and exposes `getBackend()`, and
`CodeGraph.getBackend()` is the public surface. The CLI and MCP both
call `cg.getBackend()`.

## What this does NOT fix

The root cause of users landing on WASM is environment-specific (Mac
without Xcode CLT, Node version mismatch, etc.) and not fixable in
code without changing the optionalDependencies design. The README
entry tells users what to run; `Backend: native` after rebuild is the
confirmation signal.

## Tests

New `__tests__/sqlite-backend.test.ts` (6 tests) pins the banner
recipe content (so future edits can't strip the recovery commands),
the `WASM_FALLBACK_FIX_RECIPE` constant, and per-instance
`DatabaseConnection.getBackend()` / `CodeGraph.getBackend()` reporting.
Suite: 503 → 509, all passing.

Credit to @andreinknv whose analysis on #138 (and patches on his fork
at 6d0e7a2 + 69f7001) framed the visibility approach.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 08:27:53 -05:00

87 lines
2.8 KiB
TypeScript

/**
* SQLite backend visibility tests
*
* Pins the WASM-fallback banner content + the per-instance backend
* tracking. Closes the visibility gap behind issue #138.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import {
buildWasmFallbackBanner,
WASM_FALLBACK_FIX_RECIPE,
} from '../src/db/sqlite-adapter';
import { DatabaseConnection } from '../src/db';
import { CodeGraph } from '../src';
describe('buildWasmFallbackBanner — fix-recipe content', () => {
it('includes the macOS / Linux / cross-platform fix commands', () => {
const banner = buildWasmFallbackBanner();
expect(banner).toContain('WASM SQLite fallback active');
expect(banner).toContain('5-10x slower');
expect(banner).toContain('xcode-select --install');
expect(banner).toContain('apt install build-essential');
expect(banner).toContain('npm rebuild better-sqlite3');
expect(banner).toContain('npm install better-sqlite3 --save');
expect(banner).toContain('codegraph status');
});
it('appends the native load error when one is provided', () => {
const banner = buildWasmFallbackBanner(
"Cannot find module 'better-sqlite3'"
);
expect(banner).toContain(
"Native load error: Cannot find module 'better-sqlite3'"
);
});
it('omits the load-error block when no error is supplied', () => {
const banner = buildWasmFallbackBanner();
expect(banner).not.toContain('Native load error:');
});
});
describe('WASM_FALLBACK_FIX_RECIPE — single source of truth', () => {
it('mentions the three recovery commands', () => {
expect(WASM_FALLBACK_FIX_RECIPE).toContain('xcode-select --install');
expect(WASM_FALLBACK_FIX_RECIPE).toContain('npm rebuild better-sqlite3');
expect(WASM_FALLBACK_FIX_RECIPE).toContain(
'npm install better-sqlite3 --save'
);
});
});
describe('DatabaseConnection — per-instance backend reporting', () => {
let dir: string;
beforeEach(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-backend-'));
});
afterEach(() => {
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
it('reports a concrete backend (native or wasm) for an initialized DB', () => {
const dbPath = path.join(dir, 'test.db');
const conn = DatabaseConnection.initialize(dbPath);
const backend = conn.getBackend();
expect(['native', 'wasm']).toContain(backend);
conn.close();
});
it('CodeGraph.getBackend() delegates to the underlying DatabaseConnection', async () => {
fs.writeFileSync(path.join(dir, 'x.ts'), `export function x(): void {}\n`);
const cg = await CodeGraph.init(dir, { index: true });
try {
expect(['native', 'wasm']).toContain(cg.getBackend());
} finally {
cg.destroy();
}
});
});