|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockFireTableTrigger } = vi.hoisted(() => ({ |
| 7 | + mockFireTableTrigger: vi.fn(), |
| 8 | +})) |
| 9 | + |
| 10 | +vi.mock('@/lib/table/trigger', () => ({ |
| 11 | + fireTableTrigger: mockFireTableTrigger, |
| 12 | +})) |
| 13 | + |
| 14 | +import { fireMemoryTableTrigger } from '@/lib/virtual-tables/memory-virtual-table.server' |
| 15 | + |
| 16 | +describe('fireMemoryTableTrigger', () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks() |
| 19 | + mockFireTableTrigger.mockResolvedValue(undefined) |
| 20 | + }) |
| 21 | + |
| 22 | + it('maps an updated memory record to a virtual-table update event', async () => { |
| 23 | + const previousRecord = { |
| 24 | + id: 'memory-1', |
| 25 | + workspaceId: 'workspace-1', |
| 26 | + key: 'conversation-1', |
| 27 | + data: [{ role: 'user', content: 'Hello' }], |
| 28 | + createdAt: new Date('2026-01-01T00:00:00.000Z'), |
| 29 | + updatedAt: new Date('2026-01-01T00:00:00.000Z'), |
| 30 | + deletedAt: null, |
| 31 | + } |
| 32 | + const updatedRecord = { |
| 33 | + ...previousRecord, |
| 34 | + data: [...previousRecord.data, { role: 'assistant', content: 'Hi' }], |
| 35 | + updatedAt: new Date('2026-01-02T00:00:00.000Z'), |
| 36 | + } |
| 37 | + |
| 38 | + await fireMemoryTableTrigger(updatedRecord, previousRecord, 'request-1') |
| 39 | + |
| 40 | + expect(mockFireTableTrigger).toHaveBeenCalledWith( |
| 41 | + 'system_memory_workspace-1', |
| 42 | + 'Memory', |
| 43 | + 'update', |
| 44 | + [ |
| 45 | + expect.objectContaining({ |
| 46 | + id: 'memory-1', |
| 47 | + data: expect.objectContaining({ |
| 48 | + conversation_id: 'conversation-1', |
| 49 | + message_count: 2, |
| 50 | + updated_at: '2026-01-02T00:00:00.000Z', |
| 51 | + }), |
| 52 | + }), |
| 53 | + ], |
| 54 | + new Map([ |
| 55 | + [ |
| 56 | + 'memory-1', |
| 57 | + expect.objectContaining({ |
| 58 | + conversation_id: 'conversation-1', |
| 59 | + message_count: 1, |
| 60 | + updated_at: '2026-01-01T00:00:00.000Z', |
| 61 | + }), |
| 62 | + ], |
| 63 | + ]), |
| 64 | + expect.objectContaining({ columns: expect.any(Array) }), |
| 65 | + 'request-1' |
| 66 | + ) |
| 67 | + }) |
| 68 | + |
| 69 | + it('maps a newly inserted memory record to a virtual-table insert event', async () => { |
| 70 | + const insertedRecord = { |
| 71 | + id: 'memory-1', |
| 72 | + workspaceId: 'workspace-1', |
| 73 | + key: 'conversation-1', |
| 74 | + data: [{ role: 'user', content: 'Hello' }], |
| 75 | + createdAt: new Date('2026-01-01T00:00:00.000Z'), |
| 76 | + updatedAt: new Date('2026-01-01T00:00:00.000Z'), |
| 77 | + deletedAt: null, |
| 78 | + } |
| 79 | + |
| 80 | + await fireMemoryTableTrigger(insertedRecord, null, 'request-1') |
| 81 | + |
| 82 | + expect(mockFireTableTrigger).toHaveBeenCalledWith( |
| 83 | + 'system_memory_workspace-1', |
| 84 | + 'Memory', |
| 85 | + 'insert', |
| 86 | + [expect.objectContaining({ id: 'memory-1' })], |
| 87 | + null, |
| 88 | + expect.any(Object), |
| 89 | + 'request-1' |
| 90 | + ) |
| 91 | + }) |
| 92 | + |
| 93 | + it('does not emit table events for deleted memory records hidden from the virtual table', async () => { |
| 94 | + await fireMemoryTableTrigger( |
| 95 | + { |
| 96 | + id: 'memory-1', |
| 97 | + workspaceId: 'workspace-1', |
| 98 | + key: 'conversation-1', |
| 99 | + data: [], |
| 100 | + createdAt: new Date('2026-01-01T00:00:00.000Z'), |
| 101 | + updatedAt: new Date('2026-01-01T00:00:00.000Z'), |
| 102 | + deletedAt: new Date('2026-01-02T00:00:00.000Z'), |
| 103 | + }, |
| 104 | + null, |
| 105 | + 'request-1' |
| 106 | + ) |
| 107 | + |
| 108 | + expect(mockFireTableTrigger).not.toHaveBeenCalled() |
| 109 | + }) |
| 110 | +}) |
0 commit comments