Skip to content
29 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { MAX_IN_FILTER, QueryCache, TOP_K, TOP_K_UNFILTERED, fuseRanks, queryKey, vectorQueryPlan } from "./recall.ts";
5
6test("the index is filtered by the allowed spaces, or by workspace and after when there are too many", () => {
7 assert.equal(vectorQueryPlan("w1", []), null);
8 assert.deepEqual(vectorQueryPlan("w1", ["s1", "s2", "s1"]), { topK: TOP_K, filter: { workspace_id: "w1", space_ids: ["s1", "s2"] }, filterAfter: false });
9 const many = Array.from({ length: MAX_IN_FILTER + 1 }, (_, i) => `s${i}`);
10 assert.deepEqual(vectorQueryPlan("w1", many), { topK: TOP_K_UNFILTERED, filter: { workspace_id: "w1" }, filterAfter: true });
11});
12
13test("hybrid search fuses ranks: found both ways first", () => {
14 assert.deepEqual(fuseRanks(["a", "b", "c"], ["c", "d"]), ["c", "a", "b", "d"]);
15 assert.deepEqual(fuseRanks([], ["x", "y"]), ["x", "y"]);
16});
17
18test("query embeddings are kept a minute", () => {
19 const cache = new QueryCache(60_000, 2);
20 cache.set("a", [1], 0);
21 assert.deepEqual(cache.get("a", 59_000), [1]);
22 assert.equal(cache.get("a", 61_000), null);
23 cache.set("a", [1], 0);
24 cache.set("b", [2], 1);
25 cache.set("c", [3], 2);
26 assert.equal(cache.get("a", 3), null);
27 assert.deepEqual(cache.get("c", 3), [3]);
28 assert.equal(queryKey(" How do we Deploy? "), "how do we deploy?");
29});