forked from joseairosa/recall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-time-window.js
More file actions
executable file
·333 lines (277 loc) · 9.17 KB
/
Copy pathtest-time-window.js
File metadata and controls
executable file
·333 lines (277 loc) · 9.17 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env node
/**
* Test script for time window context retrieval (v1.6.0)
*/
import Redis from 'ioredis';
import { ulid } from 'ulid';
// Initialize Redis and create simplified store for testing
const redis = new Redis({
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379'),
db: 15, // Use test database
});
// Simplified MemoryStore mock for testing
class TestMemoryStore {
constructor() {
this.workspaceId = 'test-workspace';
}
async createMemory(data) {
const id = ulid();
const timestamp = Date.now();
const memory = {
id,
timestamp: timestamp.toString(),
context_type: data.context_type,
content: data.content,
importance: data.importance.toString(),
tags: JSON.stringify(data.tags || []),
workspace_id: this.workspaceId,
is_global: 'false',
};
// Store in Redis (hset expects flat key-value pairs)
const flatData = Object.entries(memory).flat();
await redis.hset(`ws:${this.workspaceId}:memory:${id}`, ...flatData);
await redis.zadd(`ws:${this.workspaceId}:memories:timeline`, timestamp, id);
return {
id,
timestamp,
context_type: data.context_type,
content: data.content,
importance: data.importance,
tags: data.tags || [],
workspace_id: this.workspaceId,
is_global: false,
};
}
async getMemoriesByTimeWindow(startTime, endTime, minImportance, contextTypes) {
// Get IDs in time range
const ids = await redis.zrangebyscore(
`ws:${this.workspaceId}:memories:timeline`,
startTime,
endTime
);
// Retrieve memories
const memories = [];
for (const id of ids) {
const data = await redis.hgetall(`ws:${this.workspaceId}:memory:${id}`);
if (Object.keys(data).length > 0) {
// Convert string values back to correct types
const memory = {
...data,
timestamp: parseInt(data.timestamp),
importance: parseInt(data.importance),
tags: data.tags ? JSON.parse(data.tags) : [],
is_global: data.is_global === 'true',
};
memories.push(memory);
}
}
// Filter by importance
let filtered = memories;
if (minImportance !== undefined) {
filtered = filtered.filter(m => m.importance >= minImportance);
}
// Filter by context types
if (contextTypes && contextTypes.length > 0) {
filtered = filtered.filter(m => contextTypes.includes(m.context_type));
}
// Sort chronologically
filtered.sort((a, b) => a.timestamp - b.timestamp);
return filtered;
}
async deleteMemory(id) {
await redis.del(`ws:${this.workspaceId}:memory:${id}`);
await redis.zrem(`ws:${this.workspaceId}:memories:timeline`, id);
}
}
const MemoryStore = TestMemoryStore;
// Color output helpers
const colors = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
blue: '\x1b[34m',
yellow: '\x1b[33m',
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
let passed = 0;
let failed = 0;
async function runTests() {
log('\n=== Testing Time Window Context Retrieval (v1.6.0) ===\n', 'blue');
const store = new MemoryStore();
try {
// Test 1: Create test memories with timestamps
log('Test 1: Creating test memories across different time periods', 'blue');
const now = Date.now();
const oneHourAgo = now - (60 * 60 * 1000);
const twoHoursAgo = now - (2 * 60 * 60 * 1000);
const thirtyMinsAgo = now - (30 * 60 * 1000);
// Create memories
const memory1 = await store.createMemory({
content: 'Memory from 2 hours ago - decided to use PostgreSQL',
context_type: 'decision',
importance: 8,
tags: ['database'],
});
const memory2 = await store.createMemory({
content: 'Memory from 1 hour ago - implemented caching layer',
context_type: 'code_pattern',
importance: 7,
tags: ['performance'],
});
const memory3 = await store.createMemory({
content: 'Memory from 30 mins ago - fixed authentication bug',
context_type: 'error',
importance: 9,
tags: ['security', 'bug-fix'],
});
log('✓ Created 3 test memories', 'green');
passed++;
// Test 2: Get memories from last hour (should get 2 recent ones)
log('\nTest 2: Retrieve memories from last hour', 'blue');
const lastHourMemories = await store.getMemoriesByTimeWindow(
oneHourAgo,
now + 1000 // Add buffer
);
if (lastHourMemories.length >= 2) {
log(`✓ Retrieved ${lastHourMemories.length} memories from last hour`, 'green');
passed++;
} else {
log(`✗ Expected at least 2 memories, got ${lastHourMemories.length}`, 'red');
failed++;
}
// Test 3: Filter by importance
log('\nTest 3: Filter by minimum importance (>= 8)', 'blue');
const highImportanceMemories = await store.getMemoriesByTimeWindow(
twoHoursAgo,
now + 1000,
8 // min importance
);
const hasHighImportance = highImportanceMemories.every(m => m.importance >= 8);
if (hasHighImportance && highImportanceMemories.length >= 1) {
log(`✓ Retrieved ${highImportanceMemories.length} high-importance memories`, 'green');
passed++;
} else {
log(`✗ Importance filtering failed`, 'red');
failed++;
}
// Test 4: Filter by context type
log('\nTest 4: Filter by context type (decision)', 'blue');
const decisionMemories = await store.getMemoriesByTimeWindow(
twoHoursAgo,
now + 1000,
undefined,
['decision']
);
const allDecisions = decisionMemories.every(m => m.context_type === 'decision');
if (allDecisions && decisionMemories.length >= 1) {
log(`✓ Retrieved ${decisionMemories.length} decision memories`, 'green');
passed++;
} else {
log(`✗ Context type filtering failed`, 'red');
failed++;
}
// Test 5: Chronological ordering
log('\nTest 5: Verify chronological ordering', 'blue');
const allMemories = await store.getMemoriesByTimeWindow(
twoHoursAgo,
now + 1000
);
let isChronological = true;
for (let i = 1; i < allMemories.length; i++) {
if (allMemories[i].timestamp < allMemories[i - 1].timestamp) {
isChronological = false;
break;
}
}
if (isChronological) {
log('✓ Memories are in chronological order (oldest first)', 'green');
passed++;
} else {
log('✗ Memories not in chronological order', 'red');
failed++;
}
// Test 6: Empty time window
log('\nTest 6: Empty time window (no memories)', 'blue');
const futureStart = now + (24 * 60 * 60 * 1000); // Tomorrow
const futureEnd = now + (48 * 60 * 60 * 1000); // Day after
const futureMemories = await store.getMemoriesByTimeWindow(
futureStart,
futureEnd
);
if (futureMemories.length === 0) {
log('✓ Empty time window returns no memories', 'green');
passed++;
} else {
log(`✗ Expected 0 memories, got ${futureMemories.length}`, 'red');
failed++;
}
// Test 7: Multiple context types
log('\nTest 7: Filter by multiple context types', 'blue');
const multiTypeMemories = await store.getMemoriesByTimeWindow(
twoHoursAgo,
now + 1000,
undefined,
['decision', 'error']
);
const validTypes = multiTypeMemories.every(m =>
m.context_type === 'decision' || m.context_type === 'error'
);
if (validTypes && multiTypeMemories.length >= 2) {
log(`✓ Retrieved ${multiTypeMemories.length} memories with multiple types`, 'green');
passed++;
} else {
log('✗ Multiple context type filtering failed', 'red');
failed++;
}
// Test 8: Combined filters (importance + type)
log('\nTest 8: Combined filters (importance >= 8, type = error)', 'blue');
const combinedFilterMemories = await store.getMemoriesByTimeWindow(
twoHoursAgo,
now + 1000,
8,
['error']
);
const validCombined = combinedFilterMemories.every(m =>
m.importance >= 8 && m.context_type === 'error'
);
if (validCombined) {
log(`✓ Combined filtering works (${combinedFilterMemories.length} memories)`, 'green');
passed++;
} else {
log('✗ Combined filtering failed', 'red');
failed++;
}
// Cleanup
log('\nCleaning up test memories...', 'blue');
await store.deleteMemory(memory1.id);
await store.deleteMemory(memory2.id);
await store.deleteMemory(memory3.id);
log('✓ Cleanup complete', 'green');
} catch (error) {
log(`\n✗ Test failed with error: ${error.message}`, 'red');
log(error.stack, 'red');
failed++;
}
// Summary
log('\n' + '='.repeat(50), 'blue');
log(`\nTest Results:`, 'blue');
log(` Passed: ${passed}`, 'green');
log(` Failed: ${failed}`, failed > 0 ? 'red' : 'green');
log(` Total: ${passed + failed}\n`, 'blue');
if (failed === 0) {
log('✓ All tests passed!', 'green');
process.exit(0);
} else {
log('✗ Some tests failed', 'red');
process.exit(1);
}
}
// Run tests
runTests().catch(error => {
log(`Fatal error: ${error.message}`, 'red');
log(error.stack, 'red');
process.exit(1);
});