| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- import { TestBed } from '@angular/core/testing';
- import { Deliverable, DeliverableGroup, SessionFile } from './api/models';
- import { FmodeApi } from './api/fmode-api';
- import { DeliverablesService, partitionSessionFiles } from './deliverables.service';
- /** 造一条文件台账记录,只填本组用例关心的字段 */
- function sessionFile(over: Partial<SessionFile> & { id: string }): SessionFile {
- return {
- threadRoot: 'T',
- agentId: '',
- roomId: '',
- name: 'f',
- size: 0,
- mimeType: '',
- url: '',
- storageKey: '',
- uploadedBy: 'agent',
- status: 'ok',
- deliveredAt: 0,
- ...over,
- };
- }
- /**
- * 成果服务。
- *
- * 重点回归:
- * ① 空态与错误态**必须区分**(AC-08.4:失败不能伪装成「暂无成果」)
- * ② 搜索过滤后,分组计数要跟着变(否则显示「3 条」实际只有 1 条)
- * ③ 月份分组按时间倒序
- */
- describe('DeliverablesService', () => {
- const apiStub = {
- fetchDeliverables: jasmine.createSpy('fetchDeliverables'),
- fetchDeliverablesByProject: jasmine.createSpy('fetchDeliverablesByProject'),
- };
- let svc: DeliverablesService;
- function d(id: string, title: string, deliveredAt: string, extra: Partial<Deliverable> = {}): Deliverable {
- return {
- id,
- agentId: 'a1',
- title,
- artifacts: [],
- tags: [],
- deliveredAt,
- state: 'delivered',
- ...extra,
- };
- }
- beforeEach(() => {
- apiStub.fetchDeliverables.calls.reset();
- apiStub.fetchDeliverablesByProject.calls.reset();
- apiStub.fetchDeliverables.and.returnValue(Promise.resolve([]));
- apiStub.fetchDeliverablesByProject.and.returnValue(Promise.resolve([]));
- TestBed.resetTestingModule();
- TestBed.configureTestingModule({
- providers: [DeliverablesService, { provide: FmodeApi, useValue: apiStub }],
- });
- svc = TestBed.inject(DeliverablesService);
- });
- describe('加载与状态', () => {
- it('空列表 → state=empty(不是 ready)', async () => {
- await svc.load();
- expect(svc.state()).toBe('empty');
- expect(svc.empty()).toBeTrue();
- });
- it('有数据 → state=ready', async () => {
- apiStub.fetchDeliverables.and.returnValue(Promise.resolve([d('1', '报告', '2026-10-01')]));
- await svc.load();
- expect(svc.state()).toBe('ready');
- expect(svc.all().length).toBe(1);
- });
- it('🔴 接口失败 → state=error,且不伪装成「暂无成果」', async () => {
- apiStub.fetchDeliverables.and.returnValue(Promise.reject(new Error('网络异常')));
- await svc.load();
- expect(svc.state()).toBe('error');
- expect(svc.error()).toContain('网络异常');
- // 关键:错误态时 empty 必须为 false,否则 UI 会显示「暂无成果」
- expect(svc.empty()).toBeFalse();
- });
- it('ensureLoaded 加载过就不再重复请求', async () => {
- apiStub.fetchDeliverables.and.returnValue(Promise.resolve([d('1', 'x', '2026-10-01')]));
- await svc.ensureLoaded();
- await svc.ensureLoaded();
- expect(apiStub.fetchDeliverables.calls.count()).toBe(1);
- });
- it('refresh 强制重新拉取(跳过缓存)', async () => {
- apiStub.fetchDeliverables.and.returnValue(Promise.resolve([d('1', 'x', '2026-10-01')]));
- await svc.ensureLoaded();
- await svc.refresh();
- expect(apiStub.fetchDeliverables.calls.count()).toBe(2);
- });
- });
- describe('搜索过滤', () => {
- beforeEach(async () => {
- apiStub.fetchDeliverables.and.returnValue(
- Promise.resolve([
- d('1', '登录页设计', '2026-10-01', { projectName: '项目A' }),
- d('2', '接口文档', '2026-10-02', { projectName: '项目A' }),
- d('3', '登录页原型', '2026-09-15', { projectName: '项目B' }),
- ]),
- );
- await svc.load();
- });
- it('无关键字 → 全部返回', () => {
- expect(svc.filtered().length).toBe(3);
- });
- it('按标题过滤', () => {
- svc.keyword.set('登录');
- expect(svc.filtered().map((x) => x.id)).toEqual(['1', '3']);
- });
- it('按项目名过滤', () => {
- svc.keyword.set('项目B');
- expect(svc.filtered().map((x) => x.id)).toEqual(['3']);
- });
- it('大小写不敏感', () => {
- svc.keyword.set('ABC');
- expect(svc.filtered().length).toBe(0);
- });
- it('🔴 无结果时 filteredEmpty 为真(供 UI 显示「没有匹配」而不是「暂无成果」)', () => {
- svc.keyword.set('不存在的东西');
- expect(svc.filteredEmpty()).toBeTrue();
- // 但整体 empty 仍是 false(确实有成果,只是没匹配上)
- expect(svc.empty()).toBeFalse();
- });
- });
- describe('🔴 分组计数跟随搜索(曾在 UI 上显示错的条数)', () => {
- beforeEach(async () => {
- apiStub.fetchDeliverables.and.returnValue(Promise.resolve([]));
- apiStub.fetchDeliverablesByProject.and.returnValue(
- Promise.resolve([
- {
- project: 'p1',
- projectName: '项目A',
- count: 3,
- items: [d('1', '登录页', '2026-10-01'), d('2', '接口', '2026-10-02'), d('3', '报表', '2026-10-03')],
- } as DeliverableGroup,
- ]),
- );
- await svc.load();
- });
- it('无关键字 → 计数是原始值', () => {
- expect(svc.groups()[0].count).toBe(3);
- });
- it('过滤后 count 跟着变(不是恒为 3)', () => {
- svc.keyword.set('登录');
- expect(svc.groups()[0].items.length).toBe(1);
- expect(svc.groups()[0].count).toBe(1); // 关键断言
- });
- it('组内全被过滤掉 → 整组消失', () => {
- svc.keyword.set('完全不存在');
- expect(svc.groups().length).toBe(0);
- });
- });
- describe('月份分组', () => {
- it('按 YYYY-MM 分组,且倒序', async () => {
- apiStub.fetchDeliverables.and.returnValue(
- Promise.resolve([
- d('1', 'a', '2026-09-10'),
- d('2', 'b', '2026-10-05'),
- d('3', 'c', '2026-10-20'),
- ]),
- );
- await svc.load();
- const groups = svc.monthGroups();
- expect(groups.map((g) => g.key)).toEqual(['2026-10', '2026-09']);
- expect(groups[0].items.length).toBe(2);
- });
- });
- describe('按 agent / 项目 / id 查询', () => {
- beforeEach(async () => {
- apiStub.fetchDeliverables.and.returnValue(
- Promise.resolve([
- d('1', 'a', '2026-10-01', { agentId: 'lj', projectId: 'p1' }),
- d('2', 'b', '2026-10-02', { agentId: 'lj-test', projectId: 'p1' }),
- d('3', 'c', '2026-10-03', { agentId: 'lj', projectId: 'p2' }),
- ]),
- );
- await svc.load();
- });
- it('byAgent 只返回该 agent 的', () => {
- expect(svc.byAgent('lj').map((x) => x.id)).toEqual(['1', '3']);
- });
- it('byProject 只返回该项目的', () => {
- expect(svc.byProject('p1').map((x) => x.id)).toEqual(['1', '2']);
- });
- it('byId 命中 / 未命中', () => {
- expect(svc.byId('2')?.title).toBe('b');
- expect(svc.byId('nope')).toBeUndefined();
- });
- /*
- * 🔴 2026-11-09 用户裁决:成果按会话存放。
- * 会话记录里有 `deliverableUrls`,只显示这些链接对应的成果。
- */
- it('🔴 bySessionUrls 只返回链接匹配的成果', async () => {
- // 重建带 url 的样本(默认工厂 artifacts 为空)
- apiStub.fetchDeliverables.and.returnValue(
- Promise.resolve([
- d('s1', '会话一成果', '2026-10-01', {
- artifacts: [{ name: 'a.md', url: 'https://s3.fmode.cn/x/a.md', type: 'md' }],
- }),
- d('s2', '会话二成果', '2026-10-02', {
- artifacts: [{ name: 'b.md', url: 'https://s3.fmode.cn/x/b.md', type: 'md' }],
- }),
- ]),
- );
- await svc.load();
- const hit = svc.bySessionUrls(['https://s3.fmode.cn/x/b.md']);
- expect(hit.map((x) => x.id)).withContext('只应命中会话二').toEqual(['s2']);
- });
- it('🔴 bySessionUrls 无 urls → 返回空,不回退到整个容器', () => {
- expect(svc.bySessionUrls(undefined)).toEqual([]);
- expect(svc.bySessionUrls([])).toEqual([]);
- });
- /*
- * 🔴 2026-11-09 实测发现:用户上传的文件**不在 deliverables 表里**,
- * 只存在于会话记录的 deliverableUrls 中。原实现只查表,
- * 导致用户上传的文件在「成果」里永远看不到。
- */
- it('🔴 用户上传的文件(/user/…/chat/…)**不计入成果**', () => {
- const userFile = 'https://s3.fmode.cn/user/u/chat/202611/u-1794-e2e.txt';
- const out = svc.bySessionUrls([userFile]);
- expect(out.length).withContext('用户上传走「文件」页签,不进成果').toBe(0);
- });
- it('🔴 成果表里的链接照常显示,用户上传的文件被挡掉', async () => {
- apiStub.fetchDeliverables.and.returnValue(
- Promise.resolve([
- d('s1', '容器成果', '2026-10-01', {
- artifacts: [{ name: 'a.md', url: 'https://s3.fmode.cn/d/a.md', type: 'md' }],
- }),
- ]),
- );
- await svc.load();
- const userFile = 'https://s3.fmode.cn/user/u/chat/x.txt';
- const out = svc.bySessionUrls(['https://s3.fmode.cn/d/a.md', userFile]);
- expect(out.length).withContext('只保留容器成果').toBe(1);
- expect(out[0].title).withContext('表里的用富信息').toBe('容器成果');
- });
- it('成果表里没有的**非用户上传**链接仍按 URL 造最小记录', () => {
- const link = 'https://s3.fmode.cn/other/location/b.txt';
- const out = svc.bySessionUrls([link]);
- expect(out.length).toBe(1);
- expect(out[0].filename).withContext('要有文件名,否则右栏显示「暂无附件」').toBe('b.txt');
- });
- });
- /*
- * 2026-11-11 用户裁决「文件 / 成果严格互斥」。
- * 实测现象:我上传的 test-20MB.bin 在两个页签各出现一次。
- */
- describe('文件 / 成果互斥(2026-11-11)', () => {
- it('🔴 fromAgentFiles 挡掉用户上传的文件', () => {
- const out = svc.fromAgentFiles([
- { name: 'test-20MB.bin', url: 'https://s3.fmode.cn/user/u/chat/202611/abc.bin' },
- ]);
- expect(out.length).withContext('用户上传不属于容器成果').toBe(0);
- });
- it('容器产出的文件照常计入', () => {
- const out = svc.fromAgentFiles([
- { name: '周报.md', url: 'https://s3.fmode.cn/deliverables/a/202611/x/week.md' },
- ]);
- expect(out.length).toBe(1);
- expect(out[0].title).toBe('周报.md');
- });
- it('partitionSessionFiles 按 uploadedBy 分流(权威判据)', () => {
- const { userFiles, agentFiles } = partitionSessionFiles([
- sessionFile({ id: '1', uploadedBy: 'user', url: 'https://s3.fmode.cn/a/x.bin' }),
- sessionFile({ id: '2', uploadedBy: 'agent', url: 'https://s3.fmode.cn/b/y.md' }),
- ]);
- expect(userFiles.map((f) => f.id)).toEqual(['1']);
- expect(agentFiles.map((f) => f.id)).toEqual(['2']);
- });
- it('uploadedBy 缺失时用 URL 形态兜底', () => {
- const { userFiles, agentFiles } = partitionSessionFiles([
- sessionFile({ id: '1', uploadedBy: 'agent', url: 'https://s3.fmode.cn/user/u/chat/z.txt' }),
- sessionFile({ id: '2', uploadedBy: 'agent', url: 'https://s3.fmode.cn/d/z.md' }),
- ]);
- expect(userFiles.map((f) => f.id)).withContext('前缀是用户上传目录').toEqual(['1']);
- expect(agentFiles.map((f) => f.id)).toEqual(['2']);
- });
- });
- });
- /**
- * 群聊文件台账(2026-11-11,v0.2.0)。
- *
- * 伪群聊下容器只在自己的 DM 房间工作,回报文件时不带群信息。
- * 「归群」由 App 决定:用户上传直接带 groupId;容器产出靠派发锚点反查。
- * 这里只测**读侧分流**,写入与反查在云函数侧(已用 curl 端到端验证)。
- */
- describe('loadSplitForGroup(群聊按群分流)', () => {
- const groupStub = {
- fetchFilesByGroup: jasmine.createSpy('fetchFilesByGroup'),
- claimFilesToGroup: jasmine.createSpy('claimFilesToGroup'),
- };
- let gsvc: DeliverablesService;
- beforeEach(() => {
- TestBed.resetTestingModule();
- TestBed.configureTestingModule({
- providers: [DeliverablesService, { provide: FmodeApi, useValue: groupStub }],
- });
- gsvc = TestBed.inject(DeliverablesService);
- groupStub.fetchFilesByGroup.calls.reset();
- groupStub.claimFilesToGroup.calls.reset();
- });
- it('🔴 用户上传与容器产物按 uploadedBy 分流(与单聊同口径)', async () => {
- groupStub.fetchFilesByGroup.and.returnValue(
- Promise.resolve([
- sessionFile({ id: '1', groupId: 'G', uploadedBy: 'user', name: '我发的.txt' }),
- sessionFile({ id: '2', groupId: 'G', uploadedBy: 'agent', name: '容器产出.md' }),
- ]),
- );
- const { userFiles, agentFiles } = await gsvc.loadSplitForGroup('G');
- expect(userFiles.map((d) => d.title)).toEqual(['我发的.txt']);
- expect(agentFiles.map((d) => d.title)).toEqual(['容器产出.md']);
- });
- it('空 groupId 不发起请求(避免查成全局)', async () => {
- const r = await gsvc.loadSplitForGroup('');
- expect(r.userFiles).toEqual([]);
- expect(groupStub.fetchFilesByGroup).not.toHaveBeenCalled();
- });
- it('带出 sizeBytes,右栏才能显示大小', async () => {
- groupStub.fetchFilesByGroup.and.returnValue(
- Promise.resolve([sessionFile({ id: '1', groupId: 'G', uploadedBy: 'user', name: 'a.txt', size: 2048 })]),
- );
- const { userFiles } = await gsvc.loadSplitForGroup('G');
- expect(userFiles[0].sizeBytes).toBe(2048);
- });
- it('大小未知(-1)不写 sizeBytes(避免显示成 0 B)', async () => {
- groupStub.fetchFilesByGroup.and.returnValue(
- Promise.resolve([sessionFile({ id: '1', groupId: 'G', uploadedBy: 'user', name: 'a.txt', size: -1 })]),
- );
- const { userFiles } = await gsvc.loadSplitForGroup('G');
- expect(userFiles[0].sizeBytes).toBeUndefined();
- });
- it('防御:混入其它群的记录会被挡掉', async () => {
- groupStub.fetchFilesByGroup.and.returnValue(
- Promise.resolve([
- sessionFile({ id: '1', groupId: 'G', uploadedBy: 'user', name: '本群.txt' }),
- sessionFile({ id: '2', groupId: 'OTHER', uploadedBy: 'user', name: '别的群.txt' }),
- ]),
- );
- const { userFiles } = await gsvc.loadSplitForGroup('G');
- expect(userFiles.map((d) => d.title)).toEqual(['本群.txt']);
- });
- });
- /**
- * 成果合并去重(2026-11-14 用户实测)。
- *
- * 用户截图:右栏「成果 6」里,`report.md` 和 `0-mwf6f6gtj9-report.md`
- * 各出现一条 —— 同一个文件被当成两个。
- *
- * 根因:`merge` 只比 URL,而三路数据源的 URL 形态不同
- * (台账是 s3 直链、消息流可能是 mxc 或另一条地址)。
- */
- describe('DeliverablesService.merge · 跨源去重', () => {
- let svc: DeliverablesService;
- function deliverable(over: Partial<Deliverable>): Deliverable {
- return {
- id: 'd1',
- agentId: 'agent-x',
- title: 'report.md',
- filename: 'report.md',
- artifacts: [{ name: 'report.md', url: 'https://s3.fmode.cn/a/report.md', type: '' }],
- tags: [],
- deliveredAt: '',
- state: 'delivered',
- ...over,
- } as Deliverable;
- }
- beforeEach(() => {
- TestBed.configureTestingModule({
- providers: [DeliverablesService, { provide: FmodeApi, useValue: {} }],
- });
- svc = TestBed.inject(DeliverablesService);
- });
- it('🔴 同名同大小、URL 不同 → 只保留一条(带前缀名的那条被吃掉)', () => {
- const fromDb = deliverable({
- id: 'db1',
- title: '0-mwf6f6gtj9-report.md',
- filename: '0-mwf6f6gtj9-report.md',
- sizeBytes: 4700,
- artifacts: [
- { name: '0-mwf6f6gtj9-report.md', url: 'https://s3.fmode.cn/x/0-mwf6f6gtj9-report.md', type: '' },
- ],
- });
- const fromMessages = deliverable({
- id: 'msg1',
- title: 'report.md',
- filename: 'report.md',
- sizeBytes: 4700,
- artifacts: [{ name: 'report.md', url: 'mxc://fmode.cn/abc', type: '' }],
- });
- const out = svc.merge([fromDb], [fromMessages]);
- expect(out.length)
- .withContext('同名字同大小 = 同一个文件,不能显示两条')
- .toBe(1);
- expect(out[0].id).withContext('保留先到的那条(台账)').toBe('db1');
- });
- it('同名但大小不同 → 视为两个文件(不误合并)', () => {
- const a = deliverable({ id: 'a', sizeBytes: 100 });
- const b = deliverable({ id: 'b', sizeBytes: 200, artifacts: [{ name: 'report.md', url: 'u2', type: '' }] });
- expect(svc.merge([a], [b]).length).toBe(2);
- });
- it('多附件消息的同名成果大小未知、URL 不同,展示合并保留两份', () => {
- const files = svc.fromAgentFiles([
- { name: 'report.md', url: 'https://s3.fmode.cn/deliverables/first/report.md' },
- { name: 'report.md', url: 'https://s3.fmode.cn/deliverables/second/report.md' },
- ]);
- expect(svc.merge(files).map((d) => d.artifacts[0].url)).toEqual([
- 'https://s3.fmode.cn/deliverables/first/report.md',
- 'https://s3.fmode.cn/deliverables/second/report.md',
- ]);
- });
- it('大小未知的同名同 URL 成果仍去重,不把新旧来源显示成两份', () => {
- const file = { name: 'report.md', url: 'https://s3.fmode.cn/deliverables/first/report.md' };
- expect(svc.merge(svc.fromAgentFiles([file]), svc.fromAgentFiles([file])).length).toBe(1);
- });
- it('不同名 → 两条都保留', () => {
- const a = deliverable({ id: 'a', title: 'a.md', filename: 'a.md', artifacts: [{ name: 'a.md', url: 'u1', type: '' }] });
- const b = deliverable({ id: 'b', title: 'b.md', filename: 'b.md', sizeBytes: 999, artifacts: [{ name: 'b.md', url: 'u2', type: '' }] });
- expect(svc.merge([a], [b]).length).toBe(2);
- });
- it('同一 URL 重复出现(身份算不出时)→ 仍靠 URL 兜底去重', () => {
- const a = deliverable({ id: 'a', title: '', filename: '', artifacts: [{ name: '', url: 'https://s3.fmode.cn/same', type: '' }] });
- const b = deliverable({ id: 'b', title: '', filename: '', artifacts: [{ name: '', url: 'https://s3.fmode.cn/same', type: '' }] });
- expect(svc.merge([a], [b]).length).toBe(1);
- });
- });
|