deliverables.service.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. import { TestBed } from '@angular/core/testing';
  2. import { Deliverable, DeliverableGroup, SessionFile } from './api/models';
  3. import { FmodeApi } from './api/fmode-api';
  4. import { DeliverablesService, partitionSessionFiles } from './deliverables.service';
  5. /** 造一条文件台账记录,只填本组用例关心的字段 */
  6. function sessionFile(over: Partial<SessionFile> & { id: string }): SessionFile {
  7. return {
  8. threadRoot: 'T',
  9. agentId: '',
  10. roomId: '',
  11. name: 'f',
  12. size: 0,
  13. mimeType: '',
  14. url: '',
  15. storageKey: '',
  16. uploadedBy: 'agent',
  17. status: 'ok',
  18. deliveredAt: 0,
  19. ...over,
  20. };
  21. }
  22. /**
  23. * 成果服务。
  24. *
  25. * 重点回归:
  26. * ① 空态与错误态**必须区分**(AC-08.4:失败不能伪装成「暂无成果」)
  27. * ② 搜索过滤后,分组计数要跟着变(否则显示「3 条」实际只有 1 条)
  28. * ③ 月份分组按时间倒序
  29. */
  30. describe('DeliverablesService', () => {
  31. const apiStub = {
  32. fetchDeliverables: jasmine.createSpy('fetchDeliverables'),
  33. fetchDeliverablesByProject: jasmine.createSpy('fetchDeliverablesByProject'),
  34. };
  35. let svc: DeliverablesService;
  36. function d(id: string, title: string, deliveredAt: string, extra: Partial<Deliverable> = {}): Deliverable {
  37. return {
  38. id,
  39. agentId: 'a1',
  40. title,
  41. artifacts: [],
  42. tags: [],
  43. deliveredAt,
  44. state: 'delivered',
  45. ...extra,
  46. };
  47. }
  48. beforeEach(() => {
  49. apiStub.fetchDeliverables.calls.reset();
  50. apiStub.fetchDeliverablesByProject.calls.reset();
  51. apiStub.fetchDeliverables.and.returnValue(Promise.resolve([]));
  52. apiStub.fetchDeliverablesByProject.and.returnValue(Promise.resolve([]));
  53. TestBed.resetTestingModule();
  54. TestBed.configureTestingModule({
  55. providers: [DeliverablesService, { provide: FmodeApi, useValue: apiStub }],
  56. });
  57. svc = TestBed.inject(DeliverablesService);
  58. });
  59. describe('加载与状态', () => {
  60. it('空列表 → state=empty(不是 ready)', async () => {
  61. await svc.load();
  62. expect(svc.state()).toBe('empty');
  63. expect(svc.empty()).toBeTrue();
  64. });
  65. it('有数据 → state=ready', async () => {
  66. apiStub.fetchDeliverables.and.returnValue(Promise.resolve([d('1', '报告', '2026-10-01')]));
  67. await svc.load();
  68. expect(svc.state()).toBe('ready');
  69. expect(svc.all().length).toBe(1);
  70. });
  71. it('🔴 接口失败 → state=error,且不伪装成「暂无成果」', async () => {
  72. apiStub.fetchDeliverables.and.returnValue(Promise.reject(new Error('网络异常')));
  73. await svc.load();
  74. expect(svc.state()).toBe('error');
  75. expect(svc.error()).toContain('网络异常');
  76. // 关键:错误态时 empty 必须为 false,否则 UI 会显示「暂无成果」
  77. expect(svc.empty()).toBeFalse();
  78. });
  79. it('ensureLoaded 加载过就不再重复请求', async () => {
  80. apiStub.fetchDeliverables.and.returnValue(Promise.resolve([d('1', 'x', '2026-10-01')]));
  81. await svc.ensureLoaded();
  82. await svc.ensureLoaded();
  83. expect(apiStub.fetchDeliverables.calls.count()).toBe(1);
  84. });
  85. it('refresh 强制重新拉取(跳过缓存)', async () => {
  86. apiStub.fetchDeliverables.and.returnValue(Promise.resolve([d('1', 'x', '2026-10-01')]));
  87. await svc.ensureLoaded();
  88. await svc.refresh();
  89. expect(apiStub.fetchDeliverables.calls.count()).toBe(2);
  90. });
  91. });
  92. describe('搜索过滤', () => {
  93. beforeEach(async () => {
  94. apiStub.fetchDeliverables.and.returnValue(
  95. Promise.resolve([
  96. d('1', '登录页设计', '2026-10-01', { projectName: '项目A' }),
  97. d('2', '接口文档', '2026-10-02', { projectName: '项目A' }),
  98. d('3', '登录页原型', '2026-09-15', { projectName: '项目B' }),
  99. ]),
  100. );
  101. await svc.load();
  102. });
  103. it('无关键字 → 全部返回', () => {
  104. expect(svc.filtered().length).toBe(3);
  105. });
  106. it('按标题过滤', () => {
  107. svc.keyword.set('登录');
  108. expect(svc.filtered().map((x) => x.id)).toEqual(['1', '3']);
  109. });
  110. it('按项目名过滤', () => {
  111. svc.keyword.set('项目B');
  112. expect(svc.filtered().map((x) => x.id)).toEqual(['3']);
  113. });
  114. it('大小写不敏感', () => {
  115. svc.keyword.set('ABC');
  116. expect(svc.filtered().length).toBe(0);
  117. });
  118. it('🔴 无结果时 filteredEmpty 为真(供 UI 显示「没有匹配」而不是「暂无成果」)', () => {
  119. svc.keyword.set('不存在的东西');
  120. expect(svc.filteredEmpty()).toBeTrue();
  121. // 但整体 empty 仍是 false(确实有成果,只是没匹配上)
  122. expect(svc.empty()).toBeFalse();
  123. });
  124. });
  125. describe('🔴 分组计数跟随搜索(曾在 UI 上显示错的条数)', () => {
  126. beforeEach(async () => {
  127. apiStub.fetchDeliverables.and.returnValue(Promise.resolve([]));
  128. apiStub.fetchDeliverablesByProject.and.returnValue(
  129. Promise.resolve([
  130. {
  131. project: 'p1',
  132. projectName: '项目A',
  133. count: 3,
  134. items: [d('1', '登录页', '2026-10-01'), d('2', '接口', '2026-10-02'), d('3', '报表', '2026-10-03')],
  135. } as DeliverableGroup,
  136. ]),
  137. );
  138. await svc.load();
  139. });
  140. it('无关键字 → 计数是原始值', () => {
  141. expect(svc.groups()[0].count).toBe(3);
  142. });
  143. it('过滤后 count 跟着变(不是恒为 3)', () => {
  144. svc.keyword.set('登录');
  145. expect(svc.groups()[0].items.length).toBe(1);
  146. expect(svc.groups()[0].count).toBe(1); // 关键断言
  147. });
  148. it('组内全被过滤掉 → 整组消失', () => {
  149. svc.keyword.set('完全不存在');
  150. expect(svc.groups().length).toBe(0);
  151. });
  152. });
  153. describe('月份分组', () => {
  154. it('按 YYYY-MM 分组,且倒序', async () => {
  155. apiStub.fetchDeliverables.and.returnValue(
  156. Promise.resolve([
  157. d('1', 'a', '2026-09-10'),
  158. d('2', 'b', '2026-10-05'),
  159. d('3', 'c', '2026-10-20'),
  160. ]),
  161. );
  162. await svc.load();
  163. const groups = svc.monthGroups();
  164. expect(groups.map((g) => g.key)).toEqual(['2026-10', '2026-09']);
  165. expect(groups[0].items.length).toBe(2);
  166. });
  167. });
  168. describe('按 agent / 项目 / id 查询', () => {
  169. beforeEach(async () => {
  170. apiStub.fetchDeliverables.and.returnValue(
  171. Promise.resolve([
  172. d('1', 'a', '2026-10-01', { agentId: 'lj', projectId: 'p1' }),
  173. d('2', 'b', '2026-10-02', { agentId: 'lj-test', projectId: 'p1' }),
  174. d('3', 'c', '2026-10-03', { agentId: 'lj', projectId: 'p2' }),
  175. ]),
  176. );
  177. await svc.load();
  178. });
  179. it('byAgent 只返回该 agent 的', () => {
  180. expect(svc.byAgent('lj').map((x) => x.id)).toEqual(['1', '3']);
  181. });
  182. it('byProject 只返回该项目的', () => {
  183. expect(svc.byProject('p1').map((x) => x.id)).toEqual(['1', '2']);
  184. });
  185. it('byId 命中 / 未命中', () => {
  186. expect(svc.byId('2')?.title).toBe('b');
  187. expect(svc.byId('nope')).toBeUndefined();
  188. });
  189. /*
  190. * 🔴 2026-11-09 用户裁决:成果按会话存放。
  191. * 会话记录里有 `deliverableUrls`,只显示这些链接对应的成果。
  192. */
  193. it('🔴 bySessionUrls 只返回链接匹配的成果', async () => {
  194. // 重建带 url 的样本(默认工厂 artifacts 为空)
  195. apiStub.fetchDeliverables.and.returnValue(
  196. Promise.resolve([
  197. d('s1', '会话一成果', '2026-10-01', {
  198. artifacts: [{ name: 'a.md', url: 'https://s3.fmode.cn/x/a.md', type: 'md' }],
  199. }),
  200. d('s2', '会话二成果', '2026-10-02', {
  201. artifacts: [{ name: 'b.md', url: 'https://s3.fmode.cn/x/b.md', type: 'md' }],
  202. }),
  203. ]),
  204. );
  205. await svc.load();
  206. const hit = svc.bySessionUrls(['https://s3.fmode.cn/x/b.md']);
  207. expect(hit.map((x) => x.id)).withContext('只应命中会话二').toEqual(['s2']);
  208. });
  209. it('🔴 bySessionUrls 无 urls → 返回空,不回退到整个容器', () => {
  210. expect(svc.bySessionUrls(undefined)).toEqual([]);
  211. expect(svc.bySessionUrls([])).toEqual([]);
  212. });
  213. /*
  214. * 🔴 2026-11-09 实测发现:用户上传的文件**不在 deliverables 表里**,
  215. * 只存在于会话记录的 deliverableUrls 中。原实现只查表,
  216. * 导致用户上传的文件在「成果」里永远看不到。
  217. */
  218. it('🔴 用户上传的文件(/user/…/chat/…)**不计入成果**', () => {
  219. const userFile = 'https://s3.fmode.cn/user/u/chat/202611/u-1794-e2e.txt';
  220. const out = svc.bySessionUrls([userFile]);
  221. expect(out.length).withContext('用户上传走「文件」页签,不进成果').toBe(0);
  222. });
  223. it('🔴 成果表里的链接照常显示,用户上传的文件被挡掉', async () => {
  224. apiStub.fetchDeliverables.and.returnValue(
  225. Promise.resolve([
  226. d('s1', '容器成果', '2026-10-01', {
  227. artifacts: [{ name: 'a.md', url: 'https://s3.fmode.cn/d/a.md', type: 'md' }],
  228. }),
  229. ]),
  230. );
  231. await svc.load();
  232. const userFile = 'https://s3.fmode.cn/user/u/chat/x.txt';
  233. const out = svc.bySessionUrls(['https://s3.fmode.cn/d/a.md', userFile]);
  234. expect(out.length).withContext('只保留容器成果').toBe(1);
  235. expect(out[0].title).withContext('表里的用富信息').toBe('容器成果');
  236. });
  237. it('成果表里没有的**非用户上传**链接仍按 URL 造最小记录', () => {
  238. const link = 'https://s3.fmode.cn/other/location/b.txt';
  239. const out = svc.bySessionUrls([link]);
  240. expect(out.length).toBe(1);
  241. expect(out[0].filename).withContext('要有文件名,否则右栏显示「暂无附件」').toBe('b.txt');
  242. });
  243. });
  244. /*
  245. * 2026-11-11 用户裁决「文件 / 成果严格互斥」。
  246. * 实测现象:我上传的 test-20MB.bin 在两个页签各出现一次。
  247. */
  248. describe('文件 / 成果互斥(2026-11-11)', () => {
  249. it('🔴 fromAgentFiles 挡掉用户上传的文件', () => {
  250. const out = svc.fromAgentFiles([
  251. { name: 'test-20MB.bin', url: 'https://s3.fmode.cn/user/u/chat/202611/abc.bin' },
  252. ]);
  253. expect(out.length).withContext('用户上传不属于容器成果').toBe(0);
  254. });
  255. it('容器产出的文件照常计入', () => {
  256. const out = svc.fromAgentFiles([
  257. { name: '周报.md', url: 'https://s3.fmode.cn/deliverables/a/202611/x/week.md' },
  258. ]);
  259. expect(out.length).toBe(1);
  260. expect(out[0].title).toBe('周报.md');
  261. });
  262. it('partitionSessionFiles 按 uploadedBy 分流(权威判据)', () => {
  263. const { userFiles, agentFiles } = partitionSessionFiles([
  264. sessionFile({ id: '1', uploadedBy: 'user', url: 'https://s3.fmode.cn/a/x.bin' }),
  265. sessionFile({ id: '2', uploadedBy: 'agent', url: 'https://s3.fmode.cn/b/y.md' }),
  266. ]);
  267. expect(userFiles.map((f) => f.id)).toEqual(['1']);
  268. expect(agentFiles.map((f) => f.id)).toEqual(['2']);
  269. });
  270. it('uploadedBy 缺失时用 URL 形态兜底', () => {
  271. const { userFiles, agentFiles } = partitionSessionFiles([
  272. sessionFile({ id: '1', uploadedBy: 'agent', url: 'https://s3.fmode.cn/user/u/chat/z.txt' }),
  273. sessionFile({ id: '2', uploadedBy: 'agent', url: 'https://s3.fmode.cn/d/z.md' }),
  274. ]);
  275. expect(userFiles.map((f) => f.id)).withContext('前缀是用户上传目录').toEqual(['1']);
  276. expect(agentFiles.map((f) => f.id)).toEqual(['2']);
  277. });
  278. });
  279. });
  280. /**
  281. * 群聊文件台账(2026-11-11,v0.2.0)。
  282. *
  283. * 伪群聊下容器只在自己的 DM 房间工作,回报文件时不带群信息。
  284. * 「归群」由 App 决定:用户上传直接带 groupId;容器产出靠派发锚点反查。
  285. * 这里只测**读侧分流**,写入与反查在云函数侧(已用 curl 端到端验证)。
  286. */
  287. describe('loadSplitForGroup(群聊按群分流)', () => {
  288. const groupStub = {
  289. fetchFilesByGroup: jasmine.createSpy('fetchFilesByGroup'),
  290. claimFilesToGroup: jasmine.createSpy('claimFilesToGroup'),
  291. };
  292. let gsvc: DeliverablesService;
  293. beforeEach(() => {
  294. TestBed.resetTestingModule();
  295. TestBed.configureTestingModule({
  296. providers: [DeliverablesService, { provide: FmodeApi, useValue: groupStub }],
  297. });
  298. gsvc = TestBed.inject(DeliverablesService);
  299. groupStub.fetchFilesByGroup.calls.reset();
  300. groupStub.claimFilesToGroup.calls.reset();
  301. });
  302. it('🔴 用户上传与容器产物按 uploadedBy 分流(与单聊同口径)', async () => {
  303. groupStub.fetchFilesByGroup.and.returnValue(
  304. Promise.resolve([
  305. sessionFile({ id: '1', groupId: 'G', uploadedBy: 'user', name: '我发的.txt' }),
  306. sessionFile({ id: '2', groupId: 'G', uploadedBy: 'agent', name: '容器产出.md' }),
  307. ]),
  308. );
  309. const { userFiles, agentFiles } = await gsvc.loadSplitForGroup('G');
  310. expect(userFiles.map((d) => d.title)).toEqual(['我发的.txt']);
  311. expect(agentFiles.map((d) => d.title)).toEqual(['容器产出.md']);
  312. });
  313. it('空 groupId 不发起请求(避免查成全局)', async () => {
  314. const r = await gsvc.loadSplitForGroup('');
  315. expect(r.userFiles).toEqual([]);
  316. expect(groupStub.fetchFilesByGroup).not.toHaveBeenCalled();
  317. });
  318. it('带出 sizeBytes,右栏才能显示大小', async () => {
  319. groupStub.fetchFilesByGroup.and.returnValue(
  320. Promise.resolve([sessionFile({ id: '1', groupId: 'G', uploadedBy: 'user', name: 'a.txt', size: 2048 })]),
  321. );
  322. const { userFiles } = await gsvc.loadSplitForGroup('G');
  323. expect(userFiles[0].sizeBytes).toBe(2048);
  324. });
  325. it('大小未知(-1)不写 sizeBytes(避免显示成 0 B)', async () => {
  326. groupStub.fetchFilesByGroup.and.returnValue(
  327. Promise.resolve([sessionFile({ id: '1', groupId: 'G', uploadedBy: 'user', name: 'a.txt', size: -1 })]),
  328. );
  329. const { userFiles } = await gsvc.loadSplitForGroup('G');
  330. expect(userFiles[0].sizeBytes).toBeUndefined();
  331. });
  332. it('防御:混入其它群的记录会被挡掉', async () => {
  333. groupStub.fetchFilesByGroup.and.returnValue(
  334. Promise.resolve([
  335. sessionFile({ id: '1', groupId: 'G', uploadedBy: 'user', name: '本群.txt' }),
  336. sessionFile({ id: '2', groupId: 'OTHER', uploadedBy: 'user', name: '别的群.txt' }),
  337. ]),
  338. );
  339. const { userFiles } = await gsvc.loadSplitForGroup('G');
  340. expect(userFiles.map((d) => d.title)).toEqual(['本群.txt']);
  341. });
  342. });
  343. /**
  344. * 成果合并去重(2026-11-14 用户实测)。
  345. *
  346. * 用户截图:右栏「成果 6」里,`report.md` 和 `0-mwf6f6gtj9-report.md`
  347. * 各出现一条 —— 同一个文件被当成两个。
  348. *
  349. * 根因:`merge` 只比 URL,而三路数据源的 URL 形态不同
  350. * (台账是 s3 直链、消息流可能是 mxc 或另一条地址)。
  351. */
  352. describe('DeliverablesService.merge · 跨源去重', () => {
  353. let svc: DeliverablesService;
  354. function deliverable(over: Partial<Deliverable>): Deliverable {
  355. return {
  356. id: 'd1',
  357. agentId: 'agent-x',
  358. title: 'report.md',
  359. filename: 'report.md',
  360. artifacts: [{ name: 'report.md', url: 'https://s3.fmode.cn/a/report.md', type: '' }],
  361. tags: [],
  362. deliveredAt: '',
  363. state: 'delivered',
  364. ...over,
  365. } as Deliverable;
  366. }
  367. beforeEach(() => {
  368. TestBed.configureTestingModule({
  369. providers: [DeliverablesService, { provide: FmodeApi, useValue: {} }],
  370. });
  371. svc = TestBed.inject(DeliverablesService);
  372. });
  373. it('🔴 同名同大小、URL 不同 → 只保留一条(带前缀名的那条被吃掉)', () => {
  374. const fromDb = deliverable({
  375. id: 'db1',
  376. title: '0-mwf6f6gtj9-report.md',
  377. filename: '0-mwf6f6gtj9-report.md',
  378. sizeBytes: 4700,
  379. artifacts: [
  380. { name: '0-mwf6f6gtj9-report.md', url: 'https://s3.fmode.cn/x/0-mwf6f6gtj9-report.md', type: '' },
  381. ],
  382. });
  383. const fromMessages = deliverable({
  384. id: 'msg1',
  385. title: 'report.md',
  386. filename: 'report.md',
  387. sizeBytes: 4700,
  388. artifacts: [{ name: 'report.md', url: 'mxc://fmode.cn/abc', type: '' }],
  389. });
  390. const out = svc.merge([fromDb], [fromMessages]);
  391. expect(out.length)
  392. .withContext('同名字同大小 = 同一个文件,不能显示两条')
  393. .toBe(1);
  394. expect(out[0].id).withContext('保留先到的那条(台账)').toBe('db1');
  395. });
  396. it('同名但大小不同 → 视为两个文件(不误合并)', () => {
  397. const a = deliverable({ id: 'a', sizeBytes: 100 });
  398. const b = deliverable({ id: 'b', sizeBytes: 200, artifacts: [{ name: 'report.md', url: 'u2', type: '' }] });
  399. expect(svc.merge([a], [b]).length).toBe(2);
  400. });
  401. it('多附件消息的同名成果大小未知、URL 不同,展示合并保留两份', () => {
  402. const files = svc.fromAgentFiles([
  403. { name: 'report.md', url: 'https://s3.fmode.cn/deliverables/first/report.md' },
  404. { name: 'report.md', url: 'https://s3.fmode.cn/deliverables/second/report.md' },
  405. ]);
  406. expect(svc.merge(files).map((d) => d.artifacts[0].url)).toEqual([
  407. 'https://s3.fmode.cn/deliverables/first/report.md',
  408. 'https://s3.fmode.cn/deliverables/second/report.md',
  409. ]);
  410. });
  411. it('大小未知的同名同 URL 成果仍去重,不把新旧来源显示成两份', () => {
  412. const file = { name: 'report.md', url: 'https://s3.fmode.cn/deliverables/first/report.md' };
  413. expect(svc.merge(svc.fromAgentFiles([file]), svc.fromAgentFiles([file])).length).toBe(1);
  414. });
  415. it('不同名 → 两条都保留', () => {
  416. const a = deliverable({ id: 'a', title: 'a.md', filename: 'a.md', artifacts: [{ name: 'a.md', url: 'u1', type: '' }] });
  417. const b = deliverable({ id: 'b', title: 'b.md', filename: 'b.md', sizeBytes: 999, artifacts: [{ name: 'b.md', url: 'u2', type: '' }] });
  418. expect(svc.merge([a], [b]).length).toBe(2);
  419. });
  420. it('同一 URL 重复出现(身份算不出时)→ 仍靠 URL 兜底去重', () => {
  421. const a = deliverable({ id: 'a', title: '', filename: '', artifacts: [{ name: '', url: 'https://s3.fmode.cn/same', type: '' }] });
  422. const b = deliverable({ id: 'b', title: '', filename: '', artifacts: [{ name: '', url: 'https://s3.fmode.cn/same', type: '' }] });
  423. expect(svc.merge([a], [b]).length).toBe(1);
  424. });
  425. });