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"; const root = path.resolve(import.meta.dirname, "../../.."); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "library-append-wasm-unit-")); const sourcePath = path.join(root, "web/protocol/library-main-append.ts"); const identityPath = path.join(root, "web/protocol/library-operation-identity.ts"); const identityTranspiled = ts.transpileModule(fs.readFileSync(identityPath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: identityPath, reportDiagnostics: true, }); assert.deepEqual(identityTranspiled.diagnostics, []); fs.writeFileSync(path.join(temporary, "library-operation-identity.mjs"), identityTranspiled.outputText); const transpiled = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true, }); assert.deepEqual(transpiled.diagnostics, []); fs.writeFileSync(path.join(temporary, "library-main-append.mjs"), transpiled.outputText.replaceAll("./library-operation-identity\"", "./library-operation-identity.mjs\"")); const append = await import(pathToFileURL(path.join(temporary, "library-main-append.mjs"))); const manifest = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-03N/manifest.json"), "utf8")); const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(path.join(root, file))).digest("hex"); const closure = { object: "Object/M12 Append Object", mesh: "Mesh/M12 Append Mesh", material: "Material/M12 Append Material", image: "Image/M12 Append Image", }; test("M12-03N binds the independent append WASM command", () => { assert.equal(manifest.task, "M12-03N"); assert.equal(manifest.nextTask, "M12-04A"); assert.equal(sha256("web/protocol/library-main-append.ts"), manifest.artifacts.appendWasmProtocol.sha256); }); test("M12-03N validates the append closure and one-transaction receipt in the WASM lane", async () => { assert.deepEqual(append.parseLibraryAppendClosure(closure), closure); const request = { baseRevision: 11, binding: { source: { sourceLibraryId: "library:" + "a".repeat(64) }, sourceDataBlockId: closure.object, dependencyClosureSha256: await append.computeLibraryAppendClosureSha256(closure), }, expectedClosure: closure, }; const receipt = append.createLibraryMainAppendReceipt(request, 12); assert.equal(receipt.operation, "APPEND"); assert.equal(receipt.transactionCount, 1); assert.equal(receipt.baseRevision, 11); assert.equal(receipt.nextRevision, 12); assert.equal(receipt.mapping.length, 4); assert(receipt.mapping.every((item) => item.owner === "LOCAL_MAIN" && item.readOnly === false && item.source === item.local)); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));