98 lines
5.5 KiB
JavaScript
98 lines
5.5 KiB
JavaScript
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { createServer } from 'node:http';
|
||
import { readFile } from 'node:fs/promises';
|
||
import { chromium } from 'playwright';
|
||
|
||
const triangle = 'solid test\nfacet normal 0 0 1\nouter loop\nvertex 0 0 0\nvertex 1 0 0\nvertex 0 1 0\nendloop\nendfacet\nendsolid test';
|
||
const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aGasAAAAASUVORK5CYII=', 'base64');
|
||
const root = new URL('../', import.meta.url);
|
||
|
||
test('uploaded URDFs load through the real viewer and URDFLoader', async (t) => {
|
||
const assets = new Map();
|
||
const requests = [];
|
||
const server = createServer(async (req, res) => {
|
||
const path = new URL(req.url, 'http://localhost').pathname;
|
||
if (path === '/') {
|
||
res.writeHead(200, { 'Content-Type': 'text/html' }).end(`<!doctype html><script type="importmap">{"imports":{
|
||
"three":"/vendor/three/build/three.module.min.js",
|
||
"three/addons/":"/vendor/three/examples/jsm/",
|
||
"three/examples/jsm/":"/vendor/three/examples/jsm/",
|
||
"urdf-loader":"/vendor/urdf-loader/src/URDFLoader.js",
|
||
"urdf-loader/src/":"/vendor/urdf-loader/src/"
|
||
}}</script>`);
|
||
return;
|
||
}
|
||
if (path.startsWith('/uploads/')) {
|
||
requests.push(path);
|
||
const asset = assets.get(path);
|
||
if (asset) res.writeHead(200, { 'Content-Type': asset.type }).end(asset.body);
|
||
else res.writeHead(404).end();
|
||
return;
|
||
}
|
||
try {
|
||
const file = new URL(`.${path}`, root);
|
||
if (!file.href.startsWith(root.href)) throw new Error('outside fixture');
|
||
const body = await readFile(file);
|
||
res.writeHead(200, { 'Content-Type': path.endsWith('.js') ? 'text/javascript' : 'application/octet-stream' }).end(body);
|
||
} catch { res.writeHead(404).end(); }
|
||
});
|
||
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
||
const origin = `http://127.0.0.1:${server.address().port}`;
|
||
const browser = await chromium.launch({ headless: true });
|
||
t.after(async () => {
|
||
await browser.close(); server.closeAllConnections();
|
||
await new Promise((resolve) => server.close(resolve));
|
||
});
|
||
const page = await browser.newPage();
|
||
await page.goto(origin);
|
||
const add = (path, body, type = 'application/xml') => assets.set(path, { body, type });
|
||
const load = (main) => page.evaluate(async ({ main, origin }) => {
|
||
const { ensureRobot } = await import('/js/source.js');
|
||
const { RobotViewer } = await import('/js/viewer.js');
|
||
const config = await ensureRobot({ code: 'fixture', name: 'Fixture', urdfMainUrl: origin + main, urdfBaseUrl: origin + '/uploads/batch/' });
|
||
// build() performs the complete production fetch/parse/material pipeline;
|
||
// scene rendering and WebGL are independent of resource path resolution.
|
||
const viewer = Object.create(RobotViewer.prototype);
|
||
viewer.pendingBuilds = new Set(); viewer.opts = {};
|
||
viewer.environmentReady = Promise.resolve();
|
||
const robot = await viewer.build(config);
|
||
let meshes = 0;
|
||
robot.traverse((node) => { if (node.isMesh && node.geometry.attributes.position.count > 0) meshes++; });
|
||
window.fixtureRobot = robot;
|
||
return { meshes, links: Object.keys(robot.links).length };
|
||
}, { main, origin });
|
||
|
||
await t.test('AlexBot layout loads all 11 nested package meshes without a 404', async () => {
|
||
const names = ['base_link', ...Array.from({ length: 5 }, (_, i) => `leftlink${i + 1}`), ...Array.from({ length: 5 }, (_, i) => `rightlink${i + 1}`)];
|
||
const links = names.map((name) => `<link name="${name}"><visual><geometry><mesh filename="package://Alexbot_simple_urdf/meshes/${name}.STL"/></geometry></visual></link>`);
|
||
const joints = names.slice(1).map((name) => `<joint name="${name}_joint" type="fixed"><parent link="base_link"/><child link="${name}"/></joint>`);
|
||
const main = '/uploads/batch/urdf_pkg/Alexbot_simple_urdf/urdf/robot.urdf';
|
||
add(main, `<robot name="AlexBot">${links.join('')}${joints.join('')}</robot>`);
|
||
for (const name of names) add(`/uploads/batch/urdf_pkg/Alexbot_simple_urdf/meshes/${name}.STL`, triangle);
|
||
requests.length = 0;
|
||
assert.deepEqual(await load(main), { meshes: 11, links: 11 });
|
||
assert.equal(requests.length, 12);
|
||
assert.ok(requests.every((path) => assets.has(path)));
|
||
});
|
||
|
||
await t.test('legacy flattened meshes and nested package textures still load', async () => {
|
||
const main = '/uploads/batch/urdf_pkg/Alexbot_simple_urdf/urdf/textured.urdf';
|
||
add(main, `<robot name="mixed"><link name="base"><visual><geometry><mesh filename="package://example-robot-data/robots/legacy_description/meshes/body.STL"/></geometry><material name="body"><texture filename="package://Alexbot_simple_urdf/textures/body.png"/></material></visual></link></robot>`);
|
||
add('/uploads/batch/meshes/body.stl', triangle);
|
||
add('/uploads/batch/urdf_pkg/Alexbot_simple_urdf/textures/body.png', png, 'image/png');
|
||
assert.deepEqual(await load(main), { meshes: 1, links: 1 });
|
||
await page.waitForFunction(() => {
|
||
let loaded = false;
|
||
window.fixtureRobot.traverse((node) => { if (node.isMesh && node.material.map?.image?.naturalWidth > 0) loaded = true; });
|
||
return loaded;
|
||
});
|
||
});
|
||
|
||
await t.test('a missing mesh rejects the model instead of accepting a partial robot', async () => {
|
||
const main = '/uploads/batch/urdf/missing.urdf';
|
||
add(main, '<robot name="missing"><link name="base"><visual><geometry><mesh filename="package://robot/meshes/missing.stl"/></geometry></visual></link></robot>');
|
||
await assert.rejects(load(main), /1 个零件未能加载(missing.stl)/);
|
||
});
|
||
});
|