import { Silo } from "atom.io" import { afterEach, beforeEach, expect, it, vi } from "vitest" import { RESIZE_EDGES, SNAP_TARGETS } from "../src/window-geometry.ts" import { mountWindowFixture } from "./fixtures/views.tsx" const cleanups: (() => void)[] = [] let frames: Map, serial = 0 const free = { x: 100, y: 120, width: 500, height: 400 } beforeEach(() => { localStorage.clear() Object.assign(window, { innerWidth: 1200, innerHeight: 900 }) frames = new Map() vi.spyOn(window, `requestAnimationFrame`).mockImplementation((callback) => { frames.set(++serial, callback) return serial }) vi.spyOn(window, `cancelAnimationFrame`).mockImplementation((id) => { frames.delete(id) }) }) afterEach(() => { for (const cleanup of cleanups.splice(0)) cleanup() document.body.replaceChildren() vi.restoreAllMocks() Object.assign(window, { innerWidth: 1024, innerHeight: 768 }) }) function fixture(saved: unknown = { free, snap: null }) { localStorage.setItem(`🔍 Devtools Window`, JSON.stringify(saved)) const host = document.createElement(`div`) document.body.append(host) const toggle = vi.fn() const app = new Silo({ name: `window-fixture`, lifespan: `ephemeral`, isProduction: true, }) const mounted = mountWindowFixture(host, app.store, toggle) const panel = mounted.panel const toolbar = panel.querySelector(`debugger-toolbar`)! const controller = mounted.controller cleanups.push(mounted.dispose) for (const node of [ toolbar, panel.querySelector(`[data-testid="open-close-devtools"]`)!, ...panel.querySelectorAll(`[data-resize]`), ]) { node.setPointerCapture = vi.fn() node.releasePointerCapture = vi.fn() } const targets = host.querySelectorAll(`[data-snap]`) targets.forEach((button, index) => { button.getBoundingClientRect = () => new DOMRect(100 + index * 60, 10, 60, 30) }) const pointer = ( type: string, x = 200, y = 200, target: HTMLElement = toolbar, other: PointerEventInit = {}, ) => { target.dispatchEvent( new PointerEvent(type, { bubbles: true, pointerId: 1, button: 0, isPrimary: true, clientX: x, clientY: y, ...other, }), ) } const key = ( pressed: string, element: HTMLElement = toolbar, extra: KeyboardEventInit = {}, ) => { element.dispatchEvent( new KeyboardEvent(`keydown`, { key: pressed, bubbles: true, ...extra }), ) } const flush = () => { for (const [id, callback] of [...frames]) { frames.delete(id) mounted.flush() callback(0) mounted.flush() } } const rect = () => ( mounted.flush(), { x: Number.parseFloat(panel.style.left), y: Number.parseFloat(panel.style.top), width: Number.parseFloat(panel.style.width), height: Number.parseFloat(panel.style.height), } ) return { host, panel, toolbar, controller, toggle, pointer, key, flush, rect, targets, get preview() { mounted.flush() return host.querySelector(`snap-preview`)! }, get palette() { mounted.flush() return host.querySelector(`snap-palette`)! }, } } it(`moves the collapsed launcher across the viewport, preserves expanded geometry and suppresses only the drag click`, () => { const f = fixture({ free, snap: `right-half` }) const launcher = f.panel.querySelector( `[data-testid="open-close-devtools"]`, )! launcher.setAttribute(`data-drag-handle`, `true`) f.controller.visibility(false, false) expect(f.rect()).toEqual({ x: 600, y: 0, width: 32, height: 32 }) const activate = vi.fn() launcher.addEventListener(`click`, activate) f.pointer(`pointerdown`, 610, 10, launcher) f.pointer(`pointermove`, 1400, 1100, launcher) f.flush() expect(f.rect()).toEqual({ x: 1168, y: 868, width: 32, height: 32 }) expect(f.palette.hidden).toBe(true) expect(f.preview.hidden).toBe(true) f.pointer(`pointerup`, 1400, 1100, launcher) expect(JSON.parse(localStorage.getItem(`🔍 Devtools Window`)!)).toEqual({ free, snap: `right-half`, launcher: { x: 1168, y: 868 }, }) const dragClick = new MouseEvent(`click`, { bubbles: true, cancelable: true, detail: 1, }) launcher.dispatchEvent(dragClick) expect(dragClick.defaultPrevented).toBe(true) expect(activate).not.toHaveBeenCalled() f.pointer(`pointerdown`, 1178, 878, launcher) f.pointer(`pointerup`, 1179, 879, launcher) launcher.dispatchEvent(new MouseEvent(`click`, { bubbles: true, detail: 1 })) expect(activate).toHaveBeenCalledOnce() f.controller.visibility(false, true) expect(f.rect()).toEqual({ x: 600, y: 0, width: 600, height: 900 }) f.controller.choose(`full`) f.controller.visibility(false, false) expect(f.rect()).toEqual({ x: 1168, y: 868, width: 32, height: 32 }) }) it(`cancels launcher drags, allows keyboard activation and nudges only the visible launcher without losing its bounds`, () => { const f = fixture() const launcher = f.panel.querySelector( `[data-testid="open-close-devtools"]`, )! launcher.setAttribute(`data-drag-handle`, `true`) f.controller.visibility(false, false) const before = f.rect(), saved = localStorage.getItem(`🔍 Devtools Window`) f.pointer(`pointerdown`, 110, 130, launcher) f.pointer(`pointermove`, 350, 360, launcher) f.flush() f.key(`Escape`, launcher) f.pointer(`pointerup`, 350, 360, launcher) expect(f.rect()).toEqual(before) expect(localStorage.getItem(`🔍 Devtools Window`)).toBe(saved) const activate = vi.fn() launcher.addEventListener(`click`, activate) launcher.click() expect(activate).toHaveBeenCalledOnce() f.pointer(`pointerdown`, 110, 130, launcher) f.key(`Escape`, launcher) launcher.dispatchEvent(new MouseEvent(`click`, { bubbles: true, detail: 1 })) expect(activate).toHaveBeenCalledOnce() f.key(`ArrowRight`, f.panel.querySelector(`[data-drag-handle]`)!) f.key(`ArrowRight`, f.panel.querySelector(`[data-resize]`)!) expect(f.rect()).toEqual(before) f.key(`ArrowRight`, launcher) expect(f.rect()).toEqual({ ...before, x: before.x + 10 }) f.controller.visibility(true, false) f.key(`ArrowRight`, launcher) expect(f.rect()).toEqual({ ...before, x: before.x + 10 }) f.controller.visibility(false, false) f.pointer(`pointerdown`, 120, 130, launcher) f.pointer(`pointermove`, 900, 700, launcher) f.flush() f.pointer(`pointercancel`, 900, 700, launcher) expect(f.rect()).toEqual({ ...before, x: before.x + 10 }) Object.assign(window, { innerWidth: 100, innerHeight: 80 }) window.dispatchEvent(new Event(`resize`)) expect(f.rect()).toEqual({ x: 68, y: 48, width: 32, height: 32 }) const storage = vi.spyOn(window.localStorage, `setItem`) f.key(`ArrowRight`, launcher) expect(storage).not.toHaveBeenCalled() f.controller.visibility(false, true) expect(f.rect()).toEqual({ x: 0, y: 0, width: 100, height: 80 }) }) it(`activates a launcher touch tap exactly once even if the browser also emits a compatibility click`, () => { const f = fixture() const launcher = f.panel.querySelector( `[data-testid="open-close-devtools"]`, )! launcher.setAttribute(`data-drag-handle`, `true`) f.controller.visibility(false, false) const activate = vi.fn() launcher.addEventListener(`click`, activate) f.pointer(`pointerdown`, 110, 130, launcher, { pointerType: `touch` }) f.pointer(`pointerup`, 110, 130, launcher, { pointerType: `touch` }) expect(activate).toHaveBeenCalledOnce() launcher.dispatchEvent(new MouseEvent(`click`, { bubbles: true, detail: 1 })) expect(activate).toHaveBeenCalledOnce() f.pointer(`pointerdown`, 110, 130, launcher, { pointerType: `touch` }) f.pointer(`pointermove`, 300, 300, launcher, { pointerType: `touch` }) f.pointer(`pointerup`, 300, 300, launcher, { pointerType: `touch` }) expect(activate).toHaveBeenCalledOnce() }) it(`coalesces movement, preserves focused input nodes and persists only committed geometry`, () => { const f = fixture() const input = f.panel.querySelector(`input`)! input.focus() const storage = vi.spyOn(window.localStorage, `setItem`) f.pointer(`pointermove`) f.pointer(`pointerup`) f.pointer(`pointercancel`) f.pointer(`pointerdown`, 200, 200) f.pointer(`pointermove`, 230, 240) f.pointer(`pointermove`, 250, 260) expect(frames.size).toBe(1) expect(f.rect()).toEqual(free) expect(storage).not.toHaveBeenCalled() f.flush() expect(f.rect()).toEqual({ ...free, x: 150, y: 180 }) expect(document.activeElement).toBe(input) expect(input.value).toBe(`2.`) f.pointer(`pointerup`, 270, 280) expect(f.rect()).toEqual({ ...free, x: 170, y: 200 }) expect(storage).toHaveBeenCalledTimes(1) expect(f.palette.hidden).toBe(true) }) it(`preserves snap, free rectangle and persisted bytes on a click or sub-threshold pointer jitter`, () => { const cases = [ { saved: { free, snap: `right-half` }, x: 610, y: 14 }, { saved: { free, snap: `left-third` }, x: 5, y: 14 }, { saved: { free: { x: 700, y: 0, width: 500, height: 900 }, snap: null }, x: 705, y: 6, }, ] for (const { saved, x, y } of cases) { for (const selector of [ `[data-drag-handle]`, ...RESIZE_EDGES.map((edge) => `[data-resize="${edge}"]`), ]) { for (const [dx, dy] of [ [0, 0], [1, 0], [2, 2], [3, 0], ]) { const f = fixture(saved) const target = f.panel.querySelector(selector)! const before = f.rect() const persisted = localStorage.getItem(`🔍 Devtools Window`) const storage = vi.spyOn(window.localStorage, `setItem`) f.pointer(`pointerdown`, x, y, target) if (dx || dy) { f.pointer(`pointermove`, x + dx, y + dy, target) f.flush() } f.pointer(`pointerup`, x + dx, y + dy, target) expect(f.rect()).toEqual(before) expect(localStorage.getItem(`🔍 Devtools Window`)).toBe(persisted) expect(storage).not.toHaveBeenCalled() expect(f.preview.hidden).toBe(true) expect(f.palette.hidden).toBe(true) expect(frames.size).toBe(0) storage.mockRestore() // Restore must recover the full saved free rectangle after the click. f.palette.querySelector(`button:last-child`)!.click() expect(f.rect()).toEqual(saved.free) f.controller.dispose() f.host.remove() } } } }) it(`does not resize or detach a snapped window when moving along an edge without crossing it`, () => { for (const [edge, dx, dy] of [ [`n`, 20, 0], [`s`, 20, 0], [`e`, 0, 20], [`w`, 0, 20], ] as const) { const f = fixture({ free, snap: `right-half` }) const handle = f.panel.querySelector(`[data-resize="${edge}"]`)! const before = f.rect(), persisted = localStorage.getItem(`🔍 Devtools Window`) f.pointer(`pointerdown`, 610, 14, handle) f.pointer(`pointermove`, 610 + dx, 14 + dy, handle) f.pointer(`pointerup`, 610 + dx, 14 + dy, handle) expect(f.rect()).toEqual(before) expect(localStorage.getItem(`🔍 Devtools Window`)).toBe(persisted) expect(frames.size).toBe(0) } }) it(`preserves armed press geometry and storage on every cancellation path`, () => { for (const termination of [ `Escape`, `pointercancel`, `lostpointercapture`, `hide`, `minimize`, `dispose`, ]) { const f = fixture({ free, snap: `right-half` }) const before = f.rect(), persisted = localStorage.getItem(`🔍 Devtools Window`) const storage = vi.spyOn(window.localStorage, `setItem`) f.pointer(`pointerdown`, 610, 14) f.pointer(`pointermove`, 612, 15) if (termination === `Escape`) f.key(`Escape`) else if (termination === `hide`) f.controller.visibility(true, true) else if (termination === `minimize`) { f.controller.visibility(false, false) f.controller.visibility(false, true) } else if (termination === `dispose`) f.controller.dispose() else f.pointer(termination, 612, 15) expect(f.rect()).toEqual(before) expect(localStorage.getItem(`🔍 Devtools Window`)).toBe(persisted) expect(storage).not.toHaveBeenCalled() expect(frames.size).toBe(0) expect(f.toolbar.releasePointerCapture).toHaveBeenCalledExactlyOnceWith(1) storage.mockRestore() } }) it(`latches real movement at the threshold and commits the final release point before a pending frame`, () => { const f = fixture({ free, snap: `right-half` }) const persisted = localStorage.getItem(`🔍 Devtools Window`) const storage = vi.spyOn(window.localStorage, `setItem`) f.pointer(`pointerdown`, 610, 14) f.pointer(`pointermove`, 614, 14) // Exactly four pixels begins a drag. expect(f.palette.hidden).toBe(false) f.flush() expect(f.rect().width).toBe(500) f.key(`Escape`) expect(localStorage.getItem(`🔍 Devtools Window`)).toBe(persisted) f.pointer(`pointerdown`, 610, 14) f.pointer(`pointermove`, 630, 34) const staleFrame = [...frames.values()][0] f.pointer(`pointerup`, 640, 44) expect(frames.size).toBe(0) expect(f.rect().x).toBeCloseTo(631.666667) expect(f.rect().y).toBe(30) expect(JSON.parse(localStorage.getItem(`🔍 Devtools Window`)!).snap).toBeNull() expect(storage).toHaveBeenCalledTimes(1) staleFrame(0) expect(storage).toHaveBeenCalledTimes(1) // Crossing the threshold then returning before a frame still reflects a real drag. f.pointer(`pointerdown`, 700, 100) f.pointer(`pointermove`, 720, 100) f.pointer(`pointerup`, 700, 100) expect(storage).toHaveBeenCalledTimes(2) }) it(`uses meaningful release coordinates even when the final pointermove was not delivered`, () => { const f = fixture({ free, snap: `right-half` }) const storage = vi.spyOn(window.localStorage, `setItem`) f.pointer(`pointerdown`, 610, 14) f.pointer(`pointerup`, 620, 24) expect(f.rect().width).toBe(500) expect(f.rect().x).toBeCloseTo(611.666667) expect(f.rect().y).toBe(10) expect(storage).toHaveBeenCalledTimes(1) expect(frames.size).toBe(0) }) it(`ignores unrelated buttons, pointers and controls, and cancels on Escape, cancellation or capture loss`, () => { const f = fixture() for (const options of [{ button: 2 }, { isPrimary: false }]) f.pointer(`pointerdown`, 200, 200, f.toolbar, options) for (const node of [ f.panel, f.panel.querySelector(`input`)!, f.panel.querySelector(`nav button`)!, ]) f.pointer(`pointerdown`, 200, 200, node) expect(f.toolbar.setPointerCapture).not.toHaveBeenCalled() for (const cancel of [`Escape`, `pointercancel`, `lostpointercapture`]) { f.pointer(`pointerdown`) f.pointer(`pointerdown`) f.pointer(`pointermove`, 500, 500, f.toolbar, { pointerId: 2 }) f.pointer(`pointerup`, 500, 500, f.toolbar, { pointerId: 2 }) f.pointer(`pointercancel`, 500, 500, f.toolbar, { pointerId: 2 }) f.pointer(`pointermove`, 300, 300) f.flush() expect(f.rect()).not.toEqual(free) if (cancel === `Escape`) f.key(`Escape`) else f.pointer(cancel) expect(f.rect()).toEqual(free) expect(f.preview.hidden).toBe(true) } f.key(`z`) f.key(`z`, f.panel.querySelector(`input`)!) f.key(`a`, f.toolbar, { ctrlKey: true, shiftKey: true, code: `KeyA` }) expect(f.toggle).toHaveBeenCalledOnce() }) it(`previews every palette target, commits exact zones and restores the last free position`, () => { const f = fixture() SNAP_TARGETS.forEach((target, index) => { f.pointer(`pointerdown`) f.pointer(`pointermove`, 120 + index * 60, 20) f.flush() expect(f.preview.hidden).toBe(false) const preview = f.preview.style.cssText f.pointer(`pointerup`, 120 + index * 60, 20) f.rect() expect(f.panel.style.cssText).toBe(preview) expect(JSON.parse(localStorage.getItem(`🔍 Devtools Window`)!).snap).toBe( target, ) f.host .querySelector(`[aria-label="Debugger layout"]`)! .click() f.palette.querySelector(`button:last-child`)!.click() }) expect(f.rect()).toEqual(free) }) it(`unsnaps on header drag and restores the prior snap when cancelled`, () => { const f = fixture({ free, snap: `right-half` }) f.pointer(`pointerdown`, 800, 20) f.pointer(`pointermove`, 700, 100) f.flush() expect(f.rect().width).toBe(500) f.pointer(`pointercancel`) expect(f.rect()).toEqual({ x: 600, y: 0, width: 600, height: 900 }) const layout = f.panel.querySelector( `[aria-label="Debugger layout"]`, )! layout.click() f.key(`Escape`) expect(document.activeElement).toBe(layout) }) it(`snaps and restores through keyboard-accessible buttons, moves/resizes with arrows and clamps on viewport changes`, () => { const f = fixture() const layout = f.panel.querySelector( `[aria-label="Debugger layout"]`, )! layout.click() expect(f.palette.hidden).toBe(false) expect(document.activeElement).toBe(f.targets[0]) layout.click() expect(f.palette.hidden).toBe(true) layout.click() f.targets[1].click() expect(f.rect()).toEqual({ x: 600, y: 0, width: 600, height: 900 }) Object.assign(window, { innerWidth: 600, innerHeight: 300 }) window.dispatchEvent(new Event(`resize`)) expect(f.rect()).toEqual({ x: 300, y: 0, width: 300, height: 300 }) f.palette.querySelector(`button:last-child`)!.click() expect(f.rect()).toEqual({ x: 100, y: 0, width: 500, height: 300 }) const handle = f.panel.querySelector(`[data-drag-handle]`)! for (const key of [`ArrowLeft`, `ArrowRight`, `ArrowUp`, `ArrowDown`, `Enter`]) f.key(key, handle) expect(f.rect().y).toBe(0) const resize = f.panel.querySelector(`[data-resize="e"]`)! f.key(`ArrowLeft`, resize) expect(f.rect().width).toBe(490) Object.assign(window, { innerWidth: 160, innerHeight: 90 }) window.dispatchEvent(new Event(`resize`)) expect(f.rect()).toEqual({ x: 0, y: 0, width: 160, height: 90 }) }) it(`nudges and resizes snapped windows from their visible bounds`, () => { for (const [selector, key, expected] of [ [ `[data-drag-handle]`, `ArrowLeft`, { x: 590, y: 0, width: 600, height: 900 }, ], [ `[data-resize="e"]`, `ArrowLeft`, { x: 600, y: 0, width: 590, height: 900 }, ], [ `[data-resize="w"]`, `ArrowRight`, { x: 610, y: 0, width: 590, height: 900 }, ], [ `[data-resize="n"]`, `ArrowDown`, { x: 600, y: 10, width: 600, height: 890 }, ], [`[data-resize="se"]`, `ArrowUp`, { x: 600, y: 0, width: 600, height: 890 }], ] as const) { const f = fixture({ free, snap: `right-half` }) f.key(key, f.panel.querySelector(selector)!) expect(f.rect()).toEqual(expected) expect(JSON.parse(localStorage.getItem(`🔍 Devtools Window`)!)).toEqual({ free: expected, snap: null, }) } const top = fixture({ free, snap: `top-half` }) top.key( `ArrowDown`, top.panel.querySelector(`[data-drag-handle]`)!, ) expect(top.rect()).toEqual({ x: 0, y: 10, width: 1200, height: 450 }) }) it(`keeps snapped geometry and storage for unrelated resize arrows and clamped keyboard actions`, () => { for (const [selector, key] of [ [`[data-resize="e"]`, `ArrowUp`], [`[data-resize="e"]`, `ArrowDown`], [`[data-resize="w"]`, `ArrowUp`], [`[data-resize="w"]`, `ArrowDown`], [`[data-resize="n"]`, `ArrowLeft`], [`[data-resize="n"]`, `ArrowRight`], [`[data-resize="s"]`, `ArrowLeft`], [`[data-resize="s"]`, `ArrowRight`], [`[data-drag-handle]`, `ArrowRight`], [`[data-drag-handle]`, `ArrowUp`], [`[data-resize="e"]`, `ArrowRight`], ] as const) { const f = fixture({ free, snap: `right-half` }) const persisted = localStorage.getItem(`🔍 Devtools Window`) const storage = vi.spyOn(window.localStorage, `setItem`) f.key(key, f.panel.querySelector(selector)!) expect(f.rect()).toEqual({ x: 600, y: 0, width: 600, height: 900 }) expect(localStorage.getItem(`🔍 Devtools Window`)).toBe(persisted) expect(storage).not.toHaveBeenCalled() storage.mockRestore() } }) it(`resizes corners, cancels scheduled work and preserves free geometry across hide/minimize/disposal`, () => { const f = fixture() const handle = f.panel.querySelector(`[data-resize="se"]`)! f.pointer(`pointerdown`, 600, 520, handle) f.pointer(`pointermove`, 700, 620, handle) f.flush() expect(f.preview.hidden).toBe(true) expect(f.rect()).toEqual({ ...free, width: 600, height: 500 }) f.pointer(`pointerup`, 700, 620, handle) for (const [hidden, open] of [ [true, true], [false, false], ]) { f.pointer(`pointerdown`) f.pointer(`pointermove`, 300, 300) const callback = [...frames.values()][0] f.controller.visibility(hidden, open) expect(frames.size).toBe(0) callback(0) // A stale browser callback after cancellation must be harmless. f.pointer(`pointerdown`) expect(frames.size).toBe(0) f.controller.visibility(false, true) expect(f.rect()).toEqual({ ...free, width: 600, height: 500 }) } f.pointer(`pointerdown`) f.pointer(`pointermove`, 250, 250) f.controller.dispose() expect(frames.size).toBe(0) expect(f.host.querySelector(`snap-palette`)).toBeNull() }) it(`handles unavailable storage, failed capture and already-lost capture without leaking DOM`, () => { const get = vi.spyOn(window.localStorage, `getItem`).mockImplementation(() => { throw new Error(`storage`) }) const f = fixture() get.mockRestore() const set = vi.spyOn(window.localStorage, `setItem`).mockImplementation(() => { throw new Error(`storage`) }) f.toolbar.setPointerCapture = () => { throw new Error(`capture`) } f.pointer(`pointerdown`) expect(f.palette.hidden).toBe(true) f.toolbar.setPointerCapture = () => {} f.toolbar.releasePointerCapture = () => { throw new Error(`lost capture`) } f.pointer(`pointerdown`) f.pointer(`pointermove`, 0, 200) f.pointer(`pointerup`, 0, 200) expect(f.rect().x).toBe(0) f.key(`Escape`) set.mockRestore() }) it(`ignores clicks in the layout palette outside a snap action`, () => { const f = fixture() f.controller.toggleMenu() f.palette.click() expect(document.activeElement).toBe(f.targets[0]) expect(f.rect()).toEqual(free) expect(f.palette.hidden).toBe(false) })