1
0

sqlite-backend.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * SQLite backend visibility tests
  3. *
  4. * Pins the WASM-fallback banner content + the per-instance backend
  5. * tracking. Closes the visibility gap behind issue #138.
  6. */
  7. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  8. import * as fs from 'fs';
  9. import * as path from 'path';
  10. import * as os from 'os';
  11. import {
  12. buildWasmFallbackBanner,
  13. WASM_FALLBACK_FIX_RECIPE,
  14. } from '../src/db/sqlite-adapter';
  15. import { DatabaseConnection } from '../src/db';
  16. import { CodeGraph } from '../src';
  17. describe('buildWasmFallbackBanner — fix-recipe content', () => {
  18. it('includes the macOS / Linux / cross-platform fix commands', () => {
  19. const banner = buildWasmFallbackBanner();
  20. expect(banner).toContain('WASM SQLite fallback active');
  21. expect(banner).toContain('5-10x slower');
  22. expect(banner).toContain('xcode-select --install');
  23. expect(banner).toContain('apt install build-essential');
  24. expect(banner).toContain('npm rebuild better-sqlite3');
  25. expect(banner).toContain('npm install better-sqlite3 --save');
  26. expect(banner).toContain('codegraph status');
  27. });
  28. it('appends the native load error when one is provided', () => {
  29. const banner = buildWasmFallbackBanner(
  30. "Cannot find module 'better-sqlite3'"
  31. );
  32. expect(banner).toContain(
  33. "Native load error: Cannot find module 'better-sqlite3'"
  34. );
  35. });
  36. it('omits the load-error block when no error is supplied', () => {
  37. const banner = buildWasmFallbackBanner();
  38. expect(banner).not.toContain('Native load error:');
  39. });
  40. });
  41. describe('WASM_FALLBACK_FIX_RECIPE — single source of truth', () => {
  42. it('mentions the three recovery commands', () => {
  43. expect(WASM_FALLBACK_FIX_RECIPE).toContain('xcode-select --install');
  44. expect(WASM_FALLBACK_FIX_RECIPE).toContain('npm rebuild better-sqlite3');
  45. expect(WASM_FALLBACK_FIX_RECIPE).toContain(
  46. 'npm install better-sqlite3 --save'
  47. );
  48. });
  49. });
  50. describe('DatabaseConnection — per-instance backend reporting', () => {
  51. let dir: string;
  52. beforeEach(() => {
  53. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-backend-'));
  54. });
  55. afterEach(() => {
  56. if (fs.existsSync(dir)) {
  57. fs.rmSync(dir, { recursive: true, force: true });
  58. }
  59. });
  60. it('reports a concrete backend (native or wasm) for an initialized DB', () => {
  61. const dbPath = path.join(dir, 'test.db');
  62. const conn = DatabaseConnection.initialize(dbPath);
  63. const backend = conn.getBackend();
  64. expect(['native', 'wasm']).toContain(backend);
  65. conn.close();
  66. });
  67. it('CodeGraph.getBackend() delegates to the underlying DatabaseConnection', async () => {
  68. fs.writeFileSync(path.join(dir, 'x.ts'), `export function x(): void {}\n`);
  69. const cg = await CodeGraph.init(dir, { index: true });
  70. try {
  71. expect(['native', 'wasm']).toContain(cg.getBackend());
  72. } finally {
  73. cg.destroy();
  74. }
  75. });
  76. });