import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, fireEvent, waitFor, act } from '@testing-library/react' import ObjectiveWindowRoot from '../../../src/renderer/src/components/ObjectiveWindowRoot' import { useSessionStore } from '../../../src/renderer/src/stores/useSessionStore' const confirmObjective = vi.fn() const notifySessionEnded = vi.fn() let objectiveReadyCallback: ((sessionId: number, draft: unknown) => void) | undefined const onObjectiveReady = vi.fn((callback: (sessionId: number, draft: unknown) => void) => { objectiveReadyCallback = callback return () => {} }) beforeEach(() => { vi.clearAllMocks() objectiveReadyCallback = undefined ;(window as unknown as { api: Record }).api = { sessions: { confirmObjective, get: vi.fn() }, notifySessionEnded, onObjectiveReady } useSessionStore.getState().restart() }) /** * Fenetre independante du chat texte (retour utilisateur direct : la confirmation de l'objectif * ne doit jamais dependre d'ouvrir la conversation, y compris quand tout l'echange se fait au * vocal). Son etat vient exclusivement du relais "objective-ready" du main process. */ describe('', () => { it('should render nothing before the main process relays a ready objective', () => { const { container } = render() expect(container).toBeEmptyDOMElement() }) it('should show the card once the main process relays a ready objective (text or voice, same path)', () => { render() act(() => objectiveReadyCallback?.(1, { subject: 'Facturation freelance', professionalGoal: 'Facturer sans erreur', duration: '2 semaines' }) ) expect(screen.getByText('Ton objectif de formation')).toBeInTheDocument() expect(screen.getByText('Facturation freelance')).toBeInTheDocument() }) it('should confirm the objective via the store, independently of any chat window', async () => { confirmObjective.mockResolvedValueOnce({}) ;(window as unknown as { api: { sessions: Record } }).api.sessions.get = vi .fn() .mockResolvedValueOnce({ id: 1, status: 'objective_validated' }) render() act(() => objectiveReadyCallback?.(1, { subject: 'Facturation freelance', professionalGoal: 'Facturer sans erreur', duration: '2 semaines' }) ) fireEvent.click( screen.getByRole('button', { name: "C'est exactement ca, confirmer l'objectif" }) ) await waitFor(() => expect(confirmObjective).toHaveBeenCalledWith(1)) expect(await screen.findByText(/Objectif enregistre/)).toBeInTheDocument() }) })