import test from 'node:test'; import assert from 'node:assert/strict'; import { MediaTextureManager } from '../vr180player/rendering/texture-manager.js'; function createTexture(name) { return { disposed: false, name, needsUpdate: false, dispose() { this.disposed = true; } }; } function createVideo({ paused = false, ended = false } = {}) { return { ended, paused }; } test('MediaTextureManager replaces the previous texture when creating a new one', () => { const created = []; const manager = new MediaTextureManager(createVideo(), () => { const texture = createTexture(`texture-${created.length}`); created.push(texture); return texture; }, () => true); const first = manager.create(); const second = manager.create(); assert.equal(first.disposed, true); assert.equal(second.disposed, false); assert.equal(manager.current, second); assert.equal(created.length, 2); }); test('MediaTextureManager assigns and clears material maps', () => { const material = { map: null, needsUpdate: false }; const manager = new MediaTextureManager(createVideo(), () => createTexture('assigned'), () => true); const texture = manager.assignToMaterial(material); assert.equal(material.map, texture); assert.equal(material.needsUpdate, true); assert.equal(manager.current, texture); material.needsUpdate = false; manager.clearMaterial(material); assert.equal(texture.disposed, true); assert.equal(material.map, null); assert.equal(material.needsUpdate, true); assert.equal(manager.current, null); }); test('MediaTextureManager can switch sources before creating the next texture', () => { const firstSource = { name: 'first' }; const secondSource = { name: 'second' }; const createdFrom = []; const manager = new MediaTextureManager(firstSource, (source) => { createdFrom.push(source); return createTexture(source.name); }, () => true); const firstTexture = manager.create(); manager.setSource(secondSource); const secondTexture = manager.create(); assert.equal(firstTexture.disposed, true); assert.equal(secondTexture.name, 'second'); assert.deepEqual(createdFrom, [firstSource, secondSource]); }); test('MediaTextureManager only marks textures dirty when the update predicate allows it', () => { const video = createVideo(); const manager = new MediaTextureManager( video, () => createTexture('playing'), () => !video.paused && !video.ended ); const texture = manager.create(); manager.updateIfNeeded(); assert.equal(texture.needsUpdate, true); texture.needsUpdate = false; video.paused = true; manager.updateIfNeeded(); assert.equal(texture.needsUpdate, false); video.paused = false; video.ended = true; manager.updateIfNeeded(); assert.equal(texture.needsUpdate, false); });