Files
ForgeBase/Max Avatar/tests/unit/drawingWindow.test.ts
T
williamandClaude Sonnet 5 1c2f002eb3 refactor: move app code into Max Avatar/ subfolder (monorepo prep for backend)
Step 1 of adding a backend proxy server (API keys held server-side instead
of shipped in the installer). Moves everything app-specific into
"Max Avatar/" via git mv (preserves history): src/, tests/, scripts/,
assets/, build/, resources/, package.json/-lock.json, electron-builder.yml,
electron.vite.config.ts, eslint.config.mjs, playwright.config.ts,
vitest.config.mts, sonar-project.properties, tsconfig*.json, Dockerfile,
.editorconfig/.env*/.gitignore/.prettier*.

CLAUDE.md, README.md, docs/, .claude/, .gitea/, .husky/, .vscode/, and the
docker-compose files stay at the repo root, since they'll apply to both the
app and the future backend.

No application code needed changes (all its internal paths are relative to
its own tree). Adjusted what referenced the old root from outside:
- .husky/pre-commit: cd into "Max Avatar" before lint-staged
- .gitea/workflows/*.yml: working-directory + cache-dependency-path +
  artifact paths + SonarQube projectBaseDir
- docker-compose.yml: build context -> ./Max Avatar
- Max Avatar/Dockerfile: node:20-alpine -> node:24-alpine (same node:sqlite
  issue already fixed elsewhere, never applied here before)
- .vscode/launch.json, README.md: updated paths/instructions

Verified from Max Avatar/ after the move: npm ci, typecheck, lint,
test:coverage (473/473, same coverage numbers), build, and dev all work
unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:15:30 +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()
})
})