import { describe, it, expect, vi, beforeEach } from 'vitest' import { EventEmitter } from 'node:events' vi.mock('@electron-toolkit/utils', () => ({ is: { dev: false } })) class FakeWebContents extends EventEmitter { send = vi.fn() isLoading = vi.fn(() => false) } class FakeBrowserWindow extends EventEmitter { webContents = new FakeWebContents() show = vi.fn() moveTop = vi.fn() focus = vi.fn() setAlwaysOnTop = vi.fn() maximize = vi.fn() loadURL = vi.fn() loadFile = vi.fn() close = vi.fn() destroyed = false isDestroyed(): boolean { return this.destroyed } } let lastWindow: FakeBrowserWindow | null = null let nextWindowStartsLoading = false function createBrowserWindow(): FakeBrowserWindow { const window = new FakeBrowserWindow() if (nextWindowStartsLoading) { window.webContents.isLoading.mockReturnValue(true) nextWindowStartsLoading = false } lastWindow = window return window } const ipcMainListeners = new Map void>>() const ipcMain = { on: vi.fn((event: string, handler: (...args: unknown[]) => void) => { if (!ipcMainListeners.has(event)) ipcMainListeners.set(event, new Set()) ipcMainListeners.get(event)?.add(handler) }), removeListener: vi.fn((event: string, handler: (...args: unknown[]) => void) => { ipcMainListeners.get(event)?.delete(handler) }) } function fireIpc(event: string, ...args: unknown[]): void { for (const handler of ipcMainListeners.get(event) ?? []) handler(...args) } vi.mock('electron', () => ({ BrowserWindow: vi.fn(createBrowserWindow), ipcMain })) const { requestWrittenAnswers } = await import('../../src/main/coach/exerciseWindow') describe('requestWrittenAnswers()', () => { beforeEach(() => { // Reinitialise le singleton interne du module (win = null) en simulant la fermeture de la // fenetre precedente, pour qu'un nouveau test reparte toujours d'une fenetre fraiche. lastWindow?.emit('closed') vi.clearAllMocks() ipcMainListeners.clear() lastWindow = null }) it('should send the question list to the renderer and show the window', async () => { const promise = requestWrittenAnswers([{ question: 'Q1', type: 'free_text', options: null }]) expect(lastWindow?.show).toHaveBeenCalled() expect(lastWindow?.webContents.send).toHaveBeenCalledWith( 'ask-written-form', expect.objectContaining({ items: [{ id: '0', question: 'Q1', type: 'free_text', options: null }] }) ) const sentPayload = lastWindow?.webContents.send.mock.calls[0]?.[1] as { requestId: string } fireIpc('written-form-result', {}, sentPayload.requestId, [{ id: '0', answer: 'ma reponse' }]) await expect(promise).resolves.toEqual(['ma reponse']) }) it('should resolve with nulls for items missing from the submitted answers', async () => { const promise = requestWrittenAnswers([ { question: 'Q1', type: 'free_text', options: null }, { question: 'Q2', type: 'free_text', options: null } ]) const sentPayload = lastWindow?.webContents.send.mock.calls[0]?.[1] as { requestId: string } fireIpc('written-form-result', {}, sentPayload.requestId, [{ id: '0', answer: 'reponse 1' }]) await expect(promise).resolves.toEqual(['reponse 1', null]) }) it('should ignore a submission for a different request id', () => { void requestWrittenAnswers([{ question: 'Q1', type: 'free_text', options: null }]) fireIpc('written-form-result', {}, 'un-autre-id', [{ id: '0', answer: 'ignore' }]) expect(lastWindow?.close).not.toHaveBeenCalled() }) it('should resolve with all nulls when the window is closed before submission', async () => { const promise = requestWrittenAnswers([{ question: 'Q1', type: 'free_text', options: null }]) lastWindow?.emit('closed') await expect(promise).resolves.toEqual([null]) }) it('should resolve with all nulls when the timeout elapses with no submission', async () => { vi.useFakeTimers() const promise = requestWrittenAnswers( [{ question: 'Q1', type: 'free_text', options: null }], 1000 ) await vi.advanceTimersByTimeAsync(1000) await expect(promise).resolves.toEqual([null]) vi.useRealTimers() }) it('should wait for did-finish-load before sending the form when the page is still loading', () => { nextWindowStartsLoading = true void requestWrittenAnswers([{ question: 'Q1', type: 'free_text', options: null }]) expect(lastWindow?.webContents.send).not.toHaveBeenCalled() lastWindow?.webContents.emit('did-finish-load') expect(lastWindow?.webContents.send).toHaveBeenCalled() }) })