import { describe, expect, it } from "vitest"; import { createToolcraftState } from "./create-template-state"; import { createToolcraftPersistenceSnapshot, parseToolcraftPersistenceSnapshot } from "../composition/public-persistence"; import { readPanels } from "./persistence-reader-panels"; import { createPersistentTestSchema } from "./persistence.test-support"; import { toolcraftReducer } from "../composition/public-state"; describe("panel scroll preference", () => { it("round-trips the offset together with collapsed sections", () => { const schema = createPersistentTestSchema(); const sectionId = schema.panels.controls!.sections[0]!.id; const state = createToolcraftState(schema, { panels: { controls: { scrollTop: 412.5, collapsedSections: { [sectionId]: true } } }, }); const snapshot = createToolcraftPersistenceSnapshot(state, schema.persistence); const restored = createToolcraftState(schema, parseToolcraftPersistenceSnapshot(schema, JSON.stringify(snapshot))); expect(restored.panels.controls).toMatchObject({ scrollTop: 412.5, collapsedSections: { [sectionId]: true }, }); }); it.each([undefined, -1, NaN, Infinity, "120", null])("ignores invalid or absent saved offsets: %s", (scrollTop) => { const schema = createPersistentTestSchema(); const restored = readPanels(schema, { controls: { scrollTop, offset: { x: 0, y: 0 } } }); expect(restored?.controls?.scrollTop).toBeUndefined(); }); it("updates without history and survives Reset, undo, redo and offset reset", () => { let state = createToolcraftState(createPersistentTestSchema()); const history = state.history; state = toolcraftReducer(state, { type: "panels.update", panelId: "controls", patch: { scrollTop: 325 } }); expect(state.panels.controls.scrollTop).toBe(325); expect(state.history).toBe(history); expect(toolcraftReducer(state, { type: "panels.update", panelId: "controls", patch: { scrollTop: 325 } })).toBe(state); state = toolcraftReducer(state, { type: "controls.setValue", target: "generation.prompt", value: "Changed prompt" }); expect(state.history.undo.length).toBeGreaterThan(0); for (const type of ["controls.reset", "history.undo", "history.redo"] as const) { state = toolcraftReducer(state, { type }); expect(state.panels.controls.scrollTop).toBe(325); } state = toolcraftReducer(state, { type: "panels.resetOffset", panelId: "controls" }); expect(state.panels.controls.scrollTop).toBe(325); }); it.each([-1, NaN, Infinity])("rejects invalid live offsets: %s", (scrollTop) => { const state = createToolcraftState(createPersistentTestSchema()); expect(toolcraftReducer(state, { type: "panels.update", panelId: "controls", patch: { scrollTop } })).toBe(state); }); });