tests/fixtures/fake-memstate.js (2141 bytes)
1 // A stand-in for the memstated daemon: the three read routes that the Memory tab uses. It records 2 // every request, so that a test can check that the dashboard never writes to memstate. 3 4 import http from 'node:http'; 5 6 export const MEMORIES = [ 7 { id: 7, project_id: 'ideamine', keypath: 'task.summary.2026_09_21', content: 'Built the dashboard.', category: 'status', version: 2, created_at: 1790000000 }, 8 { id: 3, project_id: 'ideamine', keypath: 'decisions.sync', content: 'The server holds the archive.', category: 'decision', version: 1, created_at: 1789990000 }, 9 ]; 10 11 export async function startFakeMemstate() { 12 const requests = []; 13 const server = http.createServer((req, res) => { 14 let body = ''; 15 req.on('data', (d) => (body += d)); 16 req.on('end', () => { 17 requests.push(`${req.method} ${req.url}`); 18 const json = (status, data) => res.writeHead(status, { 'content-type': 'application/json' }).end(JSON.stringify(data)); 19 const input = body ? JSON.parse(body) : {}; 20 if (req.method === 'GET' && req.url === '/api/v1/projects') { 21 return json(200, { projects: [{ id: 'ideamine', created_at: 1789900000, memory_count: 2, last_updated_at: 1790000000 }] }); 22 } 23 if (req.method === 'POST' && req.url === '/api/v1/keypaths') { 24 const list = MEMORIES.filter((m) => m.project_id === input.project_id).map((m) => ({ ...m, content: input.include_content ? m.content : '' })); 25 return json(200, { memories: list, total_count: list.length }); 26 } 27 if (req.method === 'POST' && req.url === '/api/v1/memories/history') { 28 const versions = [ 29 { ...MEMORIES[0], version: 1, content: 'Started the dashboard.', created_at: 1789950000 }, 30 MEMORIES[0], 31 ].filter((m) => m.keypath === input.keypath); 32 return json(200, { versions, total_versions: versions.length }); 33 } 34 json(404, { error: 'not found' }); 35 }); 36 }); 37 await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); 38 return { url: `http://127.0.0.1:${server.address().port}`, requests, close: () => new Promise((resolve) => server.close(resolve)) }; 39 }