180 lines
13 KiB
JavaScript
180 lines
13 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import { resourceIndex, bindAssemblyFiles, loadProjectAssembly, loadProjectWalking, validateRobotResources, hasProjectWalking, assemblySource } from '../js/project-resources.js';
|
|
import { modelAssetUrl, modelAssetTarget } from '../js/model-assets.js';
|
|
const robot = JSON.parse(readFileSync(new URL('../docs/model-examples/microban.robot.json', import.meta.url)));
|
|
const filesFor = (prefix = '') => resourceIndex(['robot.json', robot.model, ...robot.assets.map((a) => a.path), 'policies/walk.onnx'].map((p) => ({ relativePath: prefix + p, sizeBytes: 100, url: 'https://cdn.example/' + prefix + p })));
|
|
|
|
test('duck folder uses exact API file URLs and rejects incomplete inventories before boot', async () => {
|
|
const paths = ['controller.json', 'robot/mjlab/robot_allcollisions.xml', 'robot/mjlab/kinematics.json', 'robot/mjlab/microduck.glb',
|
|
'policies/BEST_alpha_walking.onnx', 'policies/BEST_alpha_sitstand.onnx', 'policies/roulade.onnx', 'policies/ball_kick_left.onnx',
|
|
'policies/ball_kick_right.onnx', 'policies/alpha_ground_pick.onnx', 'policies/BEST_alpha_stand.onnx'];
|
|
const manifest = { schemaVersion: 1, kind: 'walking', runtimeProfile: 'microduck-alpha-v1', robotId: 'microduck-alpha', autoWalk: false,
|
|
controller: 'controller.json', files: paths.map((path) => ({ path, bytes: 100, sha256: 'a'.repeat(64) })) };
|
|
const project = { walkingResourcesFiles: [
|
|
{ relativePath: 'duck/manifest.json', sizeBytes: 123, url: 'https://cdn.example/manifest.json' },
|
|
...manifest.files.map((f, i) => ({ relativePath: 'duck/' + f.path, sizeBytes: f.bytes, url: `https://cdn.example/objects/${i}` })),
|
|
] };
|
|
const original = globalThis.fetch;
|
|
globalThis.fetch = async () => new Response(JSON.stringify(manifest));
|
|
try {
|
|
const result = await loadProjectWalking(project);
|
|
assert.equal(result.format, 'legacy');
|
|
assert.equal(result.resources[0].url, 'https://cdn.example/objects/0');
|
|
project.walkingResourcesFiles.pop();
|
|
await assert.rejects(loadProjectWalking(project), /缺少文件/);
|
|
} finally { globalThis.fetch = original; }
|
|
});
|
|
|
|
test('generic walking accepts one policy and a built-in head overlay without ball policies', () => {
|
|
const packet = validateRobotResources(robot, 'robot.json', filesFor());
|
|
assert.deepEqual(packet.capabilities.map((c) => c.skillId), ['walk', 'head']);
|
|
assert.equal(packet.resources.filter((f) => f.path.endsWith('.onnx')).length, 1);
|
|
assert.ok(!packet.resources.some((f) => /kick|ball/.test(f.path)));
|
|
});
|
|
test('nested Robot Package preserves paths and rejects missing required walking files', () => {
|
|
assert.equal(validateRobotResources(robot, 'robot/robot.json', filesFor('robot/')).entryPath, 'robot/robot.json');
|
|
const files = filesFor(); files.delete('policies/walk.onnx');
|
|
assert.throws(() => validateRobotResources(robot, 'robot.json', files), /walk.onnx/);
|
|
});
|
|
test('optional unsupported or incomplete actions do not block walking or leave stale controls', () => {
|
|
const config = structuredClone(robot);
|
|
config.skills.push({ id: 'missing', kind: 'episodic', controller: { type: 'onnx', policy: 'missing.onnx' } },
|
|
{ id: 'scripted', kind: 'episodic', controller: { type: 'python' } },
|
|
{ id: 'disabled', kind: 'episodic', available: false, controller: { type: 'onnx', policy: 'disabled.onnx' } });
|
|
config.input.bindings.push(...['missing', 'scripted', 'disabled'].map((id) => ({ action: `skill:${id}`, key: 'KeyT' })));
|
|
const packet = validateRobotResources(config, 'robot.json', filesFor());
|
|
assert.deepEqual(packet.config.skills.map((s) => s.id), ['walk', 'head']);
|
|
assert.deepEqual(packet.warnings.map((w) => w.skillId), ['missing', 'scripted']);
|
|
assert.ok(!packet.config.input.bindings.some((b) => /^skill:/.test(b.action)));
|
|
assert.equal(config.skills.length, 5); // Do not mutate the uploaded configuration.
|
|
});
|
|
test('RP1 episodic policies retain nested reference data and exact backend URLs', () => {
|
|
const config = JSON.parse(readFileSync(new URL('../sandbox/test/fixtures/rp1/robot.json', import.meta.url)));
|
|
const paths = new Set(['robot.json', config.model, ...config.assets.map((a) => a.path)]);
|
|
for (const skill of config.skills) {
|
|
paths.add(skill.controller.policy);
|
|
if (skill.reference) {
|
|
paths.add(skill.reference);
|
|
const ref = JSON.parse(readFileSync(new URL('../sandbox/test/fixtures/rp1/' + skill.reference, import.meta.url)));
|
|
paths.add(ref.data);
|
|
}
|
|
}
|
|
const files = resourceIndex([...paths].map((p, i) => ({ relativePath: 'rp1/' + p, sizeBytes: 100, url: `https://cdn.example/objects/${i}` })));
|
|
const packet = validateRobotResources(config, 'rp1/robot.json', files);
|
|
assert.deepEqual(packet.config.skills.map((s) => s.id), ['walk', 'dance', 'wave']);
|
|
assert.equal(packet.warnings.length, 0);
|
|
assert.equal(packet.resources.filter((f) => f.path.endsWith('.f32')).length, 2);
|
|
for (const file of packet.resources) assert.equal(file.url, files.get(file.path).url);
|
|
});
|
|
test('bound resource inventory rejects traversal, active URLs, duplicates and invalid sizes', () => {
|
|
const file = { relativePath: 'parts/a.glb', sizeBytes: 10, url: 'https://cdn.example/a.glb' };
|
|
for (const patch of [{ relativePath: '../a.glb' }, { url: 'javascript:alert(1)' }, { sizeBytes: -1 }, { sizeBytes: Infinity }]) assert.throws(() => resourceIndex([{ ...file, ...patch }]));
|
|
assert.throws(() => resourceIndex([file, file]));
|
|
});
|
|
test('large uploaded assemblies are not blocked by a total-size cap', () => {
|
|
const files = resourceIndex(['body.glb', 'parts.glb'].map(relativePath => ({ relativePath,
|
|
sizeBytes: 210 * 1024 * 1024, url: 'https://cdn.example/' + relativePath })));
|
|
assert.equal(files.size, 2);
|
|
});
|
|
test('assembly loads more than 2000 parts and files through the project API binding', async () => {
|
|
const count = 2100;
|
|
const files = Array.from({ length: count }, (_, index) => ({
|
|
path: `parts/${index}.glb`, bytes: 100, sha256: 'a'.repeat(64),
|
|
}));
|
|
const parts = files.map((file, index) => ({ id: `part-${index}`, file: file.path }));
|
|
const raw = { schemaVersion: 1, kind: 'assembly', unit: 'meter', files, parts,
|
|
steps: parts.map(part => ({ id: `install-${part.id}`, parts: [part.id] })) };
|
|
const project = {
|
|
assemblyJsonUrl: 'https://cdn.example/steps.json',
|
|
modelPartsFiles: files.map(file => ({ relativePath: file.path, sizeBytes: file.bytes,
|
|
url: `https://cdn.example/upload/${file.path}` })),
|
|
};
|
|
const original = globalThis.fetch;
|
|
globalThis.fetch = async () => new Response(JSON.stringify(raw));
|
|
try {
|
|
const assembly = await loadProjectAssembly(project);
|
|
assert.equal(assembly.steps.length, count);
|
|
assert.equal(assembly.files.size, count);
|
|
assert.equal(assembly.files.get(files.at(-1).path).url, project.modelPartsFiles.at(-1).url);
|
|
await assert.rejects(loadProjectAssembly({ ...project, modelPartsFiles: [] }), /清单为空/);
|
|
assert.throws(() => resourceIndex(project.modelPartsFiles), /超过 1000 个/);
|
|
} finally { globalThis.fetch = original; }
|
|
});
|
|
test('assembly resolves exact API URLs across different upload batches and rejects a mismatched part', async () => {
|
|
const raw = { schemaVersion: 1, kind: 'assembly', unit: 'meter', files: [{ path: 'parts/a.glb', bytes: 100, sha256: 'a'.repeat(64) }], parts: [{ id: 'a', file: 'parts/a.glb' }], steps: [{ id: 'one', parts: ['a'] }] };
|
|
const project = { assemblyJsonUrl: 'https://cdn.example/json/batch/steps.json', modelPartsFiles: [{ relativePath: 'parts/a.glb', sizeBytes: 100, url: 'https://cdn.example/parts/other/parts/a.glb' }] };
|
|
const original = globalThis.fetch;
|
|
globalThis.fetch = async () => new Response(JSON.stringify(raw));
|
|
try {
|
|
const result = await loadProjectAssembly(project);
|
|
assert.equal(result.files.get('parts/a.glb').url, project.modelPartsFiles[0].url);
|
|
await assert.rejects(loadProjectAssembly({ ...project, modelPartsFiles: [{ ...project.modelPartsFiles[0], sizeBytes: 101 }] }), /版本与步骤不一致/);
|
|
const stripped = await loadProjectAssembly({ ...project, modelPartsFiles: [{ ...project.modelPartsFiles[0], relativePath: 'a.glb' }] });
|
|
assert.equal(stripped.files.get('parts/a.glb').url, project.modelPartsFiles[0].url);
|
|
assert.equal(stripped.files.get('parts/a.glb').sha256, raw.files[0].sha256);
|
|
assert.equal(stripped.parts[0].file, 'parts/a.glb');
|
|
await assert.rejects(loadProjectAssembly({ ...project, modelPartsFiles: [{ ...project.modelPartsFiles[0], relativePath: 'other.glb' }] }), /目录不匹配/);
|
|
} finally { globalThis.fetch = original; }
|
|
});
|
|
|
|
test('assembly folder roots may be added, removed or renamed without flattening subdirectories', () => {
|
|
const index = (paths) => resourceIndex(paths.map((relativePath) => ({ relativePath, sizeBytes: 100, url: 'https://cdn.example/' + relativePath })));
|
|
const expected = index(['parts/left/a.glb', 'parts/right/a.glb', 'parts/body.glb']);
|
|
for (const root of ['', 'upload/', 'upload/parts/']) {
|
|
const actual = index(['left/a.glb', 'right/a.glb', 'body.glb'].map((p) => root + p));
|
|
const resolved = bindAssemblyFiles(expected, actual);
|
|
assert.equal(resolved.get('parts/left/a.glb'), actual.get(root + 'left/a.glb'));
|
|
assert.equal(resolved.get('parts/right/a.glb'), actual.get(root + 'right/a.glb'));
|
|
}
|
|
assert.throws(() => bindAssemblyFiles(expected, index(['a.glb', 'body.glb'])), /目录不匹配/);
|
|
assert.throws(() => bindAssemblyFiles(expected, index(['left/a.glb', 'elsewhere/right/a.glb', 'body.glb'])), /目录不匹配/);
|
|
assert.throws(() => bindAssemblyFiles(expected, index(['left/A.glb', 'right/a.glb', 'body.glb'])), /目录不匹配/);
|
|
assert.throws(() => bindAssemblyFiles(expected, index(['left/a.glb', 'right/a.glb'])), /目录不匹配/);
|
|
});
|
|
|
|
test('uploaded tool and screw use their own bound URLs and retain declarative docking data', async () => {
|
|
const raw = { schemaVersion: 1, kind: 'assembly', unit: 'meter',
|
|
files: ['螺丝.glb', '扳手.glb'].map(path => ({ path, bytes: 100, sha256: 'a'.repeat(64) })),
|
|
tools: [{ id: 'hex', file: '扳手.glb', tipPosition: [0.01, 0, 0.03] }],
|
|
parts: [{ id: 'screw', file: '螺丝.glb', installation: { type: 'screw', toolId: 'hex', axis: [0, 0, -1],
|
|
socketPosition: [0, 0, -0.001], direction: 'clockwise', turns: 3, travel: 0.0012 } }],
|
|
steps: [{ id: 'one', parts: ['screw'] }] };
|
|
const project = { assemblyJsonUrl: 'https://cdn.example/steps.json', modelPartsFiles: raw.files.map((file, i) => ({
|
|
relativePath: '零件模型/' + file.path, sizeBytes: file.bytes, url: `https://cdn.example/batch-${i}/file.glb`,
|
|
})) };
|
|
const original = globalThis.fetch;
|
|
globalThis.fetch = async () => new Response(JSON.stringify(raw));
|
|
try {
|
|
const pkg = await loadProjectAssembly(project);
|
|
assert.equal(pkg.parts[0].installation.type, 'screw');
|
|
assert.deepEqual(pkg.tools[0].tipPosition, [0.01, 0, 0.03]);
|
|
assert.equal(pkg.files.get('扳手.glb').url, project.modelPartsFiles[1].url);
|
|
assert.equal(pkg.parts.length, 1); // The tool is an asset, never an installed robot part.
|
|
project.modelPartsFiles.pop();
|
|
await assert.rejects(loadProjectAssembly(project), /目录不匹配/);
|
|
} finally { globalThis.fetch = original; }
|
|
});
|
|
|
|
test('assembly root matching rejects ambiguous copies and wrong versions; exact paths take priority', () => {
|
|
const entry = (relativePath, sizeBytes = 100) => ({ relativePath, sizeBytes, url: 'https://cdn.example/' + relativePath });
|
|
const expected = resourceIndex([entry('parts/a.glb'), entry('parts/b.glb')]);
|
|
const copies = [entry('copy1/a.glb'), entry('copy1/b.glb'), entry('copy2/a.glb'), entry('copy2/b.glb')];
|
|
assert.throws(() => bindAssemblyFiles(expected, resourceIndex(copies)), /多套文件/);
|
|
assert.throws(() => bindAssemblyFiles(expected, resourceIndex([entry('a.glb'), entry('b.glb', 101)])), /版本与步骤不一致/);
|
|
const exact = resourceIndex([...copies, entry('parts/a.glb'), entry('parts/b.glb')]);
|
|
assert.equal(bindAssemblyFiles(expected, exact).get('parts/a.glb'), exact.get('parts/a.glb'));
|
|
});
|
|
test('new API bindings independently control entries and project-resources works through the local proxy', () => {
|
|
assert.equal(hasProjectWalking({}), false);
|
|
assert.equal(assemblySource({}), null);
|
|
assert.equal(hasProjectWalking({ walkingResourcesBaseUrl: 'https://cdn.example/walk/', walkingResourcesFiles: [{ relativePath: 'robot.json' }] }), true);
|
|
const url = 'https://linkerbot.oss-cn-beijing.aliyuncs.com/vibemake_dev/project-resources/test/walking-resources/batch/policies/walk.onnx';
|
|
const proxy = modelAssetUrl(url, '192.168.1.9');
|
|
assert.match(proxy, /^\/__model_assets\//);
|
|
assert.equal(modelAssetTarget(proxy).href, url);
|
|
assert.equal(modelAssetTarget(proxy.replace('walk.onnx', 'code.js')), null);
|
|
assert.equal(modelAssetTarget(proxy.replace('walk.onnx', 'dance.f32')).href, url.replace('walk.onnx', 'dance.f32'));
|
|
});
|