test-acceptance-evidence.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { readMatrixHistory, taskProof, retainFirstProof, checkRetryUnsent, newFrameLoader, waitForPreviewText, previewCardsMatch, resolveTaskThread, sessionFileObjects, assertSessionIsolation, otherTaskFiles } from './lib/acceptance-evidence.mjs';
  4. const binding = { homeserver: 'https://matrix.invalid', accessToken: 'fixture', userMxId: '@me', agentMxId: '@bot' };
  5. const msg = (id, sender, body, time = 1) => ({ event_id: id, sender, type: 'm.room.message', origin_server_ts: time, content: { body } });
  6. const fixtureFetch = pages => async url => {
  7. const page = pages.shift(); assert.ok(page, 'unexpected request');
  8. if (page.from) assert.equal(url.searchParams.get('from'), page.from);
  9. return { ok: true, json: async () => page };
  10. };
  11. test('分页读取200条之外的原输入,不能据第一页判零事件', async () => {
  12. const pages = [{ chunk: Array.from({ length: 200 }, (_, n) => msg('u' + n, '@other', 'unrelated')), end: 'older' }, { from: 'older', chunk: [msg('original', '@me', 'marker')] }];
  13. const history = await readMatrixHistory(binding, '!room', { fetchImpl: fixtureFetch(pages) });
  14. assert.equal(history.events.length, 201); assert.equal(history.events.at(-1).event_id, 'original'); assert.equal(history.complete, true);
  15. });
  16. test('分页重叠的同一事件仅计一次', async () => {
  17. const event = msg('same', '@me', 'marker');
  18. const result = await readMatrixHistory(binding, '!room', { fetchImpl: fixtureFetch([{ chunk: [event], end: 'next' }, { from: 'next', chunk: [event] }]) });
  19. assert.equal(result.events.length, 1);
  20. });
  21. test('分页超限或令牌循环必须失败,不能当作零事件', async () => {
  22. await assert.rejects(() => readMatrixHistory(binding, '!room', { maxPages: 1, fetchImpl: fixtureFetch([{ chunk: [], end: 'next' }]) }), /证据不足/);
  23. await assert.rejects(() => readMatrixHistory(binding, '!room', { fetchImpl: fixtureFetch([{ chunk: [msg('x', '@other', '')], end: 'loop' }, { chunk: [msg('y', '@other', '')], end: 'loop' }]) }), /分页不前进/);
  24. });
  25. test('空页仍有后续令牌时继续读取', async () => {
  26. const result = await readMatrixHistory(binding, '!room', { fetchImpl: fixtureFetch([{ chunk: [], end: 'next' }, { chunk: [msg('old', '@me', 'marker')] }]) });
  27. assert.equal(result.events[0].event_id, 'old');
  28. });
  29. test('真正重复输入不替换已有首次proof', () => {
  30. const original = { sent: ['first'], evidenceFile: 'original.json' }, action = { marker: 'marker', proof: original };
  31. assert.throws(() => taskProof([msg('first', '@me', 'marker'), msg('second', '@me', 'marker')], binding, action), /不是唯一/);
  32. assert.equal(action.proof, original);
  33. });
  34. test('后续证明另存verifications,保留首次proof和sentIds', () => {
  35. const original = { sent: ['first'], evidenceFile: 'original.json' }, action = { proof: original, sentIds: ['first'] };
  36. retainFirstProof(action, { sent: ['first'], evidenceFile: 'readback.json' });
  37. assert.equal(action.proof, original); assert.equal(action.verifications[0].evidenceFile, 'readback.json'); assert.deepEqual(action.sentIds, ['first']);
  38. });
  39. test('回读锁定已登记线程和房间,不被相同预览文字带到其他房间', () => {
  40. const task = { marker: 'marker', threadRoot: '$original', roomId: '!original', proof: { roomId: '!original' } };
  41. const impostor = { threadRoot: '$other', roomId: '!other', lastPreview: 'marker' };
  42. const original = { threadRoot: '$original', roomId: '!original' };
  43. assert.equal(resolveTaskThread([impostor, original], task), original);
  44. assert.equal(resolveTaskThread([impostor], task), undefined);
  45. assert.throws(() => resolveTaskThread([{ ...original, roomId: '!changed' }], task), /原房间/);
  46. assert.throws(() => resolveTaskThread([original, { ...original }], task), /不唯一/);
  47. assert.equal(task.roomId, '!original');
  48. });
  49. test('未登记线程时只允许唯一候选;首次proof房间和输入不可被复核替换', () => {
  50. assert.throws(() => resolveTaskThread([{ lastPreview: 'marker' }, { lastPreview: 'marker' }], { marker: 'marker' }), /不唯一/);
  51. const action = { proof: { sent: ['first'], roomId: '!original' }, sentIds: ['first'] };
  52. assert.throws(() => retainFirstProof(action, { sent: ['other'], roomId: '!original' }), /复核不能更换首次已发送输入/);
  53. assert.throws(() => retainFirstProof(action, { sent: ['first'], roomId: '!other' }), /原房间/);
  54. assert.equal(action.verifications, undefined);
  55. });
  56. test('同URL多来源允许,异URL同名或任一来源归属错误必须失败', () => {
  57. const file = { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/a.html' };
  58. assert.equal(sessionFileObjects([file, { ...file, size: 413 }], 'agent', '!room', ['a.html']).length, 1);
  59. assert.throws(() => sessionFileObjects([file, { ...file, url: 'https://other.invalid/a.html' }], 'agent', '!room', ['a.html']), /成果必须唯一对应对象URL/);
  60. assert.throws(() => sessionFileObjects([file, { ...file, agentId: 'other' }], 'agent', '!room', ['a.html']), /来源行归属/);
  61. const legacy = { ...file, agentId: '', url: 'https://files.invalid/deliverables/agent/a.html' };
  62. assert.equal(sessionFileObjects([legacy], 'agent', '!room', ['a.html'])[0].url, legacy.url);
  63. assert.throws(() => sessionFileObjects([{ ...legacy, url: 'https://files.invalid/deliverables/other/a.html' }], 'agent', '!room', ['a.html']), /来源行归属/);
  64. assert.throws(() => sessionFileObjects([], 'agent', '!room', ['a.html']), /成果必须唯一对应对象URL/);
  65. });
  66. test('隔离审计拒绝消息、文档、来源行和未完成分页中的任一污染', () => {
  67. const own = { agentId: 'agent', roomId: '!room', marker: 'new-marker', expectedFiles: ['new.md'] };
  68. const other = { roomId: '!old', marker: 'old-marker', expectedFiles: ['old.pdf', 'old.docx'] };
  69. const history = { complete: true, events: [msg('one', '@me', 'new-marker')] };
  70. const files = [{ agentId: 'agent', roomId: '!room', url: 'https://files.invalid/new.md' }];
  71. assert.doesNotThrow(() => assertSessionIsolation(history, files, own, [other]));
  72. assert.throws(() => assertSessionIsolation({ ...history, complete: false }, files, own, [other]), /分页/);
  73. assert.throws(() => assertSessionIsolation({ ...history, events: [...history.events, msg('polluted', '@me', 'old-marker')] }, files, own, [other]), /其他容器消息/);
  74. assert.throws(() => assertSessionIsolation(history, [...files, { ...files[0], url: 'https://files.invalid/old.docx' }], own, [other]), /其他容器成果/);
  75. assert.throws(() => assertSessionIsolation(history, [{ ...files[0], roomId: '!old' }], own, [other]), /成果列表中存在其他容器/);
  76. });
  77. test('隔离审计允许同容器同房间的历史任务成果复用', () => {
  78. const own = { agentId: 'agent', roomId: '!room', marker: 'current-marker', expectedFiles: ['current.md'] };
  79. const previous = { agentId: 'agent', roomId: '!room', marker: 'previous-marker', expectedFiles: ['previous.md'] };
  80. const history = { complete: true, events: [msg('current', '@me', 'current-marker'), msg('previous', '@agent', 'previous-marker')] };
  81. const files = [
  82. { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/current.md' },
  83. { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/previous.md' },
  84. ];
  85. assert.doesNotThrow(() => assertSessionIsolation(history, files, own, [previous]));
  86. });
  87. test('隔离审计允许同容器成果按当前房间登记后的跨房间复用', () => {
  88. const own = { agentId: 'agent', roomId: '!room', marker: 'current-marker', expectedFiles: ['current.md'] };
  89. const reused = { agentId: 'agent', roomId: '!other-room', marker: 'reuse-marker', expectedFiles: ['reused.md'] };
  90. const history = { complete: true, events: [msg('current', '@me', 'current-marker')] };
  91. const files = [
  92. { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/current.md' },
  93. { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/reused.md' },
  94. ];
  95. assert.doesNotThrow(() => assertSessionIsolation(history, files, own, [reused]));
  96. });
  97. test('隔离审计仍拒绝同房间但属于另一容器的成果', () => {
  98. const own = { agentId: 'agent', roomId: '!room', marker: 'current-marker', expectedFiles: ['current.md'] };
  99. const other = { agentId: 'other-agent', roomId: '!room', marker: 'other-marker', expectedFiles: ['other.md'] };
  100. const history = { complete: true, events: [msg('current', '@me', 'current-marker')] };
  101. const files = [
  102. { agentId: 'agent', roomId: '!room', url: 'https://files.invalid/current.md' },
  103. { agentId: 'other-agent', roomId: '!room', url: 'https://files.invalid/other.md' },
  104. ];
  105. assert.throws(() => assertSessionIsolation(history, files, own, [other]), /成果列表中存在其他容器/);
  106. });
  107. test('收集其他任务成果时按稳定marker排除当前任务副本', () => {
  108. const own = { agentId: 'agent', marker: 'own', expectedFiles: ['own.pdf'] };
  109. const tasks = [
  110. { ...own },
  111. { ...own, marker: 'same-container', expectedFiles: ['same-container.pdf'] },
  112. { agentId: 'other-agent', marker: 'other', expectedFiles: ['other.docx'] },
  113. ];
  114. assert.deepEqual(otherTaskFiles(tasks, own), ['other.docx']);
  115. });
  116. test('错误发送者的回复和结束信号不能充当证明', () => {
  117. assert.equal(taskProof([msg('one', '@me', 'marker'), msg('fake', '@other', 'marker', 2)], binding, { marker: 'marker' }), null);
  118. });
  119. test('等待重连期间原事件落服,紧邻重试的回读必须拦截', async () => {
  120. const events = [], calls = [];
  121. await assert.rejects(() => checkRetryUnsent(async () => { calls.push('wait'); events.push(msg('arrived', '@me', 'marker')); }, async () => { calls.push('read'); return { complete: true, events }; }, binding, 'marker'), /禁止再重试/);
  122. assert.deepEqual(calls, ['wait', 'read']);
  123. });
  124. test('重试前不完整事件范围必须拦截,即使当前页没有输入', async () => {
  125. await assert.rejects(() => checkRetryUnsent(async () => {}, async () => ({ complete: false, events: [] }), binding, 'marker'), /范围不完整/);
  126. });
  127. test('HTML同一旧loader不能通过刷新,新loader才通过', () => {
  128. assert.equal(newFrameLoader({ loaderId: 'old' }, { loaderId: 'old' }), false);
  129. assert.equal(newFrameLoader({ loaderId: 'old' }, { loaderId: 'new' }), true);
  130. assert.equal(newFrameLoader({}, { loaderId: 'new' }), false);
  131. });
  132. test('服务端归属正常但A界面残留B成果卡片仍须失败', () => {
  133. assert.equal(previewCardsMatch(['a.md', 'b.html'], ['预览 a.md'], false), false);
  134. assert.equal(previewCardsMatch(['a.md', 'b.html'], [], false), true);
  135. assert.equal(previewCardsMatch(['a.md', 'b.html'], ['预览 a.md'], true), false);
  136. });
  137. test('新loader正文首次为空、随后加载完成时允许通过', async () => {
  138. let n = 0;
  139. assert.equal(await waitForPreviewText(async () => ++n === 1 ? '' : 'real marker', 'marker', { attempts: 2, pause: async () => {} }), 'real marker');
  140. assert.equal(n, 2);
  141. });
  142. test('新loader上下文暂不可用可恢复,但持续错误或错误正文失败', async () => {
  143. let n = 0;
  144. await waitForPreviewText(async () => { if (++n === 1) throw Error('context unavailable'); return 'marker'; }, 'marker', { attempts: 2, pause: async () => {} });
  145. await assert.rejects(() => waitForPreviewText(async () => 'wrong document', 'marker', { attempts: 3, pause: async () => {} }), /限时内未出现/);
  146. await assert.rejects(() => waitForPreviewText(async () => { throw Error('always unavailable'); }, 'marker', { attempts: 2, pause: async () => {} }), /限时内未出现/);
  147. });