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>
97 lines
3.5 KiB
TypeScript
97 lines
3.5 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { renderHook } from '@testing-library/react'
|
|
|
|
let onToggleCallback: ((transcribing: boolean) => void) | undefined
|
|
const sendMeetingAudioChunk = vi.fn()
|
|
const sendMeetingRecordingStopped = vi.fn()
|
|
const onToggleMeetingTranscription = vi.fn((cb: (transcribing: boolean) => void) => {
|
|
onToggleCallback = cb
|
|
return () => {}
|
|
})
|
|
|
|
function fakeStream(): MediaStream {
|
|
const stop = vi.fn()
|
|
return { getTracks: () => [{ stop }] } as unknown as MediaStream
|
|
}
|
|
|
|
const getUserMedia = vi.fn(async () => fakeStream())
|
|
const enumerateDevices = vi.fn(async () => [])
|
|
|
|
class FakeMediaRecorder {
|
|
static instances: FakeMediaRecorder[] = []
|
|
state: 'recording' | 'inactive' = 'recording'
|
|
ondataavailable: ((event: { data: Blob }) => void) | null = null
|
|
onstop: (() => void) | null = null
|
|
constructor(
|
|
public stream: MediaStream,
|
|
public options: { mimeType: string }
|
|
) {
|
|
FakeMediaRecorder.instances.push(this)
|
|
}
|
|
start(): void {
|
|
this.state = 'recording'
|
|
}
|
|
stop(): void {
|
|
this.state = 'inactive'
|
|
this.ondataavailable?.({ data: new Blob(['chunk'], { type: 'audio/webm' }) })
|
|
this.onstop?.()
|
|
}
|
|
}
|
|
|
|
const { useMeetingRecorder } = await import('../../../src/renderer/src/voice/useMeetingRecorder')
|
|
|
|
describe('useMeetingRecorder()', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
FakeMediaRecorder.instances = []
|
|
;(window as unknown as { api: Record<string, unknown> }).api = {
|
|
onToggleMeetingTranscription,
|
|
sendMeetingAudioChunk,
|
|
sendMeetingRecordingStopped
|
|
}
|
|
Object.defineProperty(navigator, 'mediaDevices', {
|
|
value: { getUserMedia, enumerateDevices },
|
|
configurable: true
|
|
})
|
|
vi.stubGlobal('MediaRecorder', FakeMediaRecorder)
|
|
})
|
|
|
|
it('should start recording the microphone when transcription is toggled on', async () => {
|
|
renderHook(() => useMeetingRecorder())
|
|
onToggleCallback?.(true)
|
|
await vi.waitFor(() => expect(FakeMediaRecorder.instances.length).toBe(1))
|
|
expect(getUserMedia).toHaveBeenCalledWith({ audio: true })
|
|
})
|
|
|
|
it('should give up quietly when the microphone is unavailable, without ever starting a recorder', async () => {
|
|
getUserMedia.mockRejectedValueOnce(new Error('no mic'))
|
|
renderHook(() => useMeetingRecorder())
|
|
onToggleCallback?.(true)
|
|
await vi.waitFor(() => expect(getUserMedia).toHaveBeenCalled())
|
|
// Rien n'a jamais demarre : stopper ensuite ne doit ni planter ni envoyer de notification pour
|
|
// une session qui n'a jamais existe.
|
|
expect(() => onToggleCallback?.(false)).not.toThrow()
|
|
expect(FakeMediaRecorder.instances.length).toBe(0)
|
|
})
|
|
|
|
it('should send the recorded chunk and notify the main process when stopped', async () => {
|
|
renderHook(() => useMeetingRecorder())
|
|
onToggleCallback?.(true)
|
|
await vi.waitFor(() => expect(FakeMediaRecorder.instances.length).toBe(1))
|
|
|
|
onToggleCallback?.(false)
|
|
await vi.waitFor(() => expect(sendMeetingAudioChunk).toHaveBeenCalled())
|
|
expect(sendMeetingRecordingStopped).toHaveBeenCalled()
|
|
})
|
|
|
|
it('should start a new segment automatically instead of stopping when not asked to stop', async () => {
|
|
renderHook(() => useMeetingRecorder())
|
|
onToggleCallback?.(true)
|
|
await vi.waitFor(() => expect(FakeMediaRecorder.instances.length).toBe(1))
|
|
|
|
FakeMediaRecorder.instances[0]?.stop()
|
|
await vi.waitFor(() => expect(FakeMediaRecorder.instances.length).toBe(2))
|
|
expect(sendMeetingRecordingStopped).not.toHaveBeenCalled()
|
|
})
|
|
})
|