Repository navigation
Expand file tree
/
Copy pathmemory-store.test.ts
More file actions
219 lines (197 loc) · 7.24 KB
/
Copy pathmemory-store.test.ts
File metadata and controls
219 lines (197 loc) · 7.24 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
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
MEMORY_CONTENT_MAX_LENGTH,
MEMORY_MAX_TAGS,
MEMORY_TAG_MAX_LENGTH,
MemoryStore,
MemoryValidationError,
} from "./memory-store.js";
describe("MemoryStore", () => {
let tmp: string;
let store: MemoryStore;
beforeEach(() => {
tmp = mkdtempSync(join(tmpdir(), "atomic-agent-notes-"));
store = new MemoryStore({
dbFile: join(tmp, "memory.sqlite"),
maxEntries: 1000,
});
});
afterEach(() => {
store.close();
rmSync(tmp, { recursive: true, force: true });
});
it("stores and reads back an entry", () => {
const entry = store.store(
{ content: "the user prefers concise answers", tags: ["style"] },
1_000,
);
expect(entry.id).toBeGreaterThan(0);
expect(entry.createdAt).toBe(1_000);
expect(entry.updatedAt).toBe(1_000);
expect(entry.source).toBe("agent");
expect(entry.tags).toEqual(["style"]);
const fetched = store.get(entry.id);
expect(fetched?.content).toBe("the user prefers concise answers");
});
it("deduplicates tags on input", () => {
const entry = store.store(
{ content: "dedup tags", tags: ["a", "a", "b"] },
1_000,
);
expect(entry.tags).toEqual(["a", "b"]);
});
it("recall ranks exact-phrase matches above unrelated rows", () => {
store.store({ content: "the cat sat on the mat" }, 1);
store.store({ content: "unrelated trivia about dogs and fish" }, 2);
store.store({ content: "cats love warm spots" }, 3);
const hits = store.recall("cat", { k: 3 });
expect(hits.length).toBeGreaterThanOrEqual(1);
expect(hits[0]?.content).toMatch(/cat/);
});
it("recall returns empty on empty query", () => {
store.store({ content: "hello world" }, 1);
expect(store.recall(" ")).toEqual([]);
expect(store.recall("")).toEqual([]);
});
it("recall tokenises unicode letters", () => {
store.store({ content: "пользователь предпочитает ёжиков" }, 1);
const hits = store.recall("ёжик");
expect(hits.length).toBe(1);
});
it("scope=project filters by working_dir", () => {
store.store({ content: "project-local detail", workingDir: "/repos/a" }, 1);
store.store({ content: "other project detail", workingDir: "/repos/b" }, 2);
const a = store.recall("detail", {
scope: "project",
workingDir: "/repos/a",
});
expect(a.map((e) => e.workingDir)).toEqual(["/repos/a"]);
const b = store.recall("detail", {
scope: "project",
workingDir: "/repos/b",
});
expect(b.map((e) => e.workingDir)).toEqual(["/repos/b"]);
});
it("scope=project returns empty when workingDir is missing", () => {
store.store({ content: "foo", workingDir: "/anywhere" }, 1);
const hits = store.recall("foo", { scope: "project", workingDir: null });
expect(hits).toEqual([]);
});
it("tag filter applies AND semantics across required tags", () => {
store.store({ content: "one", tags: ["a"] }, 1);
store.store({ content: "two", tags: ["a", "b"] }, 2);
store.store({ content: "three", tags: ["b"] }, 3);
const withA = store.recall("one OR two OR three", { tags: ["a"] });
expect(withA.map((e) => e.content).sort()).toEqual(["one", "two"]);
const withAB = store.recall("one OR two OR three", {
tags: ["a", "b"],
});
expect(withAB.map((e) => e.content)).toEqual(["two"]);
});
it("remove deletes the row and mirrors into FTS5", () => {
const entry = store.store({ content: "ephemeral fact" }, 1);
expect(store.remove(entry.id)).toBe(true);
expect(store.get(entry.id)).toBeNull();
expect(store.recall("ephemeral")).toEqual([]);
expect(store.remove(entry.id)).toBe(false);
});
it("eviction trims oldest rows when over maxEntries", () => {
const small = new MemoryStore({
dbFile: join(tmp, "small.sqlite"),
maxEntries: 3,
});
try {
const first = small.store({ content: "a" }, 1);
small.store({ content: "b" }, 2);
small.store({ content: "c" }, 3);
expect(small.count()).toBe(3);
small.store({ content: "d" }, 4);
expect(small.count()).toBe(3);
expect(small.get(first.id)).toBeNull();
} finally {
small.close();
}
});
it("list orders by updated_at DESC", () => {
store.store({ content: "first" }, 1_000);
store.store({ content: "second" }, 2_000);
store.store({ content: "third" }, 3_000);
const all = store.list();
expect(all.map((e) => e.content)).toEqual(["third", "second", "first"]);
});
it("listIndex returns compact pointer rows in updated_at DESC order", () => {
const a = store.store({ content: "first note", tags: ["x"] }, 1_000);
store.store({ content: "second note body here" }, 2_000);
const index = store.listIndex();
expect(index.map((e) => e.id)).toEqual([expect.any(Number), a.id]);
expect(index[0]?.preview).toBe("second note body here");
expect(index[1]?.tags).toEqual(["x"]);
});
it("listIndex clips preview to previewChars with an ellipsis", () => {
store.store(
{ content: "a very long single-line note with plenty of padding text" },
1,
);
const [entry] = store.listIndex({ previewChars: 10 });
expect(entry?.preview.length).toBe(10);
expect(entry?.preview.endsWith("…")).toBe(true);
});
it("listIndex picks the first non-empty line from multi-line content", () => {
store.store({ content: "\n\nfirst meaningful line\nsecond line" }, 1);
const [entry] = store.listIndex();
expect(entry?.preview).toBe("first meaningful line");
});
it("rejects invalid content", () => {
expect(() => store.store({ content: "" })).toThrow(MemoryValidationError);
expect(() => store.store({ content: " " })).toThrow(
MemoryValidationError,
);
expect(() =>
store.store({ content: "x".repeat(MEMORY_CONTENT_MAX_LENGTH + 1) }),
).toThrow(MemoryValidationError);
});
it("rejects invalid tags", () => {
expect(() => store.store({ content: "ok", tags: ["good", ""] })).toThrow(
MemoryValidationError,
);
expect(() =>
store.store({
content: "ok",
tags: ["a".repeat(MEMORY_TAG_MAX_LENGTH + 1)],
}),
).toThrow(MemoryValidationError);
const tooMany = Array.from(
{ length: MEMORY_MAX_TAGS + 1 },
(_, i) => `t${i}`,
);
expect(() => store.store({ content: "ok", tags: tooMany })).toThrow(
MemoryValidationError,
);
});
it("rejects non-positive ids in get/remove", () => {
expect(() => store.get(0)).toThrow(MemoryValidationError);
expect(() => store.remove(-1)).toThrow(MemoryValidationError);
});
it("survives reopen — durable across store instances", () => {
store.store({ content: "durable note", tags: ["persist"] }, 1);
store.close();
const reopened = new MemoryStore({
dbFile: join(tmp, "memory.sqlite"),
maxEntries: 1000,
});
try {
const hits = reopened.recall("durable");
expect(hits.length).toBe(1);
expect(hits[0]?.tags).toEqual(["persist"]);
} finally {
reopened.close();
}
store = new MemoryStore({
dbFile: join(tmp, "memory.sqlite"),
maxEntries: 1000,
});
});
});