import { describe, expect, it, vi } from "vitest"; import { defineToolcraft } from "../schema/define-toolcraft"; import { getLegacyToolcraftSectionCollapseEntryKey, getLegacyToolcraftSectionCollapseStorageKey, readLegacyToolcraftSectionCollapse, } from "./persistence-legacy-section-collapse"; function createSchema() { return defineToolcraft({ canvas: { enabled: true }, panels: { controls: { sections: [ { controls: { opacity: { defaultValue: 75, target: "appearance.opacity", type: "slider", }, }, id: "appearance", title: "Appearance", }, ], title: "Controls", }, }, }); } describe("legacy controls section collapse persistence", () => { it("reads the old key once and maps only known entries to stable section ids", () => { const schema = createSchema(); const sections = schema.panels.controls?.sections ?? []; const sectionIndex = sections.findIndex((section) => section.id === "appearance"); const section = sections[sectionIndex]; const storageKey = getLegacyToolcraftSectionCollapseStorageKey(schema); const entryKey = getLegacyToolcraftSectionCollapseEntryKey( section!, sectionIndex, ); const getItem = vi.fn((key: string) => key === storageKey ? JSON.stringify({ collapsed: [entryKey, "unknown:4:target", 12], version: 1, }) : null, ); const setItem = vi.fn(); expect( readLegacyToolcraftSectionCollapse(schema, { getItem, setItem }), ).toEqual({ controls: { collapsedSections: { appearance: true }, }, }); expect(getItem).toHaveBeenCalledTimes(1); expect(setItem).not.toHaveBeenCalled(); }); it("ignores invalid payloads without writing", () => { const schema = createSchema(); const getItem = vi.fn(() => JSON.stringify({ collapsed: "appearance", version: 1 })); const setItem = vi.fn(); expect(readLegacyToolcraftSectionCollapse(schema, { getItem, setItem })).toBeUndefined(); expect(getItem).toHaveBeenCalledTimes(1); expect(setItem).not.toHaveBeenCalled(); }); });