chat-names.test.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { createStore } from '../src/store.js';
  4. import { currentChatTitle, normalizeChatName, preserveChatName, refreshChatNames } from '../src/chat-names.js';
  5. const tenant = '11111111-1111-1111-1111-111111111111', self = '22222222-2222-2222-2222-222222222222';
  6. const peer = '33333333-3333-3333-3333-333333333333', third = '44444444-4444-4444-4444-444444444444';
  7. const accountId = `web-api:${tenant}:${self}`;
  8. const chat = (overrides = {}) => ({ source: 'web-api', accountId, kind: 'chat', externalId: 'fictional-chat', title: '聊天',
  9. raw: { isOneOnOne: true, titleSource: 'placeholder' }, ...overrides });
  10. const message = (id, senderId, author, overrides = {}) => ({ source: 'web-api', accountId, kind: 'message', externalId: id,
  11. parentId: 'fictional-chat', title: '聊天', text: 'Fictional message', occurredAt: '2026-09-08T10:00:00Z', raw: { senderId, selfUserId: self, author }, ...overrides });
  12. test('member naming excludes self, validates identities and preserves explicitly named generic titles', () => {
  13. const item = { title: '', isOneOnOne: true, members: [
  14. { objectId: self, mri: `8:orgid:${self}`, friendlyName: 'Fictional self' },
  15. { objectId: peer, mri: `8:orgid:${peer}`, friendlyName: '<b>Fictional peer</b>', authorization: 'secret-marker' },
  16. ] };
  17. const named = normalizeChatName(item, accountId);
  18. assert.equal(named.title, 'Fictional peer'); assert.equal(named.raw.titleSource, 'members'); assert.equal(named.raw.peerId, peer);
  19. assert.equal(named.raw.members.length, 2); assert.doesNotMatch(JSON.stringify(named), /secret-marker|8:orgid/);
  20. assert.equal(normalizeChatName({ ...item, title: '聊天' }, accountId).raw.titleSource, 'explicit');
  21. assert.equal(normalizeChatName(item, 'unknown-account').title, '聊天');
  22. assert.equal(normalizeChatName({ ...item, members: [{ objectId: peer, mri: `8:orgid:${third}`, friendlyName: 'Unverified' }] }, accountId).title, '聊天');
  23. assert.equal(normalizeChatName({ ...item, members: [{ friendlyName: 'Unidentified' }] }, accountId).title, '聊天');
  24. });
  25. test('a self-authored message cannot name the peer; the most recent other-author name can', () => {
  26. const store = createStore(':memory:');
  27. try {
  28. store.upsertRecord(chat());
  29. store.upsertRecord(message('old-peer', peer, 'Fictional former peer name'), { ingestion: 'history' });
  30. store.upsertRecord(message('new-peer', peer, 'Fictional current peer name', { occurredAt: '2026-09-08T11:00:00Z' }), { ingestion: 'history' });
  31. store.upsertRecord(message('my-message', self, 'Fictional self name', { occurredAt: '2026-09-08T12:00:00Z' }), { ingestion: 'history' });
  32. const snapshot = () => ({ records: store.readRows("SELECT * FROM records WHERE kind='message' ORDER BY id"),
  33. versions: store.readRows("SELECT v.* FROM versions v JOIN records r ON v.recordId=r.id WHERE r.kind='message' ORDER BY v.id") });
  34. const before = snapshot();
  35. assert.equal(refreshChatNames(store), 1);
  36. const resolved = store.getRecordByIdentity(chat());
  37. assert.equal(resolved.title, 'Fictional current peer name'); assert.equal(resolved.raw.titleSource, 'message-author'); assert.equal(resolved.raw.peerId, peer);
  38. assert.deepEqual(snapshot(), before);
  39. assert.equal(store.readOne('SELECT ingestion FROM change_log ORDER BY seq DESC LIMIT 1').ingestion, 'import');
  40. const originalUpsert = store.upsertRecord;
  41. store.upsertRecord = () => assert.fail('An unchanged derived title must not refresh lastSeenAt');
  42. assert.equal(refreshChatNames(store), 0);
  43. store.upsertRecord = originalUpsert;
  44. store.upsertRecord(message('same-peer-name', peer, 'Fictional current peer name', { occurredAt: '2026-09-08T13:00:00Z' }));
  45. store.upsertRecord = () => assert.fail('A newer message with the same name must not rewrite the chat');
  46. assert.equal(refreshChatNames(store), 0);
  47. } finally { store.close(); }
  48. });
  49. test('verified member names take precedence and a stored derived name tracks a new peer display name', () => {
  50. const store = createStore(':memory:');
  51. try {
  52. store.upsertRecord(chat({ raw: { isOneOnOne: true, titleSource: 'placeholder', membersComplete: true,
  53. members: [{ userId: self, displayName: 'Fictional self' }, { userId: peer, displayName: 'Fictional roster peer' }] } }));
  54. store.upsertRecord(message('peer', peer, 'Fictional historical author'));
  55. assert.equal(refreshChatNames(store), 1);
  56. assert.equal(store.getRecordByIdentity(chat()).title, 'Fictional roster peer');
  57. store.upsertRecord(chat({ title: 'Old peer name', raw: { isOneOnOne: true, titleSource: 'message-author', peerId: peer } }));
  58. assert.equal(refreshChatNames(store), 1);
  59. assert.equal(store.getRecordByIdentity(chat()).title, 'Fictional historical author');
  60. } finally { store.close(); }
  61. });
  62. test('group, unknown, ambiguous, masked and explicitly named chats are not guessed', () => {
  63. for (const overrides of [
  64. { raw: { isOneOnOne: false, titleSource: 'placeholder' } }, { raw: { titleSource: 'placeholder' } },
  65. { title: '聊天', raw: { isOneOnOne: true, titleSource: 'explicit' } },
  66. { title: 'Explicit fictional title', raw: { isOneOnOne: true } },
  67. { raw: { isOneOnOne: true, titleSource: 'placeholder', members: [{ userId: peer, isIdentityMasked: true }] } },
  68. { raw: { isOneOnOne: true, titleSource: 'placeholder', membersComplete: false, members: [{ userId: peer }] } },
  69. { raw: { isOneOnOne: true, titleSource: 'placeholder', members: [{ userId: peer }, { userId: third }] } },
  70. ]) {
  71. const store = createStore(':memory:');
  72. try { store.upsertRecord(chat(overrides)); store.upsertRecord(message('peer', peer, 'Fictional peer')); assert.equal(refreshChatNames(store), 0); }
  73. finally { store.close(); }
  74. }
  75. const store = createStore(':memory:');
  76. try {
  77. store.upsertRecord(chat()); store.upsertRecord(message('peer', peer, 'Fictional peer')); store.upsertRecord(message('third', third, 'Fictional third'));
  78. assert.equal(refreshChatNames(store), 0);
  79. } finally { store.close(); }
  80. });
  81. test('peer author lookups remain in the same account, source and conversation', () => {
  82. const store = createStore(':memory:');
  83. try {
  84. store.upsertRecord(chat());
  85. store.upsertRecord(message('other-account', peer, 'Wrong account', { accountId: 'another-account' }));
  86. store.upsertRecord(message('other-source', peer, 'Wrong source', { source: 'browser' }));
  87. store.upsertRecord(message('other-chat', peer, 'Wrong chat', { parentId: 'another-chat' }));
  88. store.upsertRecord(message('self', self, 'Fictional self'));
  89. assert.equal(refreshChatNames(store), 0);
  90. store.upsertRecord(message('correct', peer, 'Correct fictional peer'));
  91. assert.equal(refreshChatNames(store, { accountId, chatId: 'fictional-chat' }), 1);
  92. } finally { store.close(); }
  93. });
  94. test('missing list names preserve useful titles, while an explicit replacement or changed peer wins', () => {
  95. const previous = chat({ title: 'Fictional peer', raw: { isOneOnOne: true, titleSource: 'message-author', peerId: peer } });
  96. assert.equal(preserveChatName(chat(), previous).title, 'Fictional peer');
  97. assert.equal(preserveChatName(chat({ title: '聊天', raw: { isOneOnOne: true, titleSource: 'explicit' } }), previous).title, '聊天');
  98. assert.equal(preserveChatName(chat({ raw: { isOneOnOne: true, titleSource: 'placeholder', members: [{ userId: third }] } }), previous).title, '聊天');
  99. const store = createStore(':memory:');
  100. try {
  101. store.upsertRecord(previous);
  102. assert.equal(currentChatTitle(store, accountId, 'fictional-chat', '聊天'), 'Fictional peer');
  103. assert.equal(currentChatTitle(store, 'another-account', 'fictional-chat', 'Other fallback'), 'Other fallback');
  104. } finally { store.close(); }
  105. });
  106. test('derived names are not retained when the new roster contradicts private-chat or visible-identity evidence', () => {
  107. for (const titleSource of ['members', 'message-author']) {
  108. const previous = chat({ title: 'Fictional peer', raw: { isOneOnOne: true, titleSource, peerId: peer } });
  109. for (const evidence of [
  110. { chatType: 'group' }, { chatType: 'meeting' }, { threadType: 'meeting' },
  111. { membersComplete: false }, { members: [{ userId: peer, isIdentityMasked: true }] },
  112. ]) {
  113. const incoming = chat({ raw: { isOneOnOne: true, titleSource: 'placeholder', ...evidence } });
  114. assert.equal(preserveChatName(incoming, previous).title, '聊天');
  115. assert.equal(preserveChatName(incoming, previous).raw.titleSource, 'placeholder');
  116. }
  117. assert.equal(preserveChatName(chat(), previous).title, 'Fictional peer');
  118. }
  119. });
  120. const participantChatId = `19:${self}_${peer}@unq.gbl.spaces`;
  121. const participantChatRecord = (overrides = {}) => chat({ externalId: participantChatId, raw: { titleSource: 'placeholder' }, ...overrides });
  122. const participantMessage = (id, senderId, author, overrides = {}) => message(id, senderId, author, { parentId: participantChatId, ...overrides });
  123. const messageSnapshot = store => ({ records: store.readRows("SELECT * FROM records WHERE kind='message' ORDER BY id"),
  124. versions: store.readRows("SELECT v.* FROM versions v JOIN records r ON v.recordId=r.id WHERE r.kind='message' ORDER BY v.id") });
  125. test('canonical participant IDs and a local peer author name an uncatalogued emoji conversation without classification', () => {
  126. const store = createStore(':memory:');
  127. try {
  128. store.upsertRecord(participantMessage('emoji', peer, '<b>Fictional peer</b>', { text: '🔥' }), { ingestion: 'history' });
  129. store.upsertRecord(participantMessage('own', self, 'Fictional self', { occurredAt: '2026-09-08T12:00:00Z' }));
  130. const before = messageSnapshot(store);
  131. assert.equal(refreshChatNames(store, { accountId, chatId: participantChatId }), 1);
  132. const resolved = store.getRecordByIdentity(participantChatRecord());
  133. assert.equal(resolved.title, 'Fictional peer');
  134. assert.deepEqual(resolved.raw, { chatId: participantChatId, titleSource: 'conversation-peer', peerId: peer, nameEvidence: 'unq-participant-ids' });
  135. assert.equal(store.getRecord(store.getRecordByIdentity(participantMessage('emoji')).id).displayTitle, 'Fictional peer');
  136. assert.equal(store.getRecord(store.getRecordByIdentity(participantMessage('own')).id).displayTitle, 'Fictional peer');
  137. assert.deepEqual(messageSnapshot(store), before);
  138. assert.equal(store.readOne('SELECT ingestion FROM change_log ORDER BY seq DESC LIMIT 1').ingestion, 'import');
  139. const records = store.readRows('SELECT * FROM records ORDER BY id'), versions = store.readRows('SELECT * FROM versions ORDER BY id');
  140. store.upsertRecord = () => assert.fail('An unchanged peer name must not write metadata or versions');
  141. assert.equal(refreshChatNames(store), 0);
  142. assert.deepEqual(store.readRows('SELECT * FROM records ORDER BY id'), records);
  143. assert.deepEqual(store.readRows('SELECT * FROM versions ORDER BY id'), versions);
  144. } finally { store.close(); }
  145. });
  146. test('the same participant conversation resolves the opposite peer independently for each account', () => {
  147. const store = createStore(':memory:'), otherAccount = `web-api:${tenant}:${peer}`;
  148. try {
  149. store.upsertRecord(participantMessage('for-first', peer, 'Fictional second person'));
  150. store.upsertRecord(participantMessage('for-second', self, 'Fictional first person', {
  151. accountId: otherAccount, raw: { senderId: self, selfUserId: peer, author: 'Fictional first person' } }));
  152. assert.equal(refreshChatNames(store, { accountId }), 1);
  153. assert.equal(store.getRecordByIdentity(participantChatRecord({ accountId: otherAccount })), null);
  154. assert.equal(refreshChatNames(store), 1);
  155. assert.equal(currentChatTitle(store, accountId, participantChatId), 'Fictional second person');
  156. assert.equal(currentChatTitle(store, otherAccount, participantChatId), 'Fictional first person');
  157. assert.equal(store.getRecordByIdentity(participantChatRecord({ accountId: otherAccount })).raw.peerId, self);
  158. } finally { store.close(); }
  159. });
  160. test('participant fallback rejects incomplete identifiers and missing local peer evidence', () => {
  161. const cases = [
  162. { label: 'fake UUID', parentId: `19:${self}_fake@unq.gbl.spaces` },
  163. { label: 'repeated self', parentId: `19:${self}_${self}@unq.gbl.spaces` },
  164. { label: 'account absent from pair', parentId: `19:${third}_${peer}@unq.gbl.spaces` },
  165. { label: 'thread suffix only', parentId: `19:${self}_${peer}@thread.v2` },
  166. { label: 'encoded IDs', parentId: `19%3A${self}_${peer}%40unq.gbl.spaces` },
  167. { label: 'trailing content', parentId: `${participantChatId}:extra` },
  168. { label: 'invalid account tenant', accountId: `web-api:tenant:${self}` },
  169. { label: 'invalid self identity', accountId: `web-api:${tenant}:self` },
  170. { label: 'another source', source: 'browser' },
  171. { label: 'only own message', raw: { senderId: self, selfUserId: self, author: 'Fictional self' } },
  172. { label: 'deleted peer', deleted: true },
  173. { label: 'unknown sender', raw: { senderId: 'not-a-uuid', selfUserId: self, author: 'Unknown person' } },
  174. { label: 'empty author', raw: { senderId: peer, selfUserId: self, author: '<b></b>' } },
  175. { label: 'wrong self claim', raw: { senderId: peer, selfUserId: third, author: 'Fictional peer' } },
  176. ];
  177. for (const { label, ...overrides } of cases) {
  178. const store = createStore(':memory:');
  179. try {
  180. store.upsertRecord(participantMessage('candidate', peer, 'Fictional peer', overrides));
  181. assert.equal(refreshChatNames(store), 0, label);
  182. assert.equal(store.readOne("SELECT COUNT(*) n FROM records WHERE kind='chat'").n, 0, label);
  183. } finally { store.close(); }
  184. }
  185. });
  186. test('peer name discovery rejects contradictory senders, identity claims and conversation metadata', () => {
  187. const cases = [
  188. { label: 'third sender', extra: participantMessage('third', third, 'Third person') },
  189. { label: 'wrong self claim on another message', extra: participantMessage('wrong-self', self, 'Fictional self',
  190. { raw: { senderId: self, selfUserId: third, author: 'Fictional self' } }) },
  191. ...[{ isOneOnOne: false }, { chatType: 'group' }, { chatType: 'meeting' }, { threadType: 'meeting' },
  192. { isIdentityMasked: true }, { membersComplete: false },
  193. { membersComplete: true, members: [{ userId: self }, { userId: peer, isIdentityMasked: true }] },
  194. { membersComplete: true, members: [{ userId: self }, { userId: third }] },
  195. { members: 'invalid' }].map(evidence => ({ label: `message evidence ${JSON.stringify(evidence)}`,
  196. extra: participantMessage('message-evidence', self, 'Fictional self',
  197. { raw: { senderId: self, selfUserId: self, author: 'Fictional self', ...evidence } }) })),
  198. ...[
  199. { isOneOnOne: false }, { chatType: 'group' }, { chatType: 'meeting' }, { threadType: 'group' }, { threadType: 'meeting' },
  200. { membersComplete: false }, { isIdentityMasked: true }, { selfUserId: third }, { peerId: third },
  201. { members: [{ userId: self }, { userId: peer }] },
  202. { membersComplete: true, members: [{ userId: peer }] },
  203. { membersComplete: true, members: [{ userId: self }, { userId: third }] },
  204. { membersComplete: true, members: [{ userId: self }, { userId: peer, isIdentityMasked: true }] },
  205. { membersComplete: true, members: [{ userId: self }, { userId: self }] },
  206. { membersComplete: true, members: [{ userId: self }, { userId: 'invalid' }] },
  207. { membersComplete: true, members: 'invalid' },
  208. ].map(raw => ({ label: JSON.stringify(raw), chat: participantChatRecord({ raw: { titleSource: 'placeholder', ...raw } }) })),
  209. { label: 'deleted conversation', chat: participantChatRecord({ deleted: true }) },
  210. ];
  211. for (const { label, extra, chat: existing } of cases) {
  212. const store = createStore(':memory:');
  213. try {
  214. if (existing) store.upsertRecord(existing);
  215. store.upsertRecord(participantMessage('candidate', peer, 'Fictional peer'));
  216. if (extra) store.upsertRecord(extra);
  217. const before = store.readRows('SELECT * FROM records ORDER BY id');
  218. assert.equal(refreshChatNames(store), 0, label);
  219. assert.deepEqual(store.readRows('SELECT * FROM records ORDER BY id'), before, label);
  220. } finally { store.close(); }
  221. }
  222. });
  223. test('participant discovery uses only the requested source, account and conversation', () => {
  224. const store = createStore(':memory:'), otherChat = `19:${self}_${third}@unq.gbl.spaces`;
  225. try {
  226. store.upsertRecord(participantMessage('other-account', peer, 'Wrong account', { accountId: `web-api:${third}:${self}` }));
  227. store.upsertRecord(participantMessage('other-source', peer, 'Wrong source', { source: 'browser' }));
  228. store.upsertRecord(participantMessage('other-chat', peer, 'Wrong conversation', { parentId: otherChat }));
  229. store.upsertRecord(participantMessage('self-only', self, 'Fictional self'));
  230. assert.equal(refreshChatNames(store, { accountId, chatId: participantChatId }), 0);
  231. assert.equal(store.readOne("SELECT COUNT(*) n FROM records WHERE kind='chat'").n, 0);
  232. store.upsertRecord(participantMessage('right-peer', peer, 'Correct fictional peer'));
  233. assert.equal(refreshChatNames(store, { accountId, chatId: participantChatId }), 1);
  234. assert.equal(store.readOne("SELECT COUNT(*) n FROM records WHERE kind='chat'").n, 1);
  235. assert.equal(currentChatTitle(store, accountId, participantChatId), 'Correct fictional peer');
  236. } finally { store.close(); }
  237. });
  238. test('explicit and useful unknown titles take precedence over participant author fallback', () => {
  239. for (const existing of [
  240. participantChatRecord({ title: '聊天', raw: { titleSource: 'explicit' } }),
  241. participantChatRecord({ title: 'Fictional server topic', raw: { titleSource: 'conversation-details' } }),
  242. participantChatRecord({ title: 'Fictional named chat', raw: {} }),
  243. participantChatRecord({ title: 'Fictional named chat', raw: { titleSource: 'future-source' } }),
  244. participantChatRecord({ title: 'Fictional roster name', raw: { titleSource: 'members', peerId: peer } }),
  245. ]) {
  246. const store = createStore(':memory:');
  247. try {
  248. store.upsertRecord(existing); store.upsertRecord(participantMessage('peer', peer, 'Fictional peer'));
  249. assert.equal(refreshChatNames(store), 0);
  250. assert.equal(store.getRecordByIdentity(existing).title, existing.title);
  251. } finally { store.close(); }
  252. }
  253. });
  254. test('a canonical participant chat keeps verified member names ahead of historical author names', () => {
  255. const store = createStore(':memory:');
  256. try {
  257. store.upsertRecord(participantChatRecord({ raw: { titleSource: 'placeholder', isOneOnOne: true, membersComplete: true,
  258. members: [{ userId: self, displayName: 'Fictional self' }, { userId: peer, displayName: 'Fictional roster name' }] } }));
  259. store.upsertRecord(participantMessage('peer', peer, 'Fictional historic author'));
  260. assert.equal(refreshChatNames(store), 1);
  261. const resolved = store.getRecordByIdentity(participantChatRecord());
  262. assert.equal(resolved.title, 'Fictional roster name'); assert.equal(resolved.raw.titleSource, 'members');
  263. assert.equal(resolved.raw.isOneOnOne, true); assert.equal(resolved.raw.nameEvidence, undefined);
  264. assert.equal(refreshChatNames(store), 0);
  265. } finally { store.close(); }
  266. });
  267. test('participant fallback follows valid peer name changes while preserving existing metadata and message history', () => {
  268. const store = createStore(':memory:');
  269. try {
  270. store.upsertRecord(participantChatRecord({ raw: { titleSource: 'placeholder', isOneOnOne: true, membersComplete: true,
  271. members: [{ userId: self }, { userId: peer }], originalEvidence: 'retained' } }));
  272. store.upsertRecord(participantMessage('old', peer, 'Fictional old name'));
  273. assert.equal(refreshChatNames(store), 1);
  274. store.upsertRecord(participantMessage('new', peer, 'Fictional new name', { occurredAt: '2026-09-08T07:00:00-04:00' }));
  275. store.upsertRecord(participantMessage('blank', peer, '<b></b>', { occurredAt: '2026-09-08T12:00:00Z' }));
  276. store.upsertRecord(participantMessage('self', self, 'Latest self name', { occurredAt: '2026-09-08T13:00:00Z' }));
  277. const before = messageSnapshot(store);
  278. assert.equal(refreshChatNames(store), 1);
  279. const updated = store.getRecordByIdentity(participantChatRecord());
  280. assert.equal(updated.title, 'Fictional new name'); assert.equal(updated.raw.originalEvidence, 'retained');
  281. assert.equal(updated.raw.isOneOnOne, true); assert.equal(updated.raw.members.length, 2);
  282. assert.deepEqual(messageSnapshot(store), before);
  283. assert.equal(refreshChatNames(store), 0);
  284. assert.equal(store.readOne('SELECT COUNT(*) n FROM versions WHERE recordId=?', [updated.id]).n, 3);
  285. } finally { store.close(); }
  286. });
  287. test('incremental placeholder names retain participant evidence only while the account, peer and roster agree', () => {
  288. const previous = participantChatRecord({ title: 'Fictional peer', raw: { titleSource: 'conversation-peer', peerId: peer,
  289. chatId: participantChatId, nameEvidence: 'unq-participant-ids' } });
  290. const preserved = preserveChatName(participantChatRecord(), previous);
  291. assert.equal(preserved.title, 'Fictional peer'); assert.equal(preserved.raw.nameEvidence, 'unq-participant-ids');
  292. assert.equal(preserved.raw.isOneOnOne, undefined); assert.equal(preserved.raw.chatType, undefined);
  293. for (const evidence of [
  294. { isOneOnOne: false }, { chatType: 'group' }, { threadType: 'meeting' }, { peerId: third }, { selfUserId: third },
  295. { membersComplete: false }, { membersComplete: true, members: [{ userId: self }, { userId: third }] },
  296. { membersComplete: true, members: [{ userId: self }, { userId: peer, isIdentityMasked: true }] },
  297. ]) {
  298. const incoming = participantChatRecord({ raw: { titleSource: 'placeholder', ...evidence } });
  299. assert.deepEqual(preserveChatName(incoming, previous), incoming);
  300. }
  301. for (const incoming of [
  302. participantChatRecord({ accountId: `web-api:${tenant}:${peer}` }),
  303. participantChatRecord({ externalId: `19:${self}_${third}@unq.gbl.spaces` }),
  304. participantChatRecord({ source: 'browser' }),
  305. participantChatRecord({ title: 'New topic', raw: { titleSource: 'explicit' } }),
  306. ]) assert.deepEqual(preserveChatName(incoming, previous), incoming);
  307. });
  308. test('new conflicting evidence withdraws only locally derived peer names without rewriting messages', () => {
  309. const cases = [
  310. store => store.upsertRecord(participantMessage('third', third, 'Fictional third')),
  311. store => store.upsertRecord(participantMessage('wrong-self', self, 'Fictional self',
  312. { raw: { senderId: self, selfUserId: third, author: 'Fictional self' } })),
  313. store => store.upsertRecord(participantMessage('peer', peer, 'Fictional peer', { deleted: true })),
  314. ...[{ isOneOnOne: false }, { chatType: 'meeting' }, { membersComplete: false },
  315. { membersComplete: true, members: [{ userId: self }, { userId: peer, isIdentityMasked: true }] }]
  316. .map(evidence => store => {
  317. const current = store.getRecordByIdentity(participantChatRecord());
  318. store.upsertRecord({ ...current, raw: { ...current.raw, ...evidence } });
  319. }),
  320. ];
  321. for (const conflict of cases) {
  322. const store = createStore(':memory:');
  323. try {
  324. store.upsertRecord(participantMessage('peer', peer, 'Fictional peer'));
  325. assert.equal(refreshChatNames(store), 1);
  326. conflict(store);
  327. const before = messageSnapshot(store);
  328. assert.equal(refreshChatNames(store), 1);
  329. const current = store.getRecordByIdentity(participantChatRecord());
  330. assert.equal(current.title, '聊天'); assert.equal(current.raw.titleSource, 'placeholder');
  331. assert.equal(current.raw.peerId, undefined); assert.equal(current.raw.nameEvidence, undefined);
  332. assert.deepEqual(messageSnapshot(store), before);
  333. assert.equal(refreshChatNames(store), 0);
  334. } finally { store.close(); }
  335. }
  336. });