|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + getPreparedSplatRuntimeSync: vi.fn(), |
| 5 | + waitForBasePreparedSplatRuntime: vi.fn(), |
| 6 | + waitForTargetPreparedSplatRuntime: vi.fn(), |
| 7 | + loggerWarn: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('../../src/services/logger', () => ({ |
| 11 | + Logger: { |
| 12 | + create: vi.fn(() => ({ |
| 13 | + debug: vi.fn(), |
| 14 | + info: vi.fn(), |
| 15 | + warn: mocks.loggerWarn, |
| 16 | + error: vi.fn(), |
| 17 | + })), |
| 18 | + }, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('../../src/engine/three/splatRuntimeCache', () => ({ |
| 22 | + getPreparedSplatRuntimeSync: mocks.getPreparedSplatRuntimeSync, |
| 23 | + waitForBasePreparedSplatRuntime: mocks.waitForBasePreparedSplatRuntime, |
| 24 | + waitForTargetPreparedSplatRuntime: mocks.waitForTargetPreparedSplatRuntime, |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock('../../src/stores/renderTargetStore', () => ({ |
| 28 | + useRenderTargetStore: { |
| 29 | + getState: () => ({}), |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock('../../src/stores/sliceStore', () => ({ |
| 34 | + useSliceStore: { |
| 35 | + getState: () => ({}), |
| 36 | + }, |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock('../../src/stores/engineStore', () => ({ |
| 40 | + useEngineStore: { |
| 41 | + getState: () => ({}), |
| 42 | + }, |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock('../../src/stores/timeline', () => ({ |
| 46 | + useTimelineStore: { |
| 47 | + getState: () => ({ |
| 48 | + playheadPosition: 0, |
| 49 | + clips: [], |
| 50 | + tracks: [], |
| 51 | + }), |
| 52 | + }, |
| 53 | +})); |
| 54 | + |
| 55 | +vi.mock('../../src/stores/mediaStore', () => ({ |
| 56 | + DEFAULT_SCENE_CAMERA_SETTINGS: { |
| 57 | + fov: 50, |
| 58 | + near: 0.1, |
| 59 | + far: 1000, |
| 60 | + }, |
| 61 | + useMediaStore: { |
| 62 | + getState: () => ({ |
| 63 | + files: [], |
| 64 | + }), |
| 65 | + }, |
| 66 | +})); |
| 67 | + |
| 68 | +import { RenderDispatcher } from '../../src/engine/render/RenderDispatcher'; |
| 69 | + |
| 70 | +function createDispatcher(): RenderDispatcher { |
| 71 | + return new RenderDispatcher({ |
| 72 | + getDevice: () => null, |
| 73 | + isRecovering: () => false, |
| 74 | + sampler: null, |
| 75 | + previewContext: null, |
| 76 | + targetCanvases: new Map(), |
| 77 | + compositorPipeline: null, |
| 78 | + outputPipeline: null, |
| 79 | + slicePipeline: null, |
| 80 | + textureManager: null, |
| 81 | + maskTextureManager: null, |
| 82 | + renderTargetManager: { |
| 83 | + getResolution: () => ({ width: 1920, height: 1080 }), |
| 84 | + }, |
| 85 | + layerCollector: null, |
| 86 | + compositor: null, |
| 87 | + nestedCompRenderer: null, |
| 88 | + cacheManager: {} as any, |
| 89 | + exportCanvasManager: {} as any, |
| 90 | + performanceStats: {} as any, |
| 91 | + renderLoop: null, |
| 92 | + threeSceneRenderer: null, |
| 93 | + } as any); |
| 94 | +} |
| 95 | + |
| 96 | +function createThreeSplatLayer() { |
| 97 | + return [ |
| 98 | + { |
| 99 | + id: 'layer-splat', |
| 100 | + name: 'Hero Splat', |
| 101 | + visible: true, |
| 102 | + opacity: 1, |
| 103 | + is3D: true, |
| 104 | + sourceClipId: 'clip-splat', |
| 105 | + source: { |
| 106 | + type: 'gaussian-splat', |
| 107 | + gaussianSplatUrl: 'blob:hero-splat', |
| 108 | + gaussianSplatFileName: 'hero.ply', |
| 109 | + file: new File([new Uint8Array([1, 2, 3])], 'hero.ply', { |
| 110 | + type: 'application/octet-stream', |
| 111 | + }), |
| 112 | + gaussianSplatSettings: { |
| 113 | + render: { |
| 114 | + useNativeRenderer: false, |
| 115 | + maxSplats: 0, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + ] as any; |
| 121 | +} |
| 122 | + |
| 123 | +describe('RenderDispatcher.ensureExportLayersReady', () => { |
| 124 | + beforeEach(() => { |
| 125 | + vi.clearAllMocks(); |
| 126 | + mocks.getPreparedSplatRuntimeSync.mockReturnValue(null); |
| 127 | + mocks.waitForBasePreparedSplatRuntime.mockResolvedValue({}); |
| 128 | + mocks.waitForTargetPreparedSplatRuntime.mockResolvedValue({}); |
| 129 | + }); |
| 130 | + |
| 131 | + it('falls back to a cached base three.js splat runtime when the full export runtime cannot be allocated', async () => { |
| 132 | + const dispatcher = createDispatcher(); |
| 133 | + vi.spyOn(dispatcher, 'ensureThreeSceneRendererInitialized').mockResolvedValue(true); |
| 134 | + |
| 135 | + mocks.waitForTargetPreparedSplatRuntime.mockRejectedValueOnce(new Error('Array buffer allocation failed')); |
| 136 | + mocks.getPreparedSplatRuntimeSync.mockReturnValueOnce({ |
| 137 | + runtimeKey: 'cached-base-runtime', |
| 138 | + }); |
| 139 | + |
| 140 | + await expect(dispatcher.ensureExportLayersReady(createThreeSplatLayer())).resolves.toBeUndefined(); |
| 141 | + |
| 142 | + expect(mocks.waitForTargetPreparedSplatRuntime).toHaveBeenCalledTimes(1); |
| 143 | + expect(mocks.waitForBasePreparedSplatRuntime).not.toHaveBeenCalled(); |
| 144 | + expect(mocks.loggerWarn).toHaveBeenCalledWith( |
| 145 | + 'Precise export falling back to cached base Three.js splat runtime', |
| 146 | + expect.objectContaining({ |
| 147 | + fileName: 'hero.ply', |
| 148 | + }), |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it('rebuilds the base runtime once and caches export readiness after a recoverable target runtime failure', async () => { |
| 153 | + const dispatcher = createDispatcher(); |
| 154 | + vi.spyOn(dispatcher, 'ensureThreeSceneRendererInitialized').mockResolvedValue(true); |
| 155 | + |
| 156 | + mocks.waitForTargetPreparedSplatRuntime.mockRejectedValueOnce(new Error('Array buffer allocation failed')); |
| 157 | + |
| 158 | + await expect(dispatcher.ensureExportLayersReady(createThreeSplatLayer())).resolves.toBeUndefined(); |
| 159 | + await expect(dispatcher.ensureExportLayersReady(createThreeSplatLayer())).resolves.toBeUndefined(); |
| 160 | + |
| 161 | + expect(mocks.waitForTargetPreparedSplatRuntime).toHaveBeenCalledTimes(1); |
| 162 | + expect(mocks.waitForBasePreparedSplatRuntime).toHaveBeenCalledTimes(1); |
| 163 | + expect(mocks.loggerWarn).toHaveBeenCalledWith( |
| 164 | + 'Precise export falling back to rebuilt base Three.js splat runtime', |
| 165 | + expect.objectContaining({ |
| 166 | + fileName: 'hero.ply', |
| 167 | + }), |
| 168 | + ); |
| 169 | + }); |
| 170 | +}); |
0 commit comments