|
| 1 | +/** |
| 2 | + * Regression tests for issue #1882: the JS call sites that open a |
| 3 | + * `NativeDatabase` must pass the resolved `config.db.busyTimeoutMs` through |
| 4 | + * as the factory's `busyTimeoutMs` argument, instead of silently relying on |
| 5 | + * the Rust-side hardcoded default. |
| 6 | + * |
| 7 | + * Mocks `infrastructure/native.js` (the same pattern as |
| 8 | + * `tests/unit/openRepo-busy.test.ts`) so the assertions are about *what |
| 9 | + * argument each call site passes*, independent of whether a native addon is |
| 10 | + * actually built for the current platform. |
| 11 | + */ |
| 12 | +import fs from 'node:fs'; |
| 13 | +import os from 'node:os'; |
| 14 | +import path from 'node:path'; |
| 15 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 16 | + |
| 17 | +const CUSTOM_BUSY_TIMEOUT_MS = 424242; |
| 18 | + |
| 19 | +const openReadonlyCalls: Array<[string, number | undefined]> = []; |
| 20 | +const openReadWriteCalls: Array<[string, number | undefined]> = []; |
| 21 | + |
| 22 | +function makeFakeNativeDb() { |
| 23 | + return { |
| 24 | + getBuildMeta: () => null, |
| 25 | + close: () => {}, |
| 26 | + initSchema: () => {}, |
| 27 | + exec: () => {}, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +vi.mock('../../src/infrastructure/native.js', () => ({ |
| 32 | + isNativeAvailable: () => true, |
| 33 | + getNative: () => ({ |
| 34 | + NativeDatabase: { |
| 35 | + openReadonly: (dbPath: string, busyTimeoutMs?: number) => { |
| 36 | + openReadonlyCalls.push([dbPath, busyTimeoutMs]); |
| 37 | + return makeFakeNativeDb(); |
| 38 | + }, |
| 39 | + openReadWrite: (dbPath: string, busyTimeoutMs?: number) => { |
| 40 | + openReadWriteCalls.push([dbPath, busyTimeoutMs]); |
| 41 | + return makeFakeNativeDb(); |
| 42 | + }, |
| 43 | + }, |
| 44 | + }), |
| 45 | + loadNative: () => ({ |
| 46 | + NativeDatabase: { |
| 47 | + openReadonly: (dbPath: string, busyTimeoutMs?: number) => { |
| 48 | + openReadonlyCalls.push([dbPath, busyTimeoutMs]); |
| 49 | + return makeFakeNativeDb(); |
| 50 | + }, |
| 51 | + openReadWrite: (dbPath: string, busyTimeoutMs?: number) => { |
| 52 | + openReadWriteCalls.push([dbPath, busyTimeoutMs]); |
| 53 | + return makeFakeNativeDb(); |
| 54 | + }, |
| 55 | + }, |
| 56 | + }), |
| 57 | +})); |
| 58 | + |
| 59 | +import { |
| 60 | + closeDb, |
| 61 | + initSchema, |
| 62 | + openDb, |
| 63 | + openReadonlyWithNative, |
| 64 | + openRepo, |
| 65 | +} from '../../src/db/index.js'; |
| 66 | +import { PipelineContext } from '../../src/domain/graph/builder/context.js'; |
| 67 | +import { reopenNativeDb } from '../../src/domain/graph/builder/stages/native-db-lifecycle.js'; |
| 68 | + |
| 69 | +let tmpDir: string; |
| 70 | +let dbPath: string; |
| 71 | + |
| 72 | +beforeAll(() => { |
| 73 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-native-busy-threading-')); |
| 74 | + dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); |
| 75 | + const db = openDb(dbPath); |
| 76 | + initSchema(db); |
| 77 | + closeDb(db); |
| 78 | + fs.writeFileSync( |
| 79 | + path.join(tmpDir, '.codegraphrc.json'), |
| 80 | + JSON.stringify({ db: { busyTimeoutMs: CUSTOM_BUSY_TIMEOUT_MS } }), |
| 81 | + ); |
| 82 | +}); |
| 83 | + |
| 84 | +afterAll(() => { |
| 85 | + if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('openRepo threads busyTimeoutMs into NativeDatabase.openReadonly', () => { |
| 89 | + it('passes the configured busyTimeoutMs to the native factory', () => { |
| 90 | + openReadonlyCalls.length = 0; |
| 91 | + const { close } = openRepo(dbPath); |
| 92 | + close(); |
| 93 | + expect(openReadonlyCalls).toHaveLength(1); |
| 94 | + expect(openReadonlyCalls[0]?.[1]).toBe(CUSTOM_BUSY_TIMEOUT_MS); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('openReadonlyWithNative threads busyTimeoutMs into NativeDatabase.openReadonly', () => { |
| 99 | + it('passes the configured busyTimeoutMs to the native factory', () => { |
| 100 | + openReadonlyCalls.length = 0; |
| 101 | + const { close } = openReadonlyWithNative(dbPath); |
| 102 | + close(); |
| 103 | + expect(openReadonlyCalls).toHaveLength(1); |
| 104 | + expect(openReadonlyCalls[0]?.[1]).toBe(CUSTOM_BUSY_TIMEOUT_MS); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('reopenNativeDb (build pipeline) threads ctx.config.db.busyTimeoutMs into NativeDatabase.openReadWrite', () => { |
| 109 | + it('passes ctx.config.db.busyTimeoutMs to the native factory', () => { |
| 110 | + openReadWriteCalls.length = 0; |
| 111 | + const ctx = new PipelineContext(); |
| 112 | + ctx.dbPath = dbPath; |
| 113 | + ctx.opts = { engine: 'native' }; |
| 114 | + ctx.config = { db: { busyTimeoutMs: CUSTOM_BUSY_TIMEOUT_MS } } as PipelineContext['config']; |
| 115 | + |
| 116 | + reopenNativeDb(ctx, 'test'); |
| 117 | + |
| 118 | + expect(openReadWriteCalls).toHaveLength(1); |
| 119 | + expect(openReadWriteCalls[0]?.[1]).toBe(CUSTOM_BUSY_TIMEOUT_MS); |
| 120 | + }); |
| 121 | +}); |
0 commit comments