network-chat-metadata.test.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { createStore } from '../src/store.js';
  4. import { createChatMetadataCollector } from '../src/network-chat-metadata.js';
  5. import { classifyMessage } from '../src/message-signals.js';
  6. const tenant = '11111111-1111-4111-8111-111111111111';
  7. const self = '22222222-2222-4222-8222-222222222222';
  8. const other = '33333333-3333-4333-8333-333333333333';
  9. const accountId = `web-api:${tenant}:${self}`;
  10. const account2 = `web-api:${tenant}:${other}`;
  11. const chatId = '19:fictional-group@thread.v2';
  12. const base = (chat = chatId, host = 'teams.cloud.microsoft') => `https://${host}/api/chatsvc/apac/v1/users/ME/conversations/${encodeURIComponent(chat)}`;
  13. const scope = (chat = chatId, account = accountId) => ({ source: 'web-api', accountId: account, kind: 'chat', externalId: chat });
  14. const observation = (chat = chatId, account = accountId) => ({ accountId: account, chatId: chat,
  15. requestUrl: `${base(chat)}/messages?pageSize=200`, headers: { Authorization: 'Bearer private-token-marker', 'X-Ms-Client-Type': 'web' } });
  16. const messages = (chat = chatId) => ({ messages: [{ conversationid: chat, conversationLink: base(chat), content: 'Private message marker' }] });
  17. const details = (chat = chatId, topic = 'Fictional group', revision = '1788850000000') => ({ type: 'Conversation', id: chat, version: 17,
  18. properties: { secret: 'private-property-marker' }, threadProperties: { topic, threadType: 'chat', version: revision, secret: 'private-thread-marker' },
  19. lastMessage: { content: 'private-preview-marker' }, messages: 'private-messages-marker' });
  20. const publicResult = result => assert.doesNotMatch(JSON.stringify(result), /private-|Fictional|19:|web-api:|https:|Bearer/);
  21. test('observed meeting topics resolve existing messages by account without rewriting history or classifying them as direct chats', async () => {
  22. const store = createStore(':memory:');
  23. const meetingId = '19:meeting_ZmljdGlvbmFs@thread.v2';
  24. try {
  25. for (const account of [accountId, account2]) store.upsertRecord({ ...scope(meetingId, account), kind: 'message',
  26. externalId: 'meeting-message', parentId: meetingId, title: '聊天消息', text: 'Original meeting message', raw: { author: 'Fictional speaker' } });
  27. const messageScope = { ...scope(meetingId), kind: 'message', externalId: 'meeting-message' };
  28. const before = store.getRecordByIdentity(messageScope), versions = store.getRecord(before.id).versions;
  29. const body = details(meetingId, '<b>Fictional meeting &amp; review</b>'); body.threadProperties.threadType = 'meeting';
  30. const collector = createChatMetadataCollector({ store, fetch: async () => Response.json(body) });
  31. assert.equal(collector.observe(observation(meetingId), messages(meetingId)), true);
  32. const result = await collector.runOnce(); publicResult(result);
  33. assert.equal(result.successful, 1); assert.equal(result.changed, 1); assert.deepEqual(result.errors, []);
  34. const chat = store.getRecordByIdentity(scope(meetingId));
  35. assert.equal(chat.title, 'Fictional meeting & review'); assert.equal(chat.raw.threadType, 'meeting');
  36. assert.equal(chat.raw.titleSource, 'conversation-details'); assert.doesNotMatch(JSON.stringify(chat), /private-/);
  37. assert.equal(store.getRecord(before.id).displayTitle, chat.title);
  38. assert.equal(store.listRecords({ kind: 'message', accountId, q: chat.title }).total, 1);
  39. assert.equal(store.listRecords({ kind: 'message', accountId: account2, q: chat.title }).total, 0);
  40. assert.deepEqual(store.getRecordByIdentity(messageScope), before);
  41. assert.deepEqual(store.getRecord(before.id).versions, versions);
  42. assert.equal(classifyMessage(before, chat).chatType, 'meeting');
  43. assert.equal(classifyMessage(before, chat).isDirect, false);
  44. } finally { store.close(); }
  45. });
  46. test('meeting details keep identity, topic and revision validation and preserve known names when a topic is omitted', async () => {
  47. const store = createStore(':memory:'); let at = 2000000000000;
  48. const meetingId = '19:meeting_c2Vjb25k@thread.v2';
  49. try {
  50. let body;
  51. const collector = createChatMetadataCollector({ store, now: () => at, fetch: async () => Response.json(body) });
  52. collector.observe(observation(meetingId), messages(meetingId));
  53. const fresh = () => { const value = details(meetingId, 'Current meeting'); value.threadProperties.threadType = 'meeting'; return value; };
  54. body = fresh(); assert.equal((await collector.runOnce()).changed, 1);
  55. for (const change of [v => { delete v.threadProperties.topic; }, v => { v.threadProperties.topic = null; },
  56. v => { v.threadProperties.topic = ''; }, v => { v.threadProperties.topic = 'Old name'; v.threadProperties.version = '1788840000000'; }]) {
  57. at += 30 * 60_000; body = fresh(); change(body);
  58. const result = await collector.runOnce(); assert.equal(result.successful, 1); assert.equal(result.changed, 0);
  59. assert.equal(store.getRecordByIdentity(scope(meetingId)).title, 'Current meeting');
  60. }
  61. for (const change of [v => { v.id = '19:meeting_d3Jvbmc@thread.v2'; }, v => { v.threadProperties.version = 'invalid'; },
  62. v => { v.threadProperties.topic = {}; }, v => { v.threadProperties.threadType = 'unknown'; }]) {
  63. at += 30 * 60_000; body = fresh(); change(body);
  64. const result = await collector.runOnce(); assert.equal(result.changed, 0); assert.equal(result.errors[0].code, 'invalid-metadata');
  65. assert.equal(store.getRecordByIdentity(scope(meetingId)).title, 'Current meeting');
  66. }
  67. } finally { store.close(); }
  68. });
  69. test('observed conversation detail names the account-scoped chat without copying previews or changing messages', async () => {
  70. const store = createStore(':memory:'); let calls = 0;
  71. try {
  72. store.upsertRecord({ ...scope(), kind: 'message', externalId: 'message-id', parentId: chatId, title: '聊天消息', text: 'unchanged', raw: {} });
  73. const message = store.getRecordByIdentity({ ...scope(), kind: 'message', externalId: 'message-id' });
  74. const collector = createChatMetadataCollector({ store, fetch: async (url, options) => {
  75. calls++; assert.equal(url, base()); assert.equal(options.method, 'GET'); assert.equal(options.redirect, 'manual');
  76. assert.equal(options.credentials, 'omit'); assert.equal(options.headers.Authorization, 'Bearer private-token-marker');
  77. assert.ok(options.signal instanceof AbortSignal); return Response.json(details());
  78. } });
  79. assert.equal(collector.observe(observation(), messages()), true);
  80. const result = await collector.runOnce(); publicResult(result);
  81. assert.equal(calls, 1); assert.equal(result.successful, 1); assert.equal(result.changed, 1);
  82. const chat = store.getRecordByIdentity(scope());
  83. assert.equal(chat.title, 'Fictional group'); assert.equal(chat.updatedAt, '2026-09-08T06:46:40.000Z');
  84. assert.deepEqual(chat.raw, { chatId, titleSource: 'conversation-details', threadType: 'chat', version: '1788850000000' });
  85. assert.doesNotMatch(JSON.stringify(chat), /private-/);
  86. const after = store.getRecordByIdentity({ ...scope(), kind: 'message', externalId: 'message-id' });
  87. assert.equal(after.hash, message.hash); assert.equal(after.title, '聊天消息');
  88. assert.equal(store.listRecords({ kind: 'message' }).items[0].displayTitle, 'Fictional group');
  89. assert.equal((await collector.runOnce()).requests, 0);
  90. } finally { store.close(); }
  91. });
  92. test('observations reject foreign origin, conversation mismatches, activity streams, and non-observed URLs before HTTP', async () => {
  93. const store = createStore(':memory:'); let calls = 0;
  94. try {
  95. const collector = createChatMetadataCollector({ store, fetch: async () => { calls++; throw new Error('must not fetch'); } });
  96. const invalidLinks = [base().replace('teams.cloud.microsoft', 'teams.microsoft.com'), base().replace('teams.cloud.microsoft', 'evil.example'),
  97. `${base()}?token=private-token`, `${base()}#private-token`, base().replace('https:', 'http:'),
  98. base().replace('https://', 'https://user:private-token@'), base().replace('/api/', ':8443/api/'), base().replace('/api/', ':443/api/'),
  99. base().replace('/apac/', '/emea/'), `${base()}/messages`, base('19:other-chat@thread.v2'),
  100. base().replace('/ME/', '/someone/'), base().replace('/api/', '/evil/'), base().replace('https://', 'https:\\')];
  101. for (const link of invalidLinks) assert.equal(collector.observe(observation(), { messages: [{ conversationid: chatId, conversationLink: link }] }), false);
  102. assert.equal(collector.observe(observation(), { messages: [...messages().messages, { conversationid: '19:other@thread.v2', conversationLink: base() }] }), false);
  103. assert.equal(collector.observe(observation(), { messages: [] }), false);
  104. assert.equal(collector.observe(observation('48:notifications'), messages('48:notifications')), false);
  105. assert.equal(collector.observe({ ...observation(), accountId: 'other-account' }, messages()), false);
  106. assert.equal(collector.observe({ ...observation(), headers: {} }, messages()), false);
  107. assert.equal(collector.observe({ ...observation(), headers: { Authorization: 'Bearer private-token\r\ninjected' } }, messages()), false);
  108. assert.equal(collector.observe({ ...observation(), requestUrl: `${base()}/messages#private-fragment` }, messages()), false);
  109. assert.equal((await collector.runOnce()).requests, 0); assert.equal(calls, 0); assert.equal(store.stats().total, 0);
  110. } finally { store.close(); }
  111. });
  112. test('both observed Teams hosts and unq private-chat identities are supported only within the original origin', async () => {
  113. const store = createStore(':memory:');
  114. try {
  115. const chat = `19:${self}_${other}@unq.gbl.spaces`;
  116. const collector = createChatMetadataCollector({ store, fetch: async () => Response.json(details(chat)) });
  117. assert.equal(collector.observe({ ...observation(chat), requestUrl: `${base(chat, 'teams.microsoft.com')}/messages` },
  118. { messages: [{ conversationid: chat, conversationLink: base(chat, 'teams.microsoft.com') }] }), true);
  119. assert.equal((await collector.runOnce()).changed, 1);
  120. } finally { store.close(); }
  121. });
  122. test('same chat IDs remain isolated across account records and persistent cache keys', async () => {
  123. const store = createStore(':memory:'); let calls = 0;
  124. try {
  125. const collector = createChatMetadataCollector({ store, fetch: async () => Response.json(details(chatId, ++calls === 1 ? 'First group' : 'Second group')) });
  126. collector.observe(observation(), messages()); collector.observe(observation(chatId, account2), messages());
  127. const result = await collector.runOnce(); assert.equal(result.requests, 2); assert.equal(result.changed, 2);
  128. assert.equal(store.getRecordByIdentity(scope()).title, 'First group');
  129. assert.equal(store.getRecordByIdentity(scope(chatId, account2)).title, 'Second group');
  130. assert.equal(store.readRows("SELECT key FROM state WHERE key LIKE 'web-api:chat-metadata:%'").length, 2);
  131. } finally { store.close(); }
  132. });
  133. test('zero budget performs no HTTP and a larger requested budget still sends at most three detail reads', async () => {
  134. const store = createStore(':memory:'); let calls = 0;
  135. try {
  136. const collector = createChatMetadataCollector({ store, fetch: async raw => {
  137. calls++; return Response.json(details(decodeURIComponent(new URL(raw).pathname.split('/').at(-1))));
  138. } });
  139. for (let index = 0; index < 5; index++) { const chat = `19:group-${index}@thread.v2`; collector.observe(observation(chat), messages(chat)); }
  140. assert.equal((await collector.runOnce({ maxRequests: 0 })).requests, 0);
  141. assert.equal((await collector.runOnce({ maxRequests: 100 })).requests, 3); assert.equal(calls, 3);
  142. assert.deepEqual(collector.coverage(), { discovered: 5, cached: 3, pending: 2, paused: 0, rateLimited: 0 });
  143. assert.equal((await collector.runOnce()).requests, 2); assert.equal(calls, 5);
  144. } finally { store.close(); }
  145. });
  146. test('success cache survives collector recreation and unchanged valid detail does not add a record version', async () => {
  147. const store = createStore(':memory:'); let at = 2000000000000, calls = 0;
  148. try {
  149. const make = () => createChatMetadataCollector({ store, now: () => at, fetch: async () => { calls++; return Response.json(details()); } });
  150. const first = make(); first.observe(observation(), messages()); await first.runOnce(); const versions = store.stats().versions;
  151. const next = make(); next.observe(observation(), messages()); assert.equal((await next.runOnce()).requests, 0);
  152. at += 30 * 60_000 - 1; assert.equal((await next.runOnce()).requests, 0);
  153. at++; assert.equal((await next.runOnce()).successful, 1); assert.equal(calls, 2); assert.equal(store.stats().versions, versions);
  154. } finally { store.close(); }
  155. });
  156. test('empty and older topics preserve known titles; newer details retain previously normalized member evidence', async () => {
  157. const store = createStore(':memory:'); let at = 2000000000000, body = details(chatId, '');
  158. try {
  159. store.upsertRecord({ ...scope(), title: 'Current title', text: '', updatedAt: '2026-09-08T06:46:40.000Z',
  160. raw: { version: 1788850000000, titleSource: 'explicit', isOneOnOne: false, members: [{ userId: other, displayName: 'Fictional person' }] } });
  161. const collector = createChatMetadataCollector({ store, now: () => at, fetch: async () => Response.json(body) });
  162. collector.observe(observation(), messages()); assert.equal((await collector.runOnce()).changed, 0);
  163. at += 30 * 60_000; body = details(chatId, 'Old title', '1788840000000'); assert.equal((await collector.runOnce()).changed, 0);
  164. assert.equal(store.getRecordByIdentity(scope()).title, 'Current title');
  165. at += 30 * 60_000; body = details(chatId, '<b>New &amp; clean</b>', '1788860000000'); assert.equal((await collector.runOnce()).changed, 1);
  166. const chat = store.getRecordByIdentity(scope()); assert.equal(chat.title, 'New & clean'); assert.equal(chat.raw.isOneOnOne, false);
  167. assert.equal(chat.raw.members[0].userId, other); assert.equal(chat.raw.titleSource, 'conversation-details');
  168. } finally { store.close(); }
  169. });
  170. test('missing or null topics cache a valid unnamed response without inventing or erasing chat titles', async () => {
  171. const store = createStore(':memory:'); let at = 2000000000000, calls = 0;
  172. try {
  173. const knownChat = '19:known-group@thread.v2';
  174. store.upsertRecord({ ...scope(knownChat), title: 'Preserved title', text: '', raw: { titleSource: 'explicit' } });
  175. const original = store.getRecordByIdentity(scope(knownChat));
  176. const collector = createChatMetadataCollector({ store, now: () => at, fetch: async raw => {
  177. calls++;
  178. const id = decodeURIComponent(new URL(raw).pathname.split('/').at(-1)), body = details(id);
  179. if (id === chatId) delete body.threadProperties.topic;
  180. else body.threadProperties.topic = null;
  181. return Response.json(body);
  182. } });
  183. collector.observe(observation(), messages()); collector.observe(observation(knownChat), messages(knownChat));
  184. const result = await collector.runOnce();
  185. assert.equal(result.successful, 2); assert.equal(result.changed, 0); assert.deepEqual(result.errors, []);
  186. assert.equal(store.getRecordByIdentity(scope()), null);
  187. assert.equal(store.getRecordByIdentity(scope(knownChat)).hash, original.hash);
  188. assert.equal(store.getRecordByIdentity(scope(knownChat)).title, 'Preserved title');
  189. assert.equal((await collector.runOnce()).requests, 0);
  190. at += 30 * 60_000 - 1; assert.equal((await collector.runOnce()).requests, 0); assert.equal(calls, 2);
  191. at++; assert.equal((await collector.runOnce()).successful, 2); assert.equal(calls, 4); assert.equal(store.stats().versions, 1);
  192. } finally { store.close(); }
  193. });
  194. test('unrecognized or mismatched detail schemas never save names and retry only after the failure TTL', async () => {
  195. const variants = [value => { value.id = '19:other@thread.v2'; }, value => { value.type = 'Message'; },
  196. value => { value.version = '17'; }, value => { value.threadProperties = null; },
  197. value => { value.threadProperties.version = 'not-a-version'; }, value => { value.threadProperties.topic = {}; },
  198. value => { value.threadProperties.topic = 42; }, value => { value.threadProperties.topic = []; },
  199. value => { delete value.threadProperties.topic; value.id = '19:other@thread.v2'; },
  200. value => { value.threadProperties.topic = null; value.threadProperties.version = 'not-a-version'; },
  201. value => { value.threadProperties.threadType = 'unknown'; }];
  202. for (const modify of variants) {
  203. const store = createStore(':memory:'); let at = 2000000000000, calls = 0;
  204. try {
  205. const body = details(); modify(body);
  206. const collector = createChatMetadataCollector({ store, now: () => at, fetch: async () => { calls++; return Response.json(body); } });
  207. collector.observe(observation(), messages()); const result = await collector.runOnce(); publicResult(result);
  208. assert.equal(result.errors[0].status, 200); assert.equal(result.errors[0].code, 'invalid-metadata');
  209. assert.match(result.errors[0].reason, /校验/); assert.equal(store.stats().total, 0);
  210. assert.equal((await collector.runOnce()).requests, 0); at += 5 * 60_000; assert.equal((await collector.runOnce()).requests, 1); assert.equal(calls, 2);
  211. } finally { store.close(); }
  212. }
  213. });
  214. test('401 and 403 pause a detail plan even when its messages are observed again', async () => {
  215. for (const status of [401, 403]) {
  216. const store = createStore(':memory:'); let at = 2000000000000;
  217. try {
  218. const collector = createChatMetadataCollector({ store, now: () => at, fetch: async () => new Response('private-error-marker', { status }) });
  219. collector.observe(observation(), messages()); const result = await collector.runOnce(); assert.equal(result.paused, 1); publicResult(result);
  220. at += 60 * 60_000; collector.observe(observation(), messages()); assert.equal((await collector.runOnce()).requests, 0);
  221. const renewed = createChatMetadataCollector({ store, now: () => at, fetch: async () => Response.json(details()) });
  222. renewed.observe(observation(), messages()); assert.equal((await renewed.runOnce()).changed, 1);
  223. } finally { store.close(); }
  224. }
  225. });
  226. test('429 Retry-After persists an origin cooldown that defers other chats across recreation', async () => {
  227. const store = createStore(':memory:'); let at = 2000000000000, calls = 0;
  228. try {
  229. const make = response => createChatMetadataCollector({ store, now: () => at, fetch: async raw => { calls++; return response(raw); } });
  230. const first = make(() => new Response('', { status: 429, headers: { 'retry-after': '1200' } }));
  231. for (const chat of [chatId, '19:second@thread.v2']) first.observe(observation(chat), messages(chat));
  232. const result = await first.runOnce(); assert.equal(result.requests, 1); assert.equal(result.rateLimited, 2); publicResult(result);
  233. const next = make(raw => Response.json(details(decodeURIComponent(new URL(raw).pathname.split('/').at(-1)))));
  234. next.observe(observation('19:second@thread.v2'), messages('19:second@thread.v2'));
  235. at += 20 * 60_000 - 1; assert.equal((await next.runOnce()).requests, 0);
  236. at++; assert.equal((await next.runOnce()).successful, 1); assert.equal(calls, 2);
  237. } finally { store.close(); }
  238. });
  239. test('redirects, unexpected MIME, oversize streamed bodies and provider exceptions return only safe errors', async () => {
  240. const responses = [() => new Response('private-redirect', { status: 302, headers: { location: 'https://evil.example/private-target' } }),
  241. () => new Response('private-html', { headers: { 'content-type': 'text/html' } }),
  242. () => new Response('{}', { headers: { 'content-type': 'application/json', 'content-length': '2097153' } }),
  243. () => new Response('x'.repeat(2097153), { headers: { 'content-type': 'application/json' } }),
  244. () => { throw new Error('Bearer private-token-marker https://evil.example/private-secret'); }];
  245. for (const response of responses) {
  246. const store = createStore(':memory:'); let calls = 0;
  247. try {
  248. const collector = createChatMetadataCollector({ store, fetch: async () => { calls++; return response(); } });
  249. collector.observe(observation(), messages()); const result = await collector.runOnce(); publicResult(result);
  250. assert.equal(result.successful, 0); assert.equal(result.errors.length, 1); assert.equal(store.stats().total, 0); assert.equal(calls, 1);
  251. } finally { store.close(); }
  252. }
  253. });
  254. test('cancelling a pending request prevents record and cache writes even if an injected fetch ignores abort', async () => {
  255. const store = createStore(':memory:'); const controller = new AbortController(); let release;
  256. try {
  257. const collector = createChatMetadataCollector({ store, fetch: () => new Promise(resolve => { release = resolve; }) });
  258. collector.observe(observation(), messages()); const running = collector.runOnce({ signal: controller.signal });
  259. controller.abort(); const result = await running; assert.equal(result.cancelled, true);
  260. let cancelled = false;
  261. release(new Response(new ReadableStream({ cancel() { cancelled = true; } }), { headers: { 'content-type': 'application/json' } }));
  262. await new Promise(resolve => setImmediate(resolve)); assert.equal(cancelled, true);
  263. assert.equal(store.stats().total, 0); assert.equal(store.readRows("SELECT key FROM state WHERE key LIKE 'web-api:chat-metadata:%'").length, 0);
  264. } finally { store.close(); }
  265. });
  266. test('cancellation while streaming does not save parsed metadata', async () => {
  267. const store = createStore(':memory:'); const controller = new AbortController(); let streaming, cancelled = false;
  268. try {
  269. const ready = new Promise(resolve => { streaming = resolve; });
  270. const collector = createChatMetadataCollector({ store, fetch: async () => new Response(new ReadableStream({
  271. pull() { streaming(); }, cancel() { cancelled = true; }
  272. }), { headers: { 'content-type': 'application/json' } }) });
  273. collector.observe(observation(), messages()); const run = collector.runOnce({ signal: controller.signal }); await ready;
  274. controller.abort(); assert.equal((await run).cancelled, true); assert.equal(store.stats().total, 0); assert.equal(cancelled, true);
  275. // The caller is released even if a transport cannot finish its response.
  276. assert.equal(collector.coverage().pending, 1);
  277. } finally { store.close(); }
  278. });
  279. test('concurrent passes share a single request and bounded plain-text topics preserve Unicode', async () => {
  280. const store = createStore(':memory:'); let release, calls = 0;
  281. try {
  282. const collector = createChatMetadataCollector({ store, fetch: () => { calls++; return new Promise(resolve => { release = resolve; }); } });
  283. collector.observe(observation(), messages()); const first = collector.runOnce(), second = collector.runOnce();
  284. release(Response.json(details(chatId, '😀'.repeat(501)))); await Promise.all([first, second]);
  285. assert.equal(calls, 1); assert.equal([...store.getRecordByIdentity(scope()).title].length, 500); assert.equal(store.stats().versions, 1);
  286. } finally { store.close(); }
  287. });
  288. test('the ten-second timeout releases an unresponsive fetch and later cancels its response body', async context => {
  289. context.mock.timers.enable({ apis: ['setTimeout'] });
  290. const store = createStore(':memory:'); let release, cancelled = false;
  291. try {
  292. const collector = createChatMetadataCollector({ store, fetch: () => new Promise(resolve => { release = resolve; }) });
  293. collector.observe(observation(), messages()); const run = collector.runOnce();
  294. context.mock.timers.tick(9999); assert.equal(store.stats().total, 0);
  295. context.mock.timers.tick(1); const result = await run; assert.equal(result.errors[0].code, 'timeout'); publicResult(result);
  296. release(new Response(new ReadableStream({ cancel() { cancelled = true; } }), { headers: { 'content-type': 'application/json' } }));
  297. await new Promise(resolve => setImmediate(resolve)); assert.equal(cancelled, true); assert.equal(store.stats().total, 0);
  298. } finally { store.close(); context.mock.timers.reset(); }
  299. });