Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
test: cover the PMREM helper's success and context-loss paths directly (#4630)
[._.]/ Adam Eivy0fb4432c8ac7
diff --git a/client/src/lib/threejsEnvironment.js b/client/src/lib/threejsEnvironment.jsindex c1fa8cf642..40fa3573e8 100644--- a/client/src/lib/threejsEnvironment.js+++ b/client/src/lib/threejsEnvironment.js@@ -148,8 +148,13 @@ export function createSculptEnvironmentTarget(renderer, preset) {const scene = createSculptEnvironmentScene(preset);if (!scene) return null;const pmrem = new THREE.PMREMGenerator(renderer);- const target = pmrem.fromScene(scene, 0.04);- pmrem.dispose();- disposeSculptEnvironmentScene(scene);- return target;+ // `finally`, not `catch`: a lost GL context still throws through to the+ // caller, but the generator's own targets and the source scene are released+ // either way instead of leaking on the failing path.+ try {+ return pmrem.fromScene(scene, 0.04);+ } finally {+ pmrem.dispose();+ disposeSculptEnvironmentScene(scene);+ }}diff --git a/client/src/lib/threejsEnvironment.test.js b/client/src/lib/threejsEnvironment.test.jsindex a6b9cc4b87..bd999779cc 100644--- a/client/src/lib/threejsEnvironment.test.js+++ b/client/src/lib/threejsEnvironment.test.js@@ -1,6 +1,33 @@// @vitest-environment node-import { describe, expect, it, vi } from 'vitest';+import { beforeEach, describe, expect, it, vi } from 'vitest';import * as THREE from 'three';++// PMREM prefiltering needs a live WebGL context, which node has none of — so the+// generator is the one piece stubbed out. Everything the module owns around it+// (which scene it hands over, and releasing the generator and that scene+// afterwards) is exercised for real.+const pmrem = vi.hoisted(() => ({ renderers: [], calls: [], disposals: 0, sceneDisposals: [], fail: false }));+vi.mock('three', async (importOriginal) => {+ const actual = await importOriginal();+ function FakePMREMGenerator(renderer) {+ pmrem.renderers.push(renderer);+ return {+ fromScene(scene, sigma) {+ // Spy here rather than after the call: the module disposes the scene on+ // the way out, so this is the last moment its nodes are reachable.+ scene.traverse((node) => {+ if (node.geometry) pmrem.sceneDisposals.push(vi.spyOn(node.geometry, 'dispose'));+ if (node.material) pmrem.sceneDisposals.push(vi.spyOn(node.material, 'dispose'));+ });+ pmrem.calls.push({ scene, sigma });+ if (pmrem.fail) throw new Error('context lost');+ return { texture: { isTexture: true }, dispose: vi.fn() };+ },+ dispose() { pmrem.disposals += 1; },+ };+ }+ return { ...actual, PMREMGenerator: FakePMREMGenerator };+});import {THREEJS_ENVIRONMENT_PRESETS as SERVER_PRESETS,DEFAULT_THREEJS_ENVIRONMENT as SERVER_DEFAULT,@@ -109,8 +136,46 @@ describe('createSculptEnvironmentScene', () => {});describe('createSculptEnvironmentTarget', () => {+ const renderer = { isWebGLRenderer: true };++ beforeEach(() => {+ pmrem.renderers.length = 0;+ pmrem.calls.length = 0;+ pmrem.sceneDisposals.length = 0;+ pmrem.disposals = 0;+ pmrem.fail = false;+ });+it('returns null without building anything for none or a missing renderer', () => {expect(createSculptEnvironmentTarget(null, 'studio')).toBeNull();- expect(createSculptEnvironmentTarget({}, 'none')).toBeNull();+ expect(createSculptEnvironmentTarget(renderer, 'none')).toBeNull();+ expect(pmrem.calls).toHaveLength(0);+ });++ // The TARGET, not its texture: the framebuffer behind it is only freed by+ // WebGLRenderTarget.dispose(), so handing back the texture alone would leak it+ // on every preset swap.+ it('prefilters the preset scene and hands back the whole render target', () => {+ const target = createSculptEnvironmentTarget(renderer, 'studio');++ expect(pmrem.renderers).toEqual([renderer]);+ expect(pmrem.calls).toHaveLength(1);+ expect(pmrem.calls[0].scene).toBeInstanceOf(THREE.Scene);+ expect(target.texture.isTexture).toBe(true);+ });++ it('releases the generator and the source scene, on success and on failure alike', () => {+ createSculptEnvironmentTarget(renderer, 'studio');+ expect(pmrem.disposals).toBe(1);+ expect(pmrem.sceneDisposals.length).toBeGreaterThan(0);+ for (const spy of pmrem.sceneDisposals) expect(spy).toHaveBeenCalled();++ // A lost GL context still throws through to the caller — but not before the+ // generator and the scene it was handed are released.+ pmrem.fail = true;+ pmrem.sceneDisposals.length = 0;+ expect(() => createSculptEnvironmentTarget(renderer, 'neutral')).toThrow('context lost');+ expect(pmrem.disposals).toBe(2);+ for (const spy of pmrem.sceneDisposals) expect(spy).toHaveBeenCalled();});});