import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; import { defaultSidebarOpen, readStoredSidebarOpen, shouldHandleSidebarShortcut, sidebarViewportForWidth, writeStoredSidebarOpen, } from "../src/lib/sidebar-state.ts"; import { beginSessionPageLoad, mergeSessionPage } from "../src/lib/session-groups.ts"; import { homeSurface } from "./home-surface.ts"; test("classifies the exact sidebar breakpoints", () => { assert.equal(sidebarViewportForWidth(0), "mobile"); assert.equal(sidebarViewportForWidth(767), "mobile"); assert.equal(sidebarViewportForWidth(768), "tablet"); assert.equal(sidebarViewportForWidth(1023), "tablet"); assert.equal(sidebarViewportForWidth(1024), "desktop"); }); test("uses the approved reload defaults", () => { assert.equal(defaultSidebarOpen("mobile"), false); assert.equal(defaultSidebarOpen("tablet"), false); assert.equal(defaultSidebarOpen("desktop"), true); }); test("accepts Command or Control B only outside editable controls", () => { const shortcut = { key: "b", metaKey: true, ctrlKey: false, altKey: false, shiftKey: false }; assert.equal(shouldHandleSidebarShortcut({ ...shortcut, target: null }), true); assert.equal(shouldHandleSidebarShortcut({ ...shortcut, target: { tagName: "TEXTAREA" } }), false); assert.equal(shouldHandleSidebarShortcut({ ...shortcut, target: { isContentEditable: true } }), false); assert.equal(shouldHandleSidebarShortcut({ ...shortcut, key: "k", target: null }), false); }); test("renaming a session does not bump updatedAt", () => { assert.match(homeSurface, /const nextSession = \{ \.\.\.session, title \};/); assert.doesNotMatch(homeSurface, /const nextSession = \{ \.\.\.session, title, updatedAt: timestamp\(\) \};/); }); test("loadMoreSessions merges by id and only starts one in-flight request", () => { const inFlight = { current: false }; assert.equal(beginSessionPageLoad(inFlight, "cursor-1"), true); assert.equal(beginSessionPageLoad(inFlight, "cursor-1"), false); const merged = mergeSessionPage( [{ id: "keep", updatedAt: 900 }], [{ id: "keep", updatedAt: 1 }, { id: "next", updatedAt: 2 }], ); assert.deepEqual(merged.map((item) => item.id), ["keep", "next"]); assert.equal(merged[0]?.updatedAt, 900); }); /* T5 ยท the collapse state is remembered across pages (TASK-sidebar-unify D5). localStorage, not the shadcn `sidebar_state` cookie: `/`, `/chart` and `/ephemeris` are all static routes, and reading a cookie on the server would opt every one of them out of static rendering. */ function fakeStorage(initial?: string) { let value = initial; return { getItem: () => value ?? null, setItem: (_key: string, next: string) => { value = next; }, read: () => value, }; } test("a collapsed desktop sidebar is still collapsed after a remount", () => { const storage = fakeStorage(); writeStoredSidebarOpen("desktop", false, storage); assert.equal(storage.read(), "false"); // What SidebarProvider resolves on the next page: stored wins over the // breakpoint default, which for desktop would have been `true`. assert.equal(readStoredSidebarOpen("desktop", storage) ?? defaultSidebarOpen("desktop"), false); writeStoredSidebarOpen("desktop", true, storage); assert.equal(readStoredSidebarOpen("desktop", storage) ?? defaultSidebarOpen("desktop"), true); // Nothing stored, or something unreadable: the breakpoint default stands. const empty = fakeStorage(); assert.equal(readStoredSidebarOpen("desktop", empty), null); assert.equal(readStoredSidebarOpen("tablet", fakeStorage("banana")), null); assert.equal(readStoredSidebarOpen("tablet", empty) ?? defaultSidebarOpen("tablet"), false); }); test("the mobile drawer is not remembered, read or written", () => { // Reopening a phone drawer on every navigation is not a preference anyone set. const storage = fakeStorage(); writeStoredSidebarOpen("mobile", true, storage); assert.equal(storage.read(), undefined); assert.equal(readStoredSidebarOpen("mobile", fakeStorage("true")), null); assert.equal(readStoredSidebarOpen("mobile", fakeStorage("true")) ?? defaultSidebarOpen("mobile"), false); // Unavailable storage (private mode, blocked site data) degrades, never throws. assert.equal(readStoredSidebarOpen("desktop", null), null); assert.doesNotThrow(() => writeStoredSidebarOpen("desktop", true, null)); // And the provider applies it in the same effect that applied the breakpoint // default, so a stored value costs no frame the default did not already cost. const provider = readFileSync(new URL("../src/components/ui/sidebar.tsx", import.meta.url), "utf8"); assert.match(provider, /readStoredSidebarOpen\(viewport\) \?\? defaultSidebarOpen\(viewport\)/); assert.match(provider, /writeStoredSidebarOpen\(viewport, nextOpen\)/); });