import { afterEach, expect, it, vi } from "vitest"; import { floatingWindowEntries, floatingWindowOrder, raiseFloatingWindow, registerFloatingWindow, removeFloatingWindow, subscribeFloatingWindows } from "./floating-window-layer"; afterEach(() => floatingWindowOrder().forEach(removeFloatingWindow)); it("keeps a stable snapshot, follows stacking order and removes window metadata", () => { const notify = vi.fn(); const stop = subscribeFloatingWindows(notify); const first = { id: "first", title: "压力图", focus: vi.fn(), close: vi.fn() }; registerFloatingWindow(first); const snapshot = floatingWindowEntries(); expect(floatingWindowEntries()).toBe(snapshot); registerFloatingWindow({ id: "second", title: "流量图", focus: vi.fn(), close: vi.fn() }); expect(floatingWindowEntries().map((entry) => entry.id)).toEqual(["first", "second"]); raiseFloatingWindow("first"); expect(floatingWindowEntries().map((entry) => entry.id)).toEqual(["second", "first"]); removeFloatingWindow("first"); expect(floatingWindowEntries().map((entry) => entry.id)).toEqual(["second"]); removeFloatingWindow("second"); expect(floatingWindowEntries()).toEqual([]); expect(notify).toHaveBeenCalledTimes(5); stop(); }); it("updates titles without moving windows and calls the latest actions without snapshot churn", () => { const focus = vi.fn(), close = vi.fn(); registerFloatingWindow({ id: "chart", title: "旧标题", focus: vi.fn(), close: vi.fn() }); const initial = floatingWindowEntries(); registerFloatingWindow({ id: "chart", title: "旧标题", focus, close }); expect(floatingWindowEntries()).toBe(initial); initial[0].focus(); initial[0].close(); expect(focus).toHaveBeenCalledOnce(); expect(close).toHaveBeenCalledOnce(); registerFloatingWindow({ id: "chart", title: "新标题", focus, close }); expect(floatingWindowEntries()[0].title).toBe("新标题"); expect(initial[0].title).toBe("旧标题"); expect(floatingWindowOrder()).toEqual(["chart"]); removeFloatingWindow("chart"); initial[0].focus(); initial[0].close(); expect(focus).toHaveBeenCalledOnce(); expect(close).toHaveBeenCalledOnce(); });