From 5d38d9b3d56d8493d50dc147c08e8a3a3b3f31cc Mon Sep 17 00:00:00 2001 From: Pratyush Sharma <56130065+pratyush618@users.noreply.github.com> Date: Fri, 24 Apr 2026 01:47:48 +0530 Subject: [PATCH] refactor(fts): drop targetConn! assertions, add targetConn-path test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses the adversarial review on PR #726. - Replaced `targetConn!` non-null assertions in loadFTSExtension with a typed local that TypeScript can narrow without `!`. - Added an integration test exercising loadFTSExtension(conn) on a fresh Connection — the path used by the pool adapter but previously only covered transitively through search-pool tests. --- gitnexus/src/core/lbug/lbug-adapter.ts | 17 ++++++------- .../integration/lbug-core-adapter.test.ts | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) 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');