|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + * |
| 4 | + * Messages on their way into a turn, or shown as not delivered, render after its answer. Failure |
| 5 | + * modes: the running answer loses its thinking indicator because a steered message trails it (an |
| 6 | + * empty one disappears altogether); the last answer's choices stop responding because a message |
| 7 | + * not delivered trails it; a real next message leaves the old answer's choices live. |
| 8 | + */ |
| 9 | +import { act } from 'react' |
| 10 | +import { integrationMatcherMock } from '@sim/testing/mocks/integration-matcher.mock' |
| 11 | +import { libDesktopMock } from '@sim/testing/mocks/lib-desktop.mock' |
| 12 | +import { nextNavigationMock } from '@sim/testing/mocks/next-navigation.mock' |
| 13 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 14 | +import { createRoot, type Root } from 'react-dom/client' |
| 15 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 16 | +import { FeatureFlagsProvider } from '@/lib/core/config/feature-flags-context' |
| 17 | +import { MothershipChat } from '@/app/workspace/[workspaceId]/home/components/mothership-chat/mothership-chat' |
| 18 | +import type { ChatMessage } from '@/app/workspace/[workspaceId]/home/types' |
| 19 | + |
| 20 | +vi.mock('next/navigation', () => nextNavigationMock) |
| 21 | +vi.mock('@/lib/desktop', () => libDesktopMock) |
| 22 | +vi.mock('@/blocks/integration-matcher', () => integrationMatcherMock) |
| 23 | + |
| 24 | +const owner: ChatMessage = { id: 'turn', role: 'user', content: 'Count' } |
| 25 | +const steered: ChatMessage = { |
| 26 | + id: 'steer', |
| 27 | + role: 'user', |
| 28 | + content: 'Stop at two', |
| 29 | + steering: { streamId: 'turn' }, |
| 30 | + delivery: 'sending', |
| 31 | +} |
| 32 | +const notDelivered: ChatMessage = { |
| 33 | + id: 'failed', |
| 34 | + role: 'user', |
| 35 | + content: 'Then summarize', |
| 36 | + delivery: 'failed', |
| 37 | +} |
| 38 | +const choices: ChatMessage = { |
| 39 | + id: 'answer', |
| 40 | + role: 'assistant', |
| 41 | + content: '', |
| 42 | + contentBlocks: [{ type: 'options', options: [{ id: '1', label: 'Pick this one' }] }], |
| 43 | +} |
| 44 | + |
| 45 | +let root: Root |
| 46 | +let container: HTMLDivElement |
| 47 | +let client: QueryClient |
| 48 | +const onSubmit = vi.fn() |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 52 | + vi.stubGlobal( |
| 53 | + 'ResizeObserver', |
| 54 | + class { |
| 55 | + observe() {} |
| 56 | + unobserve() {} |
| 57 | + disconnect() {} |
| 58 | + } |
| 59 | + ) |
| 60 | + vi.stubGlobal('matchMedia', () => ({ |
| 61 | + matches: true, |
| 62 | + addEventListener: vi.fn(), |
| 63 | + removeEventListener: vi.fn(), |
| 64 | + })) |
| 65 | + /* The virtualizer renders nothing into a viewport with no size. */ |
| 66 | + vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(2_000) |
| 67 | + vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(800) |
| 68 | + onSubmit.mockReset() |
| 69 | + client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 70 | + container = document.createElement('div') |
| 71 | + document.body.append(container) |
| 72 | + root = createRoot(container) |
| 73 | +}) |
| 74 | + |
| 75 | +afterEach(async () => { |
| 76 | + await act(async () => root.unmount()) |
| 77 | + client.clear() |
| 78 | + container.remove() |
| 79 | +}) |
| 80 | + |
| 81 | +async function renderChat(messages: ChatMessage[], isSending: boolean) { |
| 82 | + await act(async () => |
| 83 | + root.render( |
| 84 | + <QueryClientProvider client={client}> |
| 85 | + <FeatureFlagsProvider |
| 86 | + flags={{ |
| 87 | + dashboards: false, |
| 88 | + 'workflow-tests': false, |
| 89 | + changelog: false, |
| 90 | + 'mothership-model-selector': false, |
| 91 | + 'mothership-plan-mode': false, |
| 92 | + 'mothership-computer-use': false, |
| 93 | + }} |
| 94 | + > |
| 95 | + <MothershipChat |
| 96 | + messages={messages} |
| 97 | + isSending={isSending} |
| 98 | + onSubmit={onSubmit} |
| 99 | + onStopGeneration={vi.fn()} |
| 100 | + composer={<div />} |
| 101 | + /> |
| 102 | + </FeatureFlagsProvider> |
| 103 | + </QueryClientProvider> |
| 104 | + ) |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +function findOption(): HTMLElement { |
| 109 | + const option = [...container.querySelectorAll('button')].find((button) => |
| 110 | + button.textContent?.includes('Pick this one') |
| 111 | + ) |
| 112 | + if (!option) throw new Error('The answer rendered no choice') |
| 113 | + return option |
| 114 | +} |
| 115 | + |
| 116 | +describe('rows trailing an answer', () => { |
| 117 | + it('keeps the thinking indicator on a running answer a steered message trails', async () => { |
| 118 | + await renderChat( |
| 119 | + [owner, { id: 'live', role: 'assistant', content: '', contentBlocks: [] }, steered], |
| 120 | + true |
| 121 | + ) |
| 122 | + expect(container.textContent).toContain('Stop at two') |
| 123 | + expect(container.textContent).toContain('Thinking') |
| 124 | + }) |
| 125 | + |
| 126 | + it('keeps the last answer’s choices live under a message shown as not delivered', async () => { |
| 127 | + await renderChat([owner, choices, notDelivered], false) |
| 128 | + await act(async () => findOption().click()) |
| 129 | + expect(onSubmit).toHaveBeenCalledOnce() |
| 130 | + }) |
| 131 | + |
| 132 | + it('leaves an answer’s choices dead once a real next message follows it', async () => { |
| 133 | + await renderChat( |
| 134 | + [owner, choices, { id: 'next', role: 'user', content: 'Something else' }], |
| 135 | + false |
| 136 | + ) |
| 137 | + await act(async () => findOption().click()) |
| 138 | + expect(onSubmit).not.toHaveBeenCalled() |
| 139 | + }) |
| 140 | +}) |
0 commit comments