unreadCenter.test.ts 2.2 KB

123456789101112131415161718192021222324252627
  1. // @vitest-environment node
  2. import { afterEach, expect, it } from 'vitest'
  3. import { mkdtempSync, rmSync } from 'node:fs'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import { WorkspaceStore } from '../../src/server/workspaceStore'
  7. import { unreadSnapshot, acknowledgeUnread } from '../../src/server/unreadCenter'
  8. const homes: string[] = []; const stores: Array<{ close(): void }> = []
  9. afterEach(() => { stores.reverse().forEach(s => s.close()); stores.length = 0; homes.splice(0).forEach(h => rmSync(h, { recursive: true, force: true })) })
  10. it('shares exact Bot snapshots between devices, isolates owners and Profiles, and acknowledges only seen messages', () => {
  11. const home = mkdtempSync(join(tmpdir(), 'unread-integration-')); homes.push(home)
  12. const workspace = new WorkspaceStore(home); stores.push(workspace)
  13. const a = workspace.createAgent('a', { name: 'bot', profile: 'p' }), b = workspace.createAgent('a', { name: 'two', profile: 'p' })
  14. const c = workspace.createGroup('a', { name: 'group', memberIds: [a.id, b.id], administratorId: a.id }), task = workspace.tasks('a', c.id)[0]!
  15. workspace.saveMessage('a', { id: 'bot-final', conversationId: c.id, conversationTaskId: task.id, seq: 0, role: 'assistant', status: 'complete', content: '附件答复', reasoning: '', attachments: [], tools: [], createdAt: 1 })
  16. const device1 = unreadSnapshot(workspace, 'a'), device2 = unreadSnapshot(workspace, 'a')
  17. // Ordinary chat is retired: `chat` stays 0 for older clients.
  18. expect(device1).toMatchObject({ total: 1, bot: 1, chat: 0 }); expect(device2).toEqual(device1)
  19. expect(unreadSnapshot(workspace, 'b').total).toBe(0)
  20. expect(unreadSnapshot(workspace, 'a', (_, profile) => profile !== 'p').total).toBe(0)
  21. workspace.saveMessage('a', { id: 'bot-later', conversationId: c.id, conversationTaskId: task.id, seq: 0, role: 'assistant', status: 'complete', content: '随后新消息', reasoning: '', attachments: [], tools: [], createdAt: 2 })
  22. acknowledgeUnread(workspace, 'a', device1.conversations)
  23. acknowledgeUnread(workspace, 'a', [{ mode: 'chat', id: 'retired', profile: 'p', messages: [{ id: 'x', seq: 1 }] }])
  24. expect(unreadSnapshot(workspace, 'a')).toMatchObject({ total: 1, bot: 1, chat: 0 })
  25. })