import assert from "node:assert/strict"; import test from "node:test"; import { DOCK_STORAGE_KEY, LEGACY_DOCK_STORAGE_KEY, MIN_H, MIN_W, barPlacement, defaultLayout, fitRect, handleOrientation, handleValue, keyboardCover, keyStep, legacyLayout, migrateDockStorage, normalizeLayout, phoneBarBottom, pinnedWidth, resizeRect, snapEdge, windowWidth, type DockLayout, type ResizeDir, type StorageLike, } from "../src/chat/ChatDockLayout.ts"; class MemoryStorage implements StorageLike { readonly values = new Map(); getItem(key: string): string | null { return this.values.get(key) ?? null; } setItem(key: string, value: string): void { this.values.set(key, value); } removeItem(key: string): void { this.values.delete(key); } } const VW = 1280; const VH = 800; test("a stored layout is validated and clamped into the viewport", () => { const cases: Array<[unknown, DockLayout]> = [ [null, defaultLayout(VW, VH)], [{}, defaultLayout(VW, VH)], [ { mode: "right", x: 4000, y: -50, w: 10, h: 99999 }, { mode: "right", x: 1280 - MIN_W - 8, y: 8, w: MIN_W, h: 800 - 16 }, ], [ { mode: "float", x: 100, y: 120, w: 440, h: 560 }, { mode: "float", x: 100, y: 120, w: 440, h: 560 }, ], [ { mode: "sideways", x: "10", y: Number.NaN, w: 440, h: 560 }, { ...defaultLayout(VW, VH), w: 440, h: 560 }, ], ]; for (const [raw, want] of cases) { assert.deepEqual(normalizeLayout(raw, VW, VH), want, JSON.stringify(raw)); } }); test("fitRect keeps the whole rectangle inside the viewport at any size", () => { const cases: Array<[[number, number, number, number], [number, number, number, number]]> = [ [[-100, -100, 440, 560], [8, 8, 440, 560]], [[1200, 700, 440, 560], [1280 - 440 - 8, 800 - 560 - 8, 440, 560]], [[0, 0, 99999, 99999], [8, 8, 720, 784]], [[0, 0, 100, 100], [8, 8, MIN_W, MIN_H]], ]; for (const [[x, y, w, h], want] of cases) { assert.deepEqual(fitRect({ x, y, w, h }, VW, VH), { x: want[0], y: want[1], w: want[2], h: want[3] }); } // A viewport smaller than the minimum still yields a usable rectangle. assert.deepEqual(fitRect({ x: 0, y: 0, w: 500, h: 500 }, 300, 200), { x: 8, y: 8, w: 284, h: 184 }); }); test("all eight edges and corners resize the rectangle in their own direction", () => { const origin = { x: 100, y: 100, w: 400, h: 300 }; const cases: Array<[ResizeDir, number, number, [number, number, number, number]]> = [ ["e", 20, 0, [100, 100, 420, 300]], ["e", -20, 0, [100, 100, 380, 300]], ["s", 0, 30, [100, 100, 400, 330]], ["w", 20, 0, [120, 100, 380, 300]], ["w", -20, 0, [80, 100, 420, 300]], ["n", 0, 20, [100, 120, 400, 280]], ["n", 0, -20, [100, 80, 400, 320]], ["se", 10, 10, [100, 100, 410, 310]], ["nw", -10, -10, [90, 90, 410, 310]], ["ne", 10, -10, [100, 90, 410, 310]], ["sw", -10, 10, [90, 100, 410, 310]], ]; for (const [dir, dx, dy, want] of cases) { assert.deepEqual( resizeRect(origin, dir, dx, dy, VW, VH), { x: want[0], y: want[1], w: want[2], h: want[3] }, dir, ); } }); test("resize never collapses the window below the minimum or pushes it off screen", () => { const origin = { x: 100, y: 100, w: 400, h: 300 }; assert.deepEqual(resizeRect(origin, "e", -9999, 0, VW, VH), { x: 100, y: 100, w: MIN_W, h: 300 }); assert.deepEqual(resizeRect(origin, "s", 0, -9999, VW, VH), { x: 100, y: 100, w: 400, h: MIN_H }); assert.deepEqual(resizeRect(origin, "w", 9999, 0, VW, VH), { x: 100 + 400 - MIN_W, y: 100, w: MIN_W, h: 300 }); assert.deepEqual(resizeRect(origin, "n", 0, 9999, VW, VH), { x: 100, y: 100 + 300 - MIN_H, w: 400, h: MIN_H }); assert.deepEqual(resizeRect({ x: 0, y: 0, w: 400, h: 300 }, "e", 99999, 0, VW, VH), { x: 0, y: 0, w: VW - 8, h: 300 }); assert.deepEqual(resizeRect({ x: 0, y: 0, w: 400, h: 300 }, "s", 0, 99999, VW, VH), { x: 0, y: 0, w: 400, h: VH - 8 }); }); test("a pointer near a side edge offers that pinned split, otherwise none", () => { const cases: Array<[number, "left" | "right" | null]> = [ [0, "left"], [64, "left"], [65, null], [640, null], [1215, null], [1216, "right"], [1280, "right"], ]; for (const [x, want] of cases) { assert.equal(snapEdge(x, VW), want, String(x)); } }); // Guards the minimised composer floating mid-page under where the window was. test("the minimised bar docks at the bottom of the screen on the pinned side", () => { const rect = { x: 300, y: 100, w: 440, h: 560 }; assert.deepEqual(barPlacement(rect, "float", VW), { x: 300, bottom: 8, w: 640 }); assert.deepEqual(barPlacement(rect, "left", VW), { x: 0, bottom: 8, w: 640 }); assert.deepEqual(barPlacement(rect, "right", VW), { x: 1280 - 640, bottom: 8, w: 640 }); // A bar that would run off the right edge slides back on screen. assert.deepEqual(barPlacement({ ...rect, x: 900 }, "float", VW), { x: 1280 - 640 - 8, bottom: 8, w: 640 }); // A narrow screen caps the bar at its width. assert.deepEqual(barPlacement({ ...rect, x: 0, w: 320 }, "float", 500), { x: 8, bottom: 8, w: 484 }); }); test("the old edge-pin preference migrates into the new key and is removed", () => { const cases: Array<[string | null, DockLayout | null]> = [ ['{"side":"right","width":500}', { mode: "right", x: 772, y: 216, w: 500, h: 560 }], ['{"side":"left","width":300}', { mode: "left", x: 816, y: 216, w: 320, h: 560 }], ['{"side":null,"width":380}', { mode: "float", x: 816, y: 216, w: 380, h: 560 }], ["not json", defaultLayout(VW, VH)], [null, null], ]; for (const [legacy, want] of cases) { const storage = new MemoryStorage(); if (legacy !== null) storage.setItem(LEGACY_DOCK_STORAGE_KEY, legacy); migrateDockStorage(storage, VW, VH); assert.equal(storage.getItem(LEGACY_DOCK_STORAGE_KEY), null, "legacy key removed"); if (want === null) { assert.equal(storage.getItem(DOCK_STORAGE_KEY), null); } else { assert.deepEqual(JSON.parse(storage.getItem(DOCK_STORAGE_KEY) ?? "null"), want, legacy ?? ""); } } }); test("an existing new-key layout wins and still retires the legacy key", () => { const storage = new MemoryStorage(); const kept: DockLayout = { mode: "float", x: 40, y: 40, w: 360, h: 420 }; storage.setItem(DOCK_STORAGE_KEY, JSON.stringify(kept)); storage.setItem(LEGACY_DOCK_STORAGE_KEY, '{"side":"left","width":900}'); migrateDockStorage(storage, VW, VH); assert.deepEqual(JSON.parse(storage.getItem(DOCK_STORAGE_KEY) ?? "null"), kept); assert.equal(storage.getItem(LEGACY_DOCK_STORAGE_KEY), null); }); test("legacy pin and float widths are clamped to what the viewport allows", () => { assert.equal(pinnedWidth(500, VW), 500); assert.equal(pinnedWidth(9999, VW), 720); assert.equal(pinnedWidth(100, VW), MIN_W); assert.equal(pinnedWidth(700, 900), 420); // vw - 480 caps below MAX_W assert.equal(windowWidth(440, VW), 440); assert.equal(windowWidth(9999, VW), 720); assert.equal(windowWidth(440, 400), 384); }); test("keyboard steps and handle metadata follow the contract", () => { assert.equal(keyStep(false), 10); assert.equal(keyStep(true), 40); const dirs = ["n", "s", "e", "w", "ne", "nw", "se", "sw"] as const; for (const dir of dirs) { assert.match(handleOrientation(dir), /^(horizontal|vertical)$/); assert.ok(Number.isFinite(handleValue(dir, { x: 0, y: 0, w: 412.4, h: 287.6 }))); } assert.equal(handleValue("n", { x: 0, y: 0, w: 100, h: 287.6 }), 288); assert.equal(handleValue("se", { x: 0, y: 0, w: 412.4, h: 100 }), 412); assert.equal(handleOrientation("e"), "vertical"); assert.equal(handleOrientation("w"), "vertical"); assert.equal(handleOrientation("s"), "horizontal"); }); test("legacyLayout maps only the known fields of the old preference", () => { assert.deepEqual(legacyLayout({ side: "left", width: 420 }), { mode: "left", w: 420 }); assert.deepEqual(legacyLayout({ side: "right" }), { mode: "right" }); assert.deepEqual(legacyLayout({ side: null, width: 420 }), { mode: "float", w: 420 }); assert.deepEqual(legacyLayout({ side: "up", width: "wide" }), { mode: "float" }); assert.equal(legacyLayout(null), null); assert.equal(legacyLayout("right"), null); }); test("the on-screen keyboard's cover of the layout viewport is measured, never negative", () => { const cases: Array<[{ height: number; offsetTop: number } | null, number]> = [ [null, 0], [{ height: 800, offsetTop: 0 }, 0], [{ height: 500, offsetTop: 0 }, 300], [{ height: 500, offsetTop: 20 }, 280], [{ height: 460.4, offsetTop: 10 }, 330], [{ height: 900, offsetTop: 0 }, 0], ]; for (const [vv, want] of cases) { assert.equal(keyboardCover(800, vv), want, JSON.stringify(vv)); } }); test("the phone bar sits above the keyboard while one is up, else at the safe area", () => { assert.equal(phoneBarBottom(0), "max(0.5rem, env(safe-area-inset-bottom))"); assert.equal(phoneBarBottom(300), "308px"); assert.equal(phoneBarBottom(1), "9px"); });