import { beforeEach, describe, expect, it } from "vitest"; import { DEFAULT_FILE_TREE_WIDTH, FILE_TREE_WIDTH_KEY, MAX_FILE_TREE_WIDTH, MIN_FILE_TREE_WIDTH, clampFileTreeWidth, openEditor, openPreview, resetFileTreeWidth, resetWorkspace, setFileTreeWidth, showTab, toggleWorkspace, useWorkspace, } from "./workspace"; describe("workspace store", () => { beforeEach(() => { localStorage.clear(); resetWorkspace(); }); it("starts closed on the preview tab", () => { const state = useWorkspace.getState(); expect(state.open).toBe(false); expect(state.tab).toBe("preview"); expect(state.previewPath).toBeNull(); expect(state.editorPath).toBeNull(); }); it("toggles the panel", () => { toggleWorkspace(); expect(useWorkspace.getState().open).toBe(true); toggleWorkspace(); expect(useWorkspace.getState().open).toBe(false); }); it("opens the panel when a tab is shown", () => { showTab("terminal"); const state = useWorkspace.getState(); expect(state.open).toBe(true); expect(state.tab).toBe("terminal"); }); it("openPreview targets the preview tab with the file", () => { openPreview("/work/readme.md"); const state = useWorkspace.getState(); expect(state).toMatchObject({ open: true, tab: "preview", previewPath: "/work/readme.md", }); }); it("openEditor targets the editor tab and keeps the preview file", () => { openPreview("/work/a.md"); openEditor("/work/b.go"); const state = useWorkspace.getState(); expect(state).toMatchObject({ open: true, tab: "editor", editorPath: "/work/b.go", previewPath: "/work/a.md", }); }); it("starts with the default file tree width", () => { expect(useWorkspace.getState().fileTreeWidth).toBe(DEFAULT_FILE_TREE_WIDTH); }); it("setFileTreeWidth applies the width and remembers it", () => { setFileTreeWidth(320); expect(useWorkspace.getState().fileTreeWidth).toBe(320); expect(localStorage.getItem(FILE_TREE_WIDTH_KEY)).toBe("320"); }); it("setFileTreeWidth keeps the width within the bounds", () => { setFileTreeWidth(10); expect(useWorkspace.getState().fileTreeWidth).toBe(MIN_FILE_TREE_WIDTH); setFileTreeWidth(5000); expect(useWorkspace.getState().fileTreeWidth).toBe(MAX_FILE_TREE_WIDTH); }); it("resetFileTreeWidth goes back to the default", () => { setFileTreeWidth(400); resetFileTreeWidth(); expect(useWorkspace.getState().fileTreeWidth).toBe(DEFAULT_FILE_TREE_WIDTH); expect(localStorage.getItem(FILE_TREE_WIDTH_KEY)).toBe( String(DEFAULT_FILE_TREE_WIDTH), ); }); }); describe("clampFileTreeWidth", () => { it("rounds and keeps in-range values", () => { expect(clampFileTreeWidth(300.6)).toBe(301); }); it("clamps below the minimum and above the maximum", () => { expect(clampFileTreeWidth(0)).toBe(MIN_FILE_TREE_WIDTH); expect(clampFileTreeWidth(-50)).toBe(MIN_FILE_TREE_WIDTH); expect(clampFileTreeWidth(10_000)).toBe(MAX_FILE_TREE_WIDTH); }); it("falls back to the default for NaN and infinities", () => { expect(clampFileTreeWidth(Number.NaN)).toBe(DEFAULT_FILE_TREE_WIDTH); expect(clampFileTreeWidth(Number.POSITIVE_INFINITY)).toBe( DEFAULT_FILE_TREE_WIDTH, ); }); });