77 lines
3.6 KiB
TypeScript
77 lines
3.6 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/m12_glb_main_v1/pbr.blend";
|
|
const fixture = fs.readFileSync(path.join(root, fixturePath));
|
|
const fixtureSha256 = crypto.createHash("sha256").update(fixture).digest("hex");
|
|
const expectedTypes = JSON.parse(process.env.WBV2_BATCH_TYPES ?? "[\"PRINCIPLED\"]") as string[];
|
|
const sourceUnits = JSON.parse(process.env.WBV2_BATCH_UNITS ?? "[]") as string[];
|
|
const task = process.env.WBV2_BATCH_TASK ?? "WBV2-P1113-000";
|
|
const productionPath = process.env.WBV2_BATCH_PRODUCTION ?? `tests/golden/${task}/shader-main-reader-production.json`;
|
|
|
|
test("WBV2-P1113-000 reads Shader nodes from Blender Main without JS simulation", async ({ page }) => {
|
|
test.setTimeout(120_000);
|
|
await page.goto("/");
|
|
const result = await page.evaluate(async (bytes) => {
|
|
const { WebEngineClient } = await import("/src/engine-client/WebEngineClient.ts");
|
|
const summarize = (snapshot: any) => snapshot.materials.map((material: any) => ({
|
|
id: material.id,
|
|
nodes: (material.nodes ?? []).map((node: any) => ({ type: node.type, name: node.name })),
|
|
links: material.links ?? [],
|
|
}));
|
|
const client = new WebEngineClient({ timeoutMs: 60_000 });
|
|
const reopenedClient = new WebEngineClient({ timeoutMs: 60_000 });
|
|
try {
|
|
const opened = await client.openBlend(Uint8Array.from(bytes).buffer);
|
|
const materials = summarize(opened.snapshot);
|
|
if (!materials.some((material: any) => material.nodes.length > 0)) {
|
|
throw new Error("Blender Main has no Principled shader node");
|
|
}
|
|
const saved = await client.saveBlend();
|
|
const reopened = await reopenedClient.openBlend(saved.slice(0));
|
|
return {
|
|
browser: "chromium",
|
|
fixtureSha256: await crypto.subtle.digest("SHA-256", Uint8Array.from(bytes)).then((hash) => Array.from(new Uint8Array(hash), (value) => value.toString(16).padStart(2, "0")).join("")),
|
|
materials,
|
|
reopenedMaterials: summarize(reopened.snapshot),
|
|
openedRevision: opened.snapshot.revision,
|
|
reopenedRevision: reopened.snapshot.revision,
|
|
};
|
|
}
|
|
finally {
|
|
client.terminate();
|
|
reopenedClient.terminate();
|
|
}
|
|
}, Array.from(fixture));
|
|
fs.mkdirSync(path.dirname(path.join(root, productionPath)), { recursive: true });
|
|
fs.writeFileSync(path.join(root, productionPath), `${JSON.stringify({
|
|
schemaVersion: 1,
|
|
task,
|
|
operation: "SHADER_MAIN_READER_CHROMIUM",
|
|
browser: "chromium",
|
|
queueMutation: false,
|
|
fixture: fixturePath,
|
|
fixtureSha256,
|
|
sourceUnits,
|
|
expectedTypes,
|
|
sourceNodes: {
|
|
"node:ShaderNodeBsdfPrincipled": "material.nodes[type=PRINCIPLED]",
|
|
"node:ShaderNodeOutputMaterial": "material.nodes[type=OUTPUT]",
|
|
},
|
|
...result,
|
|
}, null, 2)}\n`);
|
|
expect(result.fixtureSha256).toBe(fixtureSha256);
|
|
expect(result.materials).toEqual(result.reopenedMaterials);
|
|
const observedTypes = new Set(result.materials.flatMap((material: any) => material.nodes.map((node: any) => node.type)));
|
|
if (sourceUnits.length > 0) {
|
|
expect(result.materials.some((material: any) => material.nodes.length > 0)).toBe(true);
|
|
expect(result.materials.flatMap((material: any) => material.nodes).map((node: any) => node.name)).toEqual(expect.arrayContaining(expectedTypes.map((type) => `WBV2 ${type}`)));
|
|
} else {
|
|
expect(observedTypes.has("PRINCIPLED")).toBe(true);
|
|
}
|
|
expect(result.reopenedRevision).toBe(1);
|
|
});
|