Files
vibemake/scripts/experience-packages.test.mjs
T

116 lines
6.8 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { packageUrl, assetPath, assetUrl, validatePackage, visibleAssemblyParts, validateEmbeddedGlb, readLimited, fetchPackageJson, MAX_PACKAGE_JSON_BYTES } from '../js/experience-packages.js';
import { expectedController, validateController } from '../sandbox/src/game/package-config.js';
import { projectExperiences } from '../js/project-experiences.js';
const url = 'https://cdn.example.com/projects/another/assembly/v2/manifest.json';
const file = { path: 'parts/body.glb', bytes: 80, sha256: 'a'.repeat(64) };
const fixture = () => ({ schemaVersion: 1, kind: 'assembly', unit: 'meter', files: [file], parts: [
{ id: 'base', file: file.path }, { id: 'left', file: file.path, parentId: 'base', position: [1, 0, 0] },
{ id: 'right', file: file.path, parentId: 'base', position: [-1, 0, 0] },
], steps: [{ id: 's1', label: '底座', parts: ['base'] }, { id: 's2', label: '左侧', parts: ['left'] }, { id: 's3', label: '右侧', parts: ['right'] }] });
test('backend bindings are authoritative on every host; missing resources never use another robot', () => {
const project = { code: 'ppsx17fhhtzq', assemblyManifestUrl: null, walkingManifestUrl: null };
for (const host of ['localhost', '127.0.0.1', '10.10.12.218', 'vibemake.robotquan.com']) {
assert.equal(projectExperiences(project, host), project);
assert.equal(projectExperiences(project, host).walkingManifestUrl, null);
}
});
test('two backend fields independently control availability without a project ID lookup', () => {
assert.equal(packageUrl({ code: 'ppsx17fhhtzq' }, 'assembly', url), null);
assert.equal(packageUrl({ code: 'ppsx17fhhtzq' }, 'walking', url), null);
const project = { code: 'unrelated', assemblyManifestUrl: url, walkingManifestUrl: null };
assert.equal(packageUrl(project, 'assembly'), url);
assert.equal(packageUrl(project, 'walking'), null);
assert.equal(packageUrl({ walkingManifestUrl: '/walk/manifest.json' }, 'walking', url), 'https://cdn.example.com/walk/manifest.json');
for (const value of ['', null, 'javascript:alert(1)', 'data:application/json,{}', 'https://user:pass@example.com/m.json']) {
assert.equal(packageUrl({ assemblyManifestUrl: value }, 'assembly', url), null);
}
});
test('relocation preserves nested asset paths and rejects paths outside the package', () => {
assert.equal(assetUrl('parts/左腿.glb', url), 'https://cdn.example.com/projects/another/assembly/v2/parts/%E5%B7%A6%E8%85%BF.glb');
for (const path of ['../body.glb', '/body.glb', 'https://evil.test/a.glb', 'a/../../b.glb', 'a\\b.glb', 'a/%2e%2e/b.glb', 'a.glb?x', 'a//b.glb']) assert.throws(() => assetPath(path));
});
test('assembly steps add parts cumulatively and preserve instances of the same file', () => {
const pkg = validatePackage(fixture(), 'assembly', url);
assert.equal(pkg.files.size, 1);
assert.equal(pkg.parts.length, 3);
assert.deepEqual([...visibleAssemblyParts(pkg, 0)], ['base']);
assert.deepEqual([...visibleAssemblyParts(pkg, 1)], ['base', 'left']);
assert.deepEqual([...visibleAssemblyParts(pkg, null)], ['base', 'left', 'right']);
assert.deepEqual(pkg.parts[2].position, [-1, 0, 0]);
assert.throws(() => visibleAssemblyParts(pkg, 3));
});
test('GLB assets above 50 MB and packages above 200 MB retain their integrity metadata', () => {
const raw = fixture();
raw.files[0] = { ...file, bytes: 310 * 1024 * 1024 };
const pkg = validatePackage(raw, 'assembly', url);
assert.equal(pkg.files.get(file.path).bytes, 310 * 1024 * 1024);
assert.equal(pkg.files.get(file.path).sha256, file.sha256);
});
test('invalid hierarchy, installation order, transforms and incomplete steps are rejected', () => {
const changes = [
p => { p.parts[0].parentId = 'left'; },
p => { p.parts[1].parentId = 'unknown'; },
p => { p.parts[1].id = 'base'; },
p => { p.parts[1].file = 'missing.glb'; },
p => { p.parts[1].quaternion = [0, 0, 0, 0]; },
p => { p.parts[1].position = [Infinity, 0, 0]; },
p => { p.parts[1].scale = [0, 1, 1]; },
p => { p.steps.reverse(); },
p => { p.steps.pop(); },
p => { p.steps[1].parts.push('base'); },
p => { p.files.push({ ...file }); },
p => { p.schemaVersion = 2; },
];
for (const change of changes) { const p = fixture(); change(p); assert.throws(() => validatePackage(p, 'assembly', url)); }
});
test('GLB rejects external images and malformed headers', () => {
const json = JSON.stringify({ asset: { version: '2.0' }, images: [{ uri: 'https://elsewhere.test/texture.png' }] });
const padding = (4 - json.length % 4) % 4;
const buffer = Buffer.alloc(20 + json.length + padding, 32);
buffer.writeUInt32LE(0x46546c67, 0); buffer.writeUInt32LE(2, 4); buffer.writeUInt32LE(buffer.length, 8);
buffer.writeUInt32LE(json.length + padding, 12); buffer.writeUInt32LE(0x4e4f534a, 16); buffer.write(json, 20);
assert.throws(() => validateEmbeddedGlb(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.length)), /嵌入/);
assert.throws(() => validateEmbeddedGlb(new ArrayBuffer(4)));
});
test('walking controller rejects a different joint order or timing even when dimensions match', () => {
assert.doesNotThrow(() => validateController(expectedController()));
const swapped = expectedController();
[swapped.jointNames[0], swapped.jointNames[1]] = [swapped.jointNames[1], swapped.jointNames[0]];
assert.throws(() => validateController(swapped), /jointNames/);
assert.throws(() => validateController({ ...expectedController(), physicsTimestep: 0.01 }), /physicsTimestep/);
assert.throws(() => validateController({ ...expectedController(), actionSize: 19 }), /actionSize/);
});
test('JSON and asset readers reject oversized responses with or without Content-Length', async () => {
await assert.rejects(readLimited(new Response('12345'), 4), /超过/);
await assert.rejects(readLimited(new Response('12', { headers: { 'Content-Length': '100' } }), 4), /超过/);
assert.equal(new TextDecoder().decode(await readLimited(new Response('1234'), 4)), '1234');
});
test('large seekable assembly manifests load within the shared generator limit', async t => {
const body = JSON.stringify({ title: 'RP1', snapshots: 'x'.repeat(3 * 1024 * 1024) });
t.mock.method(globalThis, 'fetch', async () => new Response(body));
assert.equal((await fetchPackageJson(url)).title, 'RP1');
globalThis.fetch.mock.mockImplementation(async () => new Response('{}', {
headers: { 'Content-Length': String(MAX_PACKAGE_JSON_BYTES + 1) },
}));
await assert.rejects(fetchPackageJson(url), /超过/);
});
test('shipped source GLB remains a self-contained valid assembly asset', () => {
const bytes = readFileSync(new URL('../assets/models/microban-assembly.glb', import.meta.url));
assert.doesNotThrow(() => validateEmbeddedGlb(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.length)));
});