|
| 1 | +import { |
| 2 | + apiClientRequestMock, |
| 3 | + apiClientRequestMockFns, |
| 4 | +} from '@sim/testing/mocks/api-client-request.mock' |
| 5 | +import { reactQueryMock } from '@sim/testing/mocks/react-query.mock' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +vi.mock('@/lib/api/client/request', () => apiClientRequestMock) |
| 9 | +vi.mock('@tanstack/react-query', () => reactQueryMock) |
| 10 | + |
| 11 | +import { |
| 12 | + fetchMothershipChatHistory, |
| 13 | + type MothershipChatHistory, |
| 14 | + useRestoreMothershipChat, |
| 15 | +} from '@/hooks/queries/mothership-chats' |
| 16 | +import { useMothershipQueueStore } from '@/stores/mothership-queue/store' |
| 17 | + |
| 18 | +const mockRequestJson = apiClientRequestMockFns.mockRequestJson |
| 19 | + |
| 20 | +const history: MothershipChatHistory = { |
| 21 | + id: 'chat-1', |
| 22 | + mode: 'agent', |
| 23 | + title: 'Restored', |
| 24 | + messages: [], |
| 25 | + activeStreamId: null, |
| 26 | + resources: [], |
| 27 | +} |
| 28 | + |
| 29 | +/** Whether the queue store takes a send for the chat, i.e. whether its delete still holds. */ |
| 30 | +function takesSends(chatId: string): boolean { |
| 31 | + useMothershipQueueStore.getState().enqueue(chatId, { id: 'probe', content: 'probe' }) |
| 32 | + return useMothershipQueueStore.getState().queues[chatId] !== undefined |
| 33 | +} |
| 34 | + |
| 35 | +/** A server answer the test releases when it chooses. */ |
| 36 | +function deferredAnswer(value: unknown): () => void { |
| 37 | + let answer!: () => void |
| 38 | + mockRequestJson.mockReturnValue( |
| 39 | + new Promise((resolve) => { |
| 40 | + answer = () => resolve(value) |
| 41 | + }) |
| 42 | + ) |
| 43 | + return answer |
| 44 | +} |
| 45 | + |
| 46 | +/** The options `useRestoreMothershipChat` hands to `useMutation` (the mock returns them). */ |
| 47 | +interface RestoreMutation { |
| 48 | + mutationFn: (chatId: string) => Promise<void> |
| 49 | + onMutate?: (chatId: string) => { deleteSeen?: number } |
| 50 | + onSuccess: (data: undefined, chatId: string, context?: { deleteSeen?: number }) => void |
| 51 | +} |
| 52 | + |
| 53 | +async function restore(chatId: string, whileInFlight: () => void = () => {}) { |
| 54 | + const mutation = useRestoreMothershipChat() as unknown as RestoreMutation |
| 55 | + const context = mutation.onMutate?.(chatId) |
| 56 | + const answer = deferredAnswer({ success: true }) |
| 57 | + const done = mutation.mutationFn(chatId) |
| 58 | + whileInFlight() |
| 59 | + answer() |
| 60 | + await done |
| 61 | + mutation.onSuccess(undefined, chatId, context) |
| 62 | +} |
| 63 | + |
| 64 | +describe('lifting a chat delete', () => { |
| 65 | + beforeEach(() => { |
| 66 | + useMothershipQueueStore.getState().reset() |
| 67 | + mockRequestJson.mockReset() |
| 68 | + }) |
| 69 | + |
| 70 | + it('reopens a chat this tab saw deleted once the server returns it again', async () => { |
| 71 | + useMothershipQueueStore.getState().clearChat(history.id) |
| 72 | + mockRequestJson.mockResolvedValue({ chat: history }) |
| 73 | + |
| 74 | + await fetchMothershipChatHistory(history.id) |
| 75 | + |
| 76 | + expect(takesSends(history.id)).toBe(true) |
| 77 | + }) |
| 78 | + |
| 79 | + it('keeps the delete when the read returning the chat began before it', async () => { |
| 80 | + const answer = deferredAnswer({ chat: history }) |
| 81 | + const read = fetchMothershipChatHistory(history.id) |
| 82 | + useMothershipQueueStore.getState().clearChat(history.id) |
| 83 | + answer() |
| 84 | + await read |
| 85 | + |
| 86 | + expect(takesSends(history.id)).toBe(false) |
| 87 | + }) |
| 88 | + |
| 89 | + it('keeps a newer delete that lands while a read after an earlier one is in flight', async () => { |
| 90 | + useMothershipQueueStore.getState().clearChat(history.id) |
| 91 | + const answer = deferredAnswer({ chat: history }) |
| 92 | + const read = fetchMothershipChatHistory(history.id) |
| 93 | + useMothershipQueueStore.getState().reopenChat(history.id) |
| 94 | + useMothershipQueueStore.getState().clearChat(history.id) |
| 95 | + answer() |
| 96 | + await read |
| 97 | + |
| 98 | + expect(takesSends(history.id)).toBe(false) |
| 99 | + }) |
| 100 | + |
| 101 | + it('reopens a chat restored from Recently Deleted', async () => { |
| 102 | + useMothershipQueueStore.getState().clearChat(history.id) |
| 103 | + |
| 104 | + await restore(history.id) |
| 105 | + |
| 106 | + expect(takesSends(history.id)).toBe(true) |
| 107 | + }) |
| 108 | + |
| 109 | + it('keeps a delete that lands while the restore is in flight', async () => { |
| 110 | + useMothershipQueueStore.getState().clearChat(history.id) |
| 111 | + |
| 112 | + await restore(history.id, () => { |
| 113 | + useMothershipQueueStore.getState().reopenChat(history.id) |
| 114 | + useMothershipQueueStore.getState().clearChat(history.id) |
| 115 | + }) |
| 116 | + |
| 117 | + expect(takesSends(history.id)).toBe(false) |
| 118 | + }) |
| 119 | +}) |
0 commit comments