Files
ForgeBase/tests/unit/captureWorker.test.ts
T
williamandClaude Sonnet 5 a771883bf5
CI / quality (push) Failing after 7m27s
CI / quality (pull_request) Failing after 7m17s
fix(ci): require Node 24 — node:sqlite isn't recognized as a builtin by Vite before that
Node 22 makes node:sqlite importable (isBuiltin() returns true) but doesn't
list it in module.builtinModules, which Vite/Rolldown use to decide what to
externalize instead of bundle. That made the whole Vitest run fail on CI
("Cannot bundle Node.js built-in node:sqlite") even after pinning Node 22.
Root-caused by reproducing directly in node:22 vs node:24 Linux containers on
the VPS. Bumped ci.yml/release.yml/sonarqube.yml and package.json engines to
Node 24, and verified 473/473 tests pass in a real Linux Node 24 container.

Also:
- Exclude coachStore.ts/memory/store.ts/meetingStore.ts from coverage.include:
  Vitest's coverage-v8 still tries to transform them via Vite's "client"
  environment (which can't bundle Node builtins at all) when establishing the
  baseline for unimported files, independently of the Node 22/24 issue above.
- Fix tests/unit/captureWorker.test.ts: hardcoded Windows path separator in
  an expectation didn't match path.join's output on Linux.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 10:12:25 +01:00

63 lines
2.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { EventEmitter } from 'node:events'
import { join } from 'node:path'
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', () => {
const purgeDir = 'C:\\captures'
parentPort.emit('message', { purgeDir, cutoff: 1000 })
expect(unlinkSyncMock).toHaveBeenCalledTimes(1)
expect(unlinkSyncMock).toHaveBeenCalledWith(join(purgeDir, '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' })
})
})