import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readMatrixHistory, taskProof, retainFirstProof, checkRetryUnsent, newFrameLoader, waitForPreviewText, previewCardsMatch, resolveTaskThread, sessionFileObjects, assertSessionIsolation, otherTaskFiles } from './lib/acceptance-evidence.mjs'; const binding = { homeserver: 'https://matrix.invalid', accessToken: 'fixture', userMxId: '@me', agentMxId: '@bot' }; const msg = (id, sender, body, time = 1) => ({ event_id: id, sender, type: 'm.room.message', origin_server_ts: time, content: { body } }); const fixtureFetch = pages => async url => { const page = pages.shift(); assert.ok(page, 'unexpected request'); if (page.from) assert.equal(url.searchParams.get('from'), page.from); return { ok: true, json: async () => page }; }; test('分页读取200条之外的原输入,不能据第一页判零事件', async () => { const pages = [{ chunk: Array.from({ length: 200 }, (_, n) => msg('u' + n, '@other', 'unrelated')), end: 'older' }, { from: 'older', chunk: [msg('original', '@me', 'marker')] }]; const history = await readMatrixHistory(binding, '!room', { fetchImpl: fixtureFetch(pages) }); assert.equal(history.events.length, 201); assert.equal(history.events.at(-1).event_id, 'original'); assert.equal(history.complete, true); }); test('分页重叠的同一事件仅计一次', async () => { const event = msg('same', '@me', 'marker'); const result = await readMatrixHistory(binding, '!room', { fetchImpl: fixtureFetch([{ chunk: [event], end: 'next' }, { from: 'next', chunk: [event] }]) }); assert.equal(result.events.length, 1); }); test('分页超限或令牌循环必须失败,不能当作零事件', async () => { await assert.rejects(() => readMatrixHistory(binding, '!room', { maxPages: 1, fetchImpl: fixtureFetch([{ chunk: [], end: 'next' }]) }), /证据不足/); await assert.rejects(() => readMatrixHistory(binding, '!room', { fetchImpl: fixtureFetch([{ chunk: [msg('x', '@other', '')], end: 'loop' }, { chunk: [msg('y', '@other', '')], end: 'loop' }]) }), /分页不前进/); }); test('空页仍有后续令牌时继续读取', async () => { const result = await readMatrixHistory(binding, '!room', { fetchImpl: fixtureFetch([{ chunk: [], end: 'next' }, { chunk: [msg('old', '@me', 'marker')] }]) }); assert.equal(result.events[0].event_id, 'old'); }); test('真正重复输入不替换已有首次proof', () => { const original = { sent: ['first'], evidenceFile: 'original.json' }, action = { marker: 'marker', proof: original }; assert.throws(() => taskProof([msg('first', '@me', 'marker'), msg('second', '@me', 'marker')], binding, action), /不是唯一/); assert.equal(action.proof, original); }); test('后续证明另存verifications,保留首次proof和sentIds', () => { const original = { sent: ['first'], evidenceFile: 'original.json' }, action = { proof: original, sentIds: ['first'] }; retainFirstProof(action, { sent: ['first'], evidenceFile: 'readback.json' }); assert.equal(action.proof, original); assert.equal(action.verifications[0].evidenceFile, 'readback.json'); assert.deepEqual(action.sentIds, ['first']); }); test('回读锁定已登记线程和房间,不被相同预览文字带到其他房间', () => { const task = { marker: 'marker', threadRoot: '$original', roomId: '!original', proof: { roomId: '!original' } }; const impostor = { threadRoot: '$other', roomId: '!other', lastPreview: 'marker' }; const original = { threadRoot: '$original', roomId: '!original' }; assert.equal(resolveTaskThread([impostor, original], task), original); assert.equal(resolveTaskThread([impostor], task), undefined); assert.throws(() => resolveTaskThread([{ ...original, roomId: '!changed' }], task), /原房间/); assert.throws(() => resolveTaskThread([original, { ...original }], task), /不唯一/); assert.equal(task.roomId, '!original'); }); test('未登记线程时只允许唯一候选;首次proof房间和输入不可被复核替换', () => { assert.throws(() => resolveTaskThread([{ lastPreview: 'marker' }, { lastPreview: 'marker' }], { marker: 'marker' }), /不唯一/); const action = { proof: { sent: ['first'], roomId: '!original' }, sentIds: ['first'] }; assert.throws(() => retainFirstProof(action, { sent: ['other'], roomId: '!original' }), /复核不能更换首次已发送输入/); assert.throws(() => retainFirstProof(action, { sent: ['first'], roomId: '!other' }), /原房间/); assert.equal(action.verifications, undefined); }); test('同URL多来源允许,异URL同名或任一来源归属错误必须失败', () => { const file = { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/a.html' }; assert.equal(sessionFileObjects([file, { ...file, size: 413 }], 'agent', '!room', ['a.html']).length, 1); assert.throws(() => sessionFileObjects([file, { ...file, url: 'https://other.invalid/a.html' }], 'agent', '!room', ['a.html']), /成果必须唯一对应对象URL/); assert.throws(() => sessionFileObjects([file, { ...file, agentId: 'other' }], 'agent', '!room', ['a.html']), /来源行归属/); const legacy = { ...file, agentId: '', url: 'https://files.invalid/deliverables/agent/a.html' }; assert.equal(sessionFileObjects([legacy], 'agent', '!room', ['a.html'])[0].url, legacy.url); assert.throws(() => sessionFileObjects([{ ...legacy, url: 'https://files.invalid/deliverables/other/a.html' }], 'agent', '!room', ['a.html']), /来源行归属/); assert.throws(() => sessionFileObjects([], 'agent', '!room', ['a.html']), /成果必须唯一对应对象URL/); }); test('隔离审计拒绝消息、文档、来源行和未完成分页中的任一污染', () => { const own = { agentId: 'agent', roomId: '!room', marker: 'new-marker', expectedFiles: ['new.md'] }; const other = { roomId: '!old', marker: 'old-marker', expectedFiles: ['old.pdf', 'old.docx'] }; const history = { complete: true, events: [msg('one', '@me', 'new-marker')] }; const files = [{ agentId: 'agent', roomId: '!room', url: 'https://files.invalid/new.md' }]; assert.doesNotThrow(() => assertSessionIsolation(history, files, own, [other])); assert.throws(() => assertSessionIsolation({ ...history, complete: false }, files, own, [other]), /分页/); assert.throws(() => assertSessionIsolation({ ...history, events: [...history.events, msg('polluted', '@me', 'old-marker')] }, files, own, [other]), /其他容器消息/); assert.throws(() => assertSessionIsolation(history, [...files, { ...files[0], url: 'https://files.invalid/old.docx' }], own, [other]), /其他容器成果/); assert.throws(() => assertSessionIsolation(history, [{ ...files[0], roomId: '!old' }], own, [other]), /成果列表中存在其他容器/); }); test('隔离审计允许同容器同房间的历史任务成果复用', () => { const own = { agentId: 'agent', roomId: '!room', marker: 'current-marker', expectedFiles: ['current.md'] }; const previous = { agentId: 'agent', roomId: '!room', marker: 'previous-marker', expectedFiles: ['previous.md'] }; const history = { complete: true, events: [msg('current', '@me', 'current-marker'), msg('previous', '@agent', 'previous-marker')] }; const files = [ { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/current.md' }, { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/previous.md' }, ]; assert.doesNotThrow(() => assertSessionIsolation(history, files, own, [previous])); }); test('隔离审计允许同容器成果按当前房间登记后的跨房间复用', () => { const own = { agentId: 'agent', roomId: '!room', marker: 'current-marker', expectedFiles: ['current.md'] }; const reused = { agentId: 'agent', roomId: '!other-room', marker: 'reuse-marker', expectedFiles: ['reused.md'] }; const history = { complete: true, events: [msg('current', '@me', 'current-marker')] }; const files = [ { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/current.md' }, { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/reused.md' }, ]; assert.doesNotThrow(() => assertSessionIsolation(history, files, own, [reused])); }); test('隔离审计仍拒绝同房间但属于另一容器的成果', () => { const own = { agentId: 'agent', roomId: '!room', marker: 'current-marker', expectedFiles: ['current.md'] }; const other = { agentId: 'other-agent', roomId: '!room', marker: 'other-marker', expectedFiles: ['other.md'] }; const history = { complete: true, events: [msg('current', '@me', 'current-marker')] }; const files = [ { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/current.md' }, { agentId: 'other-agent', roomId: '!room', url: 'https://files.invalid/other.md' }, ]; assert.throws(() => assertSessionIsolation(history, files, own, [other]), /成果列表中存在其他容器/); }); test('收集其他任务成果时按稳定marker排除当前任务副本', () => { const own = { agentId: 'agent', marker: 'own', expectedFiles: ['own.pdf'] }; const tasks = [ { ...own }, { ...own, marker: 'same-container', expectedFiles: ['same-container.pdf'] }, { agentId: 'other-agent', marker: 'other', expectedFiles: ['other.docx'] }, ]; assert.deepEqual(otherTaskFiles(tasks, own), ['other.docx']); }); test('错误发送者的回复和结束信号不能充当证明', () => { assert.equal(taskProof([msg('one', '@me', 'marker'), msg('fake', '@other', 'marker', 2)], binding, { marker: 'marker' }), null); }); test('等待重连期间原事件落服,紧邻重试的回读必须拦截', async () => { const events = [], calls = []; await assert.rejects(() => checkRetryUnsent(async () => { calls.push('wait'); events.push(msg('arrived', '@me', 'marker')); }, async () => { calls.push('read'); return { complete: true, events }; }, binding, 'marker'), /禁止再重试/); assert.deepEqual(calls, ['wait', 'read']); }); test('重试前不完整事件范围必须拦截,即使当前页没有输入', async () => { await assert.rejects(() => checkRetryUnsent(async () => {}, async () => ({ complete: false, events: [] }), binding, 'marker'), /范围不完整/); }); test('HTML同一旧loader不能通过刷新,新loader才通过', () => { assert.equal(newFrameLoader({ loaderId: 'old' }, { loaderId: 'old' }), false); assert.equal(newFrameLoader({ loaderId: 'old' }, { loaderId: 'new' }), true); assert.equal(newFrameLoader({}, { loaderId: 'new' }), false); }); test('服务端归属正常但A界面残留B成果卡片仍须失败', () => { assert.equal(previewCardsMatch(['a.md', 'b.html'], ['预览 a.md'], false), false); assert.equal(previewCardsMatch(['a.md', 'b.html'], [], false), true); assert.equal(previewCardsMatch(['a.md', 'b.html'], ['预览 a.md'], true), false); }); test('新loader正文首次为空、随后加载完成时允许通过', async () => { let n = 0; assert.equal(await waitForPreviewText(async () => ++n === 1 ? '' : 'real marker', 'marker', { attempts: 2, pause: async () => {} }), 'real marker'); assert.equal(n, 2); }); test('新loader上下文暂不可用可恢复,但持续错误或错误正文失败', async () => { let n = 0; await waitForPreviewText(async () => { if (++n === 1) throw Error('context unavailable'); return 'marker'; }, 'marker', { attempts: 2, pause: async () => {} }); await assert.rejects(() => waitForPreviewText(async () => 'wrong document', 'marker', { attempts: 3, pause: async () => {} }), /限时内未出现/); await assert.rejects(() => waitForPreviewText(async () => { throw Error('always unavailable'); }, 'marker', { attempts: 2, pause: async () => {} }), /限时内未出现/); });