Files
workinf_Blender_Wasm/web/tests/unit/asset-preview-decode.test.mjs
mes123456 f3e21c6b98
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
chore: checkpoint web Blender migration source, tools and docs
Accumulated uncommitted work: GHOST Web backend, WebGL/GPU web layer,
web workflow engine and protocol, tooling, docs and task records.
Large generated evidence, user-testing archives, build snapshots and
imported test assets remain untracked.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-10 11:18:01 +08:00

102 lines
4.8 KiB
JavaScript

import assert from "node:assert/strict";
import crypto from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";
import test from "node:test";
import ts from "typescript";
import { assertLegacyManifestArtifacts } from "./legacy-manifest-integrity.mjs";
const root = path.resolve(import.meta.dirname, "../../..");
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "asset-preview-decode-unit-"));
const transpile = (sourceName, outputName, replacements = []) => {
const sourcePath = path.join(root, "web/protocol", sourceName);
const result = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), {
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
fileName: sourcePath,
reportDiagnostics: true,
});
assert.deepEqual(result.diagnostics, []);
fs.writeFileSync(path.join(temporary, outputName), replacements.reduce((value, [from, to]) => value.replaceAll(from, to), result.outputText));
};
transpile("asset-preview.ts", "asset-preview.mjs");
transpile("asset-preview-decode.ts", "asset-preview-decode.mjs", [['from "./asset-preview"', 'from "./asset-preview.mjs"']]);
const identityProtocol = await import(pathToFileURL(path.join(temporary, "asset-preview.mjs")));
const decode = await import(pathToFileURL(path.join(temporary, "asset-preview-decode.mjs")));
const identity = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-02B/identity.json"), "utf8"));
const content = fs.readFileSync(path.join(root, "tests/golden/M12-02B/preview.png"));
const manifest = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-02C/manifest.json"), "utf8"));
const buffer = (bytes) => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
const sha256 = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex");
async function identityFor(bytes, changes = {}) {
const base = {
...identity,
content: {
...identity.content,
byteLength: bytes.byteLength,
sha256: sha256(bytes),
...changes,
},
};
delete base.identitySha256;
return identityProtocol.createAssetPreviewIdentity(base);
}
test("M12-02C plans the checked-in PNG before decode", async () => {
assert.equal(manifest.task, "M12-02C");
assert.equal(manifest.enablingTask, true);
assert.equal(manifest.parityStateChange, false);
assert.equal(manifest.nextTask, "M12-02D");
assert.equal(manifest.negativeCaseCount, 8);
assert.deepEqual(decode.ASSET_PREVIEW_DECODE_BUDGET, manifest.budget);
assertLegacyManifestArtifacts(manifest);
assert.deepEqual(await decode.planAssetPreviewDecode(identity, buffer(content)), {
schemaVersion: 1,
identitySha256: identity.identitySha256,
mimeType: "image/png",
width: 8,
height: 8,
pixelCount: 64,
encodedByteLength: 513,
decodedByteLength: 256,
compressionRatio: 256 / 513,
});
});
test("M12-02C rejects byte, hash, MIME, dimension, pixel, and compression budgets before decode", async () => {
const cases = [];
const changedHash = buffer(content); new Uint8Array(changedHash)[changedHash.byteLength - 1] ^= 1;
cases.push([identity, changedHash, "ASSET_SOURCE_HASH_MISMATCH"]);
cases.push([{ ...identity, content: { ...identity.content, byteLength: 512 } }, buffer(content), "ASSET_PREVIEW_IDENTITY_MISMATCH"]);
cases.push([await identityFor(content, { mimeType: "image/webp" }), buffer(content), "ASSET_MANIFEST_INVALID"]);
const wrongDimensions = Buffer.from(content);
wrongDimensions.writeUInt32BE(9, 16);
cases.push([await identityFor(wrongDimensions), buffer(wrongDimensions), "ASSET_MANIFEST_INVALID"]);
const hugeDimensions = Buffer.from(content);
hugeDimensions.writeUInt32BE(4097, 16);
hugeDimensions.writeUInt32BE(4097, 20);
cases.push([await identityFor(hugeDimensions, { width: 4097, height: 4097 }), buffer(hugeDimensions), "ASSET_BUDGET_EXCEEDED"]);
const ratioDimensions = Buffer.from(content);
ratioDimensions.writeUInt32BE(4096, 16);
ratioDimensions.writeUInt32BE(4096, 20);
cases.push([await identityFor(ratioDimensions, { width: 4096, height: 4096 }), buffer(ratioDimensions), "ASSET_BUDGET_EXCEEDED"]);
const oversized = new Uint8Array(decode.ASSET_PREVIEW_DECODE_BUDGET.maxEncodedBytes + 1);
cases.push([await identityFor(oversized, { width: 1, height: 1 }), oversized.buffer, "ASSET_BUDGET_EXCEEDED"]);
const corrupt = Buffer.from(content); corrupt[0] = 0;
cases.push([await identityFor(corrupt), buffer(corrupt), "ASSET_MANIFEST_INVALID"]);
for (const [manifest, bytes, code] of cases) {
await assert.rejects(decode.planAssetPreviewDecode(manifest, bytes), { code });
assert.equal((await decode.planAssetPreviewDecode(identity, buffer(content))).identitySha256, identity.identitySha256);
}
});
test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));