import { describe, expect, test } from "bun:test"; import { createInitialAppState } from "@editor/initial-state"; import { documentAddInpaintRegionCommand, documentApplyInpaintRegionMaskOperationCommand, documentRemoveInpaintRegionCommand } from "./inpaint-region"; describe("inpaint region commands", () => { test("persists an AI edit mask independently from layer visibility", () => { const state = stateWithTarget(); const added = documentAddInpaintRegionCommand.execute({ state }, { region: { id: "region", name: "Target AI edit", targetLayerId: "target", maskAssetId: "mask", enabled: true }, maskAsset: { id: "mask", name: "Mask", mimeType: "image/png", source: "black", intrinsicSize: { w: 64, h: 32 } }, }); expect(added.document.inpaintRegions).toHaveLength(1); expect(added.document.artboards[0]?.layers[0]).not.toHaveProperty("layerMask"); expect(added.document.artboards[0]?.layers[0]?.visible).toBe(true); const painted = documentApplyInpaintRegionMaskOperationCommand.execute({ state: added }, { regionId: "region", source: "painted", operation: { type: "paint" } }); expect(painted.document.assets.find((asset) => asset.id === "mask")?.source).toBe("painted"); const removed = documentRemoveInpaintRegionCommand.execute({ state: painted }, { regionId: "region" }); expect(removed.document.inpaintRegions).toHaveLength(0); expect(removed.document.assets.some((asset) => asset.id === "mask")).toBe(false); }); }); function stateWithTarget() { const state = createInitialAppState("Test"); state.document.assets.push({ id: "source", name: "Source", mimeType: "image/png", source: "pixels", intrinsicSize: { w: 64, h: 32 } }); state.document.artboards.push({ id: "board", name: "Board", bounds: { x: 0, y: 0, w: 64, h: 32 }, backgroundColor: "transparent", visible: true, locked: false, layers: [ { id: "target", type: "raster", name: "Target", visible: true, locked: false, opacity: 1, assetId: "source", transform: { position: { x: 0, y: 0 }, scale: { x: 1, y: 1 }, rotation: 0 } }, ] }); return state; }