import { describe, expect, test } from "bun:test"; import { createInitialAppState } from "@editor/initial-state"; import { neutralColorAdjustment } from "@core/adjustment-layer"; import { documentAddArtboardCommand, documentAddAdjustmentLayerCommand, documentSetAdjustmentCommand, documentDuplicateLayerCommand, documentMoveLayerCommand } from "./document"; const transform = { position: { x: 0, y: 0 }, scale: { x: 1, y: 1 }, rotation: 0 }; function stateWithAdjustment() { let state = documentAddArtboardCommand.execute({ state: createInitialAppState("test") }, { id: "board", name: "Board", bounds: { x: 0, y: 0, w: 10, h: 10 } }); state = documentAddAdjustmentLayerCommand.execute({ state }, { artboardId: "board", layer: { id: "adjust", type: "adjustment", name: "Color", visible: true, locked: false, opacity: 1, transform, adjustment: neutralColorAdjustment } }); return state; } describe("adjustment commands", () => { test("creates and selects a persistent adjustment node", () => { const state = stateWithAdjustment(); expect(state.document.artboards[0]?.layers[0]?.type).toBe("adjustment"); expect(state.editor.selection.layerIds).toEqual(["adjust"]); }); test("validates edits and preserves locked layers", () => { const state = stateWithAdjustment(); const changed = documentSetAdjustmentCommand.execute({ state }, { layerId: "adjust", adjustment: { ...neutralColorAdjustment, brightness: .25 } }); const invalid = documentSetAdjustmentCommand.execute({ state: changed }, { layerId: "adjust", adjustment: { ...neutralColorAdjustment, contrast: 2 } }); expect((changed.document.artboards[0]?.layers[0] as { adjustment: { brightness: number } }).adjustment.brightness).toBe(.25); expect(invalid).toBe(changed); }); test("duplicates and moves adjustment nodes like tree siblings", () => { let state = stateWithAdjustment(); state = documentDuplicateLayerCommand.execute({ state }, { layerId: "adjust", idByLayerId: { adjust: "copy" } }); state = documentMoveLayerCommand.execute({ state }, { layerId: "copy", toArtboardId: "board", toIndex: 0 }); expect(state.document.artboards[0]?.layers.map((layer) => [layer.id, layer.type])).toEqual([["copy", "adjustment"], ["adjust", "adjustment"]]); }); test("rejects nested creation, movement, and grouping to preserve exact scope", () => { const state = stateWithAdjustment(); const nested = documentAddAdjustmentLayerCommand.execute({ state }, { artboardId: "board", parentGroupId: "group", layer: { id: "nested", type: "adjustment", name: "Nested", visible: true, locked: false, opacity: 1, transform, adjustment: neutralColorAdjustment } }); const moved = documentMoveLayerCommand.execute({ state }, { layerId: "adjust", toArtboardId: "board", toParentGroupId: "group", toIndex: 0 }); expect(nested).toBe(state); expect(moved).toBe(state); }); });