chat-session.service.spec.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. import { TestBed } from '@angular/core/testing';
  2. import { ChatSessionService } from './chat-session.service';
  3. import { FmodeApi } from './api/fmode-api';
  4. import { SessionService } from './session.service';
  5. import { legacySessionId } from './chat-session';
  6. /**
  7. * ChatSessionService —— 会话目录的唯一 owner。
  8. *
  9. * 覆盖:新建 / 归档 / 重命名 / 移除黑名单 / 云端单向同步 / 持久化。
  10. * 这些是「多会话」的数据底座,错一个字段用户就会看到串号的对话名。
  11. */
  12. /**
  13. * 以「带 FmodeApi 桩」的方式构造服务实例。
  14. *
  15. * 文件级声明:多个 describe 都要用它构造独立实例,
  16. * 每次重建都拿到干净的 signal 状态(`resetTestingModule`)。
  17. */
  18. function withApiStub(api: Record<string, unknown>): ChatSessionService {
  19. TestBed.resetTestingModule();
  20. TestBed.configureTestingModule({
  21. providers: [
  22. ChatSessionService,
  23. { provide: SessionService, useValue: { me: () => ({ id: 'u1' }) } },
  24. { provide: FmodeApi, useValue: api },
  25. ],
  26. });
  27. const s2 = TestBed.inject(ChatSessionService);
  28. s2.load();
  29. return s2;
  30. }
  31. describe('ChatSessionService', () => {
  32. let svc: ChatSessionService;
  33. beforeEach(() => {
  34. localStorage.clear();
  35. TestBed.configureTestingModule({
  36. providers: [
  37. ChatSessionService,
  38. { provide: SessionService, useValue: { me: () => ({ id: 'u1' }) } },
  39. ],
  40. });
  41. svc = TestBed.inject(ChatSessionService);
  42. svc.reset();
  43. });
  44. afterEach(() => localStorage.clear());
  45. const ROOM = '!room1:fmode.cn';
  46. const AGENT = 'agent-a';
  47. it('新建会话:标题默认「新对话」、未归档、计数 0', () => {
  48. const s = svc.create(ROOM, AGENT);
  49. expect(s.title).toBe('新对话');
  50. expect(s.archived).toBeFalse();
  51. expect(s.messageCount).toBe(0);
  52. expect(s.roomId).toBe(ROOM);
  53. expect(s.agentId).toBe(AGENT);
  54. });
  55. it('create 生成独立线程根、同容器可并存多条', () => {
  56. const a = svc.create(ROOM, AGENT);
  57. const b = svc.create(ROOM, AGENT);
  58. expect(b.id).not.toBe(a.id);
  59. expect(svc.listFor(AGENT).length).toBe(2);
  60. });
  61. it('为什么服务端认为是同一个 agent 的不同容器 —— 会话互不可见', () => {
  62. svc.create(ROOM, 'agent-a');
  63. svc.create(ROOM, 'agent-b');
  64. expect(svc.listFor('agent-a').length).toBe(1);
  65. expect(svc.listFor('agent-b').length).toBe(1);
  66. });
  67. it('归档后默认列表不含它,但可按 id 取到(链接可打开)', () => {
  68. const s = svc.create(ROOM, AGENT);
  69. svc.archive(s.id);
  70. expect(svc.listFor(AGENT)).toEqual([]);
  71. expect(svc.listFor(AGENT, { includeArchived: true }).length).toBe(1);
  72. expect(svc.byId(s.id)?.archived).toBeTrue();
  73. });
  74. it('取消归档后回到列表', () => {
  75. const s = svc.create(ROOM, AGENT);
  76. svc.archive(s.id);
  77. svc.archive(s.id, false);
  78. expect(svc.listFor(AGENT).length).toBe(1);
  79. });
  80. it('重命名会锁定标题,之后消息不再覆盖', () => {
  81. const s = svc.create(ROOM, AGENT);
  82. svc.rename(s.id, ' 我的任务 ');
  83. const renamed = svc.byId(s.id)!;
  84. expect(renamed.title).toBe('我的任务');
  85. expect(renamed.titleLocked).toBeTrue();
  86. svc.noteMessage(s.id, { text: '新消息内容', ts: Date.now(), fromMe: true });
  87. expect(svc.byId(s.id)!.title).toBe('我的任务');
  88. });
  89. it('重命名空字符串被忽略', () => {
  90. const s = svc.create(ROOM, AGENT);
  91. svc.rename(s.id, ' ');
  92. expect(svc.byId(s.id)!.title).toBe('新对话');
  93. });
  94. it('用户首条消息推导标题', () => {
  95. const s = svc.create(ROOM, AGENT);
  96. svc.noteMessage(s.id, { text: '帮我做个登录页', ts: 2000, fromMe: true });
  97. expect(svc.byId(s.id)!.title).toBe('做个登录页');
  98. });
  99. describe('remove(用户主动移除)', () => {
  100. it('移除后从列表消失', () => {
  101. const s = svc.create(ROOM, AGENT);
  102. svc.remove(s.id);
  103. expect(svc.byId(s.id)).toBeUndefined();
  104. });
  105. it('🔴 移除的线程不会被下一次云端拉取复活', async () => {
  106. const svc2 = withApiStub({
  107. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) =>
  108. o?.includeDeleted
  109. ? []
  110. : [
  111. {
  112. objectId: 'x', agentId: AGENT, roomId: ROOM, threadRoot: '$gone',
  113. title: '被移除的对话', titleLocked: false, messageCount: 5,
  114. lastMessageAt: 100, archived: false, lastPreview: '',
  115. },
  116. ],
  117. upsertChatThreads: async () => undefined,
  118. removeChatThread: async () => undefined,
  119. });
  120. svc2.adopt({ id: '$gone', roomId: ROOM, agentId: AGENT });
  121. svc2.remove('$gone');
  122. await svc2.hydrateFromCloud(AGENT);
  123. expect(svc2.byId('$gone')).withContext('被移除的线程又回来了').toBeUndefined();
  124. });
  125. it('isRemoved 能查到移除记录', () => {
  126. const s = svc.create(ROOM, AGENT);
  127. expect(svc.isRemoved(s.id)).toBeFalse();
  128. svc.remove(s.id);
  129. expect(svc.isRemoved(s.id)).toBeTrue();
  130. });
  131. it('移除记录会持久化', () => {
  132. const s = svc.create(ROOM, AGENT);
  133. svc.remove(s.id);
  134. svc.reset();
  135. svc.load();
  136. expect(svc.isRemoved(s.id)).withContext('重载后黑名单丢了').toBeTrue();
  137. });
  138. });
  139. describe('历史对话(承接无 thread 旧消息)', () => {
  140. it('首次创建返回 created=true', () => {
  141. const r = svc.adoptLegacy(ROOM, AGENT, '旧消息预览');
  142. expect(r.created).toBeTrue();
  143. expect(r.session.id).toBe(legacySessionId(ROOM));
  144. expect(r.session.title).toBe('历史对话');
  145. expect(r.session.legacy).toBeTrue();
  146. expect(r.session.titleLocked).withContext('历史会话不改名').toBeTrue();
  147. });
  148. it('幂等:重复调用返回 created=false 且不重复添加', () => {
  149. svc.adoptLegacy(ROOM, AGENT);
  150. const again = svc.adoptLegacy(ROOM, AGENT);
  151. expect(again.created).toBeFalse();
  152. expect(svc.listFor(AGENT).length).toBe(1);
  153. });
  154. it('不同房间的历史会话 id 不同', () => {
  155. expect(legacySessionId('!a:x')).not.toBe(legacySessionId('!b:x'));
  156. });
  157. });
  158. describe('持久化', () => {
  159. it('新建后写入 localStorage(v2 缓存键,按用户隔离)', () => {
  160. const s = svc.create(ROOM, AGENT);
  161. const raw = localStorage.getItem('fmode_chat_sessions_v2_u1');
  162. expect(raw).withContext('未按用户隔离存储').toBeTruthy();
  163. expect(JSON.parse(raw!)[0].id).toBe(s.id);
  164. });
  165. it('重新载入能恢复会话', () => {
  166. const s = svc.create(ROOM, AGENT);
  167. svc.reset();
  168. svc.load();
  169. expect(svc.byId(s.id)).toBeDefined();
  170. });
  171. // 换账号不能看到上一个账号的会话
  172. it('🔴 换用户后不串号', () => {
  173. svc.create(ROOM, AGENT);
  174. expect(svc.all().length).toBe(1);
  175. // 切换成另一个用户
  176. TestBed.resetTestingModule();
  177. TestBed.configureTestingModule({
  178. providers: [
  179. ChatSessionService,
  180. { provide: SessionService, useValue: { me: () => ({ id: 'u2' }) } },
  181. ],
  182. });
  183. const other = TestBed.inject(ChatSessionService);
  184. other.load();
  185. expect(other.all()).withContext('读到了别人的会话').toEqual([]);
  186. });
  187. it('坏 JSON 不影响后续使用(降级为空列表)', () => {
  188. localStorage.setItem('fmode_chat_sessions_u1', '{{{ 不是 json');
  189. svc.reset();
  190. expect(() => svc.load()).not.toThrow();
  191. expect(svc.all()).toEqual([]);
  192. });
  193. it('缺字段的脏数据被过滤', () => {
  194. localStorage.setItem(
  195. 'fmode_chat_sessions_v2_u1',
  196. JSON.stringify([{ id: '$x' }, { id: '$ok', roomId: '!r:x', agentId: 'a', title: 't', createdAt: 1, updatedAt: 1 }]),
  197. );
  198. svc.reset();
  199. svc.load();
  200. expect(svc.all().length).toBe(1);
  201. expect(svc.all()[0].id).toBe('$ok');
  202. });
  203. });
  204. describe('threadIdsOf', () => {
  205. it('返回该房间下所有会话的线程根', () => {
  206. const a = svc.create(ROOM, AGENT);
  207. const b = svc.create(ROOM, 'agent-b');
  208. svc.create('!other:x', AGENT);
  209. const ids = svc.threadIdsOf(ROOM);
  210. expect(ids.has(a.id)).toBeTrue();
  211. expect(ids.has(b.id)).toBeTrue();
  212. expect(ids.size).toBe(2);
  213. });
  214. });
  215. describe('消息推进必须同步云端(2026-11-01)', () => {
  216. const ROOM = '!room1:fmode.cn';
  217. const AGENT = 'agent-a';
  218. beforeEach(() => localStorage.clear());
  219. /*
  220. * 🔴 回归:历史会话名称加载不对,显示成「新对话」。
  221. *
  222. * 根因:`noteMessage` 只写本地,标题/计数/时间从不推云端。
  223. * 云端那条记录永远停在「新对话 / 0 条」,换设备或重新拉列表就退回默认名。
  224. */
  225. it('🔴 首条消息确定标题后必须推送到云端', async () => {
  226. const upsert = jasmine.createSpy('upsert').and.returnValue(Promise.resolve());
  227. const svc2 = withApiStub({
  228. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) => (o?.includeDeleted ? [] : []),
  229. upsertChatThreads: upsert,
  230. });
  231. await svc2.hydrateFromCloud(AGENT);
  232. const s = svc2.create(ROOM, AGENT);
  233. upsert.calls.reset();
  234. svc2.noteMessage(s.id, { text: '帮我做个登录页', ts: Date.now(), fromMe: true });
  235. expect(upsert).withContext('标题变了必须推云端,否则换设备退回「新对话」').toHaveBeenCalled();
  236. const pushed = upsert.calls.mostRecent().args[0][0];
  237. expect(pushed.title).toBe('做个登录页');
  238. expect(pushed.threadRoot).toBe(s.id);
  239. });
  240. it('同一会话短时间内重复消息不重复打接口(节流)', async () => {
  241. const upsert = jasmine.createSpy('upsert').and.returnValue(Promise.resolve());
  242. const svc2 = withApiStub({
  243. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) => (o?.includeDeleted ? [] : []),
  244. upsertChatThreads: upsert,
  245. });
  246. await svc2.hydrateFromCloud(AGENT);
  247. const s = svc2.create(ROOM, AGENT);
  248. upsert.calls.reset();
  249. // 首条定标题(触发一次)
  250. svc2.noteMessage(s.id, { text: '第一条', ts: 1000, fromMe: true });
  251. const afterFirst = upsert.calls.count();
  252. // 紧随其后的几条只改计数,不应再推
  253. svc2.noteMessage(s.id, { text: '第二条', ts: 1001, fromMe: true });
  254. svc2.noteMessage(s.id, { text: '第三条', ts: 1002, fromMe: true });
  255. expect(upsert.calls.count()).withContext('计数变化不该每条都打接口').toBe(afterFirst);
  256. });
  257. });
  258. describe('工作区归属(2026-11-01 上云)', () => {
  259. const ROOM = '!room1:fmode.cn';
  260. const AGENT = 'agent-a';
  261. const WS = 'ws-1';
  262. beforeEach(() => localStorage.clear());
  263. /*
  264. * 🔴 回归:用户反馈「在工作区里新建对话,结果跑到下面日常对话里去了」。
  265. *
  266. * 根因:归属只存本地,`hydrateFromCloud` 重建列表时靠本地缓存「捡」,
  267. * 捡不到就回落 null(日常)。现在云端是权威来源。
  268. */
  269. it('🔴 云端返回的 workspaceId 必须覆盖本地为空的归属', async () => {
  270. const svc2 = withApiStub({
  271. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) =>
  272. o?.includeDeleted
  273. ? []
  274. : [
  275. { objectId: 'x', agentId: AGENT, roomId: ROOM, threadRoot: '$w', title: 't', titleLocked: false, messageCount: 2, lastMessageAt: 9, archived: false, lastPreview: '', workspaceId: WS },
  276. ],
  277. upsertChatThreads: async () => undefined,
  278. });
  279. await svc2.hydrateFromCloud(AGENT);
  280. expect(svc2.byId('$w')?.workspaceId).withContext('归属丢了会掉进日常对话').toBe(WS);
  281. });
  282. it('moveTo 会把归属推到云端', async () => {
  283. const patch = jasmine.createSpy('patch').and.returnValue(Promise.resolve());
  284. const svc2 = withApiStub({
  285. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) =>
  286. o?.includeDeleted
  287. ? []
  288. : [
  289. { objectId: 'x', agentId: AGENT, roomId: ROOM, threadRoot: '$w', title: 't', titleLocked: false, messageCount: 2, lastMessageAt: 9, archived: false, lastPreview: '' },
  290. ],
  291. upsertChatThreads: async () => undefined,
  292. patchChatThread: patch,
  293. });
  294. await svc2.hydrateFromCloud(AGENT);
  295. svc2.moveTo('$w', WS);
  296. expect(patch).toHaveBeenCalledWith({ threadRoot: '$w', workspaceId: WS });
  297. expect(svc2.byId('$w')?.workspaceId).toBe(WS);
  298. });
  299. it('移回日常对话时云端收到空字符串(不是不传)', async () => {
  300. const patch = jasmine.createSpy('patch').and.returnValue(Promise.resolve());
  301. const svc2 = withApiStub({
  302. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) =>
  303. o?.includeDeleted
  304. ? []
  305. : [
  306. { objectId: 'x', agentId: AGENT, roomId: ROOM, threadRoot: '$w', title: 't', titleLocked: false, messageCount: 2, lastMessageAt: 9, archived: false, lastPreview: '', workspaceId: WS },
  307. ],
  308. upsertChatThreads: async () => undefined,
  309. patchChatThread: patch,
  310. });
  311. await svc2.hydrateFromCloud(AGENT);
  312. svc2.moveTo('$w', null);
  313. expect(patch).toHaveBeenCalledWith({ threadRoot: '$w', workspaceId: '' });
  314. expect(svc2.byId('$w')?.workspaceId).toBeNull();
  315. });
  316. });
  317. describe('云端会话目录(2026-11-01 单向同步)', () => {
  318. const ROOM = '!room1:fmode.cn';
  319. const AGENT = 'agent-a';
  320. beforeEach(() => localStorage.clear());
  321. /** 以「带 FmodeApi 桩」的方式重建服务(内部用;原 `withApi` 已改为文件级 `withApiStub`) */
  322. function withApi(api: Record<string, unknown>): ChatSessionService {
  323. return withApiStub(api);
  324. }
  325. it('云端有新会话、本地为空 → 重建到本地(换浏览器场景)', async () => {
  326. const svc2 = withApi({
  327. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) =>
  328. o?.includeDeleted
  329. ? []
  330. : [
  331. {
  332. objectId: 'x1', agentId: AGENT, roomId: ROOM, threadRoot: '$cloud1',
  333. title: '云端会话', titleLocked: false, messageCount: 3,
  334. lastMessageAt: 1000, archived: false, lastPreview: 'hi',
  335. },
  336. ],
  337. upsertChatThreads: async () => undefined,
  338. });
  339. await svc2.hydrateFromCloud(AGENT);
  340. expect(svc2.byId('$cloud1')?.title).toBe('云端会话');
  341. expect(svc2.byId('$cloud1')?.messageCount).toBe(3);
  342. });
  343. it('已移除的线程不会被云端复活', async () => {
  344. const svc2 = withApi({
  345. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) =>
  346. o?.includeDeleted
  347. ? []
  348. : [
  349. { objectId: 'x', agentId: AGENT, roomId: ROOM, threadRoot: '$gone', title: 'a', titleLocked: false, messageCount: 1, lastMessageAt: 1, archived: false, lastPreview: '' },
  350. ],
  351. upsertChatThreads: async () => undefined,
  352. removeChatThread: async () => undefined,
  353. });
  354. const s0 = svc2.create(ROOM, AGENT);
  355. svc2.remove(s0.id);
  356. await svc2.hydrateFromCloud(AGENT);
  357. expect(svc2.byId(s0.id)).toBeUndefined();
  358. });
  359. /*
  360. * 🔴 行为变更(2026-11-01):单向同步下**云端标题才是权威**。
  361. *
  362. * 旧断言「本地锁定标题不被云端覆盖」在合并式同步下成立,但正是它允许了
  363. * 「本地与云端各说各话」——改名推云端失败后,本地会永远保留一个云端不知道的标题。
  364. * 现在:改名会立刻 `patchChatThread` 推云端,云端成功后自然以云端为准;
  365. * `titleLocked` 只用于防止「首条消息自动命名」覆盖用户手动改过的名字。
  366. */
  367. it('改名成功后标题以云端为准', async () => {
  368. let remoteTitle = '云端标题';
  369. const svc2 = withApi({
  370. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) =>
  371. o?.includeDeleted
  372. ? []
  373. : [
  374. { objectId: 'x', agentId: AGENT, roomId: ROOM, threadRoot: '$t', title: remoteTitle, titleLocked: true, messageCount: 1, lastMessageAt: 5, archived: false, lastPreview: '' },
  375. ],
  376. upsertChatThreads: async () => undefined,
  377. patchChatThread: async (i: { title?: string }) => {
  378. if (i.title) remoteTitle = i.title;
  379. },
  380. });
  381. svc2.adopt({ id: '$t', roomId: ROOM, agentId: AGENT });
  382. svc2.rename('$t', '我改的标题');
  383. await svc2.hydrateFromCloud(AGENT);
  384. expect(svc2.byId('$t')?.title).withContext('云端已更新,应展示新标题').toBe('我改的标题');
  385. });
  386. it('归档状态以云端为准', async () => {
  387. const svc2 = withApi({
  388. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) =>
  389. o?.includeDeleted
  390. ? []
  391. : [
  392. { objectId: 'x', agentId: AGENT, roomId: ROOM, threadRoot: '$a', title: 't', titleLocked: false, messageCount: 1, lastMessageAt: 9, archived: true, lastPreview: '' },
  393. ],
  394. upsertChatThreads: async () => undefined,
  395. });
  396. svc2.adopt({ id: '$a', roomId: ROOM, agentId: AGENT });
  397. await svc2.hydrateFromCloud(AGENT);
  398. expect(svc2.byId('$a')?.archived).toBeTrue();
  399. });
  400. it('🔴 云端明确为空 → 本机同容器会话一并清掉(删除后不再复活)', async () => {
  401. const svc2 = withApi({
  402. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) => (o?.includeDeleted ? [] : []),
  403. upsertChatThreads: async () => undefined,
  404. });
  405. svc2.create(ROOM, AGENT);
  406. await svc2.hydrateFromCloud(AGENT);
  407. expect(svc2.listFor(AGENT, { includeArchived: true }).length).toBe(0);
  408. });
  409. it('云端明确为空时 pushAll 不得把本地会话推回去', async () => {
  410. const upsert = jasmine.createSpy('upsert').and.returnValue(Promise.resolve());
  411. const svc2 = withApi({ fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) => (o?.includeDeleted ? [] : []), upsertChatThreads: upsert });
  412. svc2.create(ROOM, AGENT); // create 自身会推一次(正常行为)
  413. await svc2.hydrateFromCloud(AGENT); // 云端为空 → 本地清空
  414. const callsAfterCreate = upsert.calls.count();
  415. await svc2.pushAll(AGENT); // 清空后不应再推
  416. expect(upsert.calls.count()).toBe(callsAfterCreate);
  417. });
  418. /*
  419. * 🔴 行为变更(2026-11-01 用户裁决):云端不可达时**列表不可信**。
  420. *
  421. * 旧行为「保留本地缓存照常展示」看着更"稳",实际是两端互相覆盖的根源:
  422. * 用户看到的可能是几小时前的列表,在上面做的删除/归档又会回写云端。
  423. */
  424. it('🔴 云端不可达 → ready=false,界面据此显示「加载失败」', async () => {
  425. const svc2 = withApi({
  426. fetchChatThreads: async () => { throw new Error('network'); },
  427. upsertChatThreads: async () => { throw new Error('network'); },
  428. });
  429. await svc2.hydrateFromCloud(AGENT);
  430. expect(svc2.ready()).toBeFalse();
  431. expect(svc2.error()).toBe('network');
  432. });
  433. it('云端不可达时 pushAll 不推送(列表本身就不可信)', async () => {
  434. const upsert = jasmine.createSpy('upsert').and.returnValue(Promise.resolve());
  435. const svc2 = withApi({
  436. fetchChatThreads: async () => { throw new Error('network'); },
  437. upsertChatThreads: upsert,
  438. });
  439. svc2.create(ROOM, AGENT);
  440. const before = upsert.calls.count();
  441. await svc2.hydrateFromCloud(AGENT);
  442. await svc2.pushAll(AGENT);
  443. expect(upsert.calls.count()).withContext('云端不可达时不应回写').toBe(before);
  444. });
  445. it('云端成功返回后 ready=true', async () => {
  446. const svc2 = withApi({
  447. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) => (o?.includeDeleted ? [] : []),
  448. upsertChatThreads: async () => undefined,
  449. });
  450. expect(svc2.ready()).toBeFalse();
  451. await svc2.hydrateFromCloud(AGENT);
  452. expect(svc2.ready()).toBeTrue();
  453. });
  454. it('🔴 云端删掉的会话,本地下一次拉取即消失', async () => {
  455. let live = [
  456. { objectId: 'a', agentId: AGENT, roomId: ROOM, threadRoot: '$a', title: 'A', titleLocked: false, messageCount: 1, lastMessageAt: 1, archived: false, lastPreview: '' },
  457. { objectId: 'b', agentId: AGENT, roomId: ROOM, threadRoot: '$b', title: 'B', titleLocked: false, messageCount: 1, lastMessageAt: 2, archived: false, lastPreview: '' },
  458. ];
  459. const svc2 = withApi({
  460. fetchChatThreads: async (_a: string, o?: { includeDeleted?: boolean }) => (o?.includeDeleted ? [] : live),
  461. upsertChatThreads: async () => undefined,
  462. });
  463. await svc2.hydrateFromCloud(AGENT);
  464. expect(svc2.listFor(AGENT).length).toBe(2);
  465. live = [live[0]];
  466. await svc2.hydrateFromCloud(AGENT);
  467. expect(svc2.listFor(AGENT).map((s) => s.id)).withContext('云端没有的应被移除').toEqual(['$a']);
  468. });
  469. });
  470. });