Apply the exact eight-file HEL-875 patch from the visual director on 31970c4. Independently reran 49 frontend tests, typecheck/lint/build and original Python requirement checks. Candidate only; intake found pagination overflow dropping older reach-outs after >250 new records, requiring author repair before approval or deployment.
Co-authored-by: multica-agent <github@multica.ai>
99 lines
7.1 KiB
TypeScript
99 lines
7.1 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { arrivals, fetchAfter, initialSeen, newestId, seenThrough, storeSeen, storedSeen, unreadOf } from '../src/adapters/inbox';
|
|
import { loadMessages, type ServerMessage } from '../src/adapters/real';
|
|
import type { Message } from '../src/domain/model';
|
|
|
|
// An isolated copy of the persisted-message contract (api/app.py GET /api/messages + life/public.py message()):
|
|
// newest first by id, limit+1 probe, rows returned oldest-first, next_before = oldest id on the page when more exist.
|
|
// No model, no embeddings, no network.
|
|
class Archive {
|
|
rows: ServerMessage[] = []; epoch = 1; calls: string[] = []; down = 0;
|
|
add(row: Partial<ServerMessage> & { role: ServerMessage['role'] }) { const id = this.rows.length + 1; this.rows.push({ id, text: `第${id}条`, turn_id: `t${id}`, status: 'sent', lifeIds: [], ...row } as ServerMessage); return id; }
|
|
reach(text = '窗外的雨停了,想跟你说一声。') { return this.add({ role: 'companion', text, proactive: true, turn_id: `reach-${this.rows.length + 1}` }); }
|
|
fetch = vi.fn(async (url: string) => {
|
|
this.calls.push(url);
|
|
if (this.down) { this.down--; throw new TypeError('Failed to fetch'); }
|
|
const u = new URL(url, 'http://x'), limit = Number(u.searchParams.get('limit') || 200), before = Number(u.searchParams.get('before') || Number.MAX_SAFE_INTEGER);
|
|
const desc = this.rows.filter(r => r.id < before).sort((a, b) => b.id - a.id).slice(0, limit + 1), more = desc.length > limit, page = desc.slice(0, limit);
|
|
return new Response(JSON.stringify({ messages: [...page].reverse(), next_before: more ? page[page.length - 1].id : null, epoch: this.epoch }));
|
|
});
|
|
}
|
|
function storage() { const data = new Map<string, string>(); vi.stubGlobal('localStorage', { getItem: (k: string) => data.has(k) ? data.get(k)! : null, setItem: (k: string, v: string) => void data.set(k, v) }); return data; }
|
|
const signal = () => new AbortController().signal;
|
|
// One poll the way RealApp runs it: read after the cursor, keep only what the page does not hold.
|
|
async function poll(page: { messages: Message[]; cursor: number }) {
|
|
const { rows, newest } = await fetchAfter(page.cursor, signal());
|
|
page.cursor = Math.max(page.cursor, newest);
|
|
const add = arrivals(page.messages, rows); page.messages = [...page.messages, ...add];
|
|
return add;
|
|
}
|
|
afterEach(() => vi.unstubAllGlobals());
|
|
|
|
describe('reach-outs reaching an open page', () => {
|
|
it('adds a new reach-out once; repeated reads, a restarted epoch and a reconnect add nothing more', async () => {
|
|
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
|
server.add({ role: 'user', turn_id: 'a' }); server.add({ role: 'companion', turn_id: 'a' });
|
|
const page = { messages: await loadMessages(), cursor: 0 }; page.cursor = newestId(page.messages);
|
|
expect(await poll(page)).toEqual([]);
|
|
const id = server.reach();
|
|
const first = await poll(page);
|
|
expect(first.map(m => [m.id, m.proactive, m.role])).toEqual([[String(id), true, 'companion']]);
|
|
expect(await poll(page)).toEqual([]);
|
|
server.epoch = 2; expect(await poll(page)).toEqual([]);
|
|
server.down = 1; await expect(poll(page)).rejects.toThrow(); expect(await poll(page)).toEqual([]);
|
|
expect(page.messages.filter(m => m.id === String(id))).toHaveLength(1);
|
|
// Reopening reads the same record in the same order: nothing lost, nothing doubled.
|
|
expect((await loadMessages()).map(m => m.id)).toEqual(page.messages.map(m => m.id));
|
|
});
|
|
it('never re-adds your own turn or her reply to it, and ignores rows still being written', async () => {
|
|
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
|
// The page holds your message and her streamed reply under local ids; the server has them under row ids.
|
|
const page: { messages: Message[]; cursor: number } = { messages: [{ id: 'local-1', role: 'user', text: '在吗', lifeIds: [], turnId: 'turn-x', status: 'sent' }, { id: 'local-2', role: 'companion', text: '在', lifeIds: [], turnId: 'turn-x' }], cursor: 0 };
|
|
server.add({ role: 'user', turn_id: 'turn-x', text: '在吗' }); server.add({ role: 'companion', turn_id: 'turn-x', text: '在' });
|
|
server.add({ role: 'user', turn_id: 'turn-y', status: 'pending' });
|
|
server.add({ role: 'companion', proactive: true, turn_id: 'turn-x' });
|
|
expect(await poll(page)).toEqual([]);
|
|
expect(page.cursor).toBe(4);
|
|
});
|
|
it('pages back after a long absence and keeps the order of what arrived', async () => {
|
|
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
|
server.add({ role: 'user' });
|
|
const page = { messages: await loadMessages(), cursor: 1 };
|
|
const ids = [server.reach('一')]; for (let i = 0; i < 70; i++) server.add({ role: 'companion' }); ids.push(server.reach('二'));
|
|
const added = await poll(page);
|
|
expect(added.map(m => m.text)).toEqual(['一', '二']);
|
|
expect(added.map(m => m.id)).toEqual(ids.map(String));
|
|
expect(server.calls.filter(c => c.includes('limit=50'))).toHaveLength(2);
|
|
});
|
|
it('reads from an empty record without paging the whole history', async () => {
|
|
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
|
const page = { messages: [] as Message[], cursor: 0 };
|
|
server.reach();
|
|
expect(await poll(page)).toHaveLength(1);
|
|
expect(server.calls).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe('unread', () => {
|
|
it('first visit from this browser counts the old record as seen; later arrivals stay unread until seen', () => {
|
|
const data = storage();
|
|
const history: Message[] = [{ id: '3', role: 'companion', text: '旧的', lifeIds: [], proactive: true }, { id: '4', role: 'user', text: '嗯', lifeIds: [] }];
|
|
const seen = initialSeen(history);
|
|
expect(seen).toBe(3); expect(unreadOf(history, seen)).toEqual([]); expect(data.get('cg.real.seen')).toBe('3');
|
|
const later = [...history, { id: '9', role: 'companion' as const, text: '新的', lifeIds: [], proactive: true }];
|
|
expect(unreadOf(later, initialSeen(later)).map(m => m.id)).toEqual(['9']);
|
|
storeSeen(seenThrough(later)); expect(unreadOf(later, storedSeen()!)).toEqual([]);
|
|
});
|
|
it('an empty record on first visit leaves a zero mark, so her first reach-out is unread', () => {
|
|
storage();
|
|
expect(initialSeen([])).toBe(0);
|
|
expect(unreadOf([{ id: '1', role: 'companion', text: '你好', lifeIds: [], proactive: true }], initialSeen([]))).toHaveLength(1);
|
|
});
|
|
it('a damaged or unavailable mark falls back to treating the record as history', () => {
|
|
vi.stubGlobal('localStorage', { getItem: () => 'abc', setItem: () => {} });
|
|
expect(initialSeen([{ id: '5', role: 'companion', text: 'x', lifeIds: [], proactive: true }])).toBe(5);
|
|
vi.stubGlobal('localStorage', { getItem: () => { throw new Error('denied'); }, setItem: () => { throw new Error('denied'); } });
|
|
expect(initialSeen([{ id: '6', role: 'companion', text: 'x', lifeIds: [], proactive: true }])).toBe(6);
|
|
});
|
|
});
|