|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { act, useEffect } from 'react' |
| 4 | +import { jsonResponse } from '@sim/testing/helpers/http' |
| 5 | +import { nextNavigationMock } from '@sim/testing/mocks/next-navigation.mock' |
| 6 | +import { sleep } from '@sim/utils/helpers' |
| 7 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 8 | +import { createRoot, type Root } from 'react-dom/client' |
| 9 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 10 | + |
| 11 | +vi.mock('next/navigation', () => nextNavigationMock) |
| 12 | + |
| 13 | +import { ToastProvider } from '@sim/emcn' |
| 14 | +import type { MothershipEffort } from '@/lib/mothership/model-options' |
| 15 | +import { useSetMothershipChatEffort } from '@/hooks/queries/mothership-chats' |
| 16 | +import { useMothershipEffortStore } from '@/stores/mothership-effort/store' |
| 17 | + |
| 18 | +const FAILURE_NOTICE = "Couldn't change reasoning effort" |
| 19 | + |
| 20 | +interface EffortPickerProps { |
| 21 | + onReady: (pick: (effort: MothershipEffort) => Promise<void>) => void |
| 22 | +} |
| 23 | + |
| 24 | +function EffortPicker({ onReady }: EffortPickerProps) { |
| 25 | + const { mutateAsync } = useSetMothershipChatEffort('chat-1') |
| 26 | + useEffect( |
| 27 | + () => onReady((effort) => mutateAsync(effort).catch(() => undefined)), |
| 28 | + [onReady, mutateAsync] |
| 29 | + ) |
| 30 | + return null |
| 31 | +} |
| 32 | + |
| 33 | +let root: Root |
| 34 | +let pick: (effort: MothershipEffort) => Promise<void> |
| 35 | + |
| 36 | +beforeEach(async () => { |
| 37 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 38 | + vi.stubGlobal( |
| 39 | + 'ResizeObserver', |
| 40 | + class { |
| 41 | + observe() {} |
| 42 | + unobserve() {} |
| 43 | + disconnect() {} |
| 44 | + } |
| 45 | + ) |
| 46 | + vi.stubGlobal('fetch', vi.fn()) |
| 47 | + useMothershipEffortStore.getState().reset() |
| 48 | + const container = document.createElement('div') |
| 49 | + document.body.appendChild(container) |
| 50 | + root = createRoot(container) |
| 51 | + await act(async () => { |
| 52 | + root.render( |
| 53 | + <QueryClientProvider client={new QueryClient()}> |
| 54 | + <ToastProvider> |
| 55 | + <EffortPicker |
| 56 | + onReady={(next) => { |
| 57 | + pick = next |
| 58 | + }} |
| 59 | + /> |
| 60 | + </ToastProvider> |
| 61 | + </QueryClientProvider> |
| 62 | + ) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +afterEach(() => { |
| 67 | + act(() => root.unmount()) |
| 68 | + document.body.innerHTML = '' |
| 69 | +}) |
| 70 | + |
| 71 | +describe('chat effort save failures', () => { |
| 72 | + it('tells the user when a failed save rolls their pick back', async () => { |
| 73 | + vi.mocked(fetch).mockResolvedValueOnce(new Response('save failed', { status: 500 })) |
| 74 | + |
| 75 | + await act(() => pick('xhigh')) |
| 76 | + |
| 77 | + expect(useMothershipEffortStore.getState().chatEfforts['chat-1']).toBeUndefined() |
| 78 | + expect(document.body.textContent).toContain(FAILURE_NOTICE) |
| 79 | + }) |
| 80 | + |
| 81 | + it('stays quiet when a newer pick already replaced the one that failed', async () => { |
| 82 | + const saves = [Promise.withResolvers<Response>(), Promise.withResolvers<Response>()] |
| 83 | + for (const save of saves) vi.mocked(fetch).mockReturnValueOnce(save.promise) |
| 84 | + |
| 85 | + let outcomes: Promise<void>[] = [] |
| 86 | + await act(async () => { |
| 87 | + outcomes = [pick('low'), pick('high')] |
| 88 | + await sleep(1) |
| 89 | + }) |
| 90 | + await act(async () => { |
| 91 | + saves[0].resolve(new Response('save failed', { status: 500 })) |
| 92 | + saves[1].resolve(jsonResponse({ success: true })) |
| 93 | + await Promise.all(outcomes) |
| 94 | + }) |
| 95 | + |
| 96 | + expect(useMothershipEffortStore.getState().chatEfforts['chat-1']?.effort).toBe('high') |
| 97 | + expect(document.body.textContent).not.toContain(FAILURE_NOTICE) |
| 98 | + }) |
| 99 | +}) |
0 commit comments