import { mount, flushPromises } from '@vue/test-utils'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { nextTick } from 'vue'; import SceneCanvas from '@/components/SceneCanvas.vue'; import DriveControls from '@/components/DriveControls.vue'; import JourneyHud from '@/components/JourneyHud.vue'; import type { MotionPolicy, QualityTier } from '@/journey/motion-policy'; import type { JourneySnapshot } from '@/journey/types'; const sceneMocks = vi.hoisted(() => ({ build: vi.fn(async () => ({ render: vi.fn(), dispose: vi.fn() })), })); vi.mock('@/scene/world/environment', () => ({ createProceduralSceneGeneration: sceneMocks.build, })); const journey = (overrides: Partial = {}): JourneySnapshot => ({ scrollY: 0, routeProgress: 0.42, direction: 1, acceleration: 0.3, driveSpeed: 0.6, activeSectionId: 'infrastructure', chapter: 'infrastructure', sign: null, arrived: false, ...overrides, }); const policy = (overrides: Partial = {}): MotionPolicy => ({ allowSceneRequest: true, allowAutomaticVideo: true, maximumTier: 'high', reason: 'full', ...overrides, }); describe('SceneCanvas', () => { beforeEach(() => sceneMocks.build.mockClear()); afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); }); it.each([ ['reduced motion', policy({ allowSceneRequest: false, maximumTier: 'static', reason: 'system-reduce' })], ['saved Static mode', policy({ allowSceneRequest: false, maximumTier: 'static', reason: 'saved-static' })], ])('does not request or render the scene for %s', async (_name, blockedPolicy) => { const wrapper = mount(SceneCanvas, { props: { journey: journey(), policy: blockedPolicy, quality: 'static', sceneDescription: 'A decorative night road behind the portfolio content.', }, }); await flushPromises(); expect(sceneMocks.build).not.toHaveBeenCalled(); expect(wrapper.findAll('canvas')).toHaveLength(0); expect(wrapper.get('img').attributes()).toMatchObject({ alt: '', width: '1600', height: '900' }); expect(wrapper.text()).toContain('A decorative night road behind the portfolio content.'); wrapper.unmount(); }); it('owns exactly one decorative canvas when full motion is allowed', async () => { const wrapper = mount(SceneCanvas, { props: { journey: journey(), policy: policy(), quality: 'high', sceneDescription: 'A decorative night road behind the portfolio content.', }, }); await flushPromises(); expect(sceneMocks.build).toHaveBeenCalledTimes(1); expect(wrapper.findAll('canvas')).toHaveLength(1); expect(wrapper.get('canvas').attributes('aria-hidden')).toBe('true'); expect(wrapper.get('.journey-world').attributes('aria-hidden')).toBe('true'); wrapper.unmount(); }); it('forwards each complete scene frame interval to adaptive quality', async () => { const callbacks = new Map(); let nextFrameId = 1; vi.stubGlobal('requestAnimationFrame', vi.fn((callback: FrameRequestCallback) => { const id = nextFrameId++; callbacks.set(id, callback); return id; })); vi.stubGlobal('cancelAnimationFrame', vi.fn((id: number) => callbacks.delete(id))); const sampleFrame = vi.fn(); const wrapper = mount(SceneCanvas, { props: { journey: journey(), policy: policy(), quality: 'high', sceneDescription: 'A decorative night road behind the portfolio content.', sampleFrame, }, }); await flushPromises(); const frame = [...callbacks.entries()][0]!; callbacks.delete(frame[0]); vi.spyOn(performance, 'now').mockReturnValueOnce(2_000).mockReturnValueOnce(2_301); frame[1](2_000); expect(sampleFrame).toHaveBeenCalledWith(2_000, 2_301); wrapper.unmount(); }); it('falls back to the poster without obscuring semantic content after scene failure', async () => { sceneMocks.build.mockRejectedValueOnce(new Error('WebGL unavailable')); const wrapper = mount(SceneCanvas, { props: { journey: journey(), policy: policy(), quality: 'medium', sceneDescription: 'A decorative night road behind the portfolio content.', }, slots: { default: '

Semantic portfolio

' }, }); await flushPromises(); expect(wrapper.get('img').isVisible()).toBe(true); expect(wrapper.findAll('canvas')).toHaveLength(0); expect(wrapper.text()).toContain('Semantic portfolio'); wrapper.unmount(); }); it('aborts and removes the canvas on a Static transition', async () => { const wrapper = mount(SceneCanvas, { props: { journey: journey(), policy: policy(), quality: 'high', sceneDescription: 'A decorative night road behind the portfolio content.', }, }); await flushPromises(); expect(wrapper.findAll('canvas')).toHaveLength(1); await wrapper.setProps({ policy: policy({ allowSceneRequest: false, maximumTier: 'static', reason: 'saved-static' }), quality: 'static', }); await nextTick(); expect(wrapper.findAll('canvas')).toHaveLength(0); expect(wrapper.get('img').isVisible()).toBe(true); wrapper.unmount(); }); it('retains the same canvas and scene generation for a visitor pause and resume', async () => { const wrapper = mount(SceneCanvas, { props: { journey: journey(), policy: policy(), quality: 'high', sceneDescription: 'A decorative night road behind the portfolio content.', }, }); await flushPromises(); const canvas = wrapper.get('canvas').element; const runtime = await sceneMocks.build.mock.results[0]!.value; await wrapper.setProps({ policy: policy({ allowSceneRequest: false, maximumTier: 'static', reason: 'visitor-pause' }), quality: 'high', }); await flushPromises(); expect(wrapper.get('canvas').element).toBe(canvas); expect(runtime.dispose).not.toHaveBeenCalled(); expect(sceneMocks.build).toHaveBeenCalledTimes(1); await wrapper.setProps({ policy: policy() }); await flushPromises(); expect(wrapper.get('canvas').element).toBe(canvas); expect(runtime.dispose).not.toHaveBeenCalled(); expect(sceneMocks.build).toHaveBeenCalledTimes(1); wrapper.unmount(); }); }); describe('DriveControls', () => { const mountControls = (motionPolicy: MotionPolicy, maximum: QualityTier = 'high') => mount(DriveControls, { props: { policy: motionPolicy, maximumQuality: maximum, sceneStatus: 'running' }, }); it('uses exact native control names and a visible non-color status', async () => { const wrapper = mountControls(policy()); expect(wrapper.get('button').text()).toBe('Pause motion'); expect(wrapper.get('select').attributes('aria-label')).toBe('Quality control'); expect(wrapper.text()).toContain('Motion: active'); await wrapper.get('button').trigger('click'); expect(wrapper.emitted('pause')).toHaveLength(1); }); it.each([ ['visitor-pause', 'Resume motion'], ['saved-static', 'Enable motion'], ] as const)('offers the correct recovery action for %s', (reason, label) => { const wrapper = mountControls(policy({ allowSceneRequest: false, maximumTier: 'static', reason })); expect(wrapper.get('button').text()).toBe(label); }); }); describe('JourneyHud', () => { it('renders route percent, current section, quality and arrival from one snapshot', () => { const wrapper = mount(JourneyHud, { props: { journey: journey({ routeProgress: 1, activeSectionId: 'contact', arrived: true }), sectionLabel: 'Contact', quality: 'low', }, }); expect(wrapper.text()).toContain('100%'); expect(wrapper.text()).toContain('Contact'); expect(wrapper.text()).toContain('Low'); expect(wrapper.text()).toContain('ARRIVED'); expect(wrapper.attributes('aria-hidden')).toBe('true'); }); });