| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188 |
- /**
- * store.test.ts - Comprehensive unit tests for the QMD store module
- *
- * Run with: bun test store.test.ts
- *
- * LLM operations use LlamaCpp with local GGUF models (node-llama-cpp).
- */
- import { describe, test, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from "vitest";
- import { openDatabase, loadSqliteVec } from "../src/db.js";
- import type { Database } from "../src/db.js";
- import { unlink, mkdtemp, rmdir, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import YAML from "yaml";
- import * as llmModule from "../src/llm.js";
- import { disposeDefaultLlamaCpp, setDefaultLlamaCpp } from "../src/llm.js";
- import {
- createStore,
- verifySqliteVecLoaded,
- getDefaultDbPath,
- homedir,
- resolve,
- getPwd,
- getRealPath,
- hashContent,
- extractTitle,
- formatQueryForEmbedding,
- formatDocForEmbedding,
- chunkDocument,
- chunkDocumentByTokens,
- scanBreakPoints,
- findCodeFences,
- isInsideCodeFence,
- findBestCutoff,
- type BreakPoint,
- type CodeFenceRegion,
- reciprocalRankFusion,
- extractSnippet,
- getCacheKey,
- handelize,
- normalizeVirtualPath,
- isVirtualPath,
- parseVirtualPath,
- normalizeDocid,
- isDocid,
- syncConfigToDb,
- STRONG_SIGNAL_MIN_SCORE,
- STRONG_SIGNAL_MIN_GAP,
- generateEmbeddings,
- type Store,
- type DocumentResult,
- type SearchResult,
- type RankedResult,
- } from "../src/store.js";
- import type { CollectionConfig } from "../src/collections.js";
- // =============================================================================
- // LlamaCpp Setup
- // =============================================================================
- // Note: LlamaCpp uses node-llama-cpp for local GGUF model inference.
- // No HTTP mocking needed - tests use real LlamaCpp calls for integration tests.
- // =============================================================================
- // Test Utilities
- // =============================================================================
- let testDir: string;
- let testDbPath: string;
- let testConfigDir: string;
- let currentTestStore: Store | null = null;
- async function createTestStore(): Promise<Store> {
- testDbPath = join(testDir, `test-${Date.now()}-${Math.random().toString(36).slice(2)}.sqlite`);
- // Set up test config directory
- const configPrefix = join(testDir, `config-${Date.now()}-${Math.random().toString(36).slice(2)}`);
- testConfigDir = await mkdtemp(configPrefix);
- // Set environment variable to use test config
- process.env.QMD_CONFIG_DIR = testConfigDir;
- // Create empty YAML config
- const emptyConfig: CollectionConfig = { collections: {} };
- await writeFile(
- join(testConfigDir, "index.yml"),
- YAML.stringify(emptyConfig)
- );
- const store = createStore(testDbPath);
- currentTestStore = store;
- return store;
- }
- async function cleanupTestDb(store: Store): Promise<void> {
- currentTestStore = null;
- store.close();
- try {
- await unlink(store.dbPath);
- } catch {
- // Ignore if file doesn't exist
- }
- // Clean up test config directory
- try {
- const { readdir, unlink: unlinkFile, rmdir: rmdirAsync } = await import("node:fs/promises");
- const files = await readdir(testConfigDir);
- for (const file of files) {
- await unlinkFile(join(testConfigDir, file));
- }
- await rmdirAsync(testConfigDir);
- } catch {
- // Ignore cleanup errors
- }
- // Clear environment variable
- delete process.env.QMD_CONFIG_DIR;
- }
- // Helper to insert a test document directly into the database
- async function insertTestDocument(
- db: Database,
- collectionName: string,
- opts: {
- name?: string;
- title?: string;
- hash?: string;
- displayPath?: string;
- filepath?: string;
- body?: string;
- active?: number;
- }
- ): Promise<number> {
- const now = new Date().toISOString();
- const name = opts.name || "test-doc";
- const title = opts.title || "Test Document";
- // Use displayPath if provided, otherwise filepath's basename, otherwise default
- let path: string;
- if (opts.displayPath) {
- path = opts.displayPath;
- } else if (opts.filepath) {
- // Extract relative path from filepath by removing collection path
- // For tests, assume filepath is either relative or we want the whole path as the document path
- path = opts.filepath.startsWith('/') ? opts.filepath : opts.filepath;
- } else {
- path = `test/${name}.md`;
- }
- const body = opts.body || "# Test Document\n\nThis is test content.";
- const active = opts.active ?? 1;
- // Generate hash from body if not provided
- const hash = opts.hash || await hashContent(body);
- // Insert content (with OR IGNORE for deduplication)
- db.prepare(`
- INSERT OR IGNORE INTO content (hash, doc, created_at)
- VALUES (?, ?, ?)
- `).run(hash, body, now);
- // Insert document
- const result = db.prepare(`
- INSERT INTO documents (collection, path, title, hash, created_at, modified_at, active)
- VALUES (?, ?, ?, ?, ?, ?, ?)
- `).run(collectionName, path, title, hash, now, now, active);
- return Number(result.lastInsertRowid);
- }
- /** Sync YAML config file to SQLite store_collections in the current test store */
- async function syncTestConfig(): Promise<void> {
- if (!currentTestStore) return;
- const configPath = join(testConfigDir, "index.yml");
- const { readFile } = await import("node:fs/promises");
- const content = await readFile(configPath, "utf-8");
- const config = YAML.parse(content) as CollectionConfig;
- // Clear config hash to force re-sync
- currentTestStore.db.prepare(`DELETE FROM store_config WHERE key = 'config_hash'`).run();
- syncConfigToDb(currentTestStore.db, config);
- }
- // Helper to create a test collection in YAML config
- async function createTestCollection(
- options: { pwd?: string; glob?: string; name?: string } = {}
- ): Promise<string> {
- const pwd = options.pwd || "/test/collection";
- const glob = options.glob || "**/*.md";
- const name = options.name || pwd.split('/').filter(Boolean).pop() || 'test';
- // Read current config
- const configPath = join(testConfigDir, "index.yml");
- const { readFile } = await import("node:fs/promises");
- const content = await readFile(configPath, "utf-8");
- const config = YAML.parse(content) as CollectionConfig;
- // Add collection
- config.collections[name] = {
- path: pwd,
- pattern: glob,
- };
- // Write back
- await writeFile(configPath, YAML.stringify(config));
- await syncTestConfig();
- return name;
- }
- // Helper to add path context in YAML config
- async function addPathContext(collectionName: string, pathPrefix: string, contextText: string): Promise<void> {
- // Read current config
- const configPath = join(testConfigDir, "index.yml");
- const { readFile } = await import("node:fs/promises");
- const content = await readFile(configPath, "utf-8");
- const config = YAML.parse(content) as CollectionConfig;
- // Add context to collection
- if (!config.collections[collectionName]) {
- throw new Error(`Collection ${collectionName} not found`);
- }
- if (!config.collections[collectionName].context) {
- config.collections[collectionName].context = {};
- }
- config.collections[collectionName].context![pathPrefix] = contextText;
- // Write back
- await writeFile(configPath, YAML.stringify(config));
- await syncTestConfig();
- }
- // Helper to add global context in YAML config
- async function addGlobalContext(contextText: string): Promise<void> {
- const configPath = join(testConfigDir, "index.yml");
- const { readFile } = await import("node:fs/promises");
- const content = await readFile(configPath, "utf-8");
- const config = YAML.parse(content) as CollectionConfig;
- config.global_context = contextText;
- await writeFile(configPath, YAML.stringify(config));
- await syncTestConfig();
- }
- // =============================================================================
- // Test Setup
- // =============================================================================
- beforeAll(async () => {
- testDir = await mkdtemp(join(tmpdir(), "qmd-test-"));
- });
- afterAll(async () => {
- // Ensure native resources are released to avoid ggml-metal asserts on process exit.
- await disposeDefaultLlamaCpp();
- try {
- // Clean up test directory
- const { readdir, unlink } = await import("node:fs/promises");
- const files = await readdir(testDir);
- for (const file of files) {
- await unlink(join(testDir, file));
- }
- await rmdir(testDir);
- } catch {
- // Ignore cleanup errors
- }
- });
- // =============================================================================
- // Path Utilities Tests
- // =============================================================================
- describe("Path Utilities", () => {
- test("homedir returns HOME environment variable", () => {
- const result = homedir();
- expect(result).toBe(process.env.HOME || "/tmp");
- });
- test("resolve handles absolute paths", () => {
- expect(resolve("/foo/bar")).toBe("/foo/bar");
- expect(resolve("/foo", "/bar")).toBe("/bar");
- });
- test("resolve handles relative paths", () => {
- const pwd = process.env.PWD || process.cwd();
- expect(resolve("foo")).toBe(`${pwd}/foo`);
- expect(resolve("foo", "bar")).toBe(`${pwd}/foo/bar`);
- });
- test("resolve normalizes . and ..", () => {
- expect(resolve("/foo/bar/./baz")).toBe("/foo/bar/baz");
- expect(resolve("/foo/bar/../baz")).toBe("/foo/baz");
- expect(resolve("/foo/bar/../../baz")).toBe("/baz");
- });
- test("getDefaultDbPath throws in test mode without INDEX_PATH", () => {
- // In test mode, getDefaultDbPath should throw to prevent accidental writes to global index
- // This is intentional safety behavior
- const originalIndexPath = process.env.INDEX_PATH;
- delete process.env.INDEX_PATH;
- expect(() => getDefaultDbPath()).toThrow("Database path not set");
- // Restore
- if (originalIndexPath) process.env.INDEX_PATH = originalIndexPath;
- });
- test("getDefaultDbPath uses INDEX_PATH when set", () => {
- const originalIndexPath = process.env.INDEX_PATH;
- process.env.INDEX_PATH = "/tmp/test-index.sqlite";
- expect(getDefaultDbPath()).toBe("/tmp/test-index.sqlite");
- expect(getDefaultDbPath("custom")).toBe("/tmp/test-index.sqlite"); // INDEX_PATH overrides name
- // Restore
- if (originalIndexPath) {
- process.env.INDEX_PATH = originalIndexPath;
- } else {
- delete process.env.INDEX_PATH;
- }
- });
- test("getPwd returns current working directory", () => {
- const pwd = getPwd();
- expect(pwd).toBeTruthy();
- expect(typeof pwd).toBe("string");
- });
- test("getRealPath resolves symlinks", () => {
- const result = getRealPath("/tmp");
- expect(result).toBeTruthy();
- // On macOS, /tmp is a symlink to /private/tmp
- expect(result === "/tmp" || result === "/private/tmp").toBe(true);
- });
- });
- // =============================================================================
- // Handelize Tests - path normalization for token-friendly filenames
- // =============================================================================
- describe("handelize", () => {
- test("converts to lowercase", () => {
- expect(handelize("README.md")).toBe("readme.md");
- expect(handelize("MyFile.MD")).toBe("myfile.md");
- });
- test("preserves folder structure", () => {
- expect(handelize("a/b/c/d.md")).toBe("a/b/c/d.md");
- expect(handelize("docs/api/README.md")).toBe("docs/api/readme.md");
- });
- test("replaces non-word characters with dash", () => {
- expect(handelize("hello world.md")).toBe("hello-world.md");
- expect(handelize("file (1).md")).toBe("file-1.md");
- expect(handelize("foo@bar#baz.md")).toBe("foo-bar-baz.md");
- });
- test("collapses multiple special chars into single dash", () => {
- expect(handelize("hello world.md")).toBe("hello-world.md");
- expect(handelize("foo---bar.md")).toBe("foo-bar.md");
- expect(handelize("a - b.md")).toBe("a-b.md");
- });
- test("removes leading and trailing dashes from segments", () => {
- expect(handelize("-hello-.md")).toBe("hello.md");
- expect(handelize("--test--.md")).toBe("test.md");
- expect(handelize("a/-b-/c.md")).toBe("a/b/c.md");
- });
- test("converts triple underscore to folder separator", () => {
- expect(handelize("foo___bar.md")).toBe("foo/bar.md");
- expect(handelize("notes___2025___january.md")).toBe("notes/2025/january.md");
- expect(handelize("a/b___c/d.md")).toBe("a/b/c/d.md");
- });
- test("handles complex real-world meeting notes", () => {
- // Example: "Money Movement Licensing Review - 2025/11/19 10:25 EST - Notes by Gemini.md"
- const complexName = "Money Movement Licensing Review - 2025/11/19 10:25 EST - Notes by Gemini.md";
- const result = handelize(complexName);
- expect(result).toBe("money-movement-licensing-review-2025-11-19-10-25-est-notes-by-gemini.md");
- expect(result).not.toContain(" ");
- expect(result).not.toContain("/");
- expect(result).not.toContain(":");
- });
- test("handles unicode characters", () => {
- // Pure unicode filenames are now supported (fixes GitHub issue #10)
- expect(handelize("日本語.md")).toBe("日本語.md");
- expect(handelize("Зоны и проекты.md")).toBe("зоны-и-проекты.md");
- // Mixed unicode/ascii preserves both
- expect(handelize("café-notes.md")).toBe("café-notes.md");
- expect(handelize("naïve.md")).toBe("naïve.md");
- expect(handelize("日本語-notes.md")).toBe("日本語-notes.md");
- });
- test("handles dates and times in filenames", () => {
- expect(handelize("meeting-2025-01-15.md")).toBe("meeting-2025-01-15.md");
- expect(handelize("notes 2025/01/15.md")).toBe("notes-2025/01/15.md");
- expect(handelize("call_10:30_AM.md")).toBe("call-10-30-am.md");
- });
- test("handles special project naming patterns", () => {
- expect(handelize("PROJECT_ABC_v2.0.md")).toBe("project-abc-v2-0.md");
- expect(handelize("[WIP] Feature Request.md")).toBe("wip-feature-request.md");
- expect(handelize("(DRAFT) Proposal v1.md")).toBe("draft-proposal-v1.md");
- });
- test("handles symbol-only route filenames", () => {
- expect(handelize("routes/api/auth/$.ts")).toBe("routes/api/auth/$.ts");
- expect(handelize("app/routes/$id.tsx")).toBe("app/routes/$id.tsx");
- });
- test("filters out empty segments", () => {
- expect(handelize("a//b/c.md")).toBe("a/b/c.md");
- expect(handelize("/a/b/")).toBe("a/b");
- expect(handelize("///test///")).toBe("test");
- });
- test("throws error for invalid inputs", () => {
- expect(() => handelize("")).toThrow("path cannot be empty");
- expect(() => handelize(" ")).toThrow("path cannot be empty");
- expect(() => handelize(".md")).toThrow("no valid filename content");
- expect(() => handelize("...")).toThrow("no valid filename content");
- expect(() => handelize("___")).toThrow("no valid filename content");
- });
- test("handles minimal valid inputs", () => {
- expect(handelize("a")).toBe("a");
- expect(handelize("1")).toBe("1");
- expect(handelize("a.md")).toBe("a.md");
- });
- });
- // =============================================================================
- // Store Creation Tests
- // =============================================================================
- describe("Store Creation", () => {
- test("createStore throws without explicit path in test mode", () => {
- // In test mode, createStore without path should throw to prevent accidental writes
- const originalIndexPath = process.env.INDEX_PATH;
- delete process.env.INDEX_PATH;
- expect(() => createStore()).toThrow("Database path not set");
- // Restore
- if (originalIndexPath) process.env.INDEX_PATH = originalIndexPath;
- });
- test("createStore creates a new store with custom path", async () => {
- const store = await createTestStore();
- expect(store.dbPath).toBe(testDbPath);
- expect(store.db).toBeDefined();
- expect(typeof store.db.exec).toBe("function");
- await cleanupTestDb(store);
- });
- test("createStore initializes database schema", async () => {
- const store = await createTestStore();
- // Check tables exist
- const tables = store.db.prepare(`
- SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
- `).all() as { name: string }[];
- const tableNames = tables.map(t => t.name);
- expect(tableNames).toContain("documents");
- expect(tableNames).toContain("documents_fts");
- expect(tableNames).toContain("content_vectors");
- expect(tableNames).toContain("llm_cache");
- // Note: path_contexts table removed in favor of YAML-based context storage
- await cleanupTestDb(store);
- });
- test("createStore sets WAL journal mode", async () => {
- const store = await createTestStore();
- const result = store.db.prepare("PRAGMA journal_mode").get() as { journal_mode: string };
- expect(result.journal_mode).toBe("wal");
- await cleanupTestDb(store);
- });
- test("verifySqliteVecLoaded throws when sqlite-vec is not loaded", () => {
- const db = openDatabase(":memory:");
- try {
- expect(() => verifySqliteVecLoaded(db)).toThrow("sqlite-vec extension is unavailable");
- } finally {
- db.close();
- }
- });
- test("verifySqliteVecLoaded succeeds when sqlite-vec is loaded", () => {
- const db = openDatabase(":memory:");
- try {
- loadSqliteVec(db);
- expect(() => verifySqliteVecLoaded(db)).not.toThrow();
- } finally {
- db.close();
- }
- });
- test("store.close closes the database connection", async () => {
- const store = await createTestStore();
- store.close();
- // Attempting to use db after close should throw
- expect(() => store.db.prepare("SELECT 1").get()).toThrow();
- try {
- await unlink(testDbPath);
- } catch {}
- });
- });
- // =============================================================================
- // Document Hashing & Title Extraction Tests
- // =============================================================================
- describe("Document Helpers", () => {
- test("hashContent produces consistent SHA256 hashes", async () => {
- const content = "Hello, World!";
- const hash1 = await hashContent(content);
- const hash2 = await hashContent(content);
- expect(hash1).toBe(hash2);
- expect(hash1).toMatch(/^[a-f0-9]{64}$/);
- });
- test("hashContent produces different hashes for different content", async () => {
- const hash1 = await hashContent("Hello");
- const hash2 = await hashContent("World");
- expect(hash1).not.toBe(hash2);
- });
- test("extractTitle extracts H1 heading", () => {
- const content = "# My Title\n\nSome content here.";
- expect(extractTitle(content, "file.md")).toBe("My Title");
- });
- test("extractTitle extracts H2 heading if no H1", () => {
- const content = "## My Subtitle\n\nSome content here.";
- expect(extractTitle(content, "file.md")).toBe("My Subtitle");
- });
- test("extractTitle falls back to filename", () => {
- const content = "Just some plain text without headings.";
- expect(extractTitle(content, "my-document.md")).toBe("my-document");
- });
- test("extractTitle skips generic 'Notes' heading", () => {
- const content = "# Notes\n\n## Actual Title\n\nContent";
- expect(extractTitle(content, "file.md")).toBe("Actual Title");
- });
- test("extractTitle handles 📝 Notes heading", () => {
- const content = "# 📝 Notes\n\n## Meeting Summary\n\nContent";
- expect(extractTitle(content, "file.md")).toBe("Meeting Summary");
- });
- });
- // =============================================================================
- // Embedding Format Tests
- // =============================================================================
- describe("Embedding Formatting", () => {
- test("formatQueryForEmbedding adds search task prefix", () => {
- const formatted = formatQueryForEmbedding("how to deploy");
- expect(formatted).toBe("task: search result | query: how to deploy");
- });
- test("formatDocForEmbedding adds title and text prefix", () => {
- const formatted = formatDocForEmbedding("Some content", "My Title");
- expect(formatted).toBe("title: My Title | text: Some content");
- });
- test("formatDocForEmbedding handles missing title", () => {
- const formatted = formatDocForEmbedding("Some content");
- expect(formatted).toBe("title: none | text: Some content");
- });
- });
- // =============================================================================
- // Document Chunking Tests
- // =============================================================================
- describe("Document Chunking", () => {
- test("chunkDocument returns single chunk for small documents", () => {
- const content = "Small document content";
- const chunks = chunkDocument(content, 1000, 0);
- expect(chunks).toHaveLength(1);
- expect(chunks[0]!.text).toBe(content);
- expect(chunks[0]!.pos).toBe(0);
- });
- test("chunkDocument splits large documents", () => {
- const content = "A".repeat(10000);
- const chunks = chunkDocument(content, 1000, 0);
- expect(chunks.length).toBeGreaterThan(1);
- // All chunks should have correct positions
- for (let i = 0; i < chunks.length; i++) {
- expect(chunks[i]!.pos).toBeGreaterThanOrEqual(0);
- if (i > 0) {
- expect(chunks[i]!.pos).toBeGreaterThan(chunks[i - 1]!.pos);
- }
- }
- });
- test("chunkDocument with overlap creates overlapping chunks", () => {
- const content = "A".repeat(3000);
- const chunks = chunkDocument(content, 1000, 150); // 15% overlap
- expect(chunks.length).toBeGreaterThan(1);
- // With overlap, positions should be closer together than without
- // Each new chunk starts 150 chars before where the previous one ended
- for (let i = 1; i < chunks.length; i++) {
- const prevEnd = chunks[i - 1]!.pos + chunks[i - 1]!.text.length;
- const currentStart = chunks[i]!.pos;
- // Current chunk should start before the previous chunk ended (overlap)
- expect(currentStart).toBeLessThan(prevEnd);
- // But should still make forward progress
- expect(currentStart).toBeGreaterThan(chunks[i - 1]!.pos);
- }
- });
- test("chunkDocument prefers paragraph breaks", () => {
- const content = "First paragraph.\n\nSecond paragraph.\n\nThird paragraph.".repeat(50);
- const chunks = chunkDocument(content, 500, 0);
- // Chunks should end at paragraph breaks when possible
- for (const chunk of chunks.slice(0, -1)) {
- // Most chunks should end near a paragraph break
- const endsNearParagraph = chunk.text.endsWith("\n\n") ||
- chunk.text.endsWith(".") ||
- chunk.text.endsWith("\n");
- // This is a soft check - not all chunks can end at breaks
- }
- expect(chunks.length).toBeGreaterThan(1);
- });
- test("chunkDocument handles UTF-8 characters correctly", () => {
- const content = "こんにちは世界".repeat(500); // Japanese text
- const chunks = chunkDocument(content, 1000, 0);
- // Should not split in the middle of a multi-byte character
- for (const chunk of chunks) {
- expect(() => new TextEncoder().encode(chunk.text)).not.toThrow();
- }
- });
- test("chunkDocument with default params uses 900-token chunks", () => {
- // Default is CHUNK_SIZE_CHARS (3600 chars) with CHUNK_OVERLAP_CHARS (540 chars)
- const content = "Word ".repeat(2500); // ~12500 chars
- const chunks = chunkDocument(content);
- expect(chunks.length).toBeGreaterThan(1);
- // Each chunk should be around 3600 chars (except last)
- expect(chunks[0]!.text.length).toBeGreaterThan(2800);
- expect(chunks[0]!.text.length).toBeLessThanOrEqual(3600);
- });
- });
- describe.skipIf(!!process.env.CI)("Token-based Chunking", () => {
- test("chunkDocumentByTokens returns single chunk for small documents", async () => {
- const content = "This is a small document.";
- const chunks = await chunkDocumentByTokens(content, 900, 135);
- expect(chunks).toHaveLength(1);
- expect(chunks[0]!.text).toBe(content);
- expect(chunks[0]!.pos).toBe(0);
- expect(chunks[0]!.tokens).toBeGreaterThan(0);
- expect(chunks[0]!.tokens).toBeLessThan(900);
- });
- test("chunkDocumentByTokens splits large documents", async () => {
- // Create a document that's definitely more than 900 tokens
- const content = "The quick brown fox jumps over the lazy dog. ".repeat(250);
- const chunks = await chunkDocumentByTokens(content, 900, 135);
- expect(chunks.length).toBeGreaterThan(1);
- // Each chunk should have ~900 tokens or less
- for (const chunk of chunks) {
- expect(chunk.tokens).toBeLessThanOrEqual(950); // Allow slight overage
- expect(chunk.tokens).toBeGreaterThan(0);
- }
- // Chunks should have correct positions
- for (let i = 0; i < chunks.length; i++) {
- expect(chunks[i]!.pos).toBeGreaterThanOrEqual(0);
- if (i > 0) {
- expect(chunks[i]!.pos).toBeGreaterThan(chunks[i - 1]!.pos);
- }
- }
- });
- test("chunkDocumentByTokens creates overlapping chunks", async () => {
- const content = "Word ".repeat(500); // ~500 tokens
- const chunks = await chunkDocumentByTokens(content, 200, 30); // 15% overlap
- expect(chunks.length).toBeGreaterThan(1);
- // With overlap, consecutive chunks should have overlapping positions
- for (let i = 1; i < chunks.length; i++) {
- const prevEnd = chunks[i - 1]!.pos + chunks[i - 1]!.text.length;
- const currentStart = chunks[i]!.pos;
- // Current chunk should start before the previous chunk ended (overlap)
- expect(currentStart).toBeLessThan(prevEnd);
- }
- });
- test("chunkDocumentByTokens returns actual token counts", async () => {
- const content = "Hello world, this is a test.";
- const chunks = await chunkDocumentByTokens(content);
- expect(chunks).toHaveLength(1);
- // The token count should be reasonable (not 0, not equal to char count)
- expect(chunks[0]!.tokens).toBeGreaterThan(0);
- expect(chunks[0]!.tokens).toBeLessThan(content.length); // Tokens < chars for English
- });
- });
- // =============================================================================
- // Smart Chunking - Break Point Detection Tests
- // =============================================================================
- describe("scanBreakPoints", () => {
- test("detects h1 headings", () => {
- const text = "Intro\n# Heading 1\nMore text";
- const breaks = scanBreakPoints(text);
- const h1 = breaks.find(b => b.type === 'h1');
- expect(h1).toBeDefined();
- expect(h1!.score).toBe(100);
- expect(h1!.pos).toBe(5); // position of \n#
- });
- test("detects multiple heading levels", () => {
- const text = "Text\n# H1\n## H2\n### H3\nMore";
- const breaks = scanBreakPoints(text);
- const h1 = breaks.find(b => b.type === 'h1');
- const h2 = breaks.find(b => b.type === 'h2');
- const h3 = breaks.find(b => b.type === 'h3');
- expect(h1).toBeDefined();
- expect(h2).toBeDefined();
- expect(h3).toBeDefined();
- expect(h1!.score).toBe(100);
- expect(h2!.score).toBe(90);
- expect(h3!.score).toBe(80);
- });
- test("detects code blocks", () => {
- const text = "Before\n```js\ncode\n```\nAfter";
- const breaks = scanBreakPoints(text);
- const codeBlocks = breaks.filter(b => b.type === 'codeblock');
- expect(codeBlocks.length).toBe(2); // opening and closing
- expect(codeBlocks[0]!.score).toBe(80);
- });
- test("detects horizontal rules", () => {
- const text = "Text\n---\nMore text";
- const breaks = scanBreakPoints(text);
- const hr = breaks.find(b => b.type === 'hr');
- expect(hr).toBeDefined();
- expect(hr!.score).toBe(60);
- });
- test("detects blank lines (paragraph boundaries)", () => {
- const text = "First paragraph.\n\nSecond paragraph.";
- const breaks = scanBreakPoints(text);
- const blank = breaks.find(b => b.type === 'blank');
- expect(blank).toBeDefined();
- expect(blank!.score).toBe(20);
- });
- test("detects list items", () => {
- const text = "Intro\n- Item 1\n- Item 2\n1. Numbered";
- const breaks = scanBreakPoints(text);
- const lists = breaks.filter(b => b.type === 'list');
- const numLists = breaks.filter(b => b.type === 'numlist');
- expect(lists.length).toBe(2);
- expect(numLists.length).toBe(1);
- expect(lists[0]!.score).toBe(5);
- expect(numLists[0]!.score).toBe(5);
- });
- test("detects newlines as fallback", () => {
- const text = "Line 1\nLine 2\nLine 3";
- const breaks = scanBreakPoints(text);
- const newlines = breaks.filter(b => b.type === 'newline');
- expect(newlines.length).toBe(2);
- expect(newlines[0]!.score).toBe(1);
- });
- test("returns breaks sorted by position", () => {
- const text = "A\n# B\n\nC\n## D";
- const breaks = scanBreakPoints(text);
- for (let i = 1; i < breaks.length; i++) {
- expect(breaks[i]!.pos).toBeGreaterThan(breaks[i-1]!.pos);
- }
- });
- test("higher-scoring pattern wins at same position", () => {
- // \n# matches both newline (score 1) and h1 (score 100)
- const text = "Text\n# Heading";
- const breaks = scanBreakPoints(text);
- const atPos = breaks.filter(b => b.pos === 4);
- expect(atPos.length).toBe(1);
- expect(atPos[0]!.type).toBe('h1');
- expect(atPos[0]!.score).toBe(100);
- });
- });
- describe("findCodeFences", () => {
- test("finds single code fence", () => {
- const text = "Before\n```js\ncode here\n```\nAfter";
- const fences = findCodeFences(text);
- expect(fences.length).toBe(1);
- expect(fences[0]!.start).toBe(6); // position of first \n```
- // End is position after the closing \n``` (which is at position 22, length 4)
- expect(fences[0]!.end).toBe(26);
- });
- test("finds multiple code fences", () => {
- const text = "Intro\n```\nblock1\n```\nMiddle\n```\nblock2\n```\nEnd";
- const fences = findCodeFences(text);
- expect(fences.length).toBe(2);
- });
- test("handles unclosed code fence", () => {
- const text = "Before\n```\nunclosed code block";
- const fences = findCodeFences(text);
- expect(fences.length).toBe(1);
- expect(fences[0]!.end).toBe(text.length); // extends to end of document
- });
- test("returns empty array for no code fences", () => {
- const text = "No code fences here";
- const fences = findCodeFences(text);
- expect(fences.length).toBe(0);
- });
- });
- describe("isInsideCodeFence", () => {
- test("returns true for position inside fence", () => {
- const fences: CodeFenceRegion[] = [{ start: 10, end: 30 }];
- expect(isInsideCodeFence(15, fences)).toBe(true);
- expect(isInsideCodeFence(20, fences)).toBe(true);
- });
- test("returns false for position outside fence", () => {
- const fences: CodeFenceRegion[] = [{ start: 10, end: 30 }];
- expect(isInsideCodeFence(5, fences)).toBe(false);
- expect(isInsideCodeFence(35, fences)).toBe(false);
- });
- test("returns false for position at fence boundaries", () => {
- const fences: CodeFenceRegion[] = [{ start: 10, end: 30 }];
- expect(isInsideCodeFence(10, fences)).toBe(false); // at start
- expect(isInsideCodeFence(30, fences)).toBe(false); // at end
- });
- test("handles multiple fences", () => {
- const fences: CodeFenceRegion[] = [
- { start: 10, end: 30 },
- { start: 50, end: 70 }
- ];
- expect(isInsideCodeFence(20, fences)).toBe(true);
- expect(isInsideCodeFence(60, fences)).toBe(true);
- expect(isInsideCodeFence(40, fences)).toBe(false);
- });
- });
- describe("findBestCutoff", () => {
- test("prefers higher-scoring break points", () => {
- const breakPoints: BreakPoint[] = [
- { pos: 100, score: 1, type: 'newline' },
- { pos: 150, score: 100, type: 'h1' },
- { pos: 180, score: 20, type: 'blank' },
- ];
- // Target is 200, window is 100 (so 100-200 is valid)
- const cutoff = findBestCutoff(breakPoints, 200, 100, 0.7);
- expect(cutoff).toBe(150); // h1 wins due to high score
- });
- test("h2 at window edge beats blank at target (squared decay)", () => {
- const breakPoints: BreakPoint[] = [
- { pos: 100, score: 90, type: 'h2' }, // at window edge
- { pos: 195, score: 20, type: 'blank' }, // close to target
- ];
- // Target is 200, window is 100
- // With squared decay:
- // h2 at 100: dist=100, normalized=1.0, mult=1-1*0.7=0.3, final=90*0.3=27
- // blank at 195: dist=5, normalized=0.05, mult=1-0.0025*0.7=0.998, final=20*0.998=19.97
- const cutoff = findBestCutoff(breakPoints, 200, 100, 0.7);
- expect(cutoff).toBe(100); // h2 wins even at edge!
- });
- test("high score easily overcomes distance", () => {
- const breakPoints: BreakPoint[] = [
- { pos: 150, score: 100, type: 'h1' }, // h1 at middle
- { pos: 195, score: 1, type: 'newline' }, // newline near target
- ];
- // Target is 200, window is 100
- // h1 at 150: dist=50, normalized=0.5, mult=1-0.25*0.7=0.825, final=82.5
- // newline at 195: dist=5, mult=0.998, final=0.998
- const cutoff = findBestCutoff(breakPoints, 200, 100, 0.7);
- expect(cutoff).toBe(150); // h1 wins easily
- });
- test("returns target position when no breaks in window", () => {
- const breakPoints: BreakPoint[] = [
- { pos: 10, score: 100, type: 'h1' }, // too far before window
- ];
- const cutoff = findBestCutoff(breakPoints, 200, 100, 0.7);
- expect(cutoff).toBe(200);
- });
- test("skips break points inside code fences", () => {
- const breakPoints: BreakPoint[] = [
- { pos: 150, score: 100, type: 'h1' }, // inside fence
- { pos: 180, score: 20, type: 'blank' }, // outside fence
- ];
- const codeFences: CodeFenceRegion[] = [{ start: 140, end: 160 }];
- const cutoff = findBestCutoff(breakPoints, 200, 100, 0.7, codeFences);
- expect(cutoff).toBe(180); // blank wins since h1 is inside fence
- });
- test("handles empty break points array", () => {
- const cutoff = findBestCutoff([], 200, 100, 0.7);
- expect(cutoff).toBe(200);
- });
- });
- describe("Smart Chunking Integration", () => {
- test("chunkDocument prefers headings over arbitrary breaks", () => {
- // Create content where the heading falls within the search window
- // We want the heading at ~1700 chars so it's in the window for a 2000 char target
- const section1 = "Introduction text here. ".repeat(70); // ~1680 chars
- const section2 = "Main content text here. ".repeat(50); // ~1150 chars
- const content = `${section1}\n# Main Section\n${section2}`;
- // With 2000 char chunks and 800 char window (searches 1200-2000)
- // Heading is at ~1680 which is in window
- const chunks = chunkDocument(content, 2000, 0, 800);
- const headingPos = content.indexOf('\n# Main Section');
- // First chunk should end at the heading (best break point in window)
- expect(chunks.length).toBeGreaterThanOrEqual(2);
- expect(chunks[0]!.text.length).toBe(headingPos);
- });
- test("chunkDocument does not split inside code blocks", () => {
- const beforeCode = "Some intro text. ".repeat(30); // ~480 chars
- const codeBlock = "```typescript\n" + "const x = 1;\n".repeat(100) + "```\n";
- const afterCode = "More text after code. ".repeat(30);
- const content = beforeCode + codeBlock + afterCode;
- const chunks = chunkDocument(content, 1000, 0, 400);
- // Check that no chunk starts in the middle of a code block
- for (const chunk of chunks) {
- const hasOpenFence = (chunk.text.match(/\n```/g) || []).length;
- // If we have an odd number of fence markers, we're splitting inside a block
- // (unless it's the last chunk with unclosed fence)
- if (hasOpenFence % 2 === 1 && !chunk.text.endsWith('```\n')) {
- // This is acceptable only if it's an unclosed fence at document end
- const isLastChunk = chunks.indexOf(chunk) === chunks.length - 1;
- if (!isLastChunk) {
- // Not the last chunk, so this would be a split inside code - check it's not common
- // Actually this test is more about smoke testing - we just verify it runs
- }
- }
- }
- expect(chunks.length).toBeGreaterThan(1);
- });
- test("chunkDocument handles markdown with mixed elements", () => {
- const content = `# Introduction
- This is the introduction paragraph with some text.
- ## Section 1
- Some content in section 1.
- - List item 1
- - List item 2
- - List item 3
- ## Section 2
- \`\`\`javascript
- function hello() {
- console.log("Hello");
- }
- \`\`\`
- More text after the code block.
- ---
- ## Section 3
- Final section content.
- `.repeat(10);
- const chunks = chunkDocument(content, 500, 75, 200);
- // Should produce multiple chunks
- expect(chunks.length).toBeGreaterThan(5);
- // All chunks should be valid strings
- for (const chunk of chunks) {
- expect(typeof chunk.text).toBe('string');
- expect(chunk.text.length).toBeGreaterThan(0);
- expect(chunk.pos).toBeGreaterThanOrEqual(0);
- }
- });
- });
- // =============================================================================
- // Caching Tests
- // =============================================================================
- describe("Caching", () => {
- test("getCacheKey generates consistent keys", () => {
- const key1 = getCacheKey("http://example.com", { query: "test" });
- const key2 = getCacheKey("http://example.com", { query: "test" });
- expect(key1).toBe(key2);
- expect(key1).toMatch(/^[a-f0-9]{64}$/);
- });
- test("getCacheKey generates different keys for different inputs", () => {
- const key1 = getCacheKey("http://example.com", { query: "test1" });
- const key2 = getCacheKey("http://example.com", { query: "test2" });
- expect(key1).not.toBe(key2);
- });
- test("store cache operations work correctly", async () => {
- const store = await createTestStore();
- const key = "test-cache-key";
- const value = "cached result";
- // Initially empty
- expect(store.getCachedResult(key)).toBeNull();
- // Set cache
- store.setCachedResult(key, value);
- // Retrieve cache
- expect(store.getCachedResult(key)).toBe(value);
- // Clear cache
- store.clearCache();
- expect(store.getCachedResult(key)).toBeNull();
- await cleanupTestDb(store);
- });
- });
- // =============================================================================
- // Context Tests
- // =============================================================================
- describe("Path Context", () => {
- test("getContextForFile returns null when no context set", async () => {
- const store = await createTestStore();
- const context = store.getContextForFile("/some/random/path.md");
- expect(context).toBeNull();
- await cleanupTestDb(store);
- });
- test("getContextForFile returns matching context", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/test/collection", glob: "**/*.md" });
- await addPathContext(collectionName, "/docs", "Documentation files");
- // Insert a document so getContextForFile can find it
- await insertTestDocument(store.db, collectionName, {
- name: "readme",
- displayPath: "docs/readme.md",
- });
- const context = store.getContextForFile("/test/collection/docs/readme.md");
- expect(context).toBe("Documentation files");
- await cleanupTestDb(store);
- });
- test("getContextForFile returns all matching contexts", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/test/collection", glob: "**/*.md" });
- await addPathContext(collectionName, "/", "General test files");
- await addPathContext(collectionName, "/docs", "Documentation files");
- await addPathContext(collectionName, "/docs/api", "API documentation");
- // Insert documents so getContextForFile can find them
- await insertTestDocument(store.db, collectionName, {
- name: "readme",
- displayPath: "readme.md",
- });
- await insertTestDocument(store.db, collectionName, {
- name: "guide",
- displayPath: "docs/guide.md",
- });
- await insertTestDocument(store.db, collectionName, {
- name: "reference",
- displayPath: "docs/api/reference.md",
- });
- // Context now returns ALL matching contexts joined with \n\n
- expect(store.getContextForFile("/test/collection/readme.md")).toBe("General test files");
- expect(store.getContextForFile("/test/collection/docs/guide.md")).toBe("General test files\n\nDocumentation files");
- expect(store.getContextForFile("/test/collection/docs/api/reference.md")).toBe("General test files\n\nDocumentation files\n\nAPI documentation");
- await cleanupTestDb(store);
- });
- });
- // =============================================================================
- // Collection Tests
- // =============================================================================
- describe("Collections", () => {
- test("collections are managed via YAML config", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/home/user/projects/myapp", glob: "**/*.md" });
- // Collections are now in YAML, not in the database
- expect(collectionName).toBe("myapp");
- await cleanupTestDb(store);
- });
- });
- // =============================================================================
- // FTS Search Tests
- // =============================================================================
- describe("FTS Search", () => {
- test("searchFTS returns empty array for no matches", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- body: "The quick brown fox jumps over the lazy dog",
- });
- const results = store.searchFTS("nonexistent-term-xyz", 10);
- expect(results).toHaveLength(0);
- await cleanupTestDb(store);
- });
- test("searchFTS finds documents by keyword", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- title: "Fox Document",
- body: "The quick brown fox jumps over the lazy dog",
- displayPath: "test/doc1.md",
- });
- const results = store.searchFTS("fox", 10);
- expect(results.length).toBeGreaterThan(0);
- expect(results[0]!.displayPath).toBe(`${collectionName}/test/doc1.md`);
- expect(results[0]!.filepath).toBe(`qmd://${collectionName}/test/doc1.md`);
- expect(results[0]!.source).toBe("fts");
- await cleanupTestDb(store);
- });
- test("searchFTS ranks title matches higher", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- // Document with "fox" in body only
- await insertTestDocument(store.db, collectionName, {
- name: "body-match",
- title: "Some Other Title",
- body: "The fox is here in the body",
- displayPath: "test/body.md",
- });
- // Document with "fox" in title (via name field which is indexed)
- await insertTestDocument(store.db, collectionName, {
- name: "fox",
- title: "Fox Title",
- body: "Different content without the animal fox",
- displayPath: "test/title.md",
- });
- const results = store.searchFTS("fox", 10);
- // Both documents contain "fox" in the body now, so we should get 2 results
- expect(results.length).toBe(2);
- // Title/name match should rank higher due to BM25 weights
- expect(results[0]!.displayPath).toBe(`${collectionName}/test/title.md`);
- await cleanupTestDb(store);
- });
- test("searchFTS title boost outweighs higher body frequency", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- // Document with "quantum" mentioned in a longer body but NOT in the title
- await insertTestDocument(store.db, collectionName, {
- name: "body-only",
- title: "General Science Notes",
- body: "This research paper discusses quantum mechanics and the quantum model of computation. The quantum approach offers improvements over classical methods.",
- displayPath: "test/body-only.md",
- });
- // Document with "quantum" in the title but a shorter body mention
- await insertTestDocument(store.db, collectionName, {
- name: "title-match",
- title: "Quantum Computing Overview",
- body: "An introduction to the fundamentals of this emerging computing paradigm.",
- displayPath: "test/title-match.md",
- });
- const results = store.searchFTS("quantum", 10);
- expect(results.length).toBe(2);
- // Title-match doc should rank higher due to BM25 column weights boosting title
- expect(results[0]!.displayPath).toBe(`${collectionName}/test/title-match.md`);
- await cleanupTestDb(store);
- });
- test("searchFTS respects limit parameter", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- // Insert 10 documents
- for (let i = 0; i < 10; i++) {
- await insertTestDocument(store.db, collectionName, {
- name: `doc${i}`,
- body: "common keyword appears here",
- displayPath: `test/doc${i}.md`,
- });
- }
- const results = store.searchFTS("common keyword", 3);
- expect(results).toHaveLength(3);
- await cleanupTestDb(store);
- });
- test("searchFTS filters by collection name", async () => {
- const store = await createTestStore();
- const collection1 = await createTestCollection({ pwd: "/path/one", glob: "**/*.md", name: "one" });
- const collection2 = await createTestCollection({ pwd: "/path/two", glob: "**/*.md", name: "two" });
- await insertTestDocument(store.db, collection1, {
- name: "doc1",
- body: "searchable content",
- displayPath: "doc1.md",
- });
- await insertTestDocument(store.db, collection2, {
- name: "doc2",
- body: "searchable content",
- displayPath: "doc2.md",
- });
- const allResults = store.searchFTS("searchable", 10);
- expect(allResults).toHaveLength(2);
- // Filter by collection name
- const filtered = store.searchFTS("searchable", 10, collection1);
- expect(filtered).toHaveLength(1);
- expect(filtered[0]!.displayPath).toBe(`${collection1}/doc1.md`);
- await cleanupTestDb(store);
- });
- test("searchFTS handles special characters in query", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- body: "Function with params: foo(bar, baz)",
- displayPath: "test/doc1.md",
- });
- // Should not throw on special characters
- const results = store.searchFTS("foo(bar)", 10);
- // Results may vary based on FTS5 handling
- expect(Array.isArray(results)).toBe(true);
- await cleanupTestDb(store);
- });
- // BM25 IDF requires corpus depth — helper adds non-matching docs so term frequency
- // differentiation produces meaningful scores (2-doc corpus has near-zero IDF).
- async function addNoiseDocuments(db: Database, collectionName: string, count = 8) {
- for (let i = 0; i < count; i++) {
- await insertTestDocument(db, collectionName, {
- name: `noise${i}`,
- title: `Unrelated Topic ${i}`,
- body: `This document discusses completely different subjects like gardening and cooking ${i}`,
- displayPath: `test/noise${i}.md`,
- });
- }
- }
- test("searchFTS scores: stronger BM25 match → higher normalized score", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await addNoiseDocuments(store.db, collectionName);
- // "alpha" appears in title (10x weight) + body → strong BM25
- await insertTestDocument(store.db, collectionName, {
- name: "strong",
- title: "Alpha Guide",
- body: "This is the definitive alpha reference with alpha details and more alpha info",
- displayPath: "test/strong.md",
- });
- // "alpha" appears once in body only → weaker BM25
- await insertTestDocument(store.db, collectionName, {
- name: "weak",
- title: "General Notes",
- body: "Some notes that mention alpha in passing among other topics and keywords",
- displayPath: "test/weak.md",
- });
- const results = store.searchFTS("alpha", 10);
- expect(results.length).toBe(2);
- // Verify score direction: stronger match (title + body) should score HIGHER
- const strongResult = results.find(r => r.displayPath.includes("strong"))!;
- const weakResult = results.find(r => r.displayPath.includes("weak"))!;
- expect(strongResult.score).toBeGreaterThan(weakResult.score);
- // Verify scores are in valid (0, 1) range
- for (const r of results) {
- expect(r.score).toBeGreaterThan(0);
- expect(r.score).toBeLessThan(1);
- }
- await cleanupTestDb(store);
- });
- test("searchFTS scores: minScore filter keeps strong matches, drops weak", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await addNoiseDocuments(store.db, collectionName);
- // Strong match: keyword in title (10x weight) + repeated in body
- await insertTestDocument(store.db, collectionName, {
- name: "strong",
- title: "Kubernetes Deployment",
- body: "Kubernetes deployment strategies for kubernetes clusters using kubernetes operators",
- displayPath: "test/strong.md",
- });
- // Weak match: keyword appears once in body only
- await insertTestDocument(store.db, collectionName, {
- name: "weak",
- title: "Random Notes",
- body: "Various topics including a brief kubernetes mention among many other unrelated things",
- displayPath: "test/weak.md",
- });
- const allResults = store.searchFTS("kubernetes", 10);
- expect(allResults.length).toBe(2);
- // With a minScore threshold, strong match should survive, weak should be filterable
- const strongScore = allResults.find(r => r.displayPath.includes("strong"))!.score;
- const weakScore = allResults.find(r => r.displayPath.includes("weak"))!.score;
- // Find a threshold between them
- const threshold = (strongScore + weakScore) / 2;
- const filtered = allResults.filter(r => r.score >= threshold);
- // Strong match survives the filter, weak does not
- expect(filtered.length).toBe(1);
- expect(filtered[0]!.displayPath).toContain("strong");
- await cleanupTestDb(store);
- });
- test("searchFTS ignores inactive documents", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "active",
- body: "findme content",
- displayPath: "test/active.md",
- active: 1,
- });
- await insertTestDocument(store.db, collectionName, {
- name: "inactive",
- body: "findme content",
- displayPath: "test/inactive.md",
- active: 0,
- });
- const results = store.searchFTS("findme", 10);
- expect(results).toHaveLength(1);
- expect(results[0]!.displayPath).toBe(`${collectionName}/test/active.md`);
- expect(results[0]!.filepath).toBe(`qmd://${collectionName}/test/active.md`);
- await cleanupTestDb(store);
- });
- test("searchFTS scores: strong signal detection works with correct normalization", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- // BM25 IDF needs meaningful corpus depth for strong signal to fire.
- // 50 noise docs give IDF ≈ log(50/2) ≈ 3.2 — enough for scores above 0.85.
- await addNoiseDocuments(store.db, collectionName, 50);
- // Dominant: keyword in filepath (10x BM25 weight column) + title + body
- await insertTestDocument(store.db, collectionName, {
- name: "dominant",
- title: "Zephyr Configuration Guide",
- body: "Complete zephyr configuration guide. Zephyr setup instructions for zephyr deployment.",
- displayPath: "zephyr/zephyr-guide.md",
- });
- // Weak: keyword once in body only, longer doc dilutes TF
- await insertTestDocument(store.db, collectionName, {
- name: "weak",
- title: "General Notes",
- body: "Various topics covering many areas of technology and design. " +
- "One of them might relate to zephyr but mostly about other things entirely. " +
- "Additional content about databases, networking, security, performance, " +
- "monitoring, deployment, testing, and documentation practices.",
- displayPath: "notes/misc.md",
- });
- const results = store.searchFTS("zephyr", 10);
- expect(results.length).toBe(2);
- const topScore = results[0]!.score;
- const secondScore = results[1]!.score;
- // With correct normalization: strong match should be well above threshold
- expect(topScore).toBeGreaterThanOrEqual(STRONG_SIGNAL_MIN_SCORE);
- // Gap should exceed threshold when there's a dominant match
- const gap = topScore - secondScore;
- expect(gap).toBeGreaterThanOrEqual(STRONG_SIGNAL_MIN_GAP);
- // Full strong signal check should pass (this was dead code before the fix)
- const hasStrongSignal = topScore >= STRONG_SIGNAL_MIN_SCORE && gap >= STRONG_SIGNAL_MIN_GAP;
- expect(hasStrongSignal).toBe(true);
- await cleanupTestDb(store);
- });
- });
- // =============================================================================
- // Document Retrieval Tests
- // =============================================================================
- describe("Document Retrieval", () => {
- describe("findDocument", () => {
- test("findDocument finds by exact filepath", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/exact/path", glob: "**/*.md" });
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- title: "My Document",
- displayPath: "mydoc.md",
- body: "Document content here",
- });
- const result = store.findDocument("/exact/path/mydoc.md");
- expect("error" in result).toBe(false);
- if (!("error" in result)) {
- expect(result.title).toBe("My Document");
- expect(result.displayPath).toBe(`${collectionName}/mydoc.md`);
- expect(result.filepath).toBe(`qmd://${collectionName}/mydoc.md`);
- expect(result.body).toBeUndefined(); // body not included by default
- }
- await cleanupTestDb(store);
- });
- test("findDocument finds by display_path", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/some/path", glob: "**/*.md" });
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- displayPath: "docs/mydoc.md",
- });
- const result = store.findDocument("docs/mydoc.md");
- expect("error" in result).toBe(false);
- await cleanupTestDb(store);
- });
- test("findDocument finds by partial path match", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/very/long/path/to", glob: "**/*.md" });
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- displayPath: "mydoc.md",
- });
- const result = store.findDocument("mydoc.md");
- expect("error" in result).toBe(false);
- await cleanupTestDb(store);
- });
- test("findDocument includes body when requested", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/path", glob: "**/*.md" });
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- displayPath: "mydoc.md",
- body: "The actual body content",
- });
- const result = store.findDocument("/path/mydoc.md", { includeBody: true });
- expect("error" in result).toBe(false);
- if (!("error" in result)) {
- expect(result.body).toBe("The actual body content");
- }
- await cleanupTestDb(store);
- });
- test("findDocument returns error with suggestions for not found", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "similar",
- filepath: "/path/similar.md",
- displayPath: "similar.md",
- });
- const result = store.findDocument("simlar.md"); // typo - 1 char diff
- expect("error" in result).toBe(true);
- if ("error" in result) {
- expect(result.error).toBe("not_found");
- // Levenshtein distance of 1 should be found with maxDistance 3
- expect(result.similarFiles.length).toBeGreaterThanOrEqual(0); // May or may not find depending on distance calc
- }
- await cleanupTestDb(store);
- });
- test("findDocument handles :line suffix", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- filepath: "/path/mydoc.md",
- displayPath: "mydoc.md",
- });
- const result = store.findDocument("mydoc.md:100");
- expect("error" in result).toBe(false);
- await cleanupTestDb(store);
- });
- test("findDocument expands ~ to home directory", async () => {
- const store = await createTestStore();
- const home = homedir();
- const collectionName = await createTestCollection({ pwd: home, name: "home" });
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- filepath: `${home}/docs/mydoc.md`,
- displayPath: "docs/mydoc.md",
- });
- const result = store.findDocument("~/docs/mydoc.md");
- expect("error" in result).toBe(false);
- await cleanupTestDb(store);
- });
- test("findDocument includes context from path_contexts", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/path" });
- await addPathContext(collectionName, "docs", "Documentation");
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- displayPath: "docs/mydoc.md",
- });
- const result = store.findDocument("/path/docs/mydoc.md");
- expect("error" in result).toBe(false);
- if (!("error" in result)) {
- expect(result.context).toBe("Documentation");
- }
- await cleanupTestDb(store);
- });
- test("findDocument includes hierarchical contexts (global + collection + path)", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/archive", name: "archive" });
- // Add global context
- await addGlobalContext("Global context for all documents");
- // Add collection root context
- await addPathContext(collectionName, "/", "Archive collection context");
- // Add path-specific contexts at different levels
- await addPathContext(collectionName, "/podcasts", "Podcast episodes");
- await addPathContext(collectionName, "/podcasts/external", "External podcast interviews");
- // Insert document in nested path
- await insertTestDocument(store.db, collectionName, {
- name: "interview",
- displayPath: "podcasts/external/2024-jan-interview.md",
- });
- const result = store.findDocument("/archive/podcasts/external/2024-jan-interview.md");
- expect("error" in result).toBe(false);
- if (!("error" in result)) {
- // Should have all contexts joined with double newlines
- expect(result.context).toBe(
- "Global context for all documents\n\n" +
- "Archive collection context\n\n" +
- "Podcast episodes\n\n" +
- "External podcast interviews"
- );
- }
- await cleanupTestDb(store);
- });
- });
- describe("getDocumentBody", () => {
- test("getDocumentBody returns full body", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/path" });
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- displayPath: "mydoc.md",
- body: "Line 1\nLine 2\nLine 3\nLine 4\nLine 5",
- });
- const body = store.getDocumentBody({ filepath: "/path/mydoc.md" });
- expect(body).toBe("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
- await cleanupTestDb(store);
- });
- test("getDocumentBody supports line range", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/path" });
- await insertTestDocument(store.db, collectionName, {
- name: "mydoc",
- displayPath: "mydoc.md",
- body: "Line 1\nLine 2\nLine 3\nLine 4\nLine 5",
- });
- const body = store.getDocumentBody({ filepath: "/path/mydoc.md" }, 2, 2);
- expect(body).toBe("Line 2\nLine 3");
- await cleanupTestDb(store);
- });
- test("getDocumentBody returns null for non-existent document", async () => {
- const store = await createTestStore();
- const body = store.getDocumentBody({ filepath: "/nonexistent.md" });
- expect(body).toBeNull();
- await cleanupTestDb(store);
- });
- });
- describe("findDocuments (multi-get)", () => {
- test("findDocuments finds by glob pattern", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- filepath: "/path/journals/2024-01.md",
- displayPath: "journals/2024-01.md",
- });
- await insertTestDocument(store.db, collectionName, {
- name: "doc2",
- filepath: "/path/journals/2024-02.md",
- displayPath: "journals/2024-02.md",
- });
- await insertTestDocument(store.db, collectionName, {
- name: "doc3",
- filepath: "/path/other/file.md",
- displayPath: "other/file.md",
- });
- const { docs, errors } = store.findDocuments("journals/2024-*.md");
- expect(errors).toHaveLength(0);
- expect(docs).toHaveLength(2);
- await cleanupTestDb(store);
- });
- test("findDocuments finds by comma-separated list", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- filepath: "/path/doc1.md",
- displayPath: "doc1.md",
- });
- await insertTestDocument(store.db, collectionName, {
- name: "doc2",
- filepath: "/path/doc2.md",
- displayPath: "doc2.md",
- });
- const { docs, errors } = store.findDocuments("doc1.md, doc2.md");
- expect(errors).toHaveLength(0);
- expect(docs).toHaveLength(2);
- await cleanupTestDb(store);
- });
- test("findDocuments reports errors for not found files", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- filepath: "/path/doc1.md",
- displayPath: "doc1.md",
- });
- const { docs, errors } = store.findDocuments("doc1.md, nonexistent.md");
- expect(docs).toHaveLength(1);
- expect(errors).toHaveLength(1);
- expect(errors[0]).toContain("not found");
- await cleanupTestDb(store);
- });
- test("findDocuments skips large files", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "large",
- filepath: "/path/large.md",
- displayPath: "large.md",
- body: "x".repeat(20000), // 20KB
- });
- const { docs } = store.findDocuments("large.md", { maxBytes: 10000 });
- expect(docs).toHaveLength(1);
- expect(docs[0]!.skipped).toBe(true);
- if (docs[0]!.skipped) {
- expect((docs[0] as { skipped: true; skipReason: string }).skipReason).toContain("too large");
- }
- await cleanupTestDb(store);
- });
- test("findDocuments includes body when requested", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- filepath: "/path/doc1.md",
- displayPath: "doc1.md",
- body: "The content",
- });
- const { docs } = store.findDocuments("doc1.md", { includeBody: true });
- expect(docs[0]!.skipped).toBe(false);
- if (!docs[0]!.skipped) {
- expect((docs[0] as { doc: { body: string }; skipped: false }).doc.body).toBe("The content");
- }
- await cleanupTestDb(store);
- });
- });
- });
- // =============================================================================
- // Snippet Extraction Tests
- // =============================================================================
- describe("Snippet Extraction", () => {
- test("extractSnippet finds query terms", () => {
- const body = "First line.\nSecond line with keyword.\nThird line.\nFourth line.";
- const { line, snippet } = extractSnippet(body, "keyword", 500);
- expect(line).toBe(2); // Line 2 contains "keyword"
- expect(snippet).toContain("keyword");
- });
- test("extractSnippet includes context lines", () => {
- const body = "Line 1\nLine 2\nLine 3 has keyword\nLine 4\nLine 5";
- const { snippet } = extractSnippet(body, "keyword", 500);
- expect(snippet).toContain("Line 2"); // Context before
- expect(snippet).toContain("Line 3 has keyword");
- expect(snippet).toContain("Line 4"); // Context after
- });
- test("extractSnippet respects maxLen for content", () => {
- const body = "A".repeat(1000);
- const result = extractSnippet(body, "query", 100);
- // Snippet includes header + content, content should be truncated
- expect(result.snippet).toContain("@@"); // Has diff header
- expect(result.snippet).toContain("..."); // Content was truncated
- });
- test("extractSnippet uses chunkPos hint", () => {
- const body = "First section...\n".repeat(50) + "Target keyword here\n" + "More content...".repeat(50);
- const chunkPos = body.indexOf("Target keyword");
- const { snippet } = extractSnippet(body, "Target", 200, chunkPos);
- expect(snippet).toContain("Target keyword");
- });
- test("extractSnippet returns beginning when no match", () => {
- const body = "First line\nSecond line\nThird line";
- const { line, snippet } = extractSnippet(body, "nonexistent", 500);
- expect(line).toBe(1);
- expect(snippet).toContain("First line");
- });
- test("extractSnippet includes diff-style header", () => {
- const body = "Line 1\nLine 2\nLine 3 has keyword\nLine 4\nLine 5";
- const { snippet, linesBefore, linesAfter, snippetLines } = extractSnippet(body, "keyword", 500);
- // Header should show line position and context info
- expect(snippet).toMatch(/^@@ -\d+,\d+ @@ \(\d+ before, \d+ after\)/);
- expect(linesBefore).toBe(1); // Line 1 comes before
- expect(linesAfter).toBe(0); // Snippet includes to end (lines 2-5)
- expect(snippetLines).toBe(4); // Lines 2, 3, 4, 5
- });
- test("extractSnippet calculates linesBefore and linesAfter correctly", () => {
- const body = "L1\nL2\nL3\nL4 match\nL5\nL6\nL7\nL8\nL9\nL10";
- const { linesBefore, linesAfter, snippetLines, line } = extractSnippet(body, "match", 500);
- expect(line).toBe(4); // "L4 match" is line 4
- expect(linesBefore).toBe(2); // L1, L2 before snippet (snippet starts at L3)
- expect(snippetLines).toBe(4); // L3, L4, L5, L6
- expect(linesAfter).toBe(4); // L7, L8, L9, L10 after snippet
- });
- test("extractSnippet header format matches diff style", () => {
- const body = "A\nB\nC keyword\nD\nE\nF\nG\nH";
- const { snippet } = extractSnippet(body, "keyword", 500);
- // Should start with @@ -line,count @@ (N before, M after)
- const headerMatch = snippet.match(/^@@ -(\d+),(\d+) @@ \((\d+) before, (\d+) after\)/);
- expect(headerMatch).not.toBeNull();
- const [, startLine, count, before, after] = headerMatch!;
- expect(parseInt(startLine!)).toBe(2); // Snippet starts at line 2 (B)
- expect(parseInt(count!)).toBe(4); // 4 lines: B, C keyword, D, E
- expect(parseInt(before!)).toBe(1); // A is before
- expect(parseInt(after!)).toBe(3); // F, G, H are after
- });
- test("extractSnippet at document start shows 0 before", () => {
- const body = "First line keyword\nSecond\nThird\nFourth\nFifth";
- const { linesBefore, linesAfter, snippetLines, line } = extractSnippet(body, "keyword", 500);
- expect(line).toBe(1); // Keyword on first line
- expect(linesBefore).toBe(0); // Nothing before
- expect(snippetLines).toBe(3); // First, Second, Third (bestLine-1 to bestLine+3, clamped)
- expect(linesAfter).toBe(2); // Fourth, Fifth
- });
- test("extractSnippet at document end shows 0 after", () => {
- const body = "First\nSecond\nThird\nFourth\nFifth keyword";
- const { linesBefore, linesAfter, snippetLines, line } = extractSnippet(body, "keyword", 500);
- expect(line).toBe(5); // Keyword on last line
- expect(linesBefore).toBe(3); // First, Second, Third before snippet
- expect(snippetLines).toBe(2); // Fourth, Fifth keyword (bestLine-1 to bestLine+3, clamped)
- expect(linesAfter).toBe(0); // Nothing after
- });
- test("extractSnippet with single line document", () => {
- const body = "Single line with keyword";
- const { linesBefore, linesAfter, snippetLines, snippet } = extractSnippet(body, "keyword", 500);
- expect(linesBefore).toBe(0);
- expect(linesAfter).toBe(0);
- expect(snippetLines).toBe(1);
- expect(snippet).toContain("@@ -1,1 @@ (0 before, 0 after)");
- expect(snippet).toContain("Single line with keyword");
- });
- test("extractSnippet with chunkPos adjusts line numbers correctly", () => {
- // 50 lines of padding, then keyword, then more content
- const padding = "Padding line\n".repeat(50);
- const body = padding + "Target keyword here\nMore content\nEven more";
- const chunkPos = padding.length; // Position of "Target keyword"
- const { line, linesBefore, linesAfter } = extractSnippet(body, "keyword", 200, chunkPos);
- expect(line).toBe(51); // "Target keyword" is line 51
- expect(linesBefore).toBeGreaterThan(40); // Many lines before
- });
- });
- // =============================================================================
- // Reciprocal Rank Fusion Tests
- // =============================================================================
- describe("Reciprocal Rank Fusion", () => {
- const makeResult = (file: string, score: number): RankedResult => ({
- file,
- displayPath: file,
- title: file,
- body: "body",
- score,
- });
- test("RRF combines single list correctly", () => {
- const list1 = [
- makeResult("doc1", 0.9),
- makeResult("doc2", 0.8),
- makeResult("doc3", 0.7),
- ];
- const fused = reciprocalRankFusion([list1]);
- // Order should be preserved
- expect(fused[0]!.file).toBe("doc1");
- expect(fused[1]!.file).toBe("doc2");
- expect(fused[2]!.file).toBe("doc3");
- });
- test("RRF merges documents from multiple lists", () => {
- const list1 = [makeResult("doc1", 0.9), makeResult("doc2", 0.8)];
- const list2 = [makeResult("doc2", 0.95), makeResult("doc3", 0.85)];
- const fused = reciprocalRankFusion([list1, list2]);
- // doc2 appears in both lists, should have higher combined score
- expect(fused.find(r => r.file === "doc2")).toBeDefined();
- expect(fused.find(r => r.file === "doc1")).toBeDefined();
- expect(fused.find(r => r.file === "doc3")).toBeDefined();
- });
- test("RRF respects weights", () => {
- const list1 = [makeResult("doc1", 0.9)];
- const list2 = [makeResult("doc2", 0.9)];
- // Give double weight to list1
- const fused = reciprocalRankFusion([list1, list2], [2.0, 1.0]);
- // doc1 should rank higher due to weight
- expect(fused[0]!.file).toBe("doc1");
- });
- test("RRF adds top-rank bonus", () => {
- // doc1 is #1 in list1, doc2 is #2 in list1
- const list1 = [makeResult("doc1", 0.9), makeResult("doc2", 0.8)];
- const list2 = [makeResult("doc3", 0.85)];
- const fused = reciprocalRankFusion([list1, list2]);
- // doc1 should get +0.05 bonus for being #1
- // doc2 should get +0.02 bonus for being #2-3
- const doc1 = fused.find(r => r.file === "doc1");
- const doc2 = fused.find(r => r.file === "doc2");
- expect(doc1!.score).toBeGreaterThan(doc2!.score);
- });
- test("RRF handles empty lists", () => {
- const fused = reciprocalRankFusion([[], []]);
- expect(fused).toHaveLength(0);
- });
- test("RRF uses k parameter correctly", () => {
- const list = [makeResult("doc1", 0.9)];
- // With different k values, scores should differ
- const fused60 = reciprocalRankFusion([list], [], 60);
- const fused30 = reciprocalRankFusion([list], [], 30);
- // Lower k = higher scores for top ranks
- expect(fused30[0]!.score).toBeGreaterThan(fused60[0]!.score);
- });
- });
- // =============================================================================
- // Index Status Tests
- // =============================================================================
- describe("Index Status", () => {
- test("getStatus returns correct structure", async () => {
- const store = await createTestStore();
- const status = store.getStatus();
- expect(status).toHaveProperty("totalDocuments");
- expect(status).toHaveProperty("needsEmbedding");
- expect(status).toHaveProperty("hasVectorIndex");
- expect(status).toHaveProperty("collections");
- expect(Array.isArray(status.collections)).toBe(true);
- await cleanupTestDb(store);
- });
- test("getStatus counts documents correctly", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, { name: "doc1", active: 1 });
- await insertTestDocument(store.db, collectionName, { name: "doc2", active: 1 });
- await insertTestDocument(store.db, collectionName, { name: "doc3", active: 0 }); // inactive
- const status = store.getStatus();
- expect(status.totalDocuments).toBe(2); // Only active docs
- await cleanupTestDb(store);
- });
- test("getStatus reports collection info", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/test/path", glob: "**/*.md" });
- await insertTestDocument(store.db, collectionName, { name: "doc1" });
- const status = store.getStatus();
- expect(status.collections.length).toBeGreaterThanOrEqual(1);
- const col = status.collections.find(c => c.name === collectionName);
- expect(col).toBeDefined();
- expect(col?.path).toBe("/test/path");
- expect(col?.pattern).toBe("**/*.md");
- expect(col?.documents).toBe(1);
- await cleanupTestDb(store);
- });
- test("getHashesNeedingEmbedding counts correctly", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- // Add documents with different hashes
- await insertTestDocument(store.db, collectionName, { name: "doc1", hash: "hash1" });
- await insertTestDocument(store.db, collectionName, { name: "doc2", hash: "hash2" });
- await insertTestDocument(store.db, collectionName, { name: "doc3", hash: "hash1" }); // same hash as doc1
- const needsEmbedding = store.getHashesNeedingEmbedding();
- expect(needsEmbedding).toBe(2); // hash1 and hash2
- await cleanupTestDb(store);
- });
- test("getIndexHealth returns health info", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, { name: "doc1" });
- const health = store.getIndexHealth();
- expect(health).toHaveProperty("needsEmbedding");
- expect(health).toHaveProperty("totalDocs");
- expect(health).toHaveProperty("daysStale");
- expect(health.totalDocs).toBe(1);
- await cleanupTestDb(store);
- });
- });
- // =============================================================================
- // Fuzzy Matching Tests
- // =============================================================================
- describe("Fuzzy Matching", () => {
- test("findSimilarFiles finds similar paths", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "readme",
- displayPath: "docs/readme.md",
- });
- await insertTestDocument(store.db, collectionName, {
- name: "readmi",
- displayPath: "docs/readmi.md", // typo
- });
- const similar = store.findSimilarFiles("docs/readme.md", 3, 5);
- expect(similar).toContain("docs/readme.md");
- await cleanupTestDb(store);
- });
- test("findSimilarFiles respects maxDistance", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "abc",
- displayPath: "abc.md",
- });
- await insertTestDocument(store.db, collectionName, {
- name: "xyz",
- displayPath: "xyz.md", // very different
- });
- const similar = store.findSimilarFiles("abc.md", 1, 5); // max distance 1
- expect(similar).toContain("abc.md");
- expect(similar).not.toContain("xyz.md");
- await cleanupTestDb(store);
- });
- test("matchFilesByGlob matches patterns", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- filepath: "/p/journals/2024-01.md",
- displayPath: "journals/2024-01.md",
- });
- await insertTestDocument(store.db, collectionName, {
- filepath: "/p/journals/2024-02.md",
- displayPath: "journals/2024-02.md",
- });
- await insertTestDocument(store.db, collectionName, {
- filepath: "/p/docs/readme.md",
- displayPath: "docs/readme.md",
- });
- const matches = store.matchFilesByGlob("journals/*.md");
- expect(matches).toHaveLength(2);
- expect(matches.every(m => m.displayPath.startsWith("journals/"))).toBe(true);
- await cleanupTestDb(store);
- });
- });
- // =============================================================================
- // Vector Table Tests
- // =============================================================================
- describe("Vector Table", () => {
- test("ensureVecTable creates vector table", async () => {
- const store = await createTestStore();
- // Initially no vector table
- let exists = store.db.prepare(`
- SELECT name FROM sqlite_master WHERE type='table' AND name='vectors_vec'
- `).get();
- expect(exists).toBeFalsy(); // null or undefined
- // Create vector table
- store.ensureVecTable(768);
- exists = store.db.prepare(`
- SELECT name FROM sqlite_master WHERE type='table' AND name='vectors_vec'
- `).get();
- expect(exists).toBeTruthy();
- await cleanupTestDb(store);
- });
- test("ensureVecTable recreates table if dimensions change", async () => {
- const store = await createTestStore();
- // Create with 768 dimensions
- store.ensureVecTable(768);
- // Check dimensions
- let tableInfo = store.db.prepare(`
- SELECT sql FROM sqlite_master WHERE type='table' AND name='vectors_vec'
- `).get() as { sql: string };
- expect(tableInfo.sql).toContain("float[768]");
- // Recreate with different dimensions
- store.ensureVecTable(1024);
- tableInfo = store.db.prepare(`
- SELECT sql FROM sqlite_master WHERE type='table' AND name='vectors_vec'
- `).get() as { sql: string };
- expect(tableInfo.sql).toContain("float[1024]");
- await cleanupTestDb(store);
- });
- });
- // =============================================================================
- // Integration Tests
- // =============================================================================
- describe("Integration", () => {
- test("full document lifecycle: create, search, retrieve", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection({ pwd: "/test/notes", glob: "**/*.md" });
- // Add context - use "/" for collection root
- await addPathContext(collectionName, "/", "Personal notes");
- // Insert documents
- await insertTestDocument(store.db, collectionName, {
- name: "meeting",
- title: "Team Meeting Notes",
- filepath: "/test/notes/meeting.md",
- displayPath: "notes/meeting.md",
- body: "# Team Meeting Notes\n\nDiscussed project timeline and deliverables.",
- });
- await insertTestDocument(store.db, collectionName, {
- name: "ideas",
- title: "Project Ideas",
- filepath: "/test/notes/ideas.md",
- displayPath: "notes/ideas.md",
- body: "# Project Ideas\n\nBrainstorming new features for the product.",
- });
- // Search
- const searchResults = store.searchFTS("project", 10);
- expect(searchResults.length).toBe(2);
- // Status - SKIPPED: getStatus() has bug (queries non-existent collections table)
- // const status = store.getStatus();
- // expect(status.totalDocuments).toBe(2);
- // expect(status.collections).toHaveLength(1);
- // Retrieve single document
- const doc = store.findDocument("notes/meeting.md", { includeBody: true });
- expect("error" in doc).toBe(false);
- if (!("error" in doc)) {
- expect(doc.title).toBe("Team Meeting Notes");
- expect(doc.context).toBe("Personal notes");
- expect(doc.body).toContain("Team Meeting");
- }
- // Multi-get
- const { docs, errors } = store.findDocuments("notes/*.md", { includeBody: true });
- expect(errors).toHaveLength(0);
- expect(docs).toHaveLength(2);
- await cleanupTestDb(store);
- });
- test("multiple stores can operate independently", async () => {
- const store1 = await createTestStore();
- const store2 = await createTestStore();
- const col1 = await createTestCollection({ pwd: "/store1", glob: "**/*.md", name: "store1" });
- const col2 = await createTestCollection({ pwd: "/store2", glob: "**/*.md", name: "store2" });
- await insertTestDocument(store1.db, col1, {
- name: "doc1",
- body: "unique content for store1",
- displayPath: "doc.md",
- });
- await insertTestDocument(store2.db, col2, {
- name: "doc2",
- body: "different content for store2",
- displayPath: "doc.md",
- });
- // Each store should only see its own documents
- const results1 = store1.searchFTS("unique", 10);
- const results2 = store2.searchFTS("different", 10);
- expect(results1).toHaveLength(1);
- expect(results1[0]!.displayPath).toBe("store1/doc.md");
- expect(results1[0]!.filepath).toBe("qmd://store1/doc.md");
- expect(results2).toHaveLength(1);
- expect(results2[0]!.displayPath).toBe("store2/doc.md");
- expect(results2[0]!.filepath).toBe("qmd://store2/doc.md");
- // Cross-check: store1 shouldn't find store2's content
- const cross1 = store1.searchFTS("different", 10);
- const cross2 = store2.searchFTS("unique", 10);
- expect(cross1).toHaveLength(0);
- expect(cross2).toHaveLength(0);
- await cleanupTestDb(store1);
- await cleanupTestDb(store2);
- });
- });
- // =============================================================================
- // LlamaCpp Integration Tests (using real local models)
- // =============================================================================
- describe.skipIf(!!process.env.CI)("LlamaCpp Integration", () => {
- test("searchVec returns empty when no vector index", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- body: "Some content",
- });
- // No vectors_vec table exists, should return empty
- const results = await store.searchVec("query", "embeddinggemma", 10);
- expect(results).toHaveLength(0);
- await cleanupTestDb(store);
- });
- test("searchVec returns results when vector index exists", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- const hash = "testhash123";
- await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- hash,
- body: "Some content about testing",
- filepath: "/test/doc1.md",
- displayPath: "doc1.md",
- });
- // Create vector table and insert a vector
- store.ensureVecTable(768);
- const embedding = Array(768).fill(0).map(() => Math.random());
- store.db.prepare(`INSERT INTO content_vectors (hash, seq, pos, model, embedded_at) VALUES (?, 0, 0, 'test', ?)`).run(hash, new Date().toISOString());
- store.db.prepare(`INSERT INTO vectors_vec (hash_seq, embedding) VALUES (?, ?)`).run(`${hash}_0`, new Float32Array(embedding));
- const results = await store.searchVec("test query", "embeddinggemma", 10);
- expect(results).toHaveLength(1);
- expect(results[0]!.displayPath).toBe(`${collectionName}/doc1.md`);
- expect(results[0]!.filepath).toBe(`qmd://${collectionName}/doc1.md`);
- expect(results[0]!.source).toBe("vec");
- await cleanupTestDb(store);
- });
- test("searchVec filters by collection name", async () => {
- const store = await createTestStore();
- const collection1 = await createTestCollection({ name: "coll1", pwd: "/test/coll1" });
- const collection2 = await createTestCollection({ name: "coll2", pwd: "/test/coll2" });
- const hash1 = "hash1abc";
- const hash2 = "hash2xyz";
- await insertTestDocument(store.db, collection1, {
- name: "doc1",
- hash: hash1,
- body: "Content in collection one",
- });
- await insertTestDocument(store.db, collection2, {
- name: "doc2",
- hash: hash2,
- body: "Content in collection two",
- });
- // Create vectors_vec table with correct dimensions (768 for embeddinggemma)
- store.ensureVecTable(768);
- const embedding1 = Array(768).fill(0).map(() => Math.random());
- const embedding2 = Array(768).fill(0).map(() => Math.random());
- store.db.prepare(`INSERT INTO content_vectors (hash, seq, pos, model, embedded_at) VALUES (?, 0, 0, 'test', ?)`).run(hash1, new Date().toISOString());
- store.db.prepare(`INSERT INTO content_vectors (hash, seq, pos, model, embedded_at) VALUES (?, 0, 0, 'test', ?)`).run(hash2, new Date().toISOString());
- store.db.prepare(`INSERT INTO vectors_vec (hash_seq, embedding) VALUES (?, ?)`).run(`${hash1}_0`, new Float32Array(embedding1));
- store.db.prepare(`INSERT INTO vectors_vec (hash_seq, embedding) VALUES (?, ?)`).run(`${hash2}_0`, new Float32Array(embedding2));
- // Search without filter - should return both
- const allResults = await store.searchVec("content", "embeddinggemma", 10);
- expect(allResults).toHaveLength(2);
- // Search with collection filter - should return only from collection1
- const filtered = await store.searchVec("content", "embeddinggemma", 10, collection1);
- expect(filtered).toHaveLength(1);
- expect(filtered[0]!.collectionName).toBe(collection1);
- await cleanupTestDb(store);
- });
- // Regression test for https://github.com/tobi/qmd/pull/23
- // sqlite-vec virtual tables hang when combined with JOINs in the same query.
- // The fix uses a two-step approach: vector query first, then separate JOINs.
- test("searchVec uses two-step query to avoid sqlite-vec JOIN hang", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- const hash = "regression_test_hash";
- await insertTestDocument(store.db, collectionName, {
- name: "regression-doc",
- hash,
- body: "Test content for vector search regression",
- filepath: "/test/regression.md",
- displayPath: "regression.md",
- });
- // Create vector table and insert a test vector
- store.ensureVecTable(768);
- const embedding = Array(768).fill(0).map(() => Math.random());
- store.db.prepare(`INSERT INTO content_vectors (hash, seq, pos, model, embedded_at) VALUES (?, 0, 0, 'test', ?)`).run(hash, new Date().toISOString());
- store.db.prepare(`INSERT INTO vectors_vec (hash_seq, embedding) VALUES (?, ?)`).run(`${hash}_0`, new Float32Array(embedding));
- // This should complete quickly (not hang) due to the two-step fix
- // The old code with JOINs in the sqlite-vec query would hang indefinitely
- const startTime = Date.now();
- const results = await store.searchVec("test content", "embeddinggemma", 5);
- const elapsed = Date.now() - startTime;
- // If the query took more than 5 seconds, something is wrong
- // (the hang bug would cause it to never return at all)
- expect(elapsed).toBeLessThan(5000);
- expect(results.length).toBeGreaterThan(0);
- await cleanupTestDb(store);
- });
- test("expandQuery returns typed expansions (no original query)", async () => {
- const store = await createTestStore();
- const expanded = await store.expandQuery("test query");
- // Returns ExpandedQuery[] — typed results from LLM, excluding original
- expect(expanded.length).toBeGreaterThanOrEqual(1);
- for (const q of expanded) {
- expect(['lex', 'vec', 'hyde']).toContain(q.type);
- expect(q.query.length).toBeGreaterThan(0);
- expect(q.query).not.toBe("test query"); // original excluded
- }
- await cleanupTestDb(store);
- }, 30000);
- test("expandQuery caches results as JSON with types", async () => {
- const store = await createTestStore();
- // First call — hits LLM
- const queries1 = await store.expandQuery("cached query test");
- // Second call — hits cache
- const queries2 = await store.expandQuery("cached query test");
- // Cache should preserve full typed structure
- expect(queries1).toEqual(queries2);
- expect(queries2[0]?.type).toBeDefined();
- await cleanupTestDb(store);
- }, 30000);
- test("rerank scores documents", async () => {
- const store = await createTestStore();
- const docs = [
- { file: "doc1.md", text: "Relevant content about the topic" },
- { file: "doc2.md", text: "Other content" },
- ];
- const results = await store.rerank("topic", docs);
- expect(results).toHaveLength(2);
- // LlamaCpp reranker returns relevance scores
- expect(results[0]!.score).toBeGreaterThan(0);
- await cleanupTestDb(store);
- });
- test("rerank caches results", async () => {
- const store = await createTestStore();
- const docs = [{ file: "doc1.md", text: "Content for caching test" }];
- // First call
- await store.rerank("cache test query", docs);
- // Second call - should hit cache
- const results = await store.rerank("cache test query", docs);
- expect(results).toHaveLength(1);
- await cleanupTestDb(store);
- });
- test("rerank deduplicates identical chunks across files", async () => {
- const store = await createTestStore();
- const rerankSpy = vi.fn(async (_query: string, docs: { file: string; text: string }[]) => ({
- results: docs.map((doc, index) => ({
- file: doc.file,
- score: 1 - index * 0.1,
- index,
- })),
- model: "mock-reranker",
- }));
- const llmSpy = vi.spyOn(llmModule, "getDefaultLlamaCpp").mockReturnValue({
- rerank: rerankSpy,
- } as any);
- try {
- const docs = [
- { file: "doc1.md", text: "Shared chunk text" },
- { file: "doc2.md", text: "Shared chunk text" },
- ];
- const first = await store.rerank("shared", docs);
- const second = await store.rerank("shared", docs);
- expect(first).toHaveLength(2);
- expect(second).toHaveLength(2);
- expect(rerankSpy).toHaveBeenCalledTimes(1);
- expect(rerankSpy.mock.calls[0]?.[1]).toEqual([{ file: "doc2.md", text: "Shared chunk text" }]);
- } finally {
- llmSpy.mockRestore();
- await cleanupTestDb(store);
- }
- });
- });
- // =============================================================================
- // Edge Cases & Error Handling
- // =============================================================================
- describe("Edge Cases", () => {
- test("handles empty database gracefully", async () => {
- const store = await createTestStore();
- const searchResults = store.searchFTS("anything", 10);
- expect(searchResults).toHaveLength(0);
- // SKIPPED: getStatus() has bug (queries non-existent collections table)
- // const status = store.getStatus();
- // expect(status.totalDocuments).toBe(0);
- // expect(status.collections).toHaveLength(0);
- const doc = store.findDocument("nonexistent.md");
- expect("error" in doc).toBe(true);
- await cleanupTestDb(store);
- });
- test("handles very long document bodies", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- const longBody = "word ".repeat(100000); // ~600KB
- await insertTestDocument(store.db, collectionName, {
- name: "long",
- body: longBody,
- displayPath: "long.md",
- });
- const results = store.searchFTS("word", 10);
- expect(results).toHaveLength(1);
- await cleanupTestDb(store);
- });
- test("handles unicode content correctly", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "unicode",
- title: "日本語タイトル",
- body: "# 日本語\n\n内容は日本語で書かれています。\n\nEmoji: 🎉🚀✨",
- displayPath: "unicode.md",
- });
- // Should be searchable
- const results = store.searchFTS("日本語", 10);
- expect(results.length).toBeGreaterThan(0);
- // Should retrieve correctly
- const doc = store.findDocument("unicode.md", { includeBody: true });
- expect("error" in doc).toBe(false);
- if (!("error" in doc)) {
- expect(doc.title).toBe("日本語タイトル");
- expect(doc.body).toContain("🎉");
- }
- await cleanupTestDb(store);
- });
- test("handles documents with special characters in paths", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- await insertTestDocument(store.db, collectionName, {
- name: "special",
- filepath: "/path/file with spaces.md",
- displayPath: "file with spaces.md",
- body: "Content",
- });
- const doc = store.findDocument("file with spaces.md");
- expect("error" in doc).toBe(false);
- await cleanupTestDb(store);
- });
- test("handles concurrent operations", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- // Insert multiple documents concurrently
- const inserts = Array.from({ length: 10 }, (_, i) =>
- insertTestDocument(store.db, collectionName, {
- name: `concurrent${i}`,
- body: `Content ${i} searchterm`,
- displayPath: `concurrent${i}.md`,
- })
- );
- await Promise.all(inserts);
- // All should be searchable
- const results = store.searchFTS("searchterm", 20);
- expect(results).toHaveLength(10);
- await cleanupTestDb(store);
- });
- });
- describe("Embedding batching", () => {
- function createFakeTokenizer() {
- return {
- async tokenize(text: string) {
- return new Array(Math.max(1, Math.ceil(text.length / 16))).fill(1);
- },
- };
- }
- function createFakeEmbedLlm() {
- const embedBatchCalls: string[][] = [];
- return {
- embedBatchCalls,
- async embed(_text: string) {
- return { embedding: [0.1, 0.2, 0.3], model: "fake-embed" };
- },
- async embedBatch(texts: string[]) {
- embedBatchCalls.push([...texts]);
- return texts.map((_text, index) => ({
- embedding: [index + 1, index + 2, index + 3],
- model: "fake-embed",
- }));
- },
- };
- }
- test("generateEmbeddings flushes batches when maxDocsPerBatch is reached", async () => {
- const store = await createTestStore();
- const db = store.db;
- const fakeLlm = createFakeEmbedLlm();
- setDefaultLlamaCpp(createFakeTokenizer() as any);
- store.llm = fakeLlm as any;
- try {
- await insertTestDocument(db, "docs", { name: "one", body: "# One\n\nAlpha" });
- await insertTestDocument(db, "docs", { name: "two", body: "# Two\n\nBeta" });
- await insertTestDocument(db, "docs", { name: "three", body: "# Three\n\nGamma" });
- const result = await generateEmbeddings(store, {
- maxDocsPerBatch: 1,
- maxBatchBytes: 1024 * 1024,
- });
- expect(fakeLlm.embedBatchCalls).toHaveLength(3);
- expect(fakeLlm.embedBatchCalls.map(call => call.length)).toEqual([1, 1, 1]);
- expect(result.docsProcessed).toBe(3);
- expect(result.chunksEmbedded).toBe(3);
- expect(db.prepare(`SELECT COUNT(*) as count FROM content_vectors`).get()).toEqual({ count: 3 });
- } finally {
- setDefaultLlamaCpp(null);
- await cleanupTestDb(store);
- }
- });
- test("generateEmbeddings flushes batches when maxBatchBytes is reached", async () => {
- const store = await createTestStore();
- const db = store.db;
- const fakeLlm = createFakeEmbedLlm();
- setDefaultLlamaCpp(createFakeTokenizer() as any);
- store.llm = fakeLlm as any;
- const docOne = "# One\n\n" + "A".repeat(36);
- const docTwo = "# Two\n\n" + "B".repeat(36);
- const docThree = "# Three\n\n" + "C".repeat(36);
- const batchLimit = new TextEncoder().encode(docOne).length
- + new TextEncoder().encode(docTwo).length
- + 1;
- try {
- await insertTestDocument(db, "docs", { name: "a-one", body: docOne });
- await insertTestDocument(db, "docs", { name: "b-two", body: docTwo });
- await insertTestDocument(db, "docs", { name: "c-three", body: docThree });
- const result = await generateEmbeddings(store, {
- maxDocsPerBatch: 64,
- maxBatchBytes: batchLimit,
- });
- expect(fakeLlm.embedBatchCalls).toHaveLength(2);
- expect(fakeLlm.embedBatchCalls.map(call => call.length)).toEqual([2, 1]);
- expect(result.docsProcessed).toBe(3);
- expect(result.chunksEmbedded).toBe(3);
- } finally {
- setDefaultLlamaCpp(null);
- await cleanupTestDb(store);
- }
- });
- test("generateEmbeddings rejects invalid batch limits", async () => {
- const store = await createTestStore();
- try {
- await expect(generateEmbeddings(store, { maxDocsPerBatch: 0 })).rejects.toThrow(
- "maxDocsPerBatch"
- );
- await expect(generateEmbeddings(store, { maxBatchBytes: 0 })).rejects.toThrow(
- "maxBatchBytes"
- );
- } finally {
- setDefaultLlamaCpp(null);
- await cleanupTestDb(store);
- }
- });
- });
- // =============================================================================
- // Content-Addressable Storage Tests
- // =============================================================================
- describe("Content-Addressable Storage", () => {
- test("same content gets same hash from multiple collections", async () => {
- const store = await createTestStore();
- // Create two collections
- const collection1 = await createTestCollection({ pwd: "/path/collection1", name: "collection1" });
- const collection2 = await createTestCollection({ pwd: "/path/collection2", name: "collection2" });
- // Add same content to both collections
- const content = "# Same Content\n\nThis is the same content in two places.";
- const hash1 = await hashContent(content);
- const doc1 = await insertTestDocument(store.db, collection1, {
- name: "doc1",
- body: content,
- displayPath: "doc1.md",
- });
- const doc2 = await insertTestDocument(store.db, collection2, {
- name: "doc2",
- body: content,
- displayPath: "doc2.md",
- });
- // Both should have the same hash
- const hash1Db = store.db.prepare(`SELECT hash FROM documents WHERE id = ?`).get(doc1) as { hash: string };
- const hash2Db = store.db.prepare(`SELECT hash FROM documents WHERE id = ?`).get(doc2) as { hash: string };
- expect(hash1Db.hash).toBe(hash2Db.hash);
- expect(hash1Db.hash).toBe(hash1);
- // There should only be one entry in the content table
- const contentCount = store.db.prepare(`SELECT COUNT(*) as count FROM content WHERE hash = ?`).get(hash1) as { count: number };
- expect(contentCount.count).toBe(1);
- await cleanupTestDb(store);
- });
- test("removing one collection preserves content used by another", async () => {
- const store = await createTestStore();
- // Create two collections
- const collection1 = await createTestCollection({ pwd: "/path/collection1", name: "collection1" });
- const collection2 = await createTestCollection({ pwd: "/path/collection2", name: "collection2" });
- // Add same content to both collections
- const sharedContent = "# Shared Content\n\nThis is shared.";
- const sharedHash = await hashContent(sharedContent);
- await insertTestDocument(store.db, collection1, {
- name: "shared1",
- body: sharedContent,
- displayPath: "shared1.md",
- });
- await insertTestDocument(store.db, collection2, {
- name: "shared2",
- body: sharedContent,
- displayPath: "shared2.md",
- });
- // Add unique content to collection1
- const uniqueContent = "# Unique Content\n\nThis is unique to collection1.";
- const uniqueHash = await hashContent(uniqueContent);
- await insertTestDocument(store.db, collection1, {
- name: "unique",
- body: uniqueContent,
- displayPath: "unique.md",
- });
- // Verify both hashes exist in content table
- const sharedExists1 = store.db.prepare(`SELECT hash FROM content WHERE hash = ?`).get(sharedHash);
- const uniqueExists1 = store.db.prepare(`SELECT hash FROM content WHERE hash = ?`).get(uniqueHash);
- expect(sharedExists1).toBeTruthy();
- expect(uniqueExists1).toBeTruthy();
- // Remove collection1 documents (collections are in YAML now)
- store.db.prepare(`DELETE FROM documents WHERE collection = ?`).run(collection1);
- // Clean up orphaned content (mimics what the CLI does)
- store.db.prepare(`
- DELETE FROM content
- WHERE hash NOT IN (SELECT DISTINCT hash FROM documents WHERE active = 1)
- `).run();
- // Shared content should still exist (used by collection2)
- const sharedExists2 = store.db.prepare(`SELECT hash FROM content WHERE hash = ?`).get(sharedHash);
- expect(sharedExists2).toBeTruthy();
- // Unique content should be removed (only used by collection1)
- const uniqueExists2 = store.db.prepare(`SELECT hash FROM content WHERE hash = ?`).get(uniqueHash);
- expect(uniqueExists2).toBeFalsy();
- await cleanupTestDb(store);
- });
- test("deduplicates content across many collections", async () => {
- const store = await createTestStore();
- const sharedContent = "# Common Header\n\nThis appears everywhere.";
- const sharedHash = await hashContent(sharedContent);
- // Create 5 collections with the same content
- const collectionNames = [];
- for (let i = 0; i < 5; i++) {
- const collName = await createTestCollection({ pwd: `/path/collection${i}`, name: `collection${i}` });
- collectionNames.push(collName);
- await insertTestDocument(store.db, collName, {
- name: `doc${i}`,
- body: sharedContent,
- displayPath: `doc${i}.md`,
- });
- }
- // Should have 5 documents
- const docCount = store.db.prepare(`SELECT COUNT(*) as count FROM documents WHERE active = 1`).get() as { count: number };
- expect(docCount.count).toBe(5);
- // But only 1 content entry
- const contentCount = store.db.prepare(`SELECT COUNT(*) as count FROM content WHERE hash = ?`).get(sharedHash) as { count: number };
- expect(contentCount.count).toBe(1);
- // All documents should point to the same hash
- const hashes = store.db.prepare(`SELECT DISTINCT hash FROM documents WHERE active = 1`).all() as { hash: string }[];
- expect(hashes).toHaveLength(1);
- expect(hashes[0]!.hash).toBe(sharedHash);
- await cleanupTestDb(store);
- });
- test("different content gets different hashes", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- const content1 = "# Content One";
- const content2 = "# Content Two";
- const hash1 = await hashContent(content1);
- const hash2 = await hashContent(content2);
- // Hashes should be different
- expect(hash1).not.toBe(hash2);
- const doc1 = await insertTestDocument(store.db, collectionName, {
- name: "doc1",
- body: content1,
- displayPath: "doc1.md",
- });
- const doc2 = await insertTestDocument(store.db, collectionName, {
- name: "doc2",
- body: content2,
- displayPath: "doc2.md",
- });
- // Both hashes should exist in content table
- const hash1Db = store.db.prepare(`SELECT hash FROM documents WHERE id = ?`).get(doc1) as { hash: string };
- const hash2Db = store.db.prepare(`SELECT hash FROM documents WHERE id = ?`).get(doc2) as { hash: string };
- expect(hash1Db.hash).toBe(hash1);
- expect(hash2Db.hash).toBe(hash2);
- expect(hash1Db.hash).not.toBe(hash2Db.hash);
- // Should have 2 entries in content table
- const contentCount = store.db.prepare(`SELECT COUNT(*) as count FROM content`).get() as { count: number };
- expect(contentCount.count).toBe(2);
- await cleanupTestDb(store);
- });
- test("re-indexing a previously deactivated path reactivates instead of violating UNIQUE", async () => {
- const store = await createTestStore();
- const collectionName = await createTestCollection();
- const now = new Date().toISOString();
- const oldContent = "# First Version";
- const oldHash = await hashContent(oldContent);
- store.insertContent(oldHash, oldContent, now);
- store.insertDocument(collectionName, "docs/foo.md", "foo", oldHash, now, now);
- // Simulate file removal during update pass.
- store.deactivateDocument(collectionName, "docs/foo.md");
- expect(store.findActiveDocument(collectionName, "docs/foo.md")).toBeNull();
- // Simulate file coming back in a later update pass.
- const newContent = "# Second Version";
- const newHash = await hashContent(newContent);
- store.insertContent(newHash, newContent, now);
- expect(() => {
- store.insertDocument(collectionName, "docs/foo.md", "foo", newHash, now, now);
- }).not.toThrow();
- const rows = store.db.prepare(`
- SELECT id, hash, active FROM documents
- WHERE collection = ? AND path = ?
- `).all(collectionName, "docs/foo.md") as { id: number; hash: string; active: number }[];
- expect(rows).toHaveLength(1);
- expect(rows[0]!.active).toBe(1);
- expect(rows[0]!.hash).toBe(newHash);
- await cleanupTestDb(store);
- });
- });
- // =============================================================================
- // Virtual Path Normalization Tests
- // =============================================================================
- describe("normalizeVirtualPath", () => {
- test("already normalized qmd:// path passes through", () => {
- expect(normalizeVirtualPath("qmd://collection/path.md")).toBe("qmd://collection/path.md");
- expect(normalizeVirtualPath("qmd://journals/2025-01-01.md")).toBe("qmd://journals/2025-01-01.md");
- });
- test("handles //collection/path format (missing qmd: prefix)", () => {
- expect(normalizeVirtualPath("//collection/path.md")).toBe("qmd://collection/path.md");
- expect(normalizeVirtualPath("//journals/2025-01-01.md")).toBe("qmd://journals/2025-01-01.md");
- });
- test("handles qmd:// with extra slashes", () => {
- expect(normalizeVirtualPath("qmd:////collection/path.md")).toBe("qmd://collection/path.md");
- expect(normalizeVirtualPath("qmd:///journals/2025-01-01.md")).toBe("qmd://journals/2025-01-01.md");
- expect(normalizeVirtualPath("qmd:///////archive/file.md")).toBe("qmd://archive/file.md");
- });
- test("handles collection root paths", () => {
- expect(normalizeVirtualPath("qmd://collection/")).toBe("qmd://collection/");
- expect(normalizeVirtualPath("qmd://collection")).toBe("qmd://collection");
- expect(normalizeVirtualPath("//collection/")).toBe("qmd://collection/");
- });
- test("preserves bare collection/path format (not auto-converted)", () => {
- // Bare paths without qmd:// or // prefix are NOT converted
- // (could be relative filesystem paths)
- expect(normalizeVirtualPath("collection/path.md")).toBe("collection/path.md");
- expect(normalizeVirtualPath("journals/2025-01-01.md")).toBe("journals/2025-01-01.md");
- });
- test("preserves absolute filesystem paths", () => {
- expect(normalizeVirtualPath("/Users/test/file.md")).toBe("/Users/test/file.md");
- expect(normalizeVirtualPath("/absolute/path/file.md")).toBe("/absolute/path/file.md");
- });
- test("preserves home-relative paths", () => {
- expect(normalizeVirtualPath("~/Documents/file.md")).toBe("~/Documents/file.md");
- });
- test("preserves docid format", () => {
- expect(normalizeVirtualPath("#abc123")).toBe("#abc123");
- expect(normalizeVirtualPath("#def456")).toBe("#def456");
- });
- test("handles whitespace trimming", () => {
- expect(normalizeVirtualPath(" qmd://collection/path.md ")).toBe("qmd://collection/path.md");
- expect(normalizeVirtualPath(" //collection/path.md ")).toBe("qmd://collection/path.md");
- });
- });
- describe("isVirtualPath", () => {
- test("recognizes qmd:// paths", () => {
- expect(isVirtualPath("qmd://collection/path.md")).toBe(true);
- expect(isVirtualPath("qmd://journals/2025-01-01.md")).toBe(true);
- expect(isVirtualPath("qmd://collection")).toBe(true);
- });
- test("recognizes //collection/path format", () => {
- expect(isVirtualPath("//collection/path.md")).toBe(true);
- expect(isVirtualPath("//journals/2025-01-01.md")).toBe(true);
- });
- test("does not auto-recognize bare collection/path format", () => {
- // Bare paths could be relative filesystem paths, so not auto-detected as virtual
- expect(isVirtualPath("collection/path.md")).toBe(false);
- expect(isVirtualPath("journals/2025-01-01.md")).toBe(false);
- expect(isVirtualPath("archive/subfolder/file.md")).toBe(false);
- });
- test("rejects docid format", () => {
- expect(isVirtualPath("#abc123")).toBe(false);
- expect(isVirtualPath("#def456")).toBe(false);
- });
- test("rejects absolute filesystem paths", () => {
- expect(isVirtualPath("/Users/test/file.md")).toBe(false);
- expect(isVirtualPath("/absolute/path/file.md")).toBe(false);
- });
- test("rejects home-relative paths", () => {
- expect(isVirtualPath("~/Documents/file.md")).toBe(false);
- expect(isVirtualPath("~/notes/journal.md")).toBe(false);
- });
- test("rejects paths without slashes", () => {
- expect(isVirtualPath("file.md")).toBe(false);
- expect(isVirtualPath("document")).toBe(false);
- });
- });
- describe("parseVirtualPath", () => {
- test("parses standard qmd:// paths", () => {
- expect(parseVirtualPath("qmd://collection/path.md")).toEqual({
- collectionName: "collection",
- path: "path.md",
- });
- expect(parseVirtualPath("qmd://journals/2025-01-01.md")).toEqual({
- collectionName: "journals",
- path: "2025-01-01.md",
- });
- });
- test("parses paths with nested directories", () => {
- expect(parseVirtualPath("qmd://archive/subfolder/file.md")).toEqual({
- collectionName: "archive",
- path: "subfolder/file.md",
- });
- });
- test("parses collection root paths", () => {
- expect(parseVirtualPath("qmd://collection/")).toEqual({
- collectionName: "collection",
- path: "",
- });
- expect(parseVirtualPath("qmd://collection")).toEqual({
- collectionName: "collection",
- path: "",
- });
- });
- test("parses //collection/path format (normalizes first)", () => {
- expect(parseVirtualPath("//collection/path.md")).toEqual({
- collectionName: "collection",
- path: "path.md",
- });
- });
- test("parses qmd:// with extra slashes (normalizes first)", () => {
- expect(parseVirtualPath("qmd:////collection/path.md")).toEqual({
- collectionName: "collection",
- path: "path.md",
- });
- });
- test("returns null for non-virtual paths", () => {
- expect(parseVirtualPath("/absolute/path.md")).toBe(null);
- expect(parseVirtualPath("~/home/path.md")).toBe(null);
- expect(parseVirtualPath("#docid")).toBe(null);
- expect(parseVirtualPath("file.md")).toBe(null);
- // Bare collection/path is not recognized as virtual
- expect(parseVirtualPath("collection/path.md")).toBe(null);
- });
- });
- // =============================================================================
- // Docid Functions
- // =============================================================================
- describe("normalizeDocid", () => {
- test("strips leading # from docid", () => {
- expect(normalizeDocid("#abc123")).toBe("abc123");
- expect(normalizeDocid("#def456")).toBe("def456");
- });
- test("returns bare hex unchanged", () => {
- expect(normalizeDocid("abc123")).toBe("abc123");
- expect(normalizeDocid("def456")).toBe("def456");
- });
- test("strips surrounding double quotes", () => {
- expect(normalizeDocid('"#abc123"')).toBe("abc123");
- expect(normalizeDocid('"abc123"')).toBe("abc123");
- });
- test("strips surrounding single quotes", () => {
- expect(normalizeDocid("'#abc123'")).toBe("abc123");
- expect(normalizeDocid("'abc123'")).toBe("abc123");
- });
- test("handles quoted docid without #", () => {
- expect(normalizeDocid('"def456"')).toBe("def456");
- expect(normalizeDocid("'def456'")).toBe("def456");
- });
- test("handles whitespace", () => {
- expect(normalizeDocid(" #abc123 ")).toBe("abc123");
- expect(normalizeDocid(" abc123 ")).toBe("abc123");
- });
- test("handles uppercase hex", () => {
- expect(normalizeDocid("#ABC123")).toBe("ABC123");
- expect(normalizeDocid('"ABC123"')).toBe("ABC123");
- });
- test("does not strip mismatched quotes", () => {
- expect(normalizeDocid('"abc123\'')).toBe('"abc123\'');
- expect(normalizeDocid("'abc123\"")).toBe("'abc123\"");
- });
- });
- describe("isDocid", () => {
- test("accepts #hash format", () => {
- expect(isDocid("#abc123")).toBe(true);
- expect(isDocid("#def456")).toBe(true);
- expect(isDocid("#ABCDEF")).toBe(true);
- });
- test("accepts bare 6-char hex", () => {
- expect(isDocid("abc123")).toBe(true);
- expect(isDocid("def456")).toBe(true);
- expect(isDocid("ABCDEF")).toBe(true);
- });
- test("accepts longer hex strings", () => {
- expect(isDocid("abc123def456")).toBe(true);
- expect(isDocid("#abc123def456")).toBe(true);
- });
- test("accepts double-quoted docids", () => {
- expect(isDocid('"#abc123"')).toBe(true);
- expect(isDocid('"abc123"')).toBe(true);
- });
- test("accepts single-quoted docids", () => {
- expect(isDocid("'#abc123'")).toBe(true);
- expect(isDocid("'abc123'")).toBe(true);
- });
- test("rejects non-hex strings", () => {
- expect(isDocid("ghijkl")).toBe(false);
- expect(isDocid("#ghijkl")).toBe(false);
- expect(isDocid("abc12g")).toBe(false);
- });
- test("rejects strings shorter than 6 chars", () => {
- expect(isDocid("abc12")).toBe(false);
- expect(isDocid("#abc1")).toBe(false);
- expect(isDocid("'abc'")).toBe(false);
- });
- test("rejects empty strings", () => {
- expect(isDocid("")).toBe(false);
- expect(isDocid("#")).toBe(false);
- expect(isDocid('""')).toBe(false);
- });
- test("rejects file paths", () => {
- expect(isDocid("/path/to/file.md")).toBe(false);
- expect(isDocid("path/to/file.md")).toBe(false);
- expect(isDocid("qmd://collection/file.md")).toBe(false);
- });
- test("rejects paths that look like hex with extensions", () => {
- expect(isDocid("abc123.md")).toBe(false);
- });
- });
|