import { afterAll, afterEach, beforeAll, beforeEach, expect, it, vi } from "vitest"; vi.mock("white-web-sdk", async () => ({ ...(await import("../__mocks__/white-web-sdk")), ScenePathType: { None: "none", Dir: "dir" }, })); vi.mock("../src/index", () => ({ WindowManager: class { static extendClass = undefined; }, })); vi.mock("../src/Utils/extendClass", () => ({ getExtendClass: (base: unknown) => base })); import { WindowManager } from "../src/index"; let Manager: typeof import("../src/AppManager").AppManager; const previousCanvasContext = globalThis.CanvasRenderingContext2D; beforeAll(async () => { (globalThis as any).CanvasRenderingContext2D = class {}; Manager = (await import("../src/AppManager")).AppManager; }); beforeEach(() => { WindowManager.container = document.createElement("div"); }); afterEach(() => { WindowManager.container = undefined; vi.restoreAllMocks(); }); afterAll(() => { globalThis.CanvasRenderingContext2D = previousCanvasContext; }); function fixture(destroyed = false) { const apps = { oldest: { kind: "Oldest", createdAt: 1 }, removed: undefined, focused: { kind: "Focused", createdAt: 2 }, }; const tasks: Array<() => Promise> = []; const manager = { destroyed, appCreateQueue: { emitReady: vi.fn(), push: (task: () => Promise) => tasks.push(task) }, appProxies: new Map(), appStatus: new Map(), pendingAppIds: new Set(), attributes: { apps, maximized: true, oldest: {}, focused: {} }, store: { focus: "focused" }, useBoxesStatus: false, notifyAppsChange: vi.fn(), baseInsertApp: vi.fn().mockResolvedValue(undefined), focusByAttributes: vi.fn(), }; return { manager, apps, tasks }; } it("reconciles live descriptors while a removed app remains enumerable", async () => { const { manager, apps, tasks } = fixture(); await Manager.prototype._attributesUpdateCallback.call(manager as any, apps); for (const task of tasks) await task(); expect(manager.notifyAppsChange).toHaveBeenCalledWith(["oldest", "focused"]); expect(manager.baseInsertApp.mock.calls.map(([, id]) => id)).toEqual(["focused", "oldest"]); }); it("reconciles a retained proxy from the settled snapshot when the close notification was missed", async () => { const { manager, apps, tasks } = fixture(); const removed = { destroy: vi.fn(async () => { manager.appProxies.delete("removed"); }) }; manager.appProxies.set("removed", removed); Object.assign(manager, { onAppDelete: (Manager.prototype as any).onAppDelete }); await Manager.prototype._attributesUpdateCallback.call(manager as any, apps); for (const task of tasks) await task(); expect(removed.destroy).toHaveBeenCalledWith(true, false, true); expect(manager.appProxies.has("removed")).toBe(false); expect(manager.baseInsertApp.mock.calls.map(([, id]) => id)).toEqual(["focused", "oldest"]); }); it("does not create apps after destruction while reconciling a retained proxy", async () => { const { manager, apps, tasks } = fixture(); manager.appProxies.set("removed", { destroy: vi.fn(async () => { manager.destroyed = true; }) }); Object.assign(manager, { onAppDelete: (Manager.prototype as any).onAppDelete }); await Manager.prototype._attributesUpdateCallback.call(manager as any, apps); expect(tasks).toHaveLength(0); expect(manager.notifyAppsChange).not.toHaveBeenCalled(); }); it("ignores an attributes callback retained by a destroyed room manager", async () => { const { manager, apps, tasks } = fixture(true); await Manager.prototype._attributesUpdateCallback.call(manager as any, apps); expect(tasks).toHaveLength(0); expect(manager.notifyAppsChange).not.toHaveBeenCalled(); }); it("does not resume reconnect work after destruction during attribute reconciliation", async () => { let reconciled!: () => void; const reconciliation = new Promise(resolve => { reconciled = resolve; }); const manager = { destroyed: false, attributes: { apps: {} }, _attributesUpdateCallback: vi.fn(() => reconciliation), appProxies: new Map(), mainViewProxy: { onReconnect: vi.fn() }, setupAllApps: vi.fn().mockResolvedValue(undefined), }; const pending = Manager.prototype.onReconnected.call(manager as any); manager.destroyed = true; reconciled(); await pending; expect(manager.mainViewProxy.onReconnect).not.toHaveBeenCalled(); expect(manager.setupAllApps).not.toHaveBeenCalled(); }); it("destroys a proxy whose enumerable descriptor has become undefined", async () => { const removed = { destroy: vi.fn().mockResolvedValue(undefined) }; const live = { destroy: vi.fn().mockResolvedValue(undefined) }; const manager = { appProxies: new Map([["removed", removed], ["live", live]]) }; await (Manager.prototype as any).onAppDelete.call(manager, { removed: undefined, live: { kind: "Presentation" }, }); expect(removed.destroy).toHaveBeenCalledWith(true, false, true); expect(live.destroy).not.toHaveBeenCalled(); }); it("does not hand focus to a deleted descriptor with a retained proxy", () => { const manager = { attributes: { apps: { closing: { kind: "Slide" }, removed: undefined } }, appProxies: new Map([["removed", { isDestroyed: false }]]), }; expect(Manager.prototype.getCloseSuccessor.call(manager as any, "closing")).toBeUndefined(); }); it("reconciles focus against live boxes when a deleted descriptor remains enumerable", () => { const manager = { boxManager: { boxSize: 1, focusBox: vi.fn() }, store: { focus: "live" }, lazySetupEnabled: false, }; Manager.prototype.focusByAttributes.call(manager as any, { live: { kind: "Presentation" }, removed: undefined, }); expect(manager.boxManager.focusBox).toHaveBeenCalledWith({ appId: "live" }); });