HEL-880: preserve inbox pagination gaps until reach-outs are delivered
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
+99
-10
@@ -1,5 +1,5 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { arrivals, fetchAfter, initialSeen, newestId, seenThrough, storeSeen, storedSeen, unreadOf } from '../src/adapters/inbox';
|
||||
import { arrivals, fetchAfter, initialSeen, newestId, seenThrough, startAt, storeSeen, storedSeen, unreadOf, type InboxCursor } from '../src/adapters/inbox';
|
||||
import { loadMessages, type ServerMessage } from '../src/adapters/real';
|
||||
import type { Message } from '../src/domain/model';
|
||||
|
||||
@@ -20,20 +20,29 @@ class Archive {
|
||||
}
|
||||
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);
|
||||
// One poll the way RealApp runs it: read after the cursor, commit it only when the read succeeded, keep only what the
|
||||
// page does not hold.
|
||||
type Page = { messages: Message[]; cursor: InboxCursor };
|
||||
async function poll(page: Page) {
|
||||
const { rows, cursor } = await fetchAfter(page.cursor, signal());
|
||||
page.cursor = cursor;
|
||||
const add = arrivals(page.messages, rows); page.messages = [...page.messages, ...add];
|
||||
return add;
|
||||
}
|
||||
// Polls again while a gap is open, as RealApp does a second later; returns everything that arrived, in order.
|
||||
async function catchUp(page: Page, most = 50) {
|
||||
const all: Message[] = [];
|
||||
for (let i = 0; i < most; i++) { all.push(...await poll(page)); if (!page.cursor.gap) return all; }
|
||||
throw new Error('never caught up');
|
||||
}
|
||||
const opened = (messages: Message[]): Page => ({ messages, cursor: startAt(newestId(messages)) });
|
||||
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);
|
||||
const page = opened(await loadMessages());
|
||||
expect(await poll(page)).toEqual([]);
|
||||
const id = server.reach();
|
||||
const first = await poll(page);
|
||||
@@ -48,17 +57,17 @@ describe('reach-outs reaching an open page', () => {
|
||||
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 };
|
||||
const page = opened([{ id: 'local-1', role: 'user', text: '在吗', lifeIds: [], turnId: 'turn-x', status: 'sent' }, { id: 'local-2', role: 'companion', text: '在', lifeIds: [], turnId: 'turn-x' }]);
|
||||
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);
|
||||
expect(page.cursor).toEqual(startAt(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 page = opened(await loadMessages());
|
||||
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(['一', '二']);
|
||||
@@ -67,13 +76,93 @@ describe('reach-outs reaching an open page', () => {
|
||||
});
|
||||
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 };
|
||||
const page = opened([]);
|
||||
server.reach();
|
||||
expect(await poll(page)).toHaveLength(1);
|
||||
expect(server.calls).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
// HEL-880: a sleeping tab can come back to more rows than one bounded read covers (5 pages × 50).
|
||||
describe('catching up after a long absence', () => {
|
||||
it('keeps the oldest reach-out when more than five pages arrived (HEL-875 review fixture)', async () => {
|
||||
// Exactly the reviewer's case: the page holds id 1; id 2 is her reach-out; plain rows follow up to id 301.
|
||||
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
||||
server.add({ role: 'user' });
|
||||
const page = opened(await loadMessages());
|
||||
server.reach('最早的那句'); while (server.rows.length < 301) server.add({ role: 'companion' });
|
||||
const first = await fetchAfter(page.cursor, signal());
|
||||
expect(first.rows).toEqual([]);
|
||||
expect(first.cursor).toMatchObject({ through: 1, gap: { before: 52, top: 301 } });
|
||||
const second = await fetchAfter(first.cursor, signal());
|
||||
expect(arrivals(page.messages, [...first.rows, ...second.rows]).map(m => m.id)).toEqual(['2']);
|
||||
expect(second.cursor).toEqual(startAt(301));
|
||||
});
|
||||
it('more than 250 rows with reach-outs spread through them: each arrives once, in order, nothing skipped', async () => {
|
||||
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
||||
server.add({ role: 'user' });
|
||||
const page = opened(await loadMessages()), ids: number[] = [];
|
||||
for (let i = 0; i < 1200; i++) { if (i % 97 === 3) ids.push(server.reach(`第${i}句`)); else server.add({ role: i % 2 ? 'companion' : 'user' }); }
|
||||
const added = await catchUp(page);
|
||||
expect(added.map(m => m.id)).toEqual(ids.map(String));
|
||||
expect(page.cursor).toEqual(startAt(server.rows.length));
|
||||
expect(await poll(page)).toEqual([]);
|
||||
expect(page.messages.filter(m => m.proactive)).toHaveLength(ids.length);
|
||||
});
|
||||
it('an empty page (cursor 0) facing more than 50 rows reads past the first page', async () => {
|
||||
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
||||
const page = opened([]);
|
||||
const ids = [server.reach('一')]; for (let i = 0; i < 120; i++) server.add({ role: 'user' }); ids.push(server.reach('二'));
|
||||
expect((await catchUp(page)).map(m => m.id)).toEqual(ids.map(String));
|
||||
expect(server.calls).toHaveLength(3);
|
||||
});
|
||||
it('a read that fails mid-way commits nothing; the next one resumes from the same place', async () => {
|
||||
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
||||
server.add({ role: 'user' });
|
||||
const page = opened(await loadMessages());
|
||||
const ids = [server.reach('一')]; for (let i = 0; i < 400; i++) server.add({ role: 'user' }); ids.push(server.reach('二'));
|
||||
expect(await poll(page)).toEqual([]);
|
||||
const kept = structuredClone(page.cursor);
|
||||
// Two pages succeed, the third fails: the call throws and the page keeps the cursor it had.
|
||||
let left = 2; const ok = server.fetch.getMockImplementation()!;
|
||||
server.fetch.mockImplementation(async (url: string) => { if (left-- <= 0) throw new TypeError('Failed to fetch'); return ok(url); });
|
||||
await expect(poll(page)).rejects.toThrow();
|
||||
expect(page.cursor).toEqual(kept);
|
||||
server.fetch.mockImplementation(ok);
|
||||
expect((await catchUp(page)).map(m => m.id)).toEqual(ids.map(String));
|
||||
});
|
||||
it('rows that arrive while it is catching up are read too, after the older ones, once', async () => {
|
||||
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
||||
server.add({ role: 'user' });
|
||||
const page = opened(await loadMessages());
|
||||
const ids = [server.reach('久别前')]; for (let i = 0; i < 600; i++) server.add({ role: 'companion' });
|
||||
expect(await poll(page)).toEqual([]);
|
||||
ids.push(server.reach('追读时新来的')); for (let i = 0; i < 300; i++) server.add({ role: 'user' }); ids.push(server.reach('最新的'));
|
||||
const added = await catchUp(page);
|
||||
expect(added.map(m => m.id)).toEqual(ids.map(String));
|
||||
expect(page.cursor).toEqual(startAt(server.rows.length));
|
||||
});
|
||||
it('repeated reads and a restarted epoch during catch-up add nothing twice', async () => {
|
||||
const server = new Archive(); vi.stubGlobal('fetch', server.fetch);
|
||||
server.add({ role: 'user' });
|
||||
const page = opened(await loadMessages());
|
||||
const ids = [server.reach()]; for (let i = 0; i < 300; i++) server.add({ role: 'user' }); ids.push(server.reach());
|
||||
const first = await fetchAfter(page.cursor, signal());
|
||||
// The same cursor read twice (a retried poll) gives the same answer; the epoch changing does not matter.
|
||||
server.epoch = 2;
|
||||
const again = await fetchAfter(page.cursor, signal());
|
||||
expect(again.cursor).toEqual(first.cursor);
|
||||
page.cursor = first.cursor;
|
||||
const added = await catchUp(page);
|
||||
expect(arrivals(added, added)).toEqual([]);
|
||||
expect(added.map(m => m.id)).toEqual(ids.map(String));
|
||||
expect(await poll(page)).toEqual([]);
|
||||
// A reach-out the page already holds (it came in through a reload) is not added again.
|
||||
const record = await loadMessages(), page2: Page = { messages: [record[0], record.find(m => m.id === String(ids[1]))!], cursor: startAt(1) };
|
||||
expect((await catchUp(page2)).map(m => m.id)).toEqual([String(ids[0])]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('unread', () => {
|
||||
it('first visit from this browser counts the old record as seen; later arrivals stay unread until seen', () => {
|
||||
const data = storage();
|
||||
|
||||
Reference in New Issue
Block a user