diff --git a/docs/web/ARMATURE-ROBOT-HEADED-TESTS.md b/docs/web/ARMATURE-ROBOT-HEADED-TESTS.md index efb76eb3d..80b469b53 100644 --- a/docs/web/ARMATURE-ROBOT-HEADED-TESTS.md +++ b/docs/web/ARMATURE-ROBOT-HEADED-TESTS.md @@ -33,6 +33,7 @@ node tools/web/check-armature-robot-headed.mjs --dir /abs/tests/golden/ARMATURE- | `armature-viewport-web` | 属性面板姿态表单 → Apply Pose | 画布 `data-armature-bone-count`、截图前后不同 | | `armature-decimal-input-web` | 用户逐键输入 `0.5` | `缩放 X` 与姿态 `Scale X` 保留小数;姿态提交后 poseScale 读回 0.5 | | `armature-ui-readback-web` | 姿态表单回填 | UI 显示角度/缩放 = 引擎 pose 通道(弧度×180/π),`data-testid="armature-pose-readback"` | +| `armature-reopen-web` | 文件 → 保存到浏览器 OPFS → 最近打开 | worker `openBlend` 成功回复为前置;重开后骨骼、骨骼父级、姿态与世界矩阵与保存前一致 | ## 门禁(checker 硬校验) @@ -70,5 +71,7 @@ node tools/web/check-armature-robot-headed.mjs --dir /abs/tests/golden/ARMATURE- 只判定 URDF 层级与装配绑定关系(6 骨骼对应 6 个转动关节、每个 link 绑定到自己的关节骨骼、无产品控制台错误)。 - 机器人工具动作「建立骨架(6 骨骼 + 刚性绑定)」已落地:Python 只建骨架与 6 根骨骼,刚性绑定经引擎 `setParent(BONE, keepTransform)` 完成,与 `armature-bind-web` 共用同一实现。 -- `armature-reopen-web`(OPFS 保存重开一致)尚未编写。 +- `armature-reopen-web` 已落地:保存走「文件 → 保存到浏览器 OPFS」,重开走「文件 → 最近打开」。 + 断言前必须等到 worker 的 `openBlend` 成功回复计数增长(证据 `openBlendReplies` 1→2),否则活着的 + Main 会直接回答快照、比较恒真;实测重开前 r4 → 重开后 r1,骨骼 / 父级 / 姿态 / 世界矩阵零偏差。 - 姿态表单「编辑 / 姿态 / 物体」模式分离与 Ctrl+Tab 进入姿态待实现。 diff --git a/tools/web/check-armature-robot-headed.mjs b/tools/web/check-armature-robot-headed.mjs index 244310ffd..395abd14a 100644 --- a/tools/web/check-armature-robot-headed.mjs +++ b/tools/web/check-armature-robot-headed.mjs @@ -19,7 +19,7 @@ import path from "node:path"; import { fileURLToPath } from "node:url"; const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); -const EXPECTED_CASES = ["robot-sample-web", "robot-assemble-check-web", "robot-pose-web", "robot-rig-web", "armature-bones-web", "armature-bind-web", "armature-pose-web", "armature-viewport-web", "armature-decimal-input-web", "armature-ui-readback-web"]; +const EXPECTED_CASES = ["robot-sample-web", "robot-assemble-check-web", "robot-pose-web", "robot-rig-web", "armature-bones-web", "armature-bind-web", "armature-pose-web", "armature-viewport-web", "armature-decimal-input-web", "armature-ui-readback-web", "armature-reopen-web"]; const errors = []; const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); diff --git a/tools/web/run-armature-robot-headed.mjs b/tools/web/run-armature-robot-headed.mjs index 4589e6602..3166d74c9 100644 --- a/tools/web/run-armature-robot-headed.mjs +++ b/tools/web/run-armature-robot-headed.mjs @@ -40,6 +40,7 @@ const KNOWN_CASES = [ { id: "armature-viewport-web", title: /\[armature-viewport-web\]/ }, { id: "armature-decimal-input-web", title: /\[armature-decimal-input-web\]/ }, { id: "armature-ui-readback-web", title: /\[armature-ui-readback-web\]/ }, + { id: "armature-reopen-web", title: /\[armature-reopen-web\]/ }, ]; function usage() { diff --git a/web/tests/e2e/armature-engine-headed-production.spec.ts b/web/tests/e2e/armature-engine-headed-production.spec.ts index b42327528..53459b0e4 100644 --- a/web/tests/e2e/armature-engine-headed-production.spec.ts +++ b/web/tests/e2e/armature-engine-headed-production.spec.ts @@ -1,7 +1,7 @@ import crypto from 'node:crypto'; import path from 'node:path'; import { expect, test, type Page, type TestInfo } from '@playwright/test'; -import { applyEngineCommand, installEngineBridge, matrixDistance, snapshotFacts, writeEvidence, type SnapshotFacts } from '../fixtures/armature-robot-workflow'; +import { applyEngineCommand, installEngineBridge, matrixDistance, openBlendReplyCount, snapshotFacts, writeEvidence, type SnapshotFacts } from '../fixtures/armature-robot-workflow'; import { productErrors as sharedProductErrors } from '../fixtures/runtime-console'; /* Engine-level armature cases: the fixture is opened through the product file @@ -355,3 +355,80 @@ test('pose form reads back the applied channels [armature-ui-readback-web]', asy expect(root?.poseScale?.[0] ?? Number.NaN).toBeCloseTo(0.5, 4); expect(productConsole(errors)).toEqual([]); }); + +test('saving to OPFS and reopening keeps the bones, the binding and the pose [armature-reopen-web]', async ({ page }, testInfo) => { + const errors = consoleCollector(page); + await installEngineBridge(page); + const opened = await openFixture(page); + const armatureId = opened.nodes?.[ARMATURE]?.id ?? ''; + await buildChain(page, armatureId); + const bound = await bindTarget(page, armatureId); + + const posed = await applyEngineCommand(page, { type: 'setPoseBoneTransform', objectId: armatureId, boneName: 'Root', translation: [0, 0, 0], rotationEuler: POSE, scale: [1, 1, 1] }); + expect(posed.ok, JSON.stringify(posed)).toBe(true); + const before = await readAfterRevision(page, bound.after.revision ?? 0, [TARGET, ARMATURE]); + const beforeWorld = before.nodes?.[TARGET]?.worldMatrix ?? null; + /* The pose must be visible before the save, otherwise a preserved world + * would prove nothing about the round trip. */ + expect(matrixDistance(bound.after.nodes?.[TARGET]?.worldMatrix, beforeWorld)).toBeGreaterThan(0.01); + + /* Product save path: 文件 → 保存到浏览器 OPFS persists through the storage + * worker; the app records the transaction outcome on main.blender-app. */ + await page.getByRole('button', { name: '文件', exact: true }).click(); + await page.getByRole('menuitem', { name: '保存到浏览器 OPFS', exact: true }).click(); + const app = page.locator('main.blender-app'); + await expect.poll(async () => app.getAttribute('data-user-action-save-status'), { timeout: 240_000, intervals: [500, 1000, 2000] }).toBe('SUCCEEDED'); + const projectId = await app.getAttribute('data-project-id'); + + /* Product reopen path: 文件 → 最近打开 → the project just saved. The open + * replaces Main in the same worker, so the bridge keeps recording. */ + const opensBefore = await openBlendReplyCount(page); + await page.getByRole('button', { name: '文件', exact: true }).click(); + await page.getByRole('menuitem', { name: '最近打开', exact: true }).click(); + await page.getByRole('menuitem', { name: 'pose_constraint_scene.blend', exact: true }).click(); + /* Prove the worker actually reopened the saved bytes: the live Main already + * answers snapshots, so without this the comparison could pass unaided. */ + await expect.poll(() => openBlendReplyCount(page), { timeout: 300_000, intervals: [500, 1000, 2000] }).toBeGreaterThan(opensBefore); + await expect.poll(async () => (await snapshotFacts(page, [TARGET, ARMATURE])).nodes?.[TARGET]?.parentBoneName ?? '', { timeout: 300_000, intervals: [500, 1000, 2000] }).toBe('Root'); + const after = await snapshotFacts(page, [TARGET, ARMATURE]); + + const rootBefore = boneOf(before, 'Root'); + const rootAfter = boneOf(after, 'Root'); + const targetBefore = before.nodes?.[TARGET]; + const targetAfter = after.nodes?.[TARGET]; + const deltas = { + rest: matrixDistance(rootBefore?.restMatrix, rootAfter?.restMatrix), + poseMatrix: matrixDistance(rootBefore?.poseMatrix, rootAfter?.poseMatrix), + poseEuler: matrixDistance(rootBefore?.poseRotationEuler, rootAfter?.poseRotationEuler), + parentBoneMatrix: matrixDistance(targetBefore?.parentBoneMatrix, targetAfter?.parentBoneMatrix), + world: matrixDistance(beforeWorld, targetAfter?.worldMatrix), + }; + const evidence = { + schemaVersion: 1, + case: 'armature-reopen-web', + candidate: CANDIDATE, + save: { route: '文件 → 保存到浏览器 OPFS', projectId }, + reopen: { route: '文件 → 最近打开 → pose_constraint_scene.blend', openBlendReplies: { before: opensBefore, after: await openBlendReplyCount(page) } }, + revisions: { before: before.revision ?? null, after: after.revision ?? null }, + bones: { before: before.boneNames ?? [], after: after.boneNames ?? [] }, + pose: { rotationEuler: rootAfter?.poseRotationEuler ?? null, matrix: rootAfter?.poseMatrix ?? null }, + parent: { parentType: targetAfter?.parentType ?? null, parentBoneName: targetAfter?.parentBoneName ?? null }, + deltas, + console: { productErrors: productConsole(errors), rawErrorCount: errors.length }, + acceptance: 'NOT_CLAIMED', + }; + await attachEvidence(testInfo, 'armature-reopen-web', evidence); + + expect(before.boneNames ?? []).toEqual(expect.arrayContaining(['Root', 'Child'])); + expect(after.boneNames ?? []).toEqual(expect.arrayContaining(['Root', 'Child'])); + expect(targetAfter?.parentType).toBe('BONE'); + expect(targetAfter?.parentBoneName).toBe('Root'); + expect(rootAfter?.poseRotationEuler?.[0] ?? Number.NaN).toBeCloseTo(POSE[0], 4); + expect(rootAfter?.poseRotationEuler?.[2] ?? Number.NaN).toBeCloseTo(POSE[2], 4); + expect(deltas.rest).toBeLessThan(1e-6); + expect(deltas.poseMatrix).toBeLessThan(1e-4); + expect(deltas.poseEuler).toBeLessThan(1e-5); + expect(deltas.parentBoneMatrix).toBeLessThan(1e-4); + expect(deltas.world).toBeLessThan(1e-4); + expect(productConsole(errors)).toEqual([]); +}); diff --git a/web/tests/fixtures/armature-robot-workflow.ts b/web/tests/fixtures/armature-robot-workflow.ts index c191f9e82..bd097430e 100644 --- a/web/tests/fixtures/armature-robot-workflow.ts +++ b/web/tests/fixtures/armature-robot-workflow.ts @@ -10,6 +10,7 @@ export async function installEngineBridge(page: Page): Promise { await page.addInitScript(() => { const state = window as unknown as Record; state.__armatureEvents = []; + state.__armatureOpenReplies = 0; const Native = window.Worker; state.Worker = class extends Native { constructor(url: string | URL, options?: WorkerOptions) { @@ -19,12 +20,17 @@ export async function installEngineBridge(page: Page): Promise { state.__armatureSent = []; const post = this.postMessage.bind(this); (this as unknown as { postMessage: (message: unknown, transfer?: Transferable[]) => void }).postMessage = (message: any, transfer?: Transferable[]) => { - state.__armatureSent.push({ type: message?.command?.type ?? null, payloadType: message?.command?.payload?.type ?? message?.type ?? null }); + state.__armatureSent.push({ requestId: message?.requestId ?? null, type: message?.command?.type ?? null, payloadType: message?.command?.payload?.type ?? message?.type ?? null }); if (state.__armatureSent.length > 64) state.__armatureSent.shift(); post(message, transfer); }; this.addEventListener('message', ({ data }: MessageEvent) => { if (!data?.requestId) return; + /* A reopen case must wait for the worker to report the open; the + * live Main already answers snapshots, so an unproven reopen would + * make the comparison trivially true. */ + const sent = (state.__armatureSent as Array<{ requestId?: string | null; type?: string | null }>).find((item) => item.requestId === data.requestId); + if (sent?.type === 'openBlend' && data.ok) state.__armatureOpenReplies += 1; const snapshot = data.result?.snapshot; state.__armatureEvents.push({ requestId: data.requestId, @@ -93,6 +99,12 @@ export async function snapshotFacts(page: Page, names: string[] = []): Promise; } +/* Successful openBlend replies seen by the captured worker; a reopen case + * polls this so its read-back cannot be answered by the pre-reopen Main. */ +export async function openBlendReplyCount(page: Page): Promise { + return page.evaluate(() => ((window as unknown as Record).__armatureOpenReplies ?? 0)) as Promise; +} + export type EngineCommandResult = { ok: boolean; errorCode: string | null; message: string | null; revision?: number }; /* Engine commands go straight to the worker the app created, exactly like the