diff --git a/gitnexus/src/core/lbug/lbug-adapter.ts b/gitnexus/src/core/lbug/lbug-adapter.ts index a765665dc..63d1b3db1 100644 --- a/gitnexus/src/core/lbug/lbug-adapter.ts +++ b/gitnexus/src/core/lbug/lbug-adapter.ts @@ -1156,12 +1156,11 @@ export const getEmbeddingTableName = (): string => EMBEDDING_TABLE_NAME; */ export const loadFTSExtension = async (targetConn?: lbug.Connection): Promise => { const useModuleState = targetConn === undefined; - if (useModuleState) { - if (ftsLoaded) return true; - if (!conn) { - throw new Error('LadybugDB not initialized. Call initLbug first.'); - } - targetConn = conn; + if (useModuleState && ftsLoaded) return true; + + const c: lbug.Connection | null = targetConn ?? conn; + if (!c) { + throw new Error('LadybugDB not initialized. Call initLbug first.'); } const markLoaded = (): true => { @@ -1171,13 +1170,13 @@ export const loadFTSExtension = async (targetConn?: lbug.Connection): Promise { + const lbug = (await import('@ladybugdb/core')).default; + const { loadFTSExtension, getDatabase } = + await import('../../src/core/lbug/lbug-adapter.js'); + + const db = getDatabase(); + expect(db).not.toBeNull(); + + // Fresh Connection on the same Database — simulates the pool adapter's + // path where loadFTSExtension is called with an explicit connection + // rather than the module-level singleton. + const freshConn = new lbug.Connection(db!); + try { + const loaded = await loadFTSExtension(freshConn); + expect(loaded).toBe(true); + + // Idempotent on the same connection — calling again still returns true + // (exercises the "already loaded" catch branch in the fallback path). + const loadedAgain = await loadFTSExtension(freshConn); + expect(loadedAgain).toBe(true); + } finally { + await freshConn.close().catch(() => {}); + } + }); + it('getLbugStats: returns correct node and edge counts for seeded data', async () => { const { getLbugStats } = await import('../../src/core/lbug/lbug-adapter.js');