Files
ForgeBase/tests/unit/components/AvatarViewer.test.tsx
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

167 lines
4.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, cleanup } from '@testing-library/react'
const disposeGeometry = vi.fn()
const disposeMaterial = vi.fn()
const disposeRenderer = vi.fn()
const sceneAdd = vi.fn()
let loadedMesh: FakeMesh | undefined
class FakeObject3D {
frustumCulled = true
traverse(cb: (obj: unknown) => void): void {
if (loadedMesh) cb(loadedMesh)
}
}
class FakeMesh extends FakeObject3D {
geometry = { dispose: disposeGeometry }
material = { dispose: disposeMaterial }
}
class FakeScene extends FakeObject3D {
add = sceneAdd
}
class FakeCamera {
fov = 30
aspect = 1
position = { set: vi.fn() }
updateProjectionMatrix = vi.fn()
lookAt = vi.fn()
}
class FakeRenderer {
domElement = document.createElement('canvas')
setPixelRatio = vi.fn()
setClearColor = vi.fn()
setSize = vi.fn()
render = vi.fn()
dispose = disposeRenderer
}
class FakeAnimationAction {
reset = vi.fn(() => this)
fadeIn = vi.fn(() => this)
fadeOut = vi.fn(() => this)
play = vi.fn(() => this)
}
class FakeAnimationMixer {
update = vi.fn()
clipAction = vi.fn(() => new FakeAnimationAction())
}
class FakeVector3 {
x = 0
y = 0
z = 0
}
class FakeBox3 {
max = { y: 2 }
min = { y: 0 }
setFromObject = vi.fn(() => this)
getCenter = vi.fn(() => new FakeVector3())
}
class FakeClock {
getDelta = vi.fn(() => 0.016)
}
vi.mock('three', () => ({
Scene: FakeScene,
PerspectiveCamera: FakeCamera,
WebGLRenderer: FakeRenderer,
HemisphereLight: vi.fn(),
DirectionalLight: class {
position = { set: vi.fn() }
},
AnimationMixer: FakeAnimationMixer,
Box3: FakeBox3,
Vector3: FakeVector3,
Clock: FakeClock,
Mesh: FakeMesh
}))
const CLIPS = [
{ name: 'talking' },
{ name: 'bow' },
{ name: 'clap' },
{ name: 'fold_arms' },
{ name: 'idle' }
]
let gltfLoadCallback: ((gltf: unknown) => void) | undefined
let gltfErrorCallback: ((error: unknown) => void) | undefined
const loaderLoad = vi.fn(
(
_file: string,
onLoad: (gltf: unknown) => void,
_onProgress: unknown,
onError: (error: unknown) => void
) => {
gltfLoadCallback = onLoad
gltfErrorCallback = onError
}
)
vi.mock('three/examples/jsm/loaders/GLTFLoader.js', () => ({
GLTFLoader: class {
load = loaderLoad
}
}))
const { default: AvatarViewer } = await import('../../../src/renderer/src/components/AvatarViewer')
describe('<AvatarViewer />', () => {
beforeEach(() => {
vi.clearAllMocks()
loadedMesh = undefined
vi.spyOn(window, 'requestAnimationFrame').mockImplementation(() => 1)
vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
})
it('should render a container div', () => {
const { container } = render(<AvatarViewer />)
expect(container.querySelector('div')).toBeInTheDocument()
})
it('should call onReady with a working handle once the model loads', () => {
const onReady = vi.fn()
render(<AvatarViewer onReady={onReady} />)
gltfLoadCallback?.({ scene: new FakeScene(), animations: CLIPS })
expect(onReady).toHaveBeenCalledWith(
expect.objectContaining({ animationNames: ['talking', 'bow', 'clap', 'fold_arms', 'idle'] })
)
})
it('should play an animation by name via the handle given to onReady', () => {
let handle: { playAnimationByName: (name: string) => void } | undefined
render(<AvatarViewer onReady={(h) => (handle = h)} />)
gltfLoadCallback?.({ scene: new FakeScene(), animations: CLIPS })
handle?.playAnimationByName('idle')
// Pas d'assertion sur l'animation interne directement observable depuis l'exterieur, mais
// l'appel ne doit pas lever d'exception et doit couvrir le chemin de recherche par nom.
expect(() => handle?.playAnimationByName('inexistante')).not.toThrow()
})
it('should log an error when the model fails to load', () => {
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
render(<AvatarViewer />)
gltfErrorCallback?.(new Error('404'))
expect(errorSpy).toHaveBeenCalled()
})
it('should dispose geometry, material and renderer resources on unmount', () => {
loadedMesh = new FakeMesh()
render(<AvatarViewer />)
cleanup()
expect(disposeGeometry).toHaveBeenCalled()
expect(disposeMaterial).toHaveBeenCalled()
expect(disposeRenderer).toHaveBeenCalled()
})
})