import { mediaSourceModule } from "../modules/built-ins/media-source/media-source-module"; import { describe, expect, it } from "vitest"; import { defineToolcraft } from "../schema/define-toolcraft"; import { createToolcraftState } from "./create-template-state"; describe("createToolcraftState image geometry", () => { it("initializes predefined image media with distinct source and scene geometry", () => { const app = defineToolcraft({ base: { identity: { id: "runtime-test", title: "Toolcraft fixture", }, canvas: { enabled: true, size: { height: 720, unit: "px", width: 1280 }, upload: true, }, media: { defaultAssets: [ { ingressPolicy: "prepared-source", position: { x: 0, y: 0 }, sourceSize: { height: 1080, unit: "px", width: 1920 }, dataUrl: "data:image/png;base64,AAAA", fileName: "background.png", id: "default-background", layerId: "default-background-layer", mimeType: "image/png", sourceTarget: "media.background", }, ], }, panels: { controls: { sections: [ { id: "fixture-section-1", controls: { background: { applicability: { mode: "always" as const }, defaultValue: null, target: "media.background", type: "fileDrop", }, }, }, ], title: "Controls", }, }, }, modules: [mediaSourceModule()], }); const state = createToolcraftState(app); expect(state.mediaAssets).toEqual([ expect.objectContaining({ fileName: "background.png", id: "default-background", layerId: "default-background-layer", mimeType: "image/png", lifecycle: "restoring", resourceRef: expect.stringMatching(/^media:image:sha256:/u), position: { x: 0, y: 0 }, size: { height: 720, unit: "px", width: 1280 }, sourceSize: { height: 1080, unit: "px", width: 1920 }, sourceTarget: "media.background", }), ]); expect(state.mediaAssets[0]).not.toHaveProperty("dataUrl"); expect(state.layers).toEqual([ expect.objectContaining({ id: "default-background-layer", name: "background", visible: true, }), ]); expect(state.selectedLayerId).toBe("default-background-layer"); }); it("preserves explicit canonical image scene and source geometry", () => { const app = defineToolcraft({ base: { identity: { id: "runtime-test", title: "Toolcraft fixture", }, canvas: { enabled: true, size: { height: 720, unit: "px", width: 1280 }, }, panels: {}, }, modules: [], }); const state = createToolcraftState(app, { mediaAssets: [ { assetKind: "image", fileName: "authored.png", id: "authored", layerId: "authored-layer", lifecycle: "ready", mimeType: "image/png", position: { x: 90, y: -30 }, resourceRef: "media:image:authored", size: { height: 180, unit: "px", width: 320 }, sourceSize: { height: 900, unit: "px", width: 1600 }, }, ], }); expect(state.mediaAssets[0]).toMatchObject({ position: { x: 90, y: -30 }, size: { height: 180, unit: "px", width: 320 }, sourceSize: { height: 900, unit: "px", width: 1600 }, }); }); it("preserves canonical default image scene geometry independently of source geometry", () => { const app = defineToolcraft({ base: { identity: { id: "runtime-test", title: "Toolcraft fixture", }, canvas: { enabled: true, size: { height: 720, unit: "px", width: 1280 }, }, media: { defaultAssets: [ { dataUrl: "data:image/png;base64,AAAA", fileName: "authored-default.png", ingressPolicy: "canonical-runtime", position: { x: 90, y: -30 }, size: { height: 180, unit: "px", width: 320 }, sourceSize: { height: 900, unit: "px", width: 1600 }, }, ], }, panels: {}, }, modules: [mediaSourceModule()], }); expect(createToolcraftState(app).mediaAssets[0]).toMatchObject({ position: { x: 90, y: -30 }, size: { height: 180, unit: "px", width: 320 }, sourceSize: { height: 900, unit: "px", width: 1600 }, }); }); });