Files
ForgeBase/tests/unit/drawingWindow.test.ts
T
williamandClaude Sonnet 5 e9690a49f9
CI / quality (push) Failing after 6m22s
feat: add CI/CD, code quality, testing and security hardening
Implements Max_Code_Quality_CICD_Production.md: TypeScript strict mode,
ESLint flat config with type-checking and security plugin, Prettier,
Husky + lint-staged, Vitest with 80%+ coverage (473 tests), Playwright
e2e, Electron security hardening (contextIsolation, CSP, IPC validation),
electron-builder + electron-updater, Dockerfile, Gitea Actions CI/release
workflows, and SonarQube docker-compose setup. Also fixes two real bugs
found while writing tests: unawaited clipboard promises and a mermaid
container ref timing issue in CoachDrawingView.

Fixes lint-staged on Windows/Node 24 by invoking eslint/prettier via
`node <bin>.js` instead of their .cmd shims, which Node's child_process
now refuses to spawn without shell:true.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-29 09:05:21 +01:00

156 lines
5.0 KiB
TypeScript

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()
destroyed = false
isDestroyed(): boolean {
return this.destroyed
}
}
let lastWindow: FakeBrowserWindow | null = null
const ipcMainListeners = new Map<string, Set<(...args: unknown[]) => 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)
}
function createBrowserWindow(): FakeBrowserWindow {
const window = new FakeBrowserWindow()
lastWindow = window
return window
}
vi.mock('electron', () => ({
BrowserWindow: vi.fn(createBrowserWindow),
ipcMain
}))
const { openDrawingWindow, revealShape, renderMermaid, playCaption } =
await import('../../src/main/coach/drawingWindow')
describe('openDrawingWindow()', () => {
beforeEach(() => {
vi.clearAllMocks()
ipcMainListeners.clear()
})
it('should create the window, load it and send init-drawing once ready', async () => {
await openDrawingWindow('Mon titre', 700, 500)
expect(lastWindow?.show).toHaveBeenCalled()
expect(lastWindow?.webContents.send).toHaveBeenCalledWith('init-drawing', {
title: 'Mon titre',
width: 700,
height: 500
})
})
it('should reuse the existing window on a second call rather than creating a new one', async () => {
await openDrawingWindow('Titre 1', 700, 500)
const firstWindow = lastWindow
await openDrawingWindow('Titre 2', 700, 500)
expect(lastWindow).toBe(firstWindow)
})
})
describe('revealShape()', () => {
beforeEach(async () => {
vi.clearAllMocks()
ipcMainListeners.clear()
await openDrawingWindow('Titre', 700, 500)
})
it('should resolve once the renderer acknowledges the reveal with the matching request id', async () => {
const promise = revealShape({ type: 'circle', x: 1, y: 1 }, null)
const sentPayload = lastWindow?.webContents.send.mock.calls.find(
(c) => c[0] === 'reveal-shape'
)?.[1] as { requestId: string }
fireIpc('shape-reveal-done', {}, sentPayload.requestId)
await expect(promise).resolves.toBeUndefined()
})
it('should resolve immediately when there is no open window', async () => {
lastWindow!.destroyed = true
await expect(revealShape({ type: 'circle', x: 1, y: 1 }, null)).resolves.toBeUndefined()
})
it('should resolve after the timeout even without an acknowledgement', async () => {
vi.useFakeTimers()
const promise = revealShape({ type: 'circle', x: 1, y: 1 }, null, 1000)
await vi.advanceTimersByTimeAsync(1000)
await expect(promise).resolves.toBeUndefined()
vi.useRealTimers()
})
})
describe('renderMermaid()', () => {
beforeEach(async () => {
vi.clearAllMocks()
ipcMainListeners.clear()
await openDrawingWindow('Titre', 700, 500)
})
it('should resolve when the renderer reports a successful render', async () => {
const promise = renderMermaid('Cycle', 'graph LR; A --> B')
await Promise.resolve()
await Promise.resolve()
const sentPayload = lastWindow?.webContents.send.mock.calls.find(
(c) => c[0] === 'render-mermaid'
)?.[1] as { requestId: string }
fireIpc('mermaid-render-done', {}, sentPayload.requestId, true)
await expect(promise).resolves.toBeUndefined()
})
it('should reject with the renderer-provided error on a failed render', async () => {
const promise = renderMermaid('Cycle', 'graph LR; A --> B')
await Promise.resolve()
await Promise.resolve()
const sentPayload = lastWindow?.webContents.send.mock.calls.find(
(c) => c[0] === 'render-mermaid'
)?.[1] as { requestId: string }
fireIpc('mermaid-render-done', {}, sentPayload.requestId, false, 'syntaxe invalide')
await expect(promise).rejects.toThrow('syntaxe invalide')
})
})
describe('playCaption()', () => {
beforeEach(async () => {
vi.clearAllMocks()
ipcMainListeners.clear()
await openDrawingWindow('Titre', 700, 500)
})
it('should resolve once the renderer acknowledges the caption is done', async () => {
const audio = new ArrayBuffer(4)
const promise = playCaption(audio)
const sentPayload = lastWindow?.webContents.send.mock.calls.find(
(c) => c[0] === 'play-caption'
)?.[1] as { requestId: string }
fireIpc('caption-done', {}, sentPayload.requestId)
await expect(promise).resolves.toBeUndefined()
})
})