network-history.test.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { createHash } from 'node:crypto';
  4. import { createStore } from '../src/store.js';
  5. import { createNetworkHistory, validateHistoryCursor } from '../src/network-history.js';
  6. import { normalizeMessages } from '../src/teams-network-records.js';
  7. import { abortableDelay } from '../src/request-pacing.js';
  8. const accountId = 'web-api:11111111-1111-1111-1111-111111111111:22222222-2222-2222-2222-222222222222';
  9. const chatId = '19:fictional-history@thread.v2';
  10. const prefix = 'https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/conversations/';
  11. const view = 'msnp24Equivalent|supportsMessageProperties';
  12. const token = `fake.${Buffer.from(JSON.stringify({ tid: '11111111-1111-1111-1111-111111111111', oid: '22222222-2222-2222-2222-222222222222' })).toString('base64url')}.signature`;
  13. function url({ chat = chatId, cursor, start = '0', size = 2 } = {}) {
  14. const result = new URL(`${prefix}${encodeURIComponent(chat)}/messages`);
  15. result.searchParams.set('view', view); result.searchParams.set('pageSize', String(size)); result.searchParams.set('startTime', start);
  16. if (cursor) result.searchParams.set('syncState', cursor);
  17. return result.href;
  18. }
  19. function message(id, overrides = {}) {
  20. return { id, conversationid: chatId, conversationLink: `${prefix}${encodeURIComponent(chatId)}`,
  21. type: 'Message', messagetype: 'RichText/Html', contenttype: 'text', content: `<p>Fictional ${id}</p>`, imdisplayname: 'Fictional author',
  22. composetime: '2023-11-14T22:13:20Z', originalarrivaltime: '2023-11-14T22:13:20Z', version: '1700000000000', ...overrides };
  23. }
  24. function page(messages, { backward, boundary = 0, sync = url({ cursor: 'incremental-secret' }) } = {}) {
  25. return { messages, tenantId: 'fictional-tenant', _metadata: { lastCompleteSegmentStartTime: boundary, lastCompleteSegmentEndTime: 1800000000000,
  26. syncState: sync, ...(backward ? { backwardLink: backward } : {}) } };
  27. }
  28. const json = (payload, status = 200) => new Response(JSON.stringify(payload), { status, headers: { 'content-type': 'application/json' } });
  29. function har() {
  30. return { log: { entries: [{ request: { method: 'GET', url: url({ start: '1700000000000', size: 200 }),
  31. headers: [{ name: 'Authorization', value: `Bearer ${token}` }] },
  32. response: { status: 200, content: { mimeType: 'application/json', text: JSON.stringify(page([message('captured')], { boundary: 1700000000000 })) } },
  33. }] } };
  34. }
  35. function setup(fetch, options = {}) {
  36. const store = createStore(':memory:'), writes = [], originalSet = store.setState.bind(store);
  37. store.setState = (key, value) => { writes.push({ key, value: structuredClone(value) }); return originalSet(key, value); };
  38. let milliseconds = Date.parse('2026-09-08T00:00:00Z');
  39. const timing = { random: () => 0.5, clock: () => milliseconds, now: () => new Date(milliseconds).toISOString(),
  40. sleep: async (delay, { signal } = {}) => { if (signal?.aborted) throw new Error('cancelled'); milliseconds += delay; } };
  41. const history = createNetworkHistory({ har: har(), store, fetch, pageSize: 2, ...timing, ...options });
  42. return { store, history, writes, timing, advance: delay => { milliseconds += delay; }, close: () => store.close() };
  43. }
  44. test('history shares validated committed message pages for optional conversation names without making metadata failures fatal', async () => {
  45. const observed = [];
  46. const payload = page([message('history-name-link')]);
  47. const f = setup(async () => json(payload), { onMessages: (scope, body) => {
  48. assert.equal(f.store.stats().total, 1, 'record must be committed before discovery');
  49. assert.equal(f.history.coverage().complete, 1, 'history must already be committed');
  50. observed.push({ scope, body });
  51. throw new Error('Optional metadata failed with private details');
  52. } });
  53. try {
  54. const result = await f.history.runOnce();
  55. assert.equal(result.complete, true); assert.equal(result.successful, 1); assert.deepEqual(result.errors, []);
  56. assert.equal(observed.length, 1); assert.deepEqual(observed[0].body, payload);
  57. assert.equal(observed[0].scope.accountId, accountId); assert.equal(observed[0].scope.chatId, chatId);
  58. assert.equal(observed[0].scope.requestUrl, url());
  59. assert.equal(observed[0].scope.headers.Authorization, `Bearer ${token}`);
  60. await f.history.runOnce(); assert.equal(observed.length, 1, 'completed history must not be rescanned');
  61. assert.doesNotMatch(JSON.stringify(result), /private details|Bearer|signature/);
  62. } finally { f.close(); }
  63. });
  64. test('rejected or cancelled history responses never supply optional conversation-name evidence', async () => {
  65. for (const scenario of ['wrong-chat', 'invalid-cursor', 'invalid-boundary', 'cancelled']) {
  66. let observations = 0;
  67. const controller = new AbortController();
  68. const f = setup(async () => {
  69. if (scenario === 'cancelled') controller.abort();
  70. const payload = page([message('rejected', scenario === 'wrong-chat' ? { conversationid: 'other-chat' } : {})],
  71. scenario === 'invalid-cursor' ? { backward: 'https://untrusted.example/messages' } : {});
  72. if (scenario === 'invalid-boundary') delete payload._metadata.lastCompleteSegmentStartTime;
  73. return json(payload);
  74. }, { onMessages: () => { observations++; } });
  75. try {
  76. const result = await f.history.runOnce({ signal: controller.signal });
  77. assert.equal(observations, 0, scenario); assert.equal(f.store.stats().total, 0, scenario);
  78. assert.ok(result.cancelled || result.errors.length > 0, scenario);
  79. } finally { f.close(); }
  80. }
  81. });
  82. test('history starts at zero, follows only backwardLink, and completes only with an explicit zero boundary', async () => {
  83. const calls = [];
  84. const f = setup(async (request, options) => {
  85. calls.push({ request, options });
  86. return json(calls.length === 1 ? page([message('newer')], { backward: url({ cursor: 'older-secret' }), boundary: 100 }) : page([message('older')]));
  87. });
  88. try {
  89. const result = await f.history.runOnce();
  90. assert.equal(result.complete, true); assert.equal(result.coverage.complete, 1); assert.equal(result.coverage.pages, 2);
  91. assert.equal(new URL(calls[0].request).searchParams.get('startTime'), '0');
  92. assert.equal(new URL(calls[0].request).searchParams.has('syncState'), false);
  93. assert.equal(new URL(calls[1].request).searchParams.get('syncState'), 'older-secret');
  94. assert.ok(calls.every(call => call.options.method === 'GET' && call.options.redirect === 'manual'));
  95. assert.equal(f.store.stats().total, 2);
  96. assert.ok(f.writes.every(write => write.key.startsWith('web-api:history:')));
  97. assert.doesNotMatch(JSON.stringify(f.writes.map(write => write.value)), /Authorization|signature|Bearer/);
  98. assert.doesNotMatch(JSON.stringify(result), /older-secret|incremental-secret|fictional-history/);
  99. await f.history.runOnce(); assert.equal(calls.length, 2);
  100. } finally { f.close(); }
  101. });
  102. test('captured draftVersion is omitted from the history seed without widening backward-link validation or resetting progress', async () => {
  103. const captured = har(), recent = new URL(captured.log.entries[0].request.url);
  104. recent.searchParams.set('draftVersion', 'fictional-private-draft'); recent.searchParams.set('syncState', 'recent-secret');
  105. captured.log.entries[0].request.url = recent.href;
  106. const calls = [];
  107. const f = setup(async request => {
  108. calls.push(new URL(request));
  109. return json(page([message('archived')], calls.length === 1 ? { backward: url({ cursor: 'older-secret' }), boundary: 100 } : {}));
  110. }, { har: captured });
  111. try {
  112. const result = await f.history.runOnce();
  113. assert.equal(result.complete, true); assert.equal(calls.length, 2);
  114. assert.equal(calls[0].searchParams.get('startTime'), '0');
  115. assert.ok(calls.every(request => !request.searchParams.has('draftVersion')));
  116. assert.equal(calls[0].searchParams.has('syncState'), false);
  117. assert.equal(calls[1].searchParams.get('syncState'), 'older-secret');
  118. assert.equal(new URL(captured.log.entries[0].request.url).searchParams.get('draftVersion'), 'fictional-private-draft');
  119. assert.doesNotMatch(JSON.stringify(f.writes), /fictional-private-draft/);
  120. const resumed = createNetworkHistory({ har: captured, store: f.store, pageSize: 2, ...f.timing, fetch: async () => { throw new Error('Completed history must not fetch again'); } });
  121. assert.equal((await resumed.runOnce()).coverage.pages, 2);
  122. const untrusted = new URL(url({ cursor: 'older-secret' })); untrusted.searchParams.set('draftVersion', 'unapproved');
  123. assert.throws(() => validateHistoryCursor(untrusted.href, recent.href), { code: 'invalid_history_cursor' });
  124. } finally { f.close(); }
  125. });
  126. test('history derives only chat metadata from the peer and uses its current title on later pages despite a stale catalogue', async () => {
  127. const self='22222222-2222-2222-2222-222222222222', peer='33333333-3333-3333-3333-333333333333';
  128. let calls=0;
  129. const sender=id=>`https://teams.cloud.microsoft/api/chatsvc/apac/v1/users/ME/contacts/${encodeURIComponent(`8:orgid:${id}`)}`;
  130. const f=setup(async()=>json(++calls===1
  131. ?page([message('peer-author',{from:sender(peer),imdisplayname:'Fictional peer'})],{backward:url({cursor:'older-page'}),boundary:1})
  132. :page([message('self-author',{from:sender(self),imdisplayname:'Fictional self'})])));
  133. try {
  134. f.store.upsertRecord({source:'web-api',accountId,kind:'chat',externalId:chatId,title:'聊天',raw:{isOneOnOne:true,titleSource:'placeholder'}});
  135. const catalogueKey='web-api:chats:'+createHash('sha256').update(accountId+'https://teams.cloud.microsoft'+'/api/chatsvc/apac/v1/users/ME/conversations/').digest('hex').slice(0,24);
  136. f.store.setState(catalogueKey,{chats:[{id:chatId,title:'聊天'}]});f.history.refreshChats();
  137. const result=await f.history.runOnce();assert.equal(result.complete,true);assert.equal(calls,2);
  138. const named=f.store.getRecordByIdentity({source:'web-api',accountId,kind:'chat',externalId:chatId});
  139. assert.equal(named.title,'Fictional peer');assert.equal(named.raw.titleSource,'message-author');
  140. const messages=f.store.listRecords({kind:'message'}).items;
  141. assert.equal(messages.find(record=>record.raw.messageId==='self-author').title,'Fictional peer');
  142. assert.equal(messages.find(record=>record.raw.messageId==='self-author').raw.author,'Fictional self');
  143. const messageChanges=f.store.readRows("SELECT c.ingestion FROM change_log c JOIN records r ON c.recordId=r.id WHERE r.kind='message'");
  144. assert.ok(messageChanges.every(row=>row.ingestion==='history'));
  145. const versions=f.store.stats().versions;f.history.refreshChats();await f.history.runOnce();
  146. assert.equal(calls,2);assert.equal(f.store.stats().versions,versions);
  147. } finally {f.close();}
  148. });
  149. test('request-budget exhaustion persists an independent checkpoint and a fresh instance resumes its next page', async () => {
  150. let calls = 0;
  151. const fetch = async request => { calls++; return json(new URL(request).searchParams.has('syncState') ? page([message('old')]) : page([message('new')], { backward: url({ cursor: 'next-history-secret' }), boundary: 100 })); };
  152. const f = setup(fetch, { maxRequests: 1 });
  153. try {
  154. f.store.setState('web-api:cursor:watch-owned', { url: 'watch-private-cursor' });
  155. const first = await f.history.runOnce();
  156. assert.equal(first.complete, false); assert.equal(first.budgetExhausted, true); assert.equal(first.coverage.pending, 1);
  157. const next = createNetworkHistory({ har: har(), store: f.store, fetch, maxRequests: 1, pageSize: 2, ...f.timing });
  158. const second = await next.runOnce();
  159. assert.equal(second.complete, true); assert.equal(calls, 2);
  160. assert.deepEqual(f.store.getState('web-api:cursor:watch-owned'), { url: 'watch-private-cursor' });
  161. assert.equal(f.store.stats().total, 2);
  162. } finally { f.close(); }
  163. });
  164. test('an empty incremental-looking response or a missing backwardLink above zero is explicitly incomplete', async () => {
  165. for (const messages of [[], [message('recent')]]) {
  166. const f = setup(async () => json(page(messages, { boundary: 1700000000000 })));
  167. try {
  168. const result = await f.history.runOnce();
  169. assert.equal(result.complete, false); assert.equal(result.coverage.truncated, 1);
  170. assert.equal(result.coverage.stopReasons['unproven-start'], 1);
  171. } finally { f.close(); }
  172. }
  173. });
  174. test('control-only pages still advance history instead of treating zero normalized records as completion', async () => {
  175. let calls = 0;
  176. const f = setup(async () => json(++calls === 1
  177. ? page([message('control', { messagetype: 'ThreadActivity/MemberJoined' })], { boundary: 100, backward: url({ cursor: 'next' }) })
  178. : page([message('user')])));
  179. try {
  180. const result = await f.history.runOnce();
  181. assert.equal(calls, 2); assert.equal(result.complete, true); assert.equal(result.skippedItems, 1); assert.equal(f.store.stats().total, 1);
  182. } finally { f.close(); }
  183. });
  184. test('invalid cross-chat and cross-origin cursors cannot advance a checkpoint or commit a page', async () => {
  185. for (const backward of [url({ chat: '19:other-chat', cursor: 'private-marker' }), url({ cursor: 'private-marker' }).replace('teams.cloud.microsoft', 'untrusted.example')]) {
  186. const f = setup(async () => json(page([message('unsafe')], { backward, boundary: 100 })));
  187. try {
  188. const result = await f.history.runOnce();
  189. assert.equal(result.successful, 0); assert.equal(result.errors.length, 1); assert.equal(f.store.stats().total, 0);
  190. assert.equal(result.coverage.errors, 1); assert.equal(f.writes.at(-1).value.pages, 0);
  191. assert.equal(new URL(f.writes.at(-1).value.nextUrl).searchParams.has('syncState'), false);
  192. assert.doesNotMatch(JSON.stringify(result), /private-marker|untrusted|other-chat/);
  193. } finally { f.close(); }
  194. }
  195. });
  196. test('cursor validation accepts path encoding differences but rejects duplicate or unsupported query parameters', () => {
  197. const original = url();
  198. const compatible = url({ cursor: 'safe' }).replace(encodeURIComponent(chatId), chatId);
  199. assert.equal(new URL(validateHistoryCursor(compatible, original)).origin, 'https://teams.cloud.microsoft');
  200. for (const candidate of [`${url({ cursor: 'safe' })}&startTime=0`, `${url()}&endTime=1`, `${url()}&token=secret`, url().replace('pageSize=2', 'pageSize=500')]) {
  201. assert.throws(() => validateHistoryCursor(candidate, original), { code: 'invalid_history_cursor' });
  202. }
  203. });
  204. test('message and checkpoint commits roll back together and the same page is retryable after storage recovers', async () => {
  205. const f = setup(async () => json(page([message('transactional')])));
  206. const originalSet = f.store.setState;
  207. try {
  208. f.store.setState = () => { throw new Error('fictional database error private-marker'); };
  209. const failed = await f.history.runOnce();
  210. assert.equal(failed.successful, 0); assert.equal(f.store.stats().total, 0); assert.equal(f.history.coverage().pages, 0);
  211. assert.doesNotMatch(JSON.stringify(failed), /private-marker/);
  212. f.store.setState = originalSet;
  213. const recovered = await f.history.runOnce();
  214. assert.equal(recovered.complete, true); assert.equal(f.store.stats().total, 1);
  215. } finally { f.close(); }
  216. });
  217. test('repeated cursors and per-conversation page limits stop explicitly rather than claiming completion', async () => {
  218. const f = setup(async () => json(page([message('loop')], { backward: url(), boundary: 100 })));
  219. try {
  220. const result = await f.history.runOnce();
  221. assert.equal(result.requests, 1); assert.equal(result.complete, false); assert.equal(result.coverage.stopReasons['repeated-cursor'], 1);
  222. } finally { f.close(); }
  223. const g = setup(async () => json(page([message('bounded')], { backward: url({ cursor: 'next' }), boundary: 100 })), { maxPages: 1 });
  224. try {
  225. const result = await g.history.runOnce();
  226. assert.equal(result.coverage.stopReasons['page-limit'], 1); assert.equal(result.complete, false);
  227. const resumed = createNetworkHistory({ har: har(), store: g.store, pageSize: 2, maxPages: 2, fetch: async () => json(page([message('last')])), ...g.timing });
  228. assert.equal((await resumed.runOnce()).complete, true);
  229. } finally { g.close(); }
  230. });
  231. test('authentication pauses until a renewed collector; 429 and 5xx defer rounds until cooldown expires', async () => {
  232. for (const status of [429, 503]) {
  233. let calls = 0;
  234. const f = setup(async () => ++calls === 1 ? json({}, status) : json(page([message('retry')])));
  235. try {
  236. assert.equal((await f.history.runOnce()).ok, false);
  237. const deferred = await f.history.runOnce();
  238. assert.equal(deferred.deferred, true); assert.equal(deferred.requests, 0); assert.equal(calls, 1);
  239. f.advance(30_000);
  240. assert.equal((await f.history.runOnce()).complete, true); assert.equal(calls, 2);
  241. } finally { f.close(); }
  242. }
  243. let authCalls = 0;
  244. const f = setup(async () => { authCalls++; return json({}, 401); });
  245. try {
  246. assert.equal((await f.history.runOnce()).authRequired, true);
  247. await f.history.runOnce(); assert.equal(authCalls, 1);
  248. const renewed = createNetworkHistory({ har: har(), store: f.store, fetch: async () => json(page([message('renewed')])), pageSize: 2, ...f.timing });
  249. assert.equal((await renewed.runOnce()).complete, true);
  250. } finally { f.close(); }
  251. });
  252. test('refreshChats reads new persisted chat records without requiring mutable watch cursors or duplicate targets', () => {
  253. const f = setup(async () => json(page([])));
  254. try {
  255. f.store.upsertRecord({ source: 'web-api', accountId, kind: 'chat', externalId: '19:later-discovered', title: 'Fictional later chat', text: '' });
  256. assert.equal(f.history.coverage().conversations, 1);
  257. assert.equal(f.history.refreshChats().conversations, 2);
  258. assert.equal(f.history.refreshChats().conversations, 2);
  259. assert.equal(f.writes.length, 0);
  260. const empty = createNetworkHistory({ har: { log: { entries: [] } }, store: f.store, ...f.timing });
  261. assert.equal(empty.coverage().conversations, 0);
  262. } finally { f.close(); }
  263. });
  264. test('older history never overwrites a newer real-time record when both versions have valid timestamps', async () => {
  265. const f = setup(async () => json(page([message('same-id')])));
  266. try {
  267. const newer = normalizeMessages(page([message('same-id', { content: '<p>New live version</p>', version: '1800000000000' })]), { accountId, chatId }).records[0];
  268. f.store.upsertRecord(newer);
  269. const result = await f.history.runOnce();
  270. assert.equal(result.complete, true); assert.equal(result.skippedOlderRecords, 1);
  271. assert.equal(f.store.getRecordByIdentity(newer).text, 'New live version');
  272. assert.equal(f.store.stats().versions, 1);
  273. } finally { f.close(); }
  274. });
  275. test('concurrent calls share one pass and cancellation does not commit an in-flight response', async () => {
  276. let release, calls = 0;
  277. const f = setup(async () => { calls++; return new Promise(resolve => { release = resolve; }); });
  278. const controller = new AbortController();
  279. try {
  280. const first = f.history.runOnce({ signal: controller.signal });
  281. while (!release) await new Promise(resolve => setImmediate(resolve));
  282. const duplicate = f.history.runOnce({ signal: controller.signal });
  283. controller.abort(); release(json(page([message('cancelled')])));
  284. const result = await first; await duplicate;
  285. assert.equal(calls, 1); assert.equal(result.cancelled, true); assert.equal(f.store.stats().total, 0); assert.equal(f.writes.length, 0);
  286. } finally { f.close(); }
  287. });
  288. test('backward pagination accepts the observed property-capability reduction only', async () => {
  289. const backward = new URL(url({ cursor: 'older' })); backward.searchParams.set('view', 'msnp24Equivalent');
  290. assert.equal(validateHistoryCursor(backward.href, url()), backward.href);
  291. const unknown = new URL(backward); unknown.searchParams.set('view', 'unobserved-mode');
  292. assert.throws(() => validateHistoryCursor(unknown.href, url()));
  293. let calls = 0;
  294. const f = setup(async () => json(++calls === 1 ? page([message('new')], { backward: backward.href, boundary: 1 }) : page([message('old')])));
  295. try { assert.equal((await f.history.runOnce()).complete, true); assert.equal(calls, 2); }
  296. finally { f.close(); }
  297. });
  298. test('history restores the same authenticated catalogue as monitoring and excludes activity service streams', () => {
  299. const f = setup(async () => json(page([])));
  300. try {
  301. const key = 'web-api:chats:' + createHash('sha256').update(accountId + 'https://teams.cloud.microsoft' + '/api/chatsvc/apac/v1/users/ME/conversations/').digest('hex').slice(0, 24);
  302. f.store.setState(key, { chats: [{ id: chatId }, { id: '19:captured-earlier@thread.v2' }, { id: '48:activity' }, { id: 'bad\nidentity' }] });
  303. const before = structuredClone(f.store.getState(key));
  304. const coverage = f.history.refreshChats();
  305. assert.equal(coverage.conversations, 2); assert.equal(coverage.skipped.activityConversation, 1); assert.equal(coverage.skipped.invalidCatalogueChat, 1);
  306. f.history.refreshChats(); assert.equal(f.history.coverage().conversations, 2); assert.deepEqual(f.store.getState(key), before);
  307. } finally { f.close(); }
  308. });
  309. test('completed legacy checkpoints rescan once for newly supported Text without duplicating existing records', async () => {
  310. const f = setup(async () => json(page([message('existing')])));
  311. try {
  312. await f.history.runOnce();
  313. const saved = f.writes.at(-1), legacy = { ...saved.value }; delete legacy.adapterVersion;
  314. f.store.setState(saved.key, legacy);
  315. let calls = 0;
  316. const fetch = async () => { calls++; return json(page([message('existing'), message('plain', { messagetype: 'Text', content: '<literal>' })])); };
  317. const upgraded = createNetworkHistory({ har: har(), store: f.store, fetch, ...f.timing });
  318. const result = await upgraded.runOnce(); assert.equal(result.complete, true); assert.equal(result.changed, 1); assert.equal(f.store.stats().total, 2);
  319. const restarted = createNetworkHistory({ har: har(), store: f.store, fetch, ...f.timing });
  320. await restarted.runOnce(); assert.equal(calls, 1); assert.equal(f.store.getState(saved.key).adapterVersion, 5);
  321. } finally { f.close(); }
  322. });
  323. test('history waits randomly before its first and following requests, then skips waiting when work is complete', async () => {
  324. const events = [], samples = [0, 1];
  325. let calls = 0;
  326. const f = setup(async () => {
  327. events.push('request');
  328. return json(++calls === 1 ? page([message('new')], { backward: url({ cursor: 'older' }), boundary: 1 }) : page([message('old')]));
  329. }, { random: () => samples.shift(), sleep: async delay => { events.push(delay); } });
  330. try {
  331. assert.equal((await f.history.runOnce()).complete, true);
  332. assert.deepEqual(events, [2000, 'request', 5000, 'request']);
  333. await f.history.runOnce(); assert.equal(events.length, 4);
  334. const empty = createNetworkHistory({ har: { log: { entries: [] } }, store: f.store,
  335. sleep: async () => { throw new Error('No work must not wait.'); }, fetch: async () => { throw new Error('No work must not request.'); } });
  336. assert.equal((await empty.runOnce()).requests, 0);
  337. } finally { f.close(); }
  338. });
  339. test('cancellation during initial jitter prevents all requests and writes', async () => {
  340. let calls = 0;
  341. const controller = new AbortController();
  342. const f = setup(async () => { calls++; return json(page([])); }, { sleep: abortableDelay });
  343. try {
  344. const run = f.history.runOnce({ signal: controller.signal });
  345. assert.equal(f.history.pacing().waiting, true);
  346. controller.abort(); const result = await run;
  347. assert.equal(result.cancelled, true); assert.equal(result.requests, 0); assert.equal(calls, 0); assert.equal(f.writes.length, 0);
  348. assert.equal(f.history.pacing().waiting, false);
  349. } finally { f.close(); }
  350. });
  351. test('cancelling between pages preserves a valid cursor and a replacement continues that page', async () => {
  352. let waits = 0, calls = 0;
  353. const controller = new AbortController();
  354. const f = setup(async () => { calls++; return json(page([message('new')], { backward: url({ cursor: 'durable-next' }), boundary: 1 })); },
  355. { sleep: async (delay, options) => { if (++waits > 1) await abortableDelay(delay, options); } });
  356. try {
  357. const run = f.history.runOnce({ signal: controller.signal });
  358. while (waits < 2) await new Promise(resolve => setImmediate(resolve));
  359. const checkpoint = f.writes.at(-1);
  360. controller.abort(); const stopped = await run;
  361. assert.equal(stopped.cancelled, true); assert.equal(calls, 1); assert.equal(stopped.coverage.pages, 1);
  362. assert.deepEqual(f.store.getState(checkpoint.key), checkpoint.value);
  363. const resumed = createNetworkHistory({ har: har(), store: f.store, ...f.timing, fetch: async request => {
  364. assert.equal(new URL(request).searchParams.get('syncState'), 'durable-next');
  365. return json(page([message('old')]));
  366. } });
  367. assert.equal((await resumed.runOnce()).complete, true);
  368. assert.equal(f.store.stats().total, 2);
  369. } finally { f.close(); }
  370. });
  371. test('Retry-After stops same-origin chat switching and stays deferred across rounds and worker restart', async () => {
  372. let calls = 0;
  373. const fetch = async () => { calls++; return new Response('{}', { status: 429, headers: { 'retry-after': '90' } }); };
  374. const f = setup(fetch);
  375. try {
  376. f.store.upsertRecord({ source: 'web-api', accountId, kind: 'chat', externalId: '19:second', title: 'Other chat' });
  377. f.history.refreshChats();
  378. const first = await f.history.runOnce();
  379. assert.equal(first.requests, 1); assert.equal(first.deferred, true); assert.equal(first.pacing.coolingDownOrigins, 1);
  380. const restarted = createNetworkHistory({ har: har(), store: f.store, fetch, ...f.timing });
  381. assert.equal((await restarted.runOnce()).requests, 0); assert.equal(calls, 1);
  382. f.advance(89_999); assert.equal((await restarted.runOnce()).requests, 0); assert.equal(calls, 1);
  383. f.advance(1); assert.equal((await restarted.runOnce()).requests, 1); assert.equal(calls, 2);
  384. assert.doesNotMatch(JSON.stringify(first.pacing), /https|fictional|second|signature/);
  385. } finally { f.close(); }
  386. });
  387. test('a received server cooldown survives simultaneous cancellation while pure cancellation adds no cooldown', async () => {
  388. for (const status of [429, 503]) {
  389. const controller = new AbortController();
  390. const f = setup(async () => { controller.abort(); return new Response('{}', { status, headers: { 'retry-after': '120' } }); });
  391. try {
  392. const result = await f.history.runOnce({ signal: controller.signal });
  393. assert.equal(result.cancelled, true); assert.equal(result.pacing.coolingDownOrigins, 1);
  394. const restarted = createNetworkHistory({ har: har(), store: f.store, ...f.timing, fetch: async () => { throw new Error('Must remain deferred.'); } });
  395. assert.equal((await restarted.runOnce()).deferred, true);
  396. assert.equal(f.history.coverage().pages, 0);
  397. } finally { f.close(); }
  398. }
  399. const controller = new AbortController();
  400. const f = setup(async () => { controller.abort(); throw new Error('cancelled network'); });
  401. try {
  402. const result = await f.history.runOnce({ signal: controller.signal });
  403. assert.equal(result.cancelled, true); assert.equal(result.pacing.coolingDownOrigins, 0); assert.equal(f.writes.length, 0);
  404. } finally { f.close(); }
  405. });
  406. test('version 3 history is rescanned once for emoji recovery and all restored changes stay historical', async () => {
  407. const f = setup(async () => json(page([])));
  408. try {
  409. await f.history.runOnce();
  410. const checkpoint = f.writes.at(-1);
  411. f.store.setState(checkpoint.key, { ...checkpoint.value, adapterVersion: 3 });
  412. let calls = 0;
  413. const fetch = async () => { calls++; return json(page([message('emoji', { messagetype: 'Text', content: '😀' })])); };
  414. const upgraded = createNetworkHistory({ har: har(), store: f.store, fetch, ...f.timing });
  415. assert.equal((await upgraded.runOnce()).changed, 1);
  416. const changes = f.store.readRows('SELECT ingestion FROM change_log');
  417. assert.deepEqual(changes.map(row => row.ingestion), ['history']);
  418. const restarted = createNetworkHistory({ har: har(), store: f.store, fetch, ...f.timing });
  419. assert.equal((await restarted.runOnce()).requests, 0); assert.equal(calls, 1);
  420. assert.equal(f.store.getState(checkpoint.key).adapterVersion, 5);
  421. } finally { f.close(); }
  422. });