Files
CyberGril/web/tests/r3memory.test.ts
T

55 lines
4.8 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { memoryDetail, memoryNote, memoryPage, NotFound, NotWired, lifeStatus, type Health } from '../src/adapters/real';
// HEL-832 §5 MemoryDTO exactly as the HEL-833 backend serializes it (episodic.py dto()).
const dto = (id: string, over: Record<string, unknown> = {}) => ({ id, title: '一起听的那场雨', text: '你说下雨天想听她弹琴,她记得你那天很累。', group: '我们一起的', source: '来自 10月23日 21:04 的聊天', lifeIds: [], messageIds: [Number(id) * 2], ts: 1761224640000, ...over });
const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status });
const signal = () => new AbortController().signal;
describe('real memories (GET /api/memories)', () => {
afterEach(() => { vi.unstubAllGlobals(); });
it('maps the contract fields, keeps the server cursor and asks for the next page with before', async () => {
const fetch = vi.fn().mockResolvedValueOnce(json({ memories: [dto('12', { lifeIds: [7] }), dto('9')], next_before: 9, epoch: 3 })).mockResolvedValueOnce(json({ memories: [dto('4')], next_before: null, epoch: 3 }));
vi.stubGlobal('fetch', fetch);
const first = await memoryPage(null, signal());
expect(first.next).toBe('9');
expect(first.memories[0]).toEqual({ id: '12', title: '一起听的那场雨', text: dto('12').text, group: '我们一起的', source: '来自 10月23日 21:04 的聊天', lifeIds: ['7'], messageIds: ['24'] });
const second = await memoryPage(first.next, signal());
expect(second).toMatchObject({ next: null, memories: [{ id: '4' }] });
expect(fetch.mock.calls.map(c => c[0])).toEqual(['/api/memories?limit=30', '/api/memories?limit=30&before=9']);
});
it('drops malformed rows instead of patching them up', async () => {
const bad = [dto('0'), dto('x1'), { ...dto('5'), id: 5 }, dto('6', { title: '' }), dto('7', { text: ' ' }), dto('8', { messageIds: [] }), dto('10', { messageIds: ['3'] }), dto('11', { lifeIds: [-1] }), dto('13', { ts: 'yesterday' }), dto('14', { group: undefined }), null, 'row'];
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(json({ memories: [...bad, dto('20')], next_before: null, epoch: 1 })));
const page = await memoryPage(null, signal());
expect(page.memories.map(m => m.id)).toEqual(['20']);
});
it('an empty list is just empty, a missing route is "not wired", and a failure is a failure', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValueOnce(json({ memories: [], next_before: null, epoch: 1 })).mockResolvedValueOnce(json({ detail: 'Not Found' }, 404)).mockResolvedValueOnce(json({ detail: 'boom' }, 500)).mockRejectedValueOnce(new TypeError('offline')).mockResolvedValueOnce(json({ nope: 1 })));
await expect(memoryPage(null, signal())).resolves.toEqual({ memories: [], next: null });
await expect(memoryPage(null, signal())).rejects.toBeInstanceOf(NotWired);
for (let i = 0; i < 3; i++) {
const e = await memoryPage(null, signal()).catch(x => x);
expect(e).toBeInstanceOf(Error);
expect(e).not.toBeInstanceOf(NotWired);
}
});
it('detail: a hidden, uncompressed or forged id is one neutral 404, malformed ids never reach the server', async () => {
const fetch = vi.fn().mockResolvedValueOnce(json({ memory: dto('12'), epoch: 2 })).mockResolvedValueOnce(json({ detail: '这一条现在打不开。' }, 404)).mockResolvedValueOnce(json({ memory: { ...dto('15'), messageIds: [] }, epoch: 2 })).mockResolvedValueOnce(json({ detail: 'x' }, 503));
vi.stubGlobal('fetch', fetch);
await expect(memoryDetail('12', signal())).resolves.toMatchObject({ id: '12', messageIds: ['24'] });
await expect(memoryDetail('13', signal())).rejects.toBeInstanceOf(NotFound);
await expect(memoryDetail('15', signal())).rejects.toBeInstanceOf(NotFound);
const e = await memoryDetail('16', signal()).catch(x => x);
expect(e).not.toBeInstanceOf(NotFound);
for (const id of ['', '0', '-1', '1e3', '../life/1', '12345678901234567890']) await expect(memoryDetail(id, signal())).rejects.toBeInstanceOf(NotFound);
expect(fetch).toHaveBeenCalledTimes(4);
});
it('the home note only calls memory connected when the server says so', () => {
const base: Health = { epoch: 1, llm: { ok: true, model: 'm' } };
expect(memoryNote(base, lifeStatus(base))).toContain('长期记忆与语音尚未接通');
expect(memoryNote({ ...base, capabilities: { memory: false } }, lifeStatus(base))).toContain('长期记忆与语音尚未接通');
expect(memoryNote({ ...base, capabilities: { memory: true } }, lifeStatus(base))).toBe('资料、聊天与回忆保存在服务器。生活与语音尚未接通。');
});
});