-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathqdrant-client.test.ts
More file actions
276 lines (234 loc) · 8.78 KB
/
qdrant-client.test.ts
File metadata and controls
276 lines (234 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { afterAll, describe, expect, mock, test } from "bun:test";
import type { MemoryConfig } from "../../config/types.ts";
import { QdrantClient } from "../qdrant-client.ts";
const TEST_CONFIG: MemoryConfig = {
qdrant: { url: "http://localhost:6333" },
ollama: { url: "http://localhost:11434", model: "nomic-embed-text" },
collections: { episodes: "episodes", semantic_facts: "semantic_facts", procedures: "procedures" },
embedding: { dimensions: 768, batch_size: 32 },
context: { max_tokens: 50000, episode_limit: 10, fact_limit: 20, procedure_limit: 5 },
};
describe("QdrantClient", () => {
const originalFetch = globalThis.fetch;
afterAll(() => {
globalThis.fetch = originalFetch;
});
test("createCollection sends PUT with correct schema", async () => {
const calls: { url: string; method: string; body: string }[] = [];
globalThis.fetch = mock((url: string | Request, init?: RequestInit) => {
const urlStr = typeof url === "string" ? url : url.url;
// collectionExists check returns 404 (doesn't exist)
if (init?.method === undefined || init?.method === "GET") {
return Promise.resolve(new Response("", { status: 404 }));
}
calls.push({
url: urlStr,
method: init?.method ?? "GET",
body: init?.body as string,
});
return Promise.resolve(
new Response(JSON.stringify({ status: "ok", result: true }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
);
}) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
await client.createCollection("test_collection", {
vectors: {
summary: { size: 768, distance: "Cosine" },
},
sparse_vectors: {
text_bm25: {},
},
});
expect(calls.length).toBe(1);
expect(calls[0].url).toContain("/collections/test_collection");
expect(calls[0].method).toBe("PUT");
const body = JSON.parse(calls[0].body);
expect(body.vectors.summary.size).toBe(768);
expect(body.sparse_vectors.text_bm25).toBeDefined();
});
test("createCollection skips if collection already exists", async () => {
let putCalled = false;
globalThis.fetch = mock((_url: string | Request, init?: RequestInit) => {
if (init?.method === "PUT") {
putCalled = true;
}
// collectionExists returns 200 (exists)
return Promise.resolve(
new Response(JSON.stringify({ status: "ok" }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
);
}) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
await client.createCollection("existing", { vectors: {} });
expect(putCalled).toBe(false);
});
test("upsert sends points with named vectors", async () => {
let capturedBody: Record<string, unknown> | null = null;
globalThis.fetch = mock((_url: string | Request, init?: RequestInit) => {
if (init?.body) {
capturedBody = JSON.parse(init.body as string);
}
return Promise.resolve(
new Response(JSON.stringify({ status: "ok", result: { operation_id: 1 } }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
);
}) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
await client.upsert("episodes", [
{
id: "test-id",
vector: {
summary: [0.1, 0.2, 0.3],
text_bm25: { indices: [1, 42], values: [0.5, 0.8] },
},
payload: { type: "task", summary: "test" },
},
]);
const body = capturedBody as unknown as Record<string, unknown>;
expect(body).not.toBeNull();
const points = body.points as Array<Record<string, unknown>>;
expect(points.length).toBe(1);
expect(points[0].id).toBe("test-id");
expect((points[0].vector as Record<string, unknown>).summary).toEqual([0.1, 0.2, 0.3]);
expect((points[0].vector as Record<string, unknown>).text_bm25).toEqual({ indices: [1, 42], values: [0.5, 0.8] });
});
test("search with hybrid search sends prefetch+RRF", async () => {
let capturedBody: Record<string, unknown> | null = null;
globalThis.fetch = mock((_url: string | Request, init?: RequestInit) => {
if (init?.body) {
capturedBody = JSON.parse(init.body as string);
}
return Promise.resolve(
new Response(
JSON.stringify({
result: {
points: [
{ id: "result-1", score: 0.95, payload: { summary: "test memory" } },
{ id: "result-2", score: 0.8, payload: { summary: "another memory" } },
],
},
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
),
);
}) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
const results = await client.search("episodes", {
denseVector: [0.1, 0.2, 0.3],
denseVectorName: "summary",
sparseVector: { indices: [1, 42], values: [0.5, 0.8] },
sparseVectorName: "text_bm25",
limit: 5,
});
expect(results.length).toBe(2);
expect(results[0].id).toBe("result-1");
expect(results[0].score).toBe(0.95);
expect(results[0].payload.summary).toBe("test memory");
// Verify hybrid search structure
const hybridBody = capturedBody as unknown as Record<string, unknown>;
expect(hybridBody).not.toBeNull();
expect(hybridBody.prefetch).toBeDefined();
expect((hybridBody.query as Record<string, unknown>).fusion).toBe("rrf");
});
test("search with dense-only sends direct query", async () => {
let capturedBody: Record<string, unknown> | null = null;
globalThis.fetch = mock((_url: string | Request, init?: RequestInit) => {
if (init?.body) {
capturedBody = JSON.parse(init.body as string);
}
return Promise.resolve(
new Response(JSON.stringify({ result: { points: [] } }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
);
}) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
await client.search("episodes", {
denseVector: [0.1, 0.2],
denseVectorName: "summary",
limit: 5,
});
const denseBody = capturedBody as unknown as Record<string, unknown>;
expect(denseBody).not.toBeNull();
expect(denseBody.query).toEqual([0.1, 0.2]);
expect(denseBody.using).toBe("summary");
expect(denseBody.prefetch).toBeUndefined();
});
test("search returns empty array when no vectors provided", async () => {
const client = new QdrantClient(TEST_CONFIG);
const results = await client.search("episodes", { limit: 5 });
expect(results).toEqual([]);
});
test("isHealthy returns true when Qdrant responds", async () => {
globalThis.fetch = mock(() =>
Promise.resolve(new Response('{"title":"ok"}', { status: 200 })),
) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
expect(await client.isHealthy()).toBe(true);
});
test("isHealthy returns false when Qdrant is down", async () => {
globalThis.fetch = mock(() => Promise.reject(new Error("ECONNREFUSED"))) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
expect(await client.isHealthy()).toBe(false);
});
test("deletePoint sends correct request", async () => {
let capturedUrl = "";
let capturedBody: Record<string, unknown> | null = null;
globalThis.fetch = mock((url: string | Request, init?: RequestInit) => {
capturedUrl = typeof url === "string" ? url : url.url;
if (init?.body) {
capturedBody = JSON.parse(init.body as string);
}
return Promise.resolve(
new Response(JSON.stringify({ status: "ok" }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
);
}) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
await client.deletePoint("episodes", "point-123");
expect(capturedUrl).toContain("/collections/episodes/points/delete");
const deleteBody = capturedBody as unknown as Record<string, unknown>;
expect(deleteBody).not.toBeNull();
expect(deleteBody.points).toEqual(["point-123"]);
});
test("scroll sends filter and returns matching points", async () => {
let capturedUrl = "";
let capturedBody: Record<string, unknown> | null = null;
globalThis.fetch = mock((url: string | Request, init?: RequestInit) => {
capturedUrl = typeof url === "string" ? url : url.url;
if (init?.body) {
capturedBody = JSON.parse(init.body as string);
}
return Promise.resolve(
new Response(
JSON.stringify({
result: {
points: [{ id: "stale-ep", score: 0, payload: { summary: "old memory" } }],
},
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
),
);
}) as unknown as typeof fetch;
const client = new QdrantClient(TEST_CONFIG);
const results = await client.scroll("episodes", {
filter: { must: [{ key: "user_id", match: { value: "user-1" } }] },
limit: 5,
withPayload: true,
});
expect(capturedUrl).toContain("/collections/episodes/points/scroll");
expect(results).toHaveLength(1);
expect(results[0].id).toBe("stale-ep");
expect((capturedBody as unknown as Record<string, unknown>).limit).toBe(5);
});
});