Files
workinf_Blender_Wasm/web/tests/e2e/wbv2-material-slot-production.spec.ts
mes123456 241a8e7107
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
fix(web): recover material slot remove batch contracts
2026-09-05 06:44:42 +08:00

62 lines
4.8 KiB
TypeScript

import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { expect, test } from "@playwright/test";
const root = path.resolve(import.meta.dirname, "../../..");
const fixturePath = process.env.WBV2_BATCH_FIXTURE ?? "tests/files/web/basic_scene.blend";
const fixture = fs.readFileSync(path.join(root, fixturePath));
const fixtureSha256 = crypto.createHash("sha256").update(fixture).digest("hex");
const task = process.env.WBV2_BATCH_TASK ?? "WBV2-P1100-003";
const productionPath = process.env.WBV2_BATCH_PRODUCTION ?? `tests/golden/${task}/material-slot-production.json`;
const sourceUnits = JSON.parse(process.env.WBV2_BATCH_UNITS ?? "[\"operator:OBJECT_OT_material_slot_add\"]") as string[];
const slotOperation = process.env.WBV2_BATCH_SLOT_OPERATION ?? "ADD";
test("WBV2-P1100-003 preserves addMaterialSlot through Main recovery and reopen", async ({ page }) => {
test.setTimeout(180_000);
await page.goto("/");
const result = await page.evaluate(async (input) => {
const { WebEngineClient } = await import("/src/engine-client/WebEngineClient.ts");
const client = new WebEngineClient({ timeoutMs: 60_000 });
const reopenedClient = new WebEngineClient({ timeoutMs: 60_000 });
const materialNames = (snapshot: any) => snapshot.materials.map((material: any) => material.name);
try {
const opened = await client.openBlend(Uint8Array.from(input.bytes).buffer);
const object = opened.snapshot.nodes.find((node: any) => node.type === "MESH");
if (!object) throw new Error("basic scene has no mesh object");
const before = materialNames(opened.snapshot);
const added = await client.applyCommand({ type: "addMaterialSlot", objectId: object.id, name: "WebSlotMaterial" } as any);
const removed = input.slotOperation === "REMOVE" ? await client.applyCommand({ type: "removeMaterialSlot", objectId: object.id, slotIndex: (added.snapshot.nodes.find((node: any) => node.id === object.id)?.materialSlotIds?.length ?? 1) - 1 } as any) : added;
const afterAdd = materialNames(removed.snapshot);
const undo = await client.applyCommand({ type: "undo" } as any);
const redo = await client.applyCommand({ type: "redo" } as any);
const malformed = await client.applyCommand({ type: input.slotOperation === "REMOVE" ? "removeMaterialSlot" : "addMaterialSlot", objectId: "object:missing", ...(input.slotOperation === "REMOVE" ? { slotIndex: 0 } : { name: "Bad" }) } as any).then(() => ({ code: "NO_ERROR" })).catch((error: any) => ({ code: error?.code, message: error?.message }));
const afterMalformed = await client.snapshot();
const saved = await client.saveBlend();
const reopened = await reopenedClient.openBlend(saved.slice(0));
return { fixtureSha256: input.fixtureSha256, objectId: object.id, before, afterAdd, undo: materialNames(undo.snapshot), redo: materialNames(redo.snapshot), malformed, malformedRevisionPreserved: afterMalformed.snapshot.revision === redo.snapshot.revision, reopened: materialNames(reopened.snapshot), beforeRevision: opened.snapshot.revision, addRevision: added.snapshot.revision, undoRevision: undo.snapshot.revision, redoRevision: redo.snapshot.revision, reopenedRevision: reopened.snapshot.revision, savedSha256: await crypto.subtle.digest("SHA-256", saved).then((hash) => Array.from(new Uint8Array(hash), (byte) => byte.toString(16).padStart(2, "0")).join("")) };
}
catch (error: any) { return { error: { code: error?.code, message: error?.message, detail: String(error) } } as any; }
finally { client.terminate(); reopenedClient.terminate(); }
}, { bytes: Array.from(fixture), fixtureSha256, slotOperation });
fs.mkdirSync(path.dirname(path.join(root, productionPath)), { recursive: true });
fs.writeFileSync(path.join(root, productionPath), `${JSON.stringify({ schemaVersion: 1, task, operation: `MATERIAL_SLOT_${slotOperation}_MAIN_CHROMIUM`, browser: "chromium", queueMutation: false, fixture: fixturePath, fixtureSha256, sourceUnits, ...result }, null, 2)}\n`);
expect(result.error).toBeUndefined();
expect(result.fixtureSha256).toBe(fixtureSha256);
if (slotOperation === "REMOVE") expect(result.afterAdd).not.toContain("WebSlotMaterial");
else expect(result.afterAdd).toContain("WebSlotMaterial");
expect(result.addRevision).toBe(result.beforeRevision + 1);
if (slotOperation === "REMOVE") {
expect(result.undo).toContain("WebSlotMaterial");
expect(result.redo).not.toContain("WebSlotMaterial");
} else {
expect(result.undo).toEqual(result.before);
expect(result.redo).toEqual(result.afterAdd);
}
expect(result.malformed.code).not.toBe("NO_ERROR");
expect(result.malformedRevisionPreserved).toBe(true);
expect(result.reopened).toEqual(result.afterAdd);
expect(result.reopenedRevision).toBe(1);
expect(result.savedSha256).not.toBe(fixtureSha256);
});