Files
ForgeBase/tests/unit/captureWorker.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

61 lines
2.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { EventEmitter } from 'node:events'
const writeFileSyncMock = vi.fn()
const readdirSyncMock = vi.fn(() => ['old.png', 'new.png'])
const statSyncMock = vi.fn((path: string) => ({
mtimeMs: path.includes('old') ? 0 : Date.now()
}))
const unlinkSyncMock = vi.fn()
vi.mock('fs', () => ({
writeFileSync: writeFileSyncMock,
readdirSync: readdirSyncMock,
statSync: statSyncMock,
unlinkSync: unlinkSyncMock
}))
const parentPort = new EventEmitter() as EventEmitter & { postMessage: (msg: unknown) => void }
parentPort.postMessage = vi.fn()
vi.mock('node:worker_threads', () => ({ parentPort }))
await import('../../src/main/coach/captureWorker')
describe('captureWorker (worker_thread message handler)', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should write the given PNG buffer to disk and acknowledge success', () => {
const png = Buffer.from([1, 2, 3])
parentPort.emit('message', { filePath: 'C:\\captures\\shot.png', png })
expect(writeFileSyncMock).toHaveBeenCalledWith('C:\\captures\\shot.png', png)
expect(parentPort.postMessage).toHaveBeenCalledWith({
ok: true,
filePath: 'C:\\captures\\shot.png'
})
})
it('should report failure when writing fails', () => {
writeFileSyncMock.mockImplementationOnce(() => {
throw new Error('disk full')
})
parentPort.emit('message', { filePath: 'C:\\captures\\shot.png', png: Buffer.from([1]) })
expect(parentPort.postMessage).toHaveBeenCalledWith({ ok: false, error: 'disk full' })
})
it('should delete only files older than the cutoff when purging', () => {
parentPort.emit('message', { purgeDir: 'C:\\captures', cutoff: 1000 })
expect(unlinkSyncMock).toHaveBeenCalledTimes(1)
expect(unlinkSyncMock).toHaveBeenCalledWith('C:\\captures\\old.png')
expect(parentPort.postMessage).toHaveBeenCalledWith({ ok: true })
})
it('should report failure when purging fails', () => {
readdirSyncMock.mockImplementationOnce(() => {
throw new Error('dir gone')
})
parentPort.emit('message', { purgeDir: 'C:\\captures', cutoff: 1000 })
expect(parentPort.postMessage).toHaveBeenCalledWith({ ok: false, error: 'dir gone' })
})
})