import assert from "node:assert/strict"; import test from "node:test"; import { JavaScriptFuzzyIndex, extractCurrentWordPrefix, extractExistingPaths, extractWords, replaceCurrentWordPrefix, shouldDelegateToBuiltIn, } from "../index.ts"; test("JavaScriptFuzzyIndex deduplicates and ranks matching candidates", async () => { const index = new JavaScriptFuzzyIndex(); const added = await index.addCandidatesAsync([ { text: "Terminal", source: "session:user" }, { text: "terminal", source: "session:assistant" }, { text: "template", source: "session:user" }, ]); assert.equal(added, 2); assert.equal(index.len(), 2); assert.deepEqual( (await index.queryAsync("te", 8)).map((match) => match.text).sort(), ["Terminal", "template"].sort(), ); }); test("extractCurrentWordPrefix only returns eligible word prefixes", () => { assert.equal(extractCurrentWordPrefix("please te"), "te"); assert.equal(extractCurrentWordPrefix("please t"), undefined); assert.equal(extractCurrentWordPrefix("number 123"), undefined); assert.equal(extractCurrentWordPrefix("path/to"), "to"); }); test("built-in completion contexts are delegated", () => { assert.equal(shouldDelegateToBuiltIn("/hel"), true); assert.equal(shouldDelegateToBuiltIn("read @src/"), true); assert.equal(shouldDelegateToBuiltIn("open ./index.ts"), true); assert.equal(shouldDelegateToBuiltIn("please te"), false); }); test("replaceCurrentWordPrefix replaces the word around the cursor", () => { assert.deepEqual( replaceCurrentWordPrefix(["please te now"], 0, 9, "test"), { lines: ["please test now"], cursorLine: 0, cursorCol: 11 }, ); }); test("extractWords returns bounded word candidates", () => { assert.deepEqual(extractWords("Use pi-frizbee and @file_name.", "session:user"), [ { text: "Use", source: "session:user" }, { text: "pi-frizbee", source: "session:user" }, { text: "and", source: "session:user" }, { text: "file_name", source: "session:user" }, ]); }); test("extractExistingPaths keeps only paths that exist", () => { const candidates = extractExistingPaths("Read ./index.ts and ./does-not-exist", process.cwd()); assert.deepEqual(candidates.map((candidate) => candidate.text), [`${process.cwd()}/index.ts`]); });