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 fixture = fs.readFileSync(path.join(root, "tests/files/web/generated/M16-GAP-00024-datablock-Text.blend")); const fixtureSha256 = crypto.createHash("sha256").update(fixture).digest("hex"); test("WBV2-P1138-001 preserves Text datablock source through Main recovery and reopen", async ({ page }) => { test.setTimeout(120_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 summary = (snapshot: any, dataId: string) => { const text = snapshot.texts?.find((entry: any) => entry.id === dataId); if (!text) throw new Error(`text missing: ${dataId}`); return { revision: snapshot.revision, source: text.source, lineCount: text.lineCount, byteLength: text.byteLength, sourcePath: text.sourcePath, lines: (text.lines ?? []).map((line: any) => ({ index: line.index, body: line.body, byteLength: line.byteLength })), }; }; try { const opened = await client.openBlend(Uint8Array.from(input.bytes).buffer); const text = opened.snapshot.texts?.find((entry: any) => entry.id === "text:WebGapText"); if (!text) throw new Error("fixture has no Text datablock"); const dataId = text.id as string; const before = summary(opened.snapshot, dataId); const nextSource = `${before.source}\nThird line`; let edited; try { edited = await client.applyCommand({ type: "setTextSource", dataId, source: nextSource } as any); } catch (error: any) { throw Object.assign(error instanceof Error ? error : new Error(String(error)), { textData: text, openedText: opened.snapshot.texts }); } const afterEdit = summary(edited.snapshot, dataId); const undo = await client.applyCommand({ type: "undo" } as any); const afterUndo = summary(undo.snapshot, dataId); const redo = await client.applyCommand({ type: "redo" } as any); const afterRedo = summary(redo.snapshot, dataId); const malformed = await client.applyCommand({ type: "setTextSource", dataId, source: "x".repeat(1_048_577) } as any).then(() => ({ code: "NO_ERROR" })).catch((error: any) => ({ code: error?.code, message: error?.message })); const afterMalformed = summary((await client.snapshot()).snapshot, dataId); const saved = await client.saveBlend(); const reopened = await reopenedClient.openBlend(saved.slice(0)); const afterReopen = summary(reopened.snapshot, dataId); return { fixtureSha256: input.fixtureSha256, dataId, before, afterEdit, afterUndo, afterRedo, malformed, afterMalformed, afterReopen, savedSha256: await crypto.subtle.digest("SHA-256", saved).then((bytes) => Array.from(new Uint8Array(bytes), (byte) => byte.toString(16).padStart(2, "0")).join("")) }; } catch (error: any) { return { error: { code: error?.code, message: error?.message, detail: String(error), json: JSON.stringify(error), textData: error?.textData, openedText: error?.openedText } } as any; } finally { client.terminate(); reopenedClient.terminate(); } }, { bytes: Array.from(fixture), fixtureSha256 }); fs.mkdirSync(path.join(root, "tests/golden/WBV2-P1138-001"), { recursive: true }); fs.writeFileSync(path.join(root, "tests/golden/WBV2-P1138-001/text-source-production.json"), `${JSON.stringify({ schemaVersion: 1, task: "WBV2-P1138-001", operation: "TEXT_SOURCE_MAIN_CHROMIUM", browser: "chromium", queueMutation: false, sourceRnaStructs: { "rna-struct:TextLine": "texts[].lines[]" }, ...result }, null, 2)}\n`); expect(result.error).toBeUndefined(); expect(result.fixtureSha256).toBe(fixtureSha256); expect(result.afterEdit.revision).toBe(result.before.revision + 1); expect(result.afterEdit.source).toContain("Third line"); expect(result.before.lines.length).toBe(result.before.lineCount); expect(result.afterEdit.lines.length).toBe(result.afterEdit.lineCount); expect(result.afterEdit.lines.map((line: any) => line.body).join("\n")).toBe(result.afterEdit.source); expect(result.afterEdit.lines.every((line: any, index: number) => line.index === index && line.byteLength === new TextEncoder().encode(line.body).byteLength)).toBe(true); expect(result.afterEdit.lines.some((line: any) => line.body === "Third line")).toBe(true); expect(result.afterUndo.source).toBe(result.before.source); expect(result.afterRedo.source).toBe(result.afterEdit.source); expect(result.malformed.code).not.toBe("NO_ERROR"); expect(result.afterMalformed).toEqual(result.afterRedo); expect(result.afterReopen.source).toBe(result.afterRedo.source); expect(result.afterReopen.lineCount).toBe(result.afterRedo.lineCount); expect(result.afterReopen.lines).toEqual(result.afterRedo.lines); expect(result.afterReopen.revision).toBe(1); expect(result.savedSha256).not.toBe(fixtureSha256); });