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>
167 lines
4.3 KiB
TypeScript
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()
|
|
})
|
|
})
|