From 27eb30e42805d7b1f14f517ae3be20f9d5f75ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BA=B7?= Date: Thu, 12 Nov 2026 11:05:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Durdf=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=92=8C=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- css/cad.css | 3 + docs/api-gaps.md | 2 +- docs/urdf-resource-audit-2026-11-12.md | 55 +++ js/cad/api.js | 6 + js/cad/app.js | 85 ++++- js/cad/engine.js | 66 +++- js/cad/i18n.js | 1 + js/source.js | 12 +- js/urdf-paths.js | 72 +++- tests/cad/api.test.js | 20 ++ tests/cad/engine-updates.browser.mjs | 91 +++++ tests/cad/platform-agent-browser.mjs | 444 +++++++++++++++++++++++++ tests/urdf-loading.browser.mjs | 97 ++++++ tests/urdf-paths.test.js | 63 +++- 14 files changed, 980 insertions(+), 37 deletions(-) create mode 100644 docs/urdf-resource-audit-2026-11-12.md create mode 100644 tests/urdf-loading.browser.mjs diff --git a/css/cad.css b/css/cad.css index 2a5b8e6..86e24a7 100644 --- a/css/cad.css +++ b/css/cad.css @@ -9,6 +9,9 @@ @media(max-width:820px){.cad-nav{padding:0 16px;gap:12px}.cad-nav-links{display:none}.cad-main{padding:28px 20px}.cad-heading{align-items:flex-start}.cad-editor .cad-project-name,.cad-editor .cad-save-state{display:none}.cad-state{padding:24px}.cad-editor .cad-nav{flex-wrap:nowrap}} @media(prefers-reduced-motion:reduce){.cad-page *{scroll-behavior:auto!important;transition:none!important;animation-duration:.01ms!important}} .cad-page .sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0} +.cad-loading{z-index:90;background:var(--cad-bg)} +.cad-loading .cad-loading-status{font-family:var(--font);letter-spacing:normal;color:var(--cad-muted)} +.cad-loading-cancel{position:absolute;top:max(20px,env(safe-area-inset-top));right:max(24px,env(safe-area-inset-right));min-height:44px!important;padding:0 16px} .cad-page .cad-card{border-radius:8px} .cad-card-art img{display:block;width:100%;height:100%;min-height:0;object-fit:contain} .cad-editor .cad-save-state[data-state=coverFailed],.cad-editor .cad-save-state[data-state=coverUnavailable]{color:#ffb870} diff --git a/docs/api-gaps.md b/docs/api-gaps.md index fa33058..8f2adfd 100644 --- a/docs/api-gaps.md +++ b/docs/api-gaps.md @@ -37,7 +37,7 @@ ## 三、3D 模型 已改为后端字段 `urdfMainUrl`:有值就在项目页、工作台显示 3D,没有就不显示(不再读取仓库里的 robot.json)。 -网格按 URDF 里的相对路径从 `urdfBaseUrl` 读取;`package://包名/…` 按资源包根目录处理。 +相对网格路径按 `urdfMainUrl` 所在目录解析;`package://包名/…` 优先匹配主文件所在的同名 ROS 包目录,同时兼容 `urdfBaseUrl` 下的旧版扁平上传。管理端上传前按文件清单改写 URDF 的网格和贴图引用,避免嵌套包目录丢失。2026-11-12 的公开资源排查见 [URDF 资源检查](urdf-resource-audit-2026-11-12.md)。 ## 四、AI 助手 diff --git a/docs/urdf-resource-audit-2026-11-12.md b/docs/urdf-resource-audit-2026-11-12.md new file mode 100644 index 0000000..62f1ace --- /dev/null +++ b/docs/urdf-resource-audit-2026-11-12.md @@ -0,0 +1,55 @@ +# URDF 上传资源检查(2026-11-12) + +检查正式环境公开社区接口列出的全部 86 个项目,其中 79 个已绑定 URDF、7 个未绑定。79 个主文件均可读取并解析。按项目对 URDF 的 mesh/texture filename 去重,共检查 1,972 条网格引用、3 条贴图引用,包含 visual 和 collision。 + +资源检查使用 HTTP HEAD,按前台实际顺序尝试候选地址,只在 404/410 后换路径。检查了修复前后的全部 1,975 条引用;修复后的成功项是 1,973 条,没有将原有成功引用改坏。此检查不覆盖 DAE/GLTF/OBJ 文件内部的二级材质、贴图和二进制引用,也不涵盖未发布项目。 + +| 项目 | 修复前失败 | 修复后失败 | 原因及处理 | +| --- | ---: | ---: | --- | +| AlexBot(d1qa612t7pc1) | 11 | 0 | `package://Alexbot_simple_urdf/` 被映射到批次根目录,丢失 `urdf_pkg/Alexbot_simple_urdf/`;前台现在保留包名并根据主文件位置匹配包根目录 | +| Poppy Humanoid(7hwzszajfvp0) | 10 | 0 | 10 个 collision 网格引用 `.STL`,实际对象使用 `.stl`;前台增加扩展名兼容,管理端按真实清单修正大小写。这些引用不影响当前仅加载 visual 的前台展示 | +| OpenPodcar(f8nbcn5ek18r) | 1 | 1 | `../../kinect/meshes/kinect.dae` 未找到可访问的上传资源;需补齐 Kinect 网格或修正源 URDF 引用 | +| Talos(m2vpg4t8nn5y) | 1 | 1 | `package://pal_urdf_utils/meshes/camera/orbbec.STL` 未找到可访问的上传资源;需补齐外部 ROS 包中的相机网格或修正源 URDF 引用 | +| 其余 75 个已绑定 URDF 的项目 | 0 | 0 | 本次直接引用检查通过 | +| 合计 | 23 | 2 | 修复 21 条引用的路径或扩展名兼容问题 | + +## AlexBot 的验证 + +URDF: + +```text +https://linkerbot.oss-cn-beijing.aliyuncs.com/vibemake/urdf/d1qa612t7pc1/61043f35d64f4c4e96ce5f209bb1ebf6/urdf_pkg/Alexbot_simple_urdf/urdf/Alexbot_simple_urdf.urdf +``` + +正确网格位置(以 base_link 为例): + +```text +https://linkerbot.oss-cn-beijing.aliyuncs.com/vibemake/urdf/d1qa612t7pc1/61043f35d64f4c4e96ce5f209bb1ebf6/urdf_pkg/Alexbot_simple_urdf/meshes/base_link.STL +``` + +另外使用浏览器、本地资源代理、正式 OSS 原始文件和实际 `RobotViewer.build()` / URDFLoader 完成读取及解码:12 次资源请求全部 200,得到 11 个 link、10 个 joint、11 个网格、149,104 个三角面,无浏览器运行错误。此验证确认文件读取、解析与几何体构建,不包含 WebGL 画面检查。 + +## 管理端上传修复 + +`vibemake-admin` 的项目上传和迁移上传都在申请签名前,按所选文件清单解析 package、相对路径、Windows 导出路径和唯一的大小写匹配,将 URDF 的 mesh/texture 引用改为相对主文件的真实路径。ROS 包名用于区分不同包中的同名资源。网格、材质和贴图文件本身及上传目录结构保持原样。 + +签名清单、OSS 上传文件与确认清单使用同一份改写结果及字节数。无效 URDF、缺失资源或无法唯一匹配的引用会在签名前报错,避免继续绑定不完整的资源包。 + +修改的是本地代码和构建产物,未改写线上 OSS 对象或项目绑定;前台发布后可兼容 AlexBot 已上传的这批文件,无需重新上传。 + +## 验证命令 + +在 `vibemake` 中: + +```sh +node --test tests/urdf-paths.test.js tests/resource-loading.test.js scripts/model-assets.test.mjs +node --test tests/urdf-loading.browser.mjs +node scripts/build.mjs +``` + +在 `vibemake-admin` 中: + +```sh +npx playwright test tests/urdf.spec.ts +npm run build:prod +``` diff --git a/js/cad/api.js b/js/cad/api.js index 56a3e22..e53aa58 100644 --- a/js/cad/api.js +++ b/js/cad/api.js @@ -137,6 +137,12 @@ export class CadApi { return capabilities; } + async aiKnowledge(body, signal) { + const result = await this.call(AI + '/knowledge', { method: 'POST', body, signal }); + if (!result || typeof result.status !== 'string' || !Array.isArray(result.documents)) throw cadError('BAD_RESPONSE'); + return result; + } + async aiChat(body, signal, onStart) { this.assertIdentity(); if (!body || typeof body !== 'object' || Array.isArray(body)) throw cadError('BAD_REQUEST'); diff --git a/js/cad/app.js b/js/cad/app.js index 1d7a451..8a634b8 100644 --- a/js/cad/app.js +++ b/js/cad/app.js @@ -162,8 +162,48 @@ function message(key, hint, action, actionLabel = 'retry') { main.querySelector('[data-action]')?.addEventListener('click', action); } function stage(key, progress) { - message(key, null, openingProject ? cancelOpening : null, 'cancel'); - const p = main.querySelector('h1'); if (progress) p.textContent += ` ${progress}`; + if (!editor) { + message(key, null, openingProject ? cancelOpening : null, 'cancel'); + const p = main.querySelector('h1'); if (progress) p.textContent += ` ${progress}`; + return; + } + main.hidden = false; + let loading = main.querySelector('.cad-loading'); + if (!loading) { + main.innerHTML = `
+
+ + +
+
+ +
`; + loading = main.querySelector('.cad-loading'); + loading.querySelector('[data-action]').addEventListener('click', cancelOpening); + } + loading.dataset.stage = key; + const meter = loading.querySelector('[role="progressbar"]'); + const label = loading.querySelector('#cad-loading-status'); + const detail = typeof progress === 'string' ? progress : ''; + const parts = detail.match(/^(\d+)\s*\/\s*(\d+)$/); + let fraction = progress?.total > 0 ? progress.loaded / progress.total + : /^\d+%$/.test(detail) ? Number.parseInt(detail, 10) / 100 + : parts && Number(parts[2]) > 0 ? Number(parts[1]) / Number(parts[2]) : null; + if (progress?.phase === 'initialize') fraction = null; + if (progress?.phase === 'ready') fraction = 1; + const caption = t(key) + (detail && !detail.endsWith('%') ? ` ${detail}` : ''); + if (label.textContent !== caption) label.textContent = caption; + const pct = loading.querySelector('.splash-pct'); + if (Number.isFinite(fraction)) { + const percent = Math.floor(Math.max(0, Math.min(1, fraction)) * 100); + loading.style.setProperty('--splash-unrevealed', `${100 - percent}%`); + meter.setAttribute('aria-valuenow', String(percent)); + pct.textContent = String(percent).padStart(3, '0') + '%'; + } else { + meter.removeAttribute('aria-valuenow'); + pct.textContent = ''; + } + loading.querySelector('[data-action]').hidden = !openingProject; } async function cancelOpening() { ++epoch; request?.abort(); @@ -193,7 +233,10 @@ async function bindAssistant() { const ctx = context(); assistantRequest?.abort(); assistantRequest = new AbortController(); const capabilityRequest = assistantRequest; - if (window.__VIBEMAKE_CAD__) window.__VIBEMAKE_CAD__.agentChat = null; + if (window.__VIBEMAKE_CAD__) { + window.__VIBEMAKE_CAD__.agentChat = null; + window.__VIBEMAKE_CAD__.agentKnowledge = null; + } let capabilities = null; try { capabilities = await api.aiCapabilities(capabilityRequest.signal); @@ -209,21 +252,40 @@ async function bindAssistant() { const client = api, engine = bridge, documentContext = context(); const host = window.__VIBEMAKE_CAD__; if (host) { + const requestRevisions = new WeakMap(); host.agentChat = capabilities?.enabled ? async (requestBody, signal, onStart) => { assertContext(documentContext, true); - const current = await engine.getState(); - const documentRevision = await agentRevision(current.revision); + let documentRevision = requestRevisions.get(requestBody); + if (documentRevision === undefined) { + const current = await engine.getState(); + documentRevision = await agentRevision(current.revision); + } assertContext(documentContext, true); + requestRevisions.set(requestBody, documentRevision); const request = { ...requestBody, cadProjectCode: project.id, - context: { ...requestBody.context, documentRevision }, + context: { ...requestBody.context, documentRevision, engineBuild: host.buildId }, }; return client.aiChat(request, signal, () => { assertContext(documentContext, true); onStart?.(); }); } : null; + host.agentKnowledge = capabilities?.enabled && capabilities.knowledgeProtocol === 'cad-knowledge-v1' + && capabilities.knowledgeMode !== 'off' ? async (argumentsBody, signal) => { + assertContext(documentContext, true); + if (signal?.aborted) throw signal.reason; + const result = await client.aiKnowledge({ + cadProjectCode: project.id, + documentSessionCode: session, + engineBuild: host.buildId, + arguments: argumentsBody, + }, signal); + assertContext(documentContext, true); + if (signal?.aborted) throw signal.reason; + return result; + } : null; } await bridge.setAgentContext({ userId: boundUser, @@ -238,7 +300,10 @@ async function bindAssistant() { async function clearAssistant() { assistantRequest?.abort(); assistantRequest = null; if (typeof bridge?.setAgentContext === 'function') await bridge.setAgentContext(null).catch(() => {}); - if (window.__VIBEMAKE_CAD__) window.__VIBEMAKE_CAD__.agentChat = null; + if (window.__VIBEMAKE_CAD__) { + window.__VIBEMAKE_CAD__.agentChat = null; + window.__VIBEMAKE_CAD__.agentKnowledge = null; + } } async function validateIdentity() { if (!auth.token) return false; @@ -340,7 +405,11 @@ async function openEditor(current) { stage('metadata'); project = await client.get(projectId, control.signal); check(); stage('engine'); viewport.hidden = false; viewport.style.visibility = 'hidden'; - try { bridge = await loadEngine(); } catch (error) { if (error.code === 'FONT_UNAVAILABLE') { bridge = window.vibemakeCad; showFontError(error); } throw error; } + try { + bridge = await loadEngine({ signal: control.signal, onProgress: (progress) => { + if (current === epoch && !control.signal.aborted) stage(progress.phase === 'download' ? 'engine' : 'engineStarting', progress); + } }); + } catch (error) { if (error.code === 'FONT_UNAVAILABLE') { bridge = window.vibemakeCad; showFontError(error); } throw error; } check(); const engineReady = await bridge.ready(); check(); assistantAvailable = engineReady.capabilities?.assistant === true && typeof bridge.setAgentContext === 'function'; diff --git a/js/cad/engine.js b/js/cad/engine.js index c2d29f6..0b69065 100644 --- a/js/cad/engine.js +++ b/js/cad/engine.js @@ -1,24 +1,74 @@ import { CAD } from './config.js'; import { cadError } from './api.js'; import { locale } from './i18n.js'; -let engine, workerRegistration; -export function loadEngine() { - return engine ??= start().catch((error) => { engine = null; throw error; }); +let engine, workerRegistration, initializedBuild; +let engineProgress = { phase: 'download', loaded: 0, total: null }; +const progressListeners = new Set(); +function reportProgress(phase, loaded, total) { + engineProgress = { phase, loaded, total }; + for (const listener of progressListeners) listener(engineProgress); +} +export function loadEngine({ onProgress, signal } = {}) { + if (signal?.aborted) return Promise.reject(signal.reason); + if (!engine) engineProgress = { phase: 'download', loaded: 0, total: null }; + if (onProgress) { progressListeners.add(onProgress); onProgress(engineProgress); } + // Loading is shared across retries. Cancelling an opening detaches its observer, + // while the engine can finish safely for the next opening. + const pending = engine ??= start().catch((error) => { engine = null; throw error; }); + return new Promise((resolve, reject) => { + const cleanup = () => { progressListeners.delete(onProgress); signal?.removeEventListener('abort', abort); }; + const abort = () => { cleanup(); reject(signal.reason); }; + signal?.addEventListener('abort', abort, { once: true }); + pending.then((value) => { cleanup(); resolve(value); }, (error) => { cleanup(); reject(error); }); + }); } async function start() { - const response = await fetch(CAD.engineManifest, { cache: 'no-store' }); - if (!response.ok) throw cadError('ENGINE_MISSING'); - const manifest = await response.json(); + const manifestResponse = await fetch(CAD.engineManifest, { cache: 'no-store' }); + if (!manifestResponse.ok) throw cadError('ENGINE_MISSING'); + const manifest = await manifestResponse.json(); if (manifest.bridgeVersion !== CAD.bridgeVersion || !/^[a-zA-Z0-9_-]+$/.test(manifest.buildId)) throw cadError('FORMAT_UNSUPPORTED'); const prefix = `/cad/_assets/${manifest.buildId}/`; for (const key of ['js', 'wasm', 'chineseFont']) if (!manifest[key]?.startsWith(prefix) || /[?#]|\.\./.test(manifest[key])) throw cadError('BAD_RESPONSE'); + const sizeOf = (path) => { + const size = Array.isArray(manifest.files) ? manifest.files.find((file) => file.path === path)?.size : null; + return Number.isSafeInteger(size) && size > 0 ? size : null; + }; + const jsSize = sizeOf(manifest.js), wasmSize = sizeOf(manifest.wasm); + const total = jsSize && wasmSize ? jsSize + wasmSize : null; + let loaded = 0; // Set before WASM starts. Standalone engine builds keep their original behaviour. window.__VIBEMAKE_CAD__ = { bridgeVersion: 1, buildId: manifest.buildId, locale: locale(), assetBase: prefix, chineseFont: manifest.chineseFont }; - const { default: init } = await import(manifest.js); - await init({ module_or_path: manifest.wasm }); + if (initializedBuild !== manifest.buildId) { + reportProgress('download', loaded, total); + const [{ default: init }, response] = await Promise.all([ + import(manifest.js).then((module) => { loaded += jsSize || 0; reportProgress('download', loaded, total); return module; }), + fetch(manifest.wasm).then((response) => { if (!response.ok) throw cadError('ENGINE_MISSING'); return response; }), + ]); + let wasmResponse = response; + if (response.body) { + const headers = new Headers(response.headers); + // Fetch chunks are decoded bytes, like manifest sizes, even on gzip/Brotli hosts. + headers.delete('content-encoding'); headers.delete('content-length'); + const body = response.body.pipeThrough(new TransformStream({ + transform(chunk, controller) { + loaded += chunk.byteLength; + reportProgress('download', loaded, total); + controller.enqueue(chunk); + }, + flush() { reportProgress('initialize', loaded, total); }, + })); + // Keep streaming compilation and the module's original import.meta.url. + wasmResponse = new Response(body, { headers, status: response.status, statusText: response.statusText }); + } + await init({ module_or_path: wasmResponse }); + // Font failures can be retried without downloading an initialized WASM again. + initializedBuild = manifest.buildId; + } + reportProgress('initialize', loaded, total); const deadline = performance.now() + 90000; while (!window.vibemakeCad) { if (performance.now() > deadline) throw cadError('ENGINE_TIMEOUT'); await new Promise((r) => setTimeout(r, 30)); } await window.vibemakeCad.ready(); + reportProgress('ready', loaded, total); if ('serviceWorker' in navigator) { workerRegistration = navigator.serviceWorker.register('/cad/worker.js', { scope: '/cad/', updateViaCache: 'none' }).then((registration) => { let notifiedBuild; diff --git a/js/cad/i18n.js b/js/cad/i18n.js index f3c4dc9..6ad7fd2 100644 --- a/js/cad/i18n.js +++ b/js/cad/i18n.js @@ -17,6 +17,7 @@ const messages = { logout: ['退出登录', 'Sign out'], home: ['返回首页', 'Home'], list: ['返回项目列表', 'Projects'], previous: ['上一页', 'Previous'], next: ['下一页', 'Next'], loading: ['加载中…', 'Loading…'], identity: ['验证账号…', 'Verifying account…'], metadata: ['读取项目…', 'Reading project…'], engine: ['加载编辑器…', 'Loading editor…'], download: ['下载工程…', 'Downloading design…'], rebuild: ['重建模型…', 'Rebuilding model…'], + engineStarting: ['正在启动编辑器…', 'Starting editor…'], file: ['文件', 'File'], preferences: ['首选项', 'Preferences'], help: ['帮助', 'Help'], undo: ['撤销', 'Undo'], redo: ['重做', 'Redo'], save: ['保存', 'Save'], never: ['尚未保存到云端', 'Not saved to cloud'], saved: ['已保存到云端', 'Saved to cloud'], dirty: ['有未保存更改', 'Unsaved changes'], preparing: ['正在准备…', 'Preparing…'], uploading: ['正在上传…', 'Uploading…'], verifying: ['正在确认…', 'Confirming…'], diff --git a/js/source.js b/js/source.js index 0cb58d6..c204f94 100644 --- a/js/source.js +++ b/js/source.js @@ -4,7 +4,7 @@ // (data/community.json + robots/),保留最早的完整样子作参考。 import * as api from './api.js'; import { normalizeRobot } from './registry.js'; -import { meshUrlCandidates } from './urdf-paths.js'; +import { urdfResourceUrlCandidates } from './urdf-paths.js'; import { modelUrlResolver } from './model-assets.js'; export const STATIC_HOSTS = ['127.0.0.1']; @@ -92,7 +92,7 @@ export function prefetchRoot(code) { const rootTree = (p) => prefetchRoot(p.code); // ---------- 3D 模型:项目的 urdfMainUrl 字段(URDF 主文件的公开地址),没有就不显示 3D ---------- -// 网格按 URDF 里的相对路径从同一个资源目录(urdfBaseUrl)读取;package://包名/xxx 按资源包根目录处理 +// 相对路径按主文件目录解析;package:// 同时兼容嵌套 ROS 包和旧版扁平上传目录。 export function ensureRobot(p) { if (p.robot !== undefined && p.robot !== null) return Promise.resolve(p.robot); p.robot = p.urdfMainUrl ? robotFromUrdf(p) : null; @@ -101,14 +101,16 @@ export function ensureRobot(p) { function robotFromUrdf(p) { const base = (p.urdfBaseUrl || p.urdfMainUrl.replace(/[^/]*$/, '')).replace(/\/+$/, ''); + const candidates = (url) => urdfResourceUrlCandidates(base, url, p.urdfMainUrl); + const resolveUrl = modelUrlResolver(base); return normalizeRobot({ id: p.code, name: p.name, description: p.description, urdf: p.urdfMainUrl, - resolveUrl: modelUrlResolver(base), - resolveMeshUrls: (url) => meshUrlCandidates(base, url), - packages: () => base, + resolveUrl: (url) => resolveUrl(candidates(url)[0]), + resolveMeshUrls: candidates, + packages: (name) => `package://${name}`, archetype: 'generic', // 没有关节映射:演示时所有关节在限位内摆动 source: { project: p.code }, }); diff --git a/js/urdf-paths.js b/js/urdf-paths.js index 1907a76..80ca557 100644 --- a/js/urdf-paths.js +++ b/js/urdf-paths.js @@ -1,11 +1,11 @@ const dirname = (path) => path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : ''; -function normalize(path) { +function normalize(path, clamp = false) { const out = []; for (const seg of path.split('/')) { if (!seg || seg === '.') continue; if (seg === '..') { - if (!out.length) return null; + if (!out.length) { if (clamp) continue; return null; } out.pop(); } else out.push(seg); } @@ -20,12 +20,13 @@ function relative(fromDir, to) { } function localPath(filename, mainPath) { - const value = String(filename).trim(); - const pkg = value.match(/^package:\/\/[^/]+\/(.*)$/i); - if (pkg) return normalize(pkg[1]); + const value = String(filename).trim().replace(/\\/g, '/'); + const pkg = value.match(/^package:\/\/[^/]*\/(.*)$/i); + if (pkg) return normalize(pkg[1], true); if (/^https?:\/\//i.test(value) || /^(data|blob):/i.test(value)) return null; - if (/^file:\/\//i.test(value)) return normalize(value.replace(/^file:\/+/i, '')); - return normalize(value.startsWith('/') ? value.slice(1) : `${dirname(mainPath)}/${value}`); + if (/^file:\/\//i.test(value)) return normalize(value.replace(/^file:\/+/i, ''), true); + if (/^[a-z]:\//i.test(value)) return normalize(value.slice(3), true); + return normalize(value.startsWith('/') ? value.slice(1) : `${dirname(mainPath)}/${value}`, true); } export function meshUrlCandidates(base, url) { @@ -33,16 +34,49 @@ export function meshUrlCandidates(base, url) { const root = String(base).replace(/\/+$/, ''); const prefix = `${root}/`; if (!value.startsWith(prefix)) return [value]; - const path = value.slice(prefix.length).split(/[?#]/, 1)[0]; + const rawPath = value.slice(prefix.length); + const suffix = rawPath.match(/[?#].*$/)?.[0] || ''; + // Keep legacy references inside their upload batch, including ../ paths and + // Windows exporter paths, before trying shorter uploaded directory prefixes. + const path = normalize(rawPath.split(/[?#]/, 1)[0].replace(/\\/g, '/'), true); + if (!path) return [value]; + const canonical = `${root}/${path}${suffix}`; const parts = path.split('/'); - if (parts.some((part) => !part || part === '.' || part === '..')) return [value]; const resourceIndex = parts.findIndex((part, index) => index > 1 && (part === 'meshes' || part === 'qb_meshes')); const rootResource = parts[0] === 'robots' && resourceIndex > 1 - ? `${root}/${parts.slice(resourceIndex).join('/')}` + ? `${root}/${parts.slice(resourceIndex).join('/')}${suffix}` : null; // Older uploads can retain source directories that are absent from the OSS batch. - const fallbacks = parts.slice(1, 9).map((_, i) => `${root}/${parts.slice(i + 1).join('/')}`); - return [...new Set([rootResource, value, ...fallbacks].filter(Boolean))]; + const fallbacks = parts.slice(1, 9).map((_, i) => `${root}/${parts.slice(i + 1).join('/')}${suffix}`); + return [...new Set([rootResource, canonical, ...fallbacks].filter(Boolean))]; +} + +// URDFLoader keeps the package URI intact until this step so different ROS +// packages can resolve to different directories within the same upload batch. +export function urdfResourceUrlCandidates(base, url, mainUrl) { + const root = String(base).replace(/\/+$/, ''); + const value = String(url); + const pkg = value.match(/^package:\/\/([^/]*)(?:\/(.*))?$/i); + let candidates; + if (pkg) { + const [, name, path = ''] = pkg; + const roots = [root]; + const main = String(mainUrl || '').split(/[?#]/, 1)[0]; + const dirs = main.startsWith(`${root}/`) ? main.slice(root.length + 1).split('/').slice(0, -1) : []; + const index = dirs.lastIndexOf(name); + if (name && index >= 0) roots.unshift(`${root}/${dirs.slice(0, index + 1).join('/')}`); + if (name && name !== '.' && name !== '..') roots.push(`${root}/${name}`); + const paths = [...new Set(roots)].map((dir) => `${dir}/${path}`); + const first = meshUrlCandidates(root, paths[0]); + candidates = [first[0], ...paths.slice(1), ...first.slice(1), ...paths.slice(1).flatMap((p) => meshUrlCandidates(root, p))]; + } else { + const file = value.match(/(?:^|\/)file:\/+(.*)$/i); + candidates = meshUrlCandidates(root, file ? `${root}/${file[1]}` : value); + } + candidates = [...new Set(candidates)]; + // Some older exports use .STL while the uploaded object ends in .stl. + const lowerExtensions = candidates.map((p) => p.replace(/\.[A-Z\d]+(?=[?#]|$)/, (ext) => ext.toLowerCase())); + return [...new Set([...candidates, ...lowerExtensions])]; } // Resolve references against the uploaded file manifest. A unique suffix match @@ -51,8 +85,11 @@ export function resolveUrdfResourcePath(filename, mainPath, availablePaths) { const target = localPath(filename, mainPath); if (!target) return null; - const available = [...new Set(availablePaths.map(normalize).filter(Boolean))]; - let match = available.includes(target) ? target : null; + const available = [...new Set(availablePaths.map((path) => normalize(path)).filter(Boolean))]; + const pkg = String(filename).trim().replace(/\\/g, '/').match(/^package:\/\/([^/]+)\/(.*)$/i); + const qualified = pkg && pkg[1] !== '..' ? normalize(`${pkg[1]}/${pkg[2]}`) : null; + const packageMatches = qualified ? available.filter((path) => path === qualified || path.endsWith(`/${qualified}`)) : []; + let match = packageMatches.length === 1 ? packageMatches[0] : available.includes(target) ? target : null; if (!match) { const targetParts = target.split('/'); let bestScore = 1; @@ -73,6 +110,13 @@ export function resolveUrdfResourcePath(filename, mainPath, availablePaths) { } } + if (!match) { + const lowerTarget = target.toLowerCase(); + const caseMatches = available.filter((path) => path.toLowerCase() === lowerTarget + || path.toLowerCase().endsWith(`/${lowerTarget}`)); + if (caseMatches.length === 1) match = caseMatches[0]; + } + return match ? relative(dirname(mainPath), match) : null; } diff --git a/tests/cad/api.test.js b/tests/cad/api.test.js index ae40a7c..a307593 100644 --- a/tests/cad/api.test.js +++ b/tests/cad/api.test.js @@ -14,6 +14,26 @@ const { errorText, setLocale } = await import('../../js/cad/i18n.js'); beforeEach(() => { storage.clear(); auth.set({ token: 'token-a', user: { userCode: 'owner-a' } }); }); +test('CAD knowledge uses the authenticated cancellable endpoint and rejects stale identity', async () => { + const control = new AbortController(); + const body = { cadProjectCode: 'project-a', documentSessionCode: 'session-a', engineBuild: 'build-a', arguments: { query: 'gear' } }; + const api = new CadApi({ fetcher: async (url, options) => { + assert.equal(url, '/api/cad/ai/knowledge'); + assert.equal(options.method, 'POST'); + assert.equal(options.headers.Authorization, 'Bearer token-a'); + assert.equal(options.cache, 'no-store'); + assert.equal(options.signal, control.signal); + assert.deepEqual(JSON.parse(options.body), body); + return Response.json({ code: 'OK', data: { status: 'uncovered', documents: [] } }); + } }); + assert.equal((await api.aiKnowledge(body, control.signal)).status, 'uncovered'); + const stale = new CadApi({ fetcher: async () => { + auth.set({ token: 'token-b', user: { userCode: 'owner-b' } }); + return Response.json({ code: 'OK', data: { status: 'matched', documents: [] } }); + } }); + await assert.rejects(stale.aiKnowledge(body), { code: 'STALE_IDENTITY' }); +}); + test('CAD project covers use the documented signature and anonymous OSS form upload', async () => { const calls = [], bytes = new Uint8Array([137, 80, 78, 71]); const api = new CadApi({ fetcher: async (url, options) => { diff --git a/tests/cad/engine-updates.browser.mjs b/tests/cad/engine-updates.browser.mjs index 4fbbb3c..2c06456 100644 --- a/tests/cad/engine-updates.browser.mjs +++ b/tests/cad/engine-updates.browser.mjs @@ -2,6 +2,7 @@ import test from 'node:test'; import assert from 'node:assert/strict'; import { createServer } from 'node:http'; import { readFile } from 'node:fs/promises'; +import { createGzip, constants } from 'node:zlib'; import { chromium } from 'playwright'; import { cadWorker } from '../../scripts/cad-engine.mjs'; @@ -38,6 +39,96 @@ self.addEventListener('activate',()=>{}); self.addEventListener('fetch',event=>{const u=new URL(event.request.url);if(event.request.method!=='GET'||u.origin!==self.location.origin||u.search||!FILES.has(u.pathname))return;event.respondWith((async()=>{const cache=await caches.open(CACHE);const hit=await cache.match(event.request);if(hit)return hit;const response=await fetch(event.request);if(response.ok)await cache.put(event.request,response.clone());return response;})());}); `; +test('CAD loading measures decoded bytes, waits for readiness and detaches cancelled openings', async (t) => { + // A real, valid WASM with a custom section large enough to pause mid-download. + const leb = (number) => { const bytes = []; do { const byte = number & 127; number >>>= 7; bytes.push(byte | (number ? 128 : 0)); } while (number); return bytes; }; + const padding = 256 * 1024; + const wasm = Buffer.concat([Buffer.from([0, 97, 115, 109, 1, 0, 0, 0, 0, ...leb(padding), 0]), Buffer.alloc(padding - 1)]); + const split = Math.floor(wasm.length / 2); + const module = `export default async function({module_or_path}) { + window.receivedResponse = module_or_path instanceof Response; + await WebAssembly.instantiateStreaming(module_or_path); + window.vibemakeCad = { ready: async () => { + if (window.failFont) throw Object.assign(new Error('Font unavailable'), {code:'FONT_UNAVAILABLE'}); + await new Promise(resolve => { window.releaseReady = resolve; }); + return {}; + }}; + }`; + const prefix = '/cad/_assets/progress-v1/'; + const manifest = { bridgeVersion: 1, buildId: 'progress-v1', js: prefix + 'engine.js', wasm: prefix + 'engine.wasm', chineseFont: prefix + 'font.otf', + files: [{ path: prefix + 'engine.js', size: Buffer.byteLength(module) }, { path: prefix + 'engine.wasm', size: wasm.length }] }; + let releaseDownload, paused = true, failDownload = false, wasmRequests = 0; + const html = ``; + const server = createServer(async (req, res) => { + const path = new URL(req.url, 'http://localhost').pathname; + res.setHeader('Cache-Control', 'no-store'); + if (path === '/') res.writeHead(200, { 'Content-Type': 'text/html' }).end(html); + else if (path === '/cad/engine.json') res.writeHead(200, { 'Content-Type': 'application/json' }).end(JSON.stringify(manifest)); + else if (path === manifest.js) res.writeHead(200, { 'Content-Type': 'text/javascript' }).end(module); + else if (path === manifest.wasm) { + wasmRequests++; + if (failDownload) { res.writeHead(503).end(); return; } + res.writeHead(200, { 'Content-Type': 'application/wasm', 'Content-Encoding': 'gzip' }); + const gzip = createGzip(); gzip.pipe(res); + if (paused) { gzip.write(wasm.subarray(0, split)); gzip.flush(constants.Z_SYNC_FLUSH); releaseDownload = () => gzip.end(wasm.subarray(split)); } + else gzip.end(wasm); + } else if (path.startsWith('/js/')) { + try { res.writeHead(200, { 'Content-Type': 'text/javascript' }).end(await readFile(new URL(path.slice(1), root))); } + catch { res.writeHead(404).end(); } + } else res.writeHead(404).end(); + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + 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(); + const openHarness = async () => { await page.goto(`http://127.0.0.1:${server.address().port}/`); await page.waitForFunction(() => window.harnessReady); }; + await openHarness(); + await page.evaluate(() => beginLoad()); + const partial = Buffer.byteLength(module) + split, total = Buffer.byteLength(module) + wasm.length; + await page.waitForFunction((bytes) => samples.at(-1)?.loaded === bytes, partial); + assert.equal(await page.evaluate(() => samples.at(-1).total), total); + assert.equal(await page.evaluate(() => window.receivedResponse), true); + await page.clock.install(); await page.clock.fastForward(12000); + assert.equal(await page.evaluate(() => samples.at(-1).loaded), partial, 'elapsed time must not advance progress'); + await page.clock.resume(); + await page.evaluate(() => controller.abort()); + await page.waitForFunction(() => engineError === 'AbortError'); + const cancelledSamples = await page.evaluate(() => samples.length); + releaseDownload(); + await page.waitForFunction(() => !!window.releaseReady); + assert.equal(await page.evaluate(() => samples.length), cancelledSamples, 'cancelled progress observers must be detached'); + await page.evaluate(() => beginLoad()); + assert.deepEqual(await page.evaluate(() => samples.at(-1)), { phase: 'initialize', loaded: total, total }); + assert.equal(await page.evaluate(() => engineReady), false, 'download completion must still wait for fonts and the bridge'); + await page.evaluate(() => releaseReady()); + await page.waitForFunction(() => engineReady); + assert.equal(wasmRequests, 1, 'retrying an opening must share its engine download'); + assert.equal(await page.evaluate(() => samples.at(-1).phase), 'ready'); + + // An HTTP failure is retryable and never reports successful initialization. + await page.clock.resume(); + failDownload = true; paused = false; + await openHarness(); await page.evaluate(() => beginLoad()); + await page.waitForFunction(() => engineError === 'ENGINE_MISSING'); + assert.equal(await page.evaluate(() => samples.some((p) => p.phase === 'ready')), false); + failDownload = false; + await page.evaluate(() => { window.failFont = true; beginLoad(); }); + await page.waitForFunction(() => engineError === 'FONT_UNAVAILABLE'); + const requestsBeforeFontRetry = wasmRequests; + await page.evaluate(() => { window.failFont = false; window.releaseReady = null; beginLoad(); }); + await page.waitForFunction(() => !!window.releaseReady); + await page.evaluate(() => releaseReady()); + await page.waitForFunction(() => engineReady); + assert.equal(wasmRequests, requestsBeforeFontRetry, 'font retries must reuse initialized WASM'); +}); + test('CAD engine updates complete without clearing browser data', async (t) => { let buildId = 'v1', legacy = false, workerUnavailable = false; const manifest = () => ({ diff --git a/tests/cad/platform-agent-browser.mjs b/tests/cad/platform-agent-browser.mjs index 12eb2f5..cdae608 100644 --- a/tests/cad/platform-agent-browser.mjs +++ b/tests/cad/platform-agent-browser.mjs @@ -43,6 +43,355 @@ async function until(page, predicate, message, timeoutMs = 30000) { throw new Error(`${message}: ${JSON.stringify(await chat(page)).slice(0, 2000)}`); } +async function modelRecoveryChecks(page, checks) { + const endpoint = '**/api/cad/ai/chat'; + const event = (body, type, data, code = 'OK') => `event:${type}\ndata:${JSON.stringify({ + code, message: code, requestId: 'model-recovery-test', + data: { documentSessionCode: body.documentSessionCode, turnCode: body.turnCode, ...data }, + })}\n\n`; + const stream = (route, body) => route.fulfill({ status: 200, contentType: 'text/event-stream', body }); + const finish = (body, text) => event(body, 'completed', { + finishReason: 'stop', output: [{ type: 'message', role: 'assistant', content: [{ type: 'text', text }] }], usage: null, + }); + const error = (body, code, retryable) => event(body, 'error', { retryable }, code); + const baseline = (await inspect(page)).bodies.length; + await chat(page, { new_chat: true }); + const requests = []; + const applied = { type: 'toolCall', callCode: 'recovery-applied', name: 'execute', arguments: { + command: 'solid.box', params: { length: 2, width: 3, height: 4, body_name: 'RecoveryBox' }, + } }; + const ghost = { ...applied, callCode: 'recovery-never-applied' }; + await page.route(endpoint, async (route) => { + const body = route.request().postDataJSON(); + requests.push(body); + const step = requests.length; + if (step === 1) return stream(route, event(body, 'tool_call', applied) + + event(body, 'completed', { finishReason: 'tool_calls', output: [applied], usage: null })); + if (step === 2) { + const before = (await page.evaluate(() => window.vibemakeCad.getState())).revision; + await page.evaluate(() => window.solidcraft.execute('select.clear', {})); + assert.notEqual((await page.evaluate(() => window.vibemakeCad.getState())).revision, before, + 'non-design selection changes exercise the host revision snapshot'); + return stream(route, event(body, 'text_delta', { text: 'DISCARDED_PARTIAL_RESPONSE' }) + event(body, 'tool_call', ghost)); + } + if (step === 3) return stream(route, error(body, 'CAD_AI_PROVIDER_FAILURE', true)); + return stream(route, finish(body, 'RECOVERY_CONFIRMED')); + }); + try { + await chat(page, { send: 'Recover this task while keeping dimensions and applied geometry.', attach_view: false, attach_selection: false }); + const state = await until(page, (s) => !s.turn && s.items.some((i) => i.agent === 'RECOVERY_CONFIRMED'), 'temporary failures did not recover'); + assert.equal(requests.length, 4, 'one successful CAD step, two failed model attempts and one recovered response'); + assert.deepEqual(requests[2], requests[1]); + assert.deepEqual(requests[3], requests[1], 'all retry input including history and runtime context is frozen'); + assert.ok(requests[1].history.some((i) => i.type === 'toolResult' && i.callCode === applied.callCode)); + assert.equal((await inspect(page)).bodies.length, baseline + 1, 'the already-applied box is never replayed'); + assert.equal(state.items.filter((i) => i.title === 'execute').length, 1, 'failed attempts leave no pending tool cards'); + assert.ok(!state.items.some((i) => i.agent?.includes('DISCARDED_PARTIAL_RESPONSE'))); + assert.ok(!state.items.some((i) => i.error)); + assert.equal(state.items.filter((i) => i.notice?.includes('正在恢复当前任务')).length, 2, + 'asynchronous recovery notices keep the current UI language'); + checks.push('model-recovery-preserves-full-request-applied-geometry-and-one-confirmed-response'); + if (process.env.VM_CAD_SCREENSHOT) await page.screenshot({ path: process.env.VM_CAD_SCREENSHOT.replace(/\.png$/, '-recovery.png') }); + await chat(page, { undo: true }); + assert.equal((await inspect(page)).bodies.length, baseline, 'recovered task stays one undo step'); + } finally { await page.unroute(endpoint); } + + // A rejected model response never ran its announced tools. Only its diagnostic + // is added to the next request; completed CAD calls stay in the original history. + await chat(page, { new_chat: true }); + const protocolRequests = []; + const firstProtocolCall = { ...applied, callCode: 'protocol-already-applied', arguments: { + command: 'solid.box', params: { length: 2, width: 3, height: 4, body_name: 'ProtocolExistingBox' }, + } }; + const correctedProtocolCall = { ...applied, callCode: 'protocol-corrected', arguments: { + command: 'solid.box', params: { length: 2, width: 3, height: 5, body_name: 'ProtocolCorrectedBox' }, + } }; + const rejectedProtocolCall = { ...applied, callCode: 'protocol-never-applied' }; + const correction = { tool: 'execute', rule: 'required', path: '$.params' }; + const toolCompletion = (body, calls) => calls.map((call) => event(body, 'tool_call', call)).join('') + + event(body, 'completed', { finishReason: 'tool_calls', output: calls, usage: null }); + const withoutCorrection = (body) => { + const copy = structuredClone(body); + delete copy.context.modelRecovery; + return copy; + }; + await page.route(endpoint, async (route) => { + const body = route.request().postDataJSON(); + protocolRequests.push(body); + if (protocolRequests.length === 1) return stream(route, toolCompletion(body, [firstProtocolCall])); + if (protocolRequests.length === 2) return stream(route, + event(body, 'text_delta', { text: 'PROTOCOL_DISCARDED_PARTIAL' }) + + event(body, 'tool_call', rejectedProtocolCall) + + event(body, 'error', { retryable: true, correction }, 'CAD_AI_INVALID_MODEL_TOOL')); + if (protocolRequests.length === 3) return stream(route, toolCompletion(body, [correctedProtocolCall])); + return stream(route, finish(body, 'PROTOCOL_RECOVERY_CONFIRMED')); + }); + try { + await chat(page, { send: 'Continue this task after correcting invalid tool parameters, retaining every dimension.', + attach_view: false, attach_selection: false }); + const state = await until(page, (s) => !s.turn && s.items.some((i) => i.agent === 'PROTOCOL_RECOVERY_CONFIRMED'), + 'model tool protocol error did not recover'); + assert.equal(protocolRequests.length, 4, 'completed step, rejected response, corrected step, final completion'); + assert.deepEqual(protocolRequests[2].context.modelRecovery, correction, 'exact safe validator feedback reaches the retry'); + assert.deepEqual(withoutCorrection(protocolRequests[2]), protocolRequests[1], + 'correction preserves the full history, model inputs and host document revision'); + assert.ok(protocolRequests[1].history.some((item) => item.type === 'toolResult' && item.callCode === firstProtocolCall.callCode)); + assert.equal(protocolRequests[3].context.modelRecovery, undefined, 'successful correction does not leak into the next model step'); + assert.ok(!protocolRequests[3].history.some((item) => item.callCode === rejectedProtocolCall.callCode)); + assert.equal((await inspect(page)).bodies.length, baseline + 2, 'each confirmed CAD step executes exactly once'); + assert.equal(state.items.filter((item) => item.title === 'execute').length, 2, 'the rejected attempt leaves no phantom tool card'); + assert.ok(!state.items.some((item) => item.agent?.includes('PROTOCOL_DISCARDED_PARTIAL'))); + assert.ok(!state.items.some((item) => item.error)); + assert.equal(state.items.filter((item) => item.notice && !item.error).length, 1, 'correction keeps the same task active'); + checks.push('model-protocol-recovery-preserves-inputs-applied-geometry-and-corrects-only-the-rejected-step'); + if (process.env.VM_CAD_SCREENSHOT) await page.screenshot({ path: process.env.VM_CAD_SCREENSHOT.replace(/\.png$/, '-protocol-recovery.png') }); + await chat(page, { undo: true }); + assert.equal((await inspect(page)).bodies.length, baseline, 'all confirmed steps in the recovered task remain one undo step'); + } finally { await page.unroute(endpoint); } + + for (const scenario of ['legacy-tool', 'local-arguments', 'duplicate-call-code', 'completed-mismatch', 'completed-text', 'completed-output']) { + await chat(page, { new_chat: true }); + const attempts = []; + await page.route(endpoint, async (route) => { + const body = route.request().postDataJSON(); + attempts.push(body); + if (attempts.length > 1) return stream(route, finish(body, 'PROTOCOL_EDGE_RECOVERY_CONFIRMED')); + if (scenario === 'legacy-tool') return stream(route, event(body, 'error', {}, 'CAD_AI_INVALID_MODEL_TOOL')); + if (scenario === 'local-arguments') return stream(route, toolCompletion(body, [rejectedProtocolCall, + { ...correctedProtocolCall, arguments: { command: 17, params: {} } }])); + if (scenario === 'duplicate-call-code') return stream(route, toolCompletion(body, [rejectedProtocolCall, rejectedProtocolCall])); + if (scenario === 'completed-mismatch') return stream(route, event(body, 'tool_call', rejectedProtocolCall) + + event(body, 'completed', { finishReason: 'tool_calls', output: [correctedProtocolCall], usage: null })); + if (scenario === 'completed-text') return stream(route, event(body, 'text_delta', { text: 'PROTOCOL_DISCARDED_PARTIAL' }) + + finish(body, 'UNCONFIRMED_DIFFERENT_TEXT')); + return stream(route, event(body, 'completed', { finishReason: 'tool_calls', + output: [{ ...rejectedProtocolCall, arguments: [] }], usage: null })); + }); + try { + await chat(page, { send: `Recover ${scenario} without executing unconfirmed calls.`, attach_view: false, attach_selection: false }); + const state = await until(page, (s) => !s.turn && s.items.some((i) => i.agent === 'PROTOCOL_EDGE_RECOVERY_CONFIRMED'), + `${scenario} was not corrected`); + assert.equal(attempts.length, 2, `${scenario} retries only the rejected model response`); + assert.deepEqual(withoutCorrection(attempts[1]), attempts[0], `${scenario} retains every original input`); + assert.ok(attempts[1].context.modelRecovery?.rule, `${scenario} sends bounded correction data`); + if (scenario === 'legacy-tool') assert.deepEqual(attempts[1].context.modelRecovery, + { tool: 'unknown', rule: 'invalid_tool', path: '$' }, 'older backends still receive safe correction guidance'); + assert.equal((await inspect(page)).bodies.length, baseline, `${scenario} cannot run a tool from the rejected response`); + assert.equal(state.items.filter((item) => item.title).length, 0, `${scenario} cannot leave an unexecuted tool card`); + assert.ok(!state.items.some((item) => item.error || item.agent?.includes('PROTOCOL_DISCARDED_PARTIAL') + || item.agent?.includes('UNCONFIRMED_DIFFERENT_TEXT'))); + checks.push(`model-protocol-recovery-${scenario}-regenerates-before-any-tool-executes`); + } finally { await page.unroute(endpoint); } + } + + await chat(page, { new_chat: true }); + const reusedRequests = []; + await page.route(endpoint, async (route) => { + const body = route.request().postDataJSON(); + reusedRequests.push(body); + if (reusedRequests.length <= 2) return stream(route, toolCompletion(body, [firstProtocolCall])); + return stream(route, finish(body, 'REUSED_CALL_RECOVERY_CONFIRMED')); + }); + try { + await chat(page, { send: 'Recover a reused call code without repeating the completed operation.', attach_view: false, attach_selection: false }); + const state = await until(page, (s) => !s.turn && s.items.some((i) => i.agent === 'REUSED_CALL_RECOVERY_CONFIRMED'), + 'reused call code did not trigger model correction'); + assert.equal(reusedRequests.length, 3); + assert.deepEqual(withoutCorrection(reusedRequests[2]), reusedRequests[1]); + assert.ok(reusedRequests[2].context.modelRecovery?.rule); + assert.equal((await inspect(page)).bodies.length, baseline + 1, 'a repeated call code cannot replay the first operation'); + assert.equal(state.items.filter((item) => item.title === 'execute').length, 1); + assert.ok(!state.items.some((item) => item.error)); + checks.push('model-protocol-recovery-reused-call-code-preserves-the-original-operation-once'); + await chat(page, { undo: true }); + assert.equal((await inspect(page)).bodies.length, baseline); + } finally { await page.unroute(endpoint); } + + for (const scenario of ['exhausted', 'disabled', 'stop']) { + await chat(page, { new_chat: true }); + if (scenario === 'disabled') await page.evaluate(() => window.solidcraft.request('ui.prefs', { agent_model_recovery: false })); + let attempts = 0; + await page.route(endpoint, async (route) => { + const body = route.request().postDataJSON(); + attempts++; + return stream(route, event(body, 'error', { retryable: true, correction }, 'CAD_AI_INVALID_MODEL_TOOL')); + }); + try { + await chat(page, { send: `Check protocol correction ${scenario}.`, attach_view: false, attach_selection: false }); + if (scenario === 'stop') { + await until(page, (s) => s.turn && s.items.some((item) => item.notice && !item.error), 'protocol correction did not remain active'); + await chat(page, { cancel: true }); + await until(page, (s) => !s.turn, 'Stop did not cancel protocol correction'); + await page.waitForTimeout(1200); + } else { + const state = await until(page, (s) => !s.turn && s.state === 'ready', `${scenario} protocol task did not finish`); + assert.ok(state.items.some((item) => item.error), 'unrecovered invalid tools still report the error'); + } + assert.equal(attempts, scenario === 'exhausted' ? 3 : 1, `${scenario} applies the existing retry budget and controls`); + assert.equal((await inspect(page)).bodies.length, baseline); + checks.push(`model-protocol-recovery-${scenario}-respects-task-controls`); + } finally { + await page.unroute(endpoint); + if (scenario === 'disabled') await page.evaluate(() => window.solidcraft.request('ui.prefs', { agent_model_recovery: true })); + } + } + + for (const scenario of ['permanent', 'legacy', 'stale', 'exhausted', 'timeout', 'http', 'disabled', 'stop', 'edited']) { + await chat(page, { new_chat: true }); + if (scenario === 'disabled') await page.evaluate(() => window.solidcraft.request('ui.prefs', { agent_model_recovery: false })); + let attempts = 0; + await page.route(endpoint, async (route) => { + const body = route.request().postDataJSON(); + attempts++; + if (['timeout', 'http'].includes(scenario) && attempts > 1) return stream(route, finish(body, 'RECOVERY_CONFIRMED')); + if (scenario === 'permanent') return stream(route, error(body, 'CAD_AI_OUTPUT_LIMIT', true)); + if (scenario === 'legacy') return stream(route, error(body, 'CAD_AI_PROVIDER_FAILURE', false)); + if (scenario === 'stale') return stream(route, error({ ...body, turnCode: 'another-turn' }, 'CAD_AI_PROVIDER_FAILURE', true)); + if (scenario === 'timeout') return stream(route, error(body, 'CAD_AI_TIMEOUT', true)); + if (scenario === 'http') return route.fulfill({ status: 502, contentType: 'text/plain', body: 'temporary gateway failure' }); + return stream(route, error(body, 'CAD_AI_PROVIDER_FAILURE', true)); + }); + try { + await chat(page, { send: `Check ${scenario} recovery.`, attach_view: false, attach_selection: false }); + if (scenario === 'stop' || scenario === 'edited') { + await until(page, (s) => s.turn && s.items.some((i) => i.notice && !i.error), 'retry did not keep the task active'); + if (scenario === 'edited') { + await page.evaluate(() => window.solidcraft.execute('solid.box', { length: 1, width: 2, height: 3, body_name: 'HumanEditDuringRecovery' })); + } else { + await chat(page, { cancel: true }); + } + await until(page, (s) => !s.turn, 'Stop did not cancel recovery'); + await page.waitForTimeout(1200); + } else { + await until(page, (s) => !s.turn && s.state === 'ready', `${scenario} task did not finish`); + } + const expected = scenario === 'exhausted' ? 3 : ['timeout', 'http'].includes(scenario) ? 2 : 1; + assert.equal(attempts, expected, `${scenario} retry count`); + if (scenario === 'edited') { + assert.equal((await inspect(page)).bodies.length, baseline + 1, 'manual edits are kept when they cancel recovery'); + await page.evaluate(() => window.solidcraft.execute('edit.undo', {})); + } + assert.equal((await inspect(page)).bodies.length, baseline, `${scenario} cannot change geometry`); + checks.push(`model-recovery-${scenario}-respects-request-boundaries`); + } finally { + await page.unroute(endpoint); + if (scenario === 'disabled') await page.evaluate(() => window.solidcraft.request('ui.prefs', { agent_model_recovery: true })); + } + } + + // A late result from a cancelled request must not clear the next turn's abort slot. + await chat(page, { new_chat: true }); + await page.evaluate(() => { + window.recoveryOriginalChat = window.__VIBEMAKE_CAD__.agentChat; + window.recoveryPending = []; + window.__VIBEMAKE_CAD__.agentChat = (body, signal, onStart) => { + onStart(); + return new Promise((resolve) => window.recoveryPending.push({ body, signal, resolve })); + }; + }); + try { + await chat(page, { send: 'First delayed response', attach_view: false, attach_selection: false }); + await page.waitForFunction(() => window.recoveryPending.length === 1); + await chat(page, { cancel: true }); + await until(page, (s) => !s.turn, 'first delayed response did not cancel'); + await chat(page, { send: 'Second delayed response', attach_view: false, attach_selection: false }); + await page.waitForFunction(() => window.recoveryPending.length === 2); + await page.evaluate(() => window.recoveryPending[0].resolve(new Response('', { headers: { 'Content-Type': 'text/event-stream' } }))); + await page.waitForTimeout(100); + assert.ok((await chat(page)).turn, 'the late result cannot end the newer turn'); + await chat(page, { cancel: true }); + await until(page, (s) => !s.turn, 'newer request did not cancel'); + assert.equal(await page.evaluate(() => window.recoveryPending[1].signal.aborted), true, + 'the newer controller remains reachable after old-request cleanup'); + checks.push('model-recovery-late-old-response-cannot-detach-new-turn-cancellation'); + } finally { + await page.evaluate(() => { + window.__VIBEMAKE_CAD__.agentChat = window.recoveryOriginalChat; + for (const request of window.recoveryPending) request.resolve(new Response('')); + delete window.recoveryOriginalChat; + delete window.recoveryPending; + }); + } + + await chat(page, { new_chat: true }); + await page.evaluate(() => { + window.recoveryOriginalChat = window.__VIBEMAKE_CAD__.agentChat; + window.recoverySignals = []; + window.__VIBEMAKE_CAD__.agentChat = (body, signal, onStart) => { + onStart(); + window.recoverySignals.push(signal); + if (window.recoverySignals.length === 1) return Promise.reject(new TypeError('temporary network interruption')); + return new Promise((resolve) => { window.recoveryResolve = resolve; }); + }; + }); + try { + await chat(page, { send: 'Check fresh request cancellation', attach_view: false, attach_selection: false }); + await page.waitForFunction(() => window.recoverySignals.length === 2); + assert.deepEqual(await page.evaluate(() => window.recoverySignals.map((signal) => signal.aborted)), [true, false]); + await chat(page, { cancel: true }); + await until(page, (s) => !s.turn, 'retried request did not cancel'); + assert.deepEqual(await page.evaluate(() => window.recoverySignals.map((signal) => signal.aborted)), [true, true]); + checks.push('model-recovery-creates-fresh-abort-signal-for-each-attempt'); + } finally { + await page.evaluate(() => { + window.__VIBEMAKE_CAD__.agentChat = window.recoveryOriginalChat; + window.recoveryResolve?.(new Response('')); + delete window.recoveryOriginalChat; + delete window.recoverySignals; + delete window.recoveryResolve; + }); + } + + // Shorten only the model deadline so the real WASM timeout callback is exercised. + await chat(page, { new_chat: true }); + await page.evaluate(() => { + window.recoveryOriginalChat = window.__VIBEMAKE_CAD__.agentChat; + window.recoverySetTimeout = window.setTimeout; + window.recoveryTimers = []; + window.recoveryTimerFires = 0; + window.recoveryTimerRequests = 0; + window.setTimeout = (callback, ms, ...args) => { + if (ms !== 60000) return window.recoverySetTimeout.call(window, callback, ms, ...args); + const id = window.recoverySetTimeout.call(window, () => { + window.recoveryTimerFires++; + callback(...args); + }, 75); + window.recoveryTimers.push(id); + return id; + }; + window.__VIBEMAKE_CAD__.agentChat = (body, signal, onStart) => { + onStart(); + if (++window.recoveryTimerRequests === 1) return new Promise((resolve, reject) => { + signal.addEventListener('abort', () => reject(new DOMException('Aborted', 'AbortError')), { once: true }); + }); + const data = { documentSessionCode: body.documentSessionCode, turnCode: body.turnCode, finishReason: 'stop', + output: [{ type: 'message', role: 'assistant', content: [{ type: 'text', text: 'TIMER_RECOVERY_CONFIRMED' }] }] }; + return Promise.resolve(new Response(`event:completed\ndata:${JSON.stringify({ code: 'OK', requestId: 'timer-test', data })}\n\n`, + { headers: { 'Content-Type': 'text/event-stream' } })); + }; + }); + try { + await chat(page, { send: 'Recover from the actual model deadline', attach_view: false, attach_selection: false }); + await until(page, (s) => !s.turn && s.items.some((i) => i.agent === 'TIMER_RECOVERY_CONFIRMED'), 'local request timeout did not recover'); + await page.waitForTimeout(125); + assert.deepEqual(await page.evaluate(() => ({ attempts: window.recoveryTimerRequests, arms: window.recoveryTimers.length, + unique: new Set(window.recoveryTimers).size, fires: window.recoveryTimerFires })), + { attempts: 2, arms: 2, unique: 2, fires: 1 }, 'each attempt arms a fresh timer and successful cleanup clears its timer'); + checks.push('model-recovery-real-local-timeout-rearms-and-cleans-up-each-deadline'); + } finally { + await page.evaluate(() => { + window.__VIBEMAKE_CAD__.agentChat = window.recoveryOriginalChat; + window.setTimeout = window.recoverySetTimeout; + for (const id of window.recoveryTimers) clearTimeout(id); + delete window.recoveryOriginalChat; + delete window.recoverySetTimeout; + delete window.recoveryTimers; + delete window.recoveryTimerFires; + delete window.recoveryTimerRequests; + }); + } +} + async function run(browser) { const checks = [], errors = []; const project = await (await fetch(`${base}/api/cad/workspace/projects`, { @@ -144,6 +493,95 @@ async function run(browser) { await page.evaluate(() => window.solidcraft.request('ui.prefs', { button: 'cancel' })); checks.push('platform-preferences-hide-local-assistant-configuration'); + assert.equal((await chat(page, { permissions: 'ask' })).permissions, 'all', 'legacy permissions cannot reintroduce approval prompts'); + const composerHandles = await page.evaluate(() => window.solidcraft.request('ui.handles', { prefix: 'agent_chat:' })); + assert.ok(composerHandles.some((handle) => handle.name === 'agent_chat:attachment')); + for (const removed of ['add', 'add_file', 'paste_picture', 'upload_attachment', 'permissions']) { + assert.ok(!composerHandles.some((handle) => handle.name === `agent_chat:${removed}`), `no obsolete ${removed} action`); + } + const pictureBase64 = await page.evaluate(() => { + const canvas = document.createElement('canvas'); canvas.width = 160; canvas.height = 90; + const paint = canvas.getContext('2d'); + paint.fillStyle = '#d9e2eb'; paint.fillRect(0, 0, 160, 90); + paint.fillStyle = '#337db1'; paint.fillRect(30, 20, 22, 50); paint.fillRect(30, 55, 100, 15); + return canvas.toDataURL('image/png').split(',')[1]; + }); + async function paste({ text = '', image = false, name = 'pasted-reference.png', outside = false } = {}) { + await page.evaluate(({ text, image, name, outside, pictureBase64 }) => { + const data = new DataTransfer(); + if (text) data.setData('text/plain', text); + if (image) data.items.add(new File([Uint8Array.from(atob(pictureBase64), (c) => c.charCodeAt(0))], name, { type: 'image/png' })); + let target = document.activeElement; + if (outside) { target = document.createElement('input'); target.id = 'clipboard-test-outside'; document.body.append(target); target.focus(); } + target.dispatchEvent(new ClipboardEvent('paste', { clipboardData: data, bubbles: true, cancelable: true })); + }, { text, image, name, outside, pictureBase64 }); + } + await page.bringToFront(); + await clickHandle(page, 'agent_chat:input'); + await page.keyboard.type('vm-test:read ', { delay: 20 }); + await paste({ text: '剪贴板文字' }); + await page.waitForTimeout(200); + await page.evaluate(() => { + window.__composerArrayBuffer = File.prototype.arrayBuffer; + File.prototype.arrayBuffer = async function () { + await new Promise((resolve) => setTimeout(resolve, 1500)); + return window.__composerArrayBuffer.call(this); + }; + }); + await paste({ image: true }); + await page.waitForTimeout(150); + await page.keyboard.press('ControlOrMeta+Enter'); + await page.waitForTimeout(250); + assert.ok(!(await chat(page)).items.some((item) => item.user), 'sending waits for pasted image bytes'); + await until(page, (state) => state.attachments?.includes('pasted-reference.png'), 'image paste did not attach'); + await page.evaluate(() => { File.prototype.arrayBuffer = window.__composerArrayBuffer; delete window.__composerArrayBuffer; }); + await paste({ image: true, text: '和图片', name: 'mixed-reference.png' }); + await until(page, (state) => state.attachments?.length === 2, 'mixed text and image paste did not attach'); + await paste({ image: true, outside: true, name: 'outside.png' }); + await page.waitForTimeout(350); + assert.equal((await chat(page)).attachments.length, 2, 'pasting outside the composer cannot attach a file'); + await page.evaluate(() => document.querySelector('#clipboard-test-outside')?.remove()); + const chooser = page.waitForEvent('filechooser'); + await clickHandle(page, 'agent_chat:attachment'); + await (await chooser).setFiles({ name: 'requirements.txt', mimeType: 'text/plain', buffer: Buffer.from('bracket requirement: thickness 1.5 mm') }); + await until(page, (state) => state.attachments?.length === 3 && state.loading?.length === 0, 'paperclip did not open the attachment picker'); + if (process.env.VM_CAD_SCREENSHOT) await page.screenshot({ path: process.env.VM_CAD_SCREENSHOT.replace(/\.png$/, '-composer.png') }); + await chat(page, { remove_file: 0 }); + assert.deepEqual((await chat(page)).attachments, ['mixed-reference.png', 'requirements.txt']); + await clickHandle(page, 'agent_chat:input'); + await page.keyboard.press('Enter'); + const pasted = await until(page, (state) => !state.turn && state.items.some((item) => item.user), 'pasted prompt did not finish'); + assert.equal(pasted.items.find((item) => item.user).user, 'vm-test:read 剪贴板文字和图片'); + const pasteCall = (await agentCalls()).find((call) => call.path === '/cad/ai/chat' && call.body?.cadProjectCode === project.data.cadProjectCode + && call.body.history.some((item) => item.role === 'user' && item.content?.some((part) => part.text?.includes('剪贴板文字和图片')))); + assert.ok(pasteCall?.body.history.some((item) => item.content?.some((part) => part.type === 'image')), 'pasted image reaches the model request'); + assert.ok(pasteCall?.body.history.some((item) => item.content?.some((part) => part.text?.includes('bracket requirement: thickness 1.5 mm'))), 'uploaded text reaches the same model request'); + checks.push('single-paperclip-uploads-files-and-direct-paste-retains-text-images-and-removal'); + checks.push('send-shortcut-waits-for-pasted-image-bytes-without-losing-the-prompt'); + checks.push('paste-outside-composer-does-not-attach-or-read-the-clipboard'); + + await chat(page, { new_chat: true }); + let release; + const held = new Promise((resolve) => { release = resolve; }); + const holdChat = async (route) => { await held; await route.continue().catch(() => {}); }; + await page.route('**/api/cad/ai/chat', holdChat); + try { + await chat(page, { send: 'vm-test:read composer busy state', attach_view: false, attach_selection: false }); + await until(page, (state) => !!state.turn, 'turn did not start'); + await page.waitForFunction(async () => (await window.solidcraft.request('ui.handles', { prefix: 'agent_chat:stopturn' })).length === 1); + const busyHandles = await page.evaluate(() => window.solidcraft.request('ui.handles', { prefix: 'agent_chat:' })); + assert.ok(!busyHandles.some((handle) => handle.name === 'agent_chat:send'), 'queue button is absent while busy'); + assert.ok(!busyHandles.some((handle) => handle.name === 'agent_chat:permissions')); + await clickHandle(page, 'agent_chat:stopturn'); + await until(page, (state) => !state.turn, 'stop button did not cancel'); + } finally { release(); await page.unroute('**/api/cad/ai/chat', holdChat); } + checks.push('busy-composer-has-stop-without-queue-or-permission-controls'); + await chat(page, { new_chat: true }); + if (process.env.VM_CAD_AGENT_BROWSER_SUITE === 'composer') { + assert.deepEqual(errors, []); + return { checks, errors, url: page.url() }; + } + // Real browser IME events: the hidden text bridge must retain its native caret after // each commit, while the visible egui editor still supports insertion in the middle. await page.bringToFront(); @@ -222,6 +660,12 @@ async function run(browser) { assert.equal((await inspect(page)).bodies[0].name, 'PlatformAIBox'); checks.push('fragmented-sse-and-completed-output-authorize-tool-execution'); + await modelRecoveryChecks(page, checks); + if (process.env.VM_CAD_RECOVERY_ONLY) { + assert.deepEqual(errors, []); + return { checks, errors, url: page.url() }; + } + await chat(page, { new_chat: true }); await chat(page, { send: 'vm-test:truncated', attach_view: false, attach_selection: false }); await until(page, (state) => !state.turn && state.state === 'ready', 'truncated stream did not stop', 30000); diff --git a/tests/urdf-loading.browser.mjs b/tests/urdf-loading.browser.mjs new file mode 100644 index 0000000..ec11b28 --- /dev/null +++ b/tests/urdf-loading.browser.mjs @@ -0,0 +1,97 @@ +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(``); + 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) => ``); + const joints = names.slice(1).map((name) => ``); + const main = '/uploads/batch/urdf_pkg/Alexbot_simple_urdf/urdf/robot.urdf'; + add(main, `${links.join('')}${joints.join('')}`); + 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, ``); + 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, ''); + await assert.rejects(load(main), /1 个零件未能加载(missing.stl)/); + }); +}); diff --git a/tests/urdf-paths.test.js b/tests/urdf-paths.test.js index d370ae1..9e368d7 100644 --- a/tests/urdf-paths.test.js +++ b/tests/urdf-paths.test.js @@ -1,6 +1,6 @@ import test from 'node:test'; import assert from 'node:assert/strict'; -import { meshUrlCandidates, resolveUrdfResourcePath } from '../js/urdf-paths.js'; +import { meshUrlCandidates, resolveUrdfResourcePath, urdfResourceUrlCandidates } from '../js/urdf-paths.js'; test('maps package references with a stale source directory prefix to uploaded paths', () => { assert.equal( @@ -55,3 +55,64 @@ test('keeps normal package mesh URLs first', () => { ['https://oss.example/batch/meshes/body.stl', 'https://oss.example/batch/body.stl'], ); }); + +const base = 'https://oss.example/batch'; +const alexMain = `${base}/urdf_pkg/Alexbot_simple_urdf/urdf/Alexbot_simple_urdf.urdf`; + +test('loads an existing nested ROS package from its actual package directory', () => { + assert.equal(urdfResourceUrlCandidates(base, + 'package://Alexbot_simple_urdf/meshes/base_link.STL', alexMain)[0], + `${base}/urdf_pkg/Alexbot_simple_urdf/meshes/base_link.STL`); +}); + +test('retains flattened legacy packages, including stale source prefixes', () => { + assert.equal(urdfResourceUrlCandidates(base, 'package://robot/meshes/body.stl', `${base}/urdf/robot.urdf`)[0], + `${base}/meshes/body.stl`); + assert.equal(urdfResourceUrlCandidates(base, + 'package://example-robot-data/robots/asr_twodof_description/meshes/arm.dae', `${base}/urdf/robot.urdf`)[0], + `${base}/meshes/arm.dae`); +}); + +test('keeps a package-qualified fallback for other packages in the same upload', () => { + assert.ok(urdfResourceUrlCandidates(base, 'package://camera/meshes/body.stl', alexMain) + .slice(0, 9).includes(`${base}/camera/meshes/body.stl`)); +}); + +test('handles empty package names, file URIs and Windows exporter paths', () => { + assert.equal(urdfResourceUrlCandidates(base, 'package:///antenna.stl', `${base}/robot.urdf`)[0], `${base}/antenna.stl`); + assert.equal(urdfResourceUrlCandidates(base, `${base}/urdf/file:////meshes/body.stl`, alexMain)[0], `${base}/meshes/body.stl`); + assert.ok(urdfResourceUrlCandidates(base, `${base}/urdfs/D:\\Data\\Robot/meshes/hand.stl`, alexMain) + .slice(0, 9).includes(`${base}/meshes/hand.stl`)); +}); + +test('normalizes relative references within the upload batch and supports lowercase STL objects', () => { + const candidates = urdfResourceUrlCandidates(base, `${base}/robots/../meshes/body.STL`, `${base}/robots/robot.urdf`); + assert.equal(candidates[0], `${base}/meshes/body.STL`); + assert.ok(candidates.slice(0, 9).includes(`${base}/meshes/body.stl`)); + assert.equal(urdfResourceUrlCandidates(base, `${base}/urdf/../../kinect/meshes/kinect.dae`, alexMain)[0], + `${base}/kinect/meshes/kinect.dae`); +}); + +test('keeps signed queries and external resources intact', () => { + const url = `${base}/meshes/body.STL?Signature=x%2Fy&Expires=123`; + assert.equal(urdfResourceUrlCandidates(base, url, alexMain)[0], url); + assert.ok(urdfResourceUrlCandidates(base, url, alexMain).every((candidate) => candidate.endsWith('?Signature=x%2Fy&Expires=123'))); + const external = 'https://other.example/body.stl?Signature=external'; + assert.deepEqual(urdfResourceUrlCandidates(base, external, alexMain), [external]); +}); + +test('rewrites nested uploads and disambiguates files belonging to different packages', () => { + assert.equal(resolveUrdfResourcePath('package://Alexbot_simple_urdf/meshes/base_link.STL', + 'urdf_pkg/Alexbot_simple_urdf/urdf/robot.urdf', ['urdf_pkg/Alexbot_simple_urdf/meshes/base_link.STL']), + '../meshes/base_link.STL'); + assert.equal(resolveUrdfResourcePath('package://camera/meshes/body.stl', 'robot/urdf/robot.urdf', + ['camera/meshes/body.stl', 'robot/meshes/body.stl', 'meshes/body.stl']), + '../../camera/meshes/body.stl'); +}); + +test('matches exported Windows paths, empty packages and unique filename casing against the manifest', () => { + assert.equal(resolveUrdfResourcePath('D:\\Data\\Robot\\meshes\\body.STL', 'urdf/robot.urdf', ['meshes/body.STL']), '../meshes/body.STL'); + assert.equal(resolveUrdfResourcePath('package:///body.stl', 'robot.urdf', ['body.stl']), 'body.stl'); + assert.equal(resolveUrdfResourcePath('../meshes/body.STL', 'robots/robot.urdf', ['meshes/body.stl']), '../meshes/body.stl'); + assert.equal(resolveUrdfResourcePath('meshes/body.STL', 'robot.urdf', ['meshes/BODY.stl', 'meshes/body.stl']), null); +});