workspaceKnowledgePanel.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { mount, flushPromises } from '@vue/test-utils'
  2. import { afterEach, expect, it, vi } from 'vitest'
  3. import WorkspaceKnowledgePanel from '@/components/workspace/WorkspaceKnowledgePanel.vue'
  4. import { apiRequest } from '@/api/client'
  5. import type { WorkspaceAgent } from '@shared/workspace'
  6. import type { WorkspaceMemoryJob, WorkspaceMemoryJobResult } from '@shared/workspaceKnowledge'
  7. vi.mock('@/api/client', () => ({ apiRequest: vi.fn() }))
  8. afterEach(() => vi.restoreAllMocks())
  9. const counts = (values: Partial<WorkspaceMemoryJobResult> = {}): WorkspaceMemoryJobResult => ({
  10. extractedCount: 0, writtenCount: 0, duplicateCount: 0, replayedCount: 0, skippedReasons: {}, ...values,
  11. })
  12. it('distinguishes saved, empty, filtered, duplicate, failed and historical extraction jobs', async () => {
  13. const variants: Partial<WorkspaceMemoryJob>[] = [
  14. { result: counts({ extractedCount: 2, writtenCount: 2 }) },
  15. { result: counts() },
  16. { result: counts({ extractedCount: 2, skippedReasons: { quote_mismatch: 1, project_unbound: 1 } }) },
  17. { result: counts({ extractedCount: 1, duplicateCount: 1 }) },
  18. { result: counts({ extractedCount: 1, replayedCount: 1 }) },
  19. { status: 'failed', error: '连接失败', result: counts({ extractedCount: 2, writtenCount: 1 }) },
  20. {},
  21. ]
  22. const jobs = variants.map((variant, index) => ({ id: `job-${index}`, status: 'complete', createdAt: 1, ...variant }))
  23. vi.mocked(apiRequest).mockImplementation(async path => {
  24. if (path === '/api/app/workspace/projects') return { projects: [] } as never
  25. if (String(path).includes('/memory-jobs')) return { jobs } as never
  26. if (String(path).includes('/memories')) return { memories: [] } as never
  27. throw new Error(String(path))
  28. })
  29. const view = mount(WorkspaceKnowledgePanel, {
  30. props: { agents: [{ id: 'bot-a', name: '测试 Bot' } as WorkspaceAgent], conversations: [], embedded: true, initialTab: 'agent', selectedAgentId: 'bot-a' },
  31. global: { stubs: { AppIcon: true, AgentAvatar: true } },
  32. })
  33. try {
  34. await flushPromises()
  35. const text = view.text()
  36. for (const expected of ['已保存 2 条记忆', '本轮没有可记录内容', '候选记忆均未保存', '引用与原文不一致:1 条', '项目记忆缺少关联项目:1 条', '未新增记忆,已有相同内容', '重试已确认 1 条记录', '连接失败', '旧记录未统计保存结果']) expect(text).toContain(expected)
  37. expect(view.findAll('button').filter(button => button.text() === '重试')).toHaveLength(1)
  38. expect(text).toContain('本次提炼 2 条 · 新增保存 1 条')
  39. } finally { view.unmount() }
  40. })