|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { renderHook } from '@testing-library/react'; |
| 3 | +import { MemoryRouter } from 'react-router-dom'; |
| 4 | +import type { PropsWithChildren } from 'react'; |
| 5 | +import { useAutoSubmit } from './useAutoSubmit'; |
| 6 | +import { ChatState } from '../types/chatState'; |
| 7 | +import type { Session } from '../api'; |
| 8 | +import type { UserInput } from '../types/message'; |
| 9 | + |
| 10 | +function makeSession(overrides: Partial<Session> = {}): Session { |
| 11 | + return { |
| 12 | + id: 'sess-1', |
| 13 | + name: 'untitled', |
| 14 | + message_count: 0, |
| 15 | + created_at: new Date().toISOString(), |
| 16 | + updated_at: new Date().toISOString(), |
| 17 | + working_dir: '/tmp', |
| 18 | + extension_data: { active: [], installed: [] }, |
| 19 | + ...overrides, |
| 20 | + } as Session; |
| 21 | +} |
| 22 | + |
| 23 | +const initialMessage: UserInput = { |
| 24 | + msg: 'Run the recipe', |
| 25 | + images: [], |
| 26 | +}; |
| 27 | + |
| 28 | +describe('useAutoSubmit', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('does not auto-submit while recipe acceptance is unresolved', () => { |
| 34 | + const handleSubmit = vi.fn(); |
| 35 | + const dispatchEventSpy = vi.spyOn(window, 'dispatchEvent'); |
| 36 | + |
| 37 | + const wrapper = ({ children }: PropsWithChildren) => ( |
| 38 | + <MemoryRouter initialEntries={['/pair?resumeSessionId=sess-1']}>{children}</MemoryRouter> |
| 39 | + ); |
| 40 | + |
| 41 | + renderHook( |
| 42 | + () => |
| 43 | + useAutoSubmit({ |
| 44 | + sessionId: 'sess-1', |
| 45 | + session: makeSession(), |
| 46 | + messages: [], |
| 47 | + chatState: ChatState.Idle, |
| 48 | + initialMessage, |
| 49 | + canAutoSubmit: false, |
| 50 | + handleSubmit, |
| 51 | + }), |
| 52 | + { wrapper } |
| 53 | + ); |
| 54 | + |
| 55 | + expect(handleSubmit).not.toHaveBeenCalled(); |
| 56 | + expect(dispatchEventSpy).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('auto-submits once recipe acceptance is confirmed', () => { |
| 60 | + const handleSubmit = vi.fn(); |
| 61 | + const dispatchEventSpy = vi.spyOn(window, 'dispatchEvent'); |
| 62 | + |
| 63 | + const wrapper = ({ children }: PropsWithChildren) => ( |
| 64 | + <MemoryRouter initialEntries={['/pair?resumeSessionId=sess-1']}>{children}</MemoryRouter> |
| 65 | + ); |
| 66 | + |
| 67 | + const { rerender } = renderHook( |
| 68 | + ({ canAutoSubmit }) => |
| 69 | + useAutoSubmit({ |
| 70 | + sessionId: 'sess-1', |
| 71 | + session: makeSession(), |
| 72 | + messages: [], |
| 73 | + chatState: ChatState.Idle, |
| 74 | + initialMessage, |
| 75 | + canAutoSubmit, |
| 76 | + handleSubmit, |
| 77 | + }), |
| 78 | + { |
| 79 | + initialProps: { canAutoSubmit: false }, |
| 80 | + wrapper, |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + expect(handleSubmit).not.toHaveBeenCalled(); |
| 85 | + |
| 86 | + rerender({ canAutoSubmit: true }); |
| 87 | + |
| 88 | + expect(handleSubmit).toHaveBeenCalledTimes(1); |
| 89 | + expect(handleSubmit).toHaveBeenCalledWith(initialMessage); |
| 90 | + expect(dispatchEventSpy).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | +}); |
0 commit comments