Files
vibemake/tests/assembly-memory.test.js
T

33 lines
1.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import * as THREE from 'three';
import { disposeAssemblyObjects } from '../js/assembly-memory.js';
import { readLimited } from '../js/experience-packages.js';
const bitmap = (width = 1024, height = 1024) => ({ width, height, closed: 0, close() { this.closed++; } });
const scene = (...textures) => {
const root = new THREE.Group();
for (const normalMap of textures) root.add(new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshStandardMaterial({ color: 0xff6600, metalness: 0.7, roughness: 0.2, normalMap })));
return root;
};
test('scene and template clones release each shared GPU resource and bitmap once', () => {
const image = bitmap(), root = scene(new THREE.Texture(image)), clone = root.clone(true);
const mesh = root.children[0], counts = { geometry: 0, material: 0, texture: 0 };
mesh.geometry.addEventListener('dispose', () => counts.geometry++);
mesh.material.addEventListener('dispose', () => counts.material++);
mesh.material.normalMap.addEventListener('dispose', () => counts.texture++);
disposeAssemblyObjects([root, clone]);
assert.deepEqual(counts, { geometry: 1, material: 1, texture: 1 }); assert.equal(image.closed, 1);
});
test('streaming GLBs preserves bytes for exact, missing, and incorrect length headers', async () => {
for (const length of [null, '5', '2', '8']) {
const response = new Response(new ReadableStream({ start(c) {
c.enqueue(new Uint8Array([1, 2, 3])); c.enqueue(new Uint8Array([4, 5])); c.close();
} }), { headers: length ? { 'Content-Length': length } : {} });
assert.deepEqual(await readLimited(response, 10), new Uint8Array([1, 2, 3, 4, 5]));
}
await assert.rejects(readLimited(new Response(new Uint8Array([1, 2, 3])), 2), /超过/);
});