-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathagent-memory.test.js
More file actions
298 lines (239 loc) · 9.43 KB
/
agent-memory.test.js
File metadata and controls
298 lines (239 loc) · 9.43 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { AgentMemory, MEMORY_TYPES } from '../../src/agent-memory.js';
// ── Globals ───────────────────────────────────────────────────────────────
const localStorageMock = (() => {
let store = {};
return {
getItem: vi.fn((key) => store[key] ?? null),
setItem: vi.fn((key, val) => {
store[key] = val;
}),
removeItem: vi.fn((key) => {
delete store[key];
}),
clear: vi.fn(() => {
store = {};
}),
_reset() {
store = {};
},
_store() {
return store;
},
};
})();
vi.stubGlobal('localStorage', localStorageMock);
const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) });
vi.stubGlobal('fetch', fetchMock);
// ── Reset ─────────────────────────────────────────────────────────────────
beforeEach(() => {
localStorageMock._reset();
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockClear();
fetchMock.mockClear();
});
afterEach(() => {
vi.useRealTimers();
});
// ── Tests ─────────────────────────────────────────────────────────────────
describe('AgentMemory.add()', () => {
it('stores entry in memory and returns a string id', () => {
const mem = new AgentMemory('agent-1');
const id = mem.add({ type: MEMORY_TYPES.USER, content: 'hello' });
expect(typeof id).toBe('string');
expect(id.length).toBeGreaterThan(0);
const results = mem.query();
expect(results).toHaveLength(1);
expect(results[0].id).toBe(id);
expect(results[0].content).toBe('hello');
});
it('persists entry to localStorage immediately', () => {
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.USER, content: 'persist me' });
expect(localStorageMock.setItem).toHaveBeenCalled();
const lastCall = localStorageMock.setItem.mock.calls.at(-1);
const key = lastCall[0];
const stored = JSON.parse(lastCall[1]);
expect(key).toBe('agent_memory_agent-1');
expect(Array.isArray(stored)).toBe(true);
expect(stored[0].content).toBe('persist me');
});
it('schedules a backend sync when backendSync is enabled', () => {
const mem = new AgentMemory('agent-1', { backendSync: true });
// Constructor also fires _hydrateFromBackend — clear before the assertion.
fetchMock.mockClear();
mem.add({ type: MEMORY_TYPES.USER, content: 'sync me' });
expect(fetchMock).toHaveBeenCalledOnce();
const [url, opts] = fetchMock.mock.calls[0];
expect(url).toContain('agent-1');
expect(opts.method).toBe('POST');
});
it('does NOT call fetch when backendSync is false (default)', () => {
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.USER, content: 'no sync' });
expect(fetchMock).not.toHaveBeenCalled();
});
});
describe('AgentMemory.query() — filtering', () => {
it('query({ type }) returns only entries of that type', () => {
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.USER, content: 'user note' });
mem.add({ type: MEMORY_TYPES.PROJECT, content: 'project note' });
mem.add({ type: MEMORY_TYPES.REFERENCE, content: 'ref note' });
const results = mem.query({ type: MEMORY_TYPES.USER });
expect(results).toHaveLength(1);
expect(results[0].content).toBe('user note');
});
it('query({ tags }) returns only entries containing ALL specified tags', () => {
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.USER, content: 'both tags', tags: ['foo', 'bar'] });
mem.add({ type: MEMORY_TYPES.USER, content: 'one tag', tags: ['foo'] });
mem.add({ type: MEMORY_TYPES.USER, content: 'no tags' });
const fooBar = mem.query({ tags: ['foo', 'bar'] });
expect(fooBar).toHaveLength(1);
expect(fooBar[0].content).toBe('both tags');
const fooOnly = mem.query({ tags: ['foo'] });
expect(fooOnly).toHaveLength(2);
});
it('query({ limit }) returns at most limit results', () => {
const mem = new AgentMemory('agent-1');
for (let i = 0; i < 10; i++) {
mem.add({ type: MEMORY_TYPES.PROJECT, content: `entry ${i}` });
}
expect(mem.query({ limit: 3 })).toHaveLength(3);
expect(mem.query({ limit: 1 })).toHaveLength(1);
});
it('query({ since }) excludes entries created before the timestamp', () => {
vi.useFakeTimers();
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.PROJECT, content: 'old' });
vi.advanceTimersByTime(5000);
const cutoff = Date.now();
vi.advanceTimersByTime(1000);
mem.add({ type: MEMORY_TYPES.PROJECT, content: 'new' });
const results = mem.query({ since: cutoff });
expect(results).toHaveLength(1);
expect(results[0].content).toBe('new');
});
it('excludes expired entries (expiresAt in the past)', () => {
vi.useFakeTimers();
const now = Date.now();
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.PROJECT, content: 'active' });
mem.add({
type: MEMORY_TYPES.PROJECT,
content: 'expired',
expiresAt: now - 1000,
});
const results = mem.query();
expect(results).toHaveLength(1);
expect(results[0].content).toBe('active');
});
it('includes entries whose expiresAt is in the future', () => {
vi.useFakeTimers();
const now = Date.now();
const mem = new AgentMemory('agent-1');
mem.add({
type: MEMORY_TYPES.PROJECT,
content: 'not yet expired',
expiresAt: now + 60_000,
});
expect(mem.query()).toHaveLength(1);
});
});
describe('AgentMemory.query() — scoring', () => {
it('sorts entries by salience × recency (highest score first)', () => {
vi.useFakeTimers();
const mem = new AgentMemory('agent-1');
// All added at the same time so recency is identical —
// sort order is determined purely by salience.
// feedback base = 0.8, reference base = 0.5.
mem.add({ type: MEMORY_TYPES.REFERENCE, content: 'low' });
mem.add({ type: MEMORY_TYPES.FEEDBACK, content: 'high' });
mem.add({ type: MEMORY_TYPES.USER, content: 'mid' });
const results = mem.query();
expect(results[0].content).toBe('high'); // feedback: 0.8
expect(results[1].content).toBe('mid'); // user: 0.7
expect(results[2].content).toBe('low'); // reference: 0.5
});
it('entry.important=true gives salience 1.0, appearing before all others', () => {
vi.useFakeTimers();
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.FEEDBACK, content: 'high' });
mem.add({ type: MEMORY_TYPES.PROJECT, content: 'critical', important: true });
const results = mem.query();
expect(results[0].content).toBe('critical');
expect(results[0].salience).toBe(1.0);
});
});
describe('AgentMemory — localStorage hydration', () => {
it('loads existing entries from localStorage on construction', () => {
const existing = [
{
id: 'hydrated-1',
type: MEMORY_TYPES.USER,
content: 'remembered',
tags: [],
context: {},
salience: 0.7,
createdAt: Date.now(),
expiresAt: null,
},
];
localStorageMock.getItem.mockReturnValueOnce(JSON.stringify(existing));
const mem = new AgentMemory('agent-1');
const results = mem.query();
expect(results).toHaveLength(1);
expect(results[0].id).toBe('hydrated-1');
expect(results[0].content).toBe('remembered');
});
it('starts with empty entries when localStorage has nothing', () => {
// default getItem mock returns null
const mem = new AgentMemory('agent-1');
expect(mem.query()).toHaveLength(0);
});
it('starts with empty entries when localStorage contains invalid JSON', () => {
localStorageMock.getItem.mockReturnValueOnce('not-valid-json{{{');
const mem = new AgentMemory('agent-1');
expect(mem.query()).toHaveLength(0);
});
it('uses the agentId-scoped storage key', () => {
new AgentMemory('my-special-agent');
expect(localStorageMock.getItem).toHaveBeenCalledWith('agent_memory_my-special-agent');
});
});
describe('AgentMemory.forget() / clear()', () => {
it('forget(id) removes the entry from query results', () => {
const mem = new AgentMemory('agent-1');
const id = mem.add({ type: MEMORY_TYPES.PROJECT, content: 'delete me' });
mem.add({ type: MEMORY_TYPES.PROJECT, content: 'keep me' });
mem.forget(id);
const results = mem.query();
expect(results).toHaveLength(1);
expect(results[0].content).toBe('keep me');
});
it('forget(id) persists the removal to localStorage', () => {
const mem = new AgentMemory('agent-1');
const id = mem.add({ type: MEMORY_TYPES.PROJECT, content: 'gone' });
localStorageMock.setItem.mockClear();
mem.forget(id);
expect(localStorageMock.setItem).toHaveBeenCalled();
const stored = JSON.parse(localStorageMock.setItem.mock.calls.at(-1)[1]);
expect(stored.find((e) => e.id === id)).toBeUndefined();
});
it('clear(type) removes only entries of that type', () => {
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.USER, content: 'user' });
mem.add({ type: MEMORY_TYPES.PROJECT, content: 'project' });
mem.clear(MEMORY_TYPES.USER);
expect(mem.query()).toHaveLength(1);
expect(mem.query()[0].content).toBe('project');
});
it('clear() with no argument removes all entries', () => {
const mem = new AgentMemory('agent-1');
mem.add({ type: MEMORY_TYPES.USER, content: 'a' });
mem.add({ type: MEMORY_TYPES.PROJECT, content: 'b' });
mem.clear();
expect(mem.query()).toHaveLength(0);
});
});