/** * 02-wm-close — 窗口关闭与重入保护 * * 使用 vitest 原汁原味的 vi.fn() + spy 模式。 * 测试 WM composable 的关闭逻辑、confirmClose 防重入机制。 */ import { describe, it, expect, vi } from 'vitest' import { wm } from '../../src/composables/useWM' describe('02-wm-close — 窗口管理', () => { // ==================== 初始状态 ==================== describe('初始状态', () => { it('初始无窗口', () => { expect(wm.windows).toHaveLength(0) }) it('activeWin 初始为 null', () => { expect(wm.activeWin.value).toBeNull() }) it('windowsForApp 返回空', () => { expect(wm.windowsForApp('finder')).toHaveLength(0) }) it('anyVisible 返回 false', () => { expect(wm.anyVisible()).toBe(false) }) it('usableRect 返回有效区域', () => { const r = wm.usableRect() expect(r.w).toBeGreaterThan(400) expect(r.h).toBeGreaterThan(300) expect(r.y).toBe(30) }) }) // ==================== close 操作 ==================== describe('close', () => { it('close 不存在的窗口返回 alreadyClosed', async () => { const result = await wm.close({ windows: [] } as any) expect(result).toBe('alreadyClosed') }) }) // ==================== confirmClose 机制 ==================== describe('confirmClose', () => { it('app 可注入确认回调', () => { const doneCb = vi.fn() const cancelCb = vi.fn() const mockWin: any = { id: 'test', appId: 't', title: '', icon: '', rect: { x: 0, y: 0, w: 0, h: 0 }, prevRect: null, minW: 200, minH: 120, state: 'normal', el: null, body: null, titleEl: null, timers: [], confirmClose: (done: () => void, cancel: () => void) => { cancel() }, } expect(typeof mockWin.confirmClose).toBe('function') mockWin.confirmClose(doneCb, cancelCb) expect(cancelCb).toHaveBeenCalledTimes(1) expect(doneCb).not.toHaveBeenCalled() }) }) // ==================== closePromise 防重入 ==================== describe('closePromise 防重入', () => { it('同一窗口 closePromise 相同', () => { const mock: any = { id: 'test', appId: 't', title: '', icon: '', rect: { x: 0, y: 0, w: 0, h: 0 }, prevRect: null, minW: 200, minH: 120, state: 'normal', el: { remove: vi.fn(), classList: { remove: vi.fn(), add: vi.fn() }, }, body: null, titleEl: null, timers: [], closePromise: null, } mock.closePromise = Promise.resolve('closed') const p1 = mock.closePromise const p2 = mock.closePromise expect(p1).toBe(p2) }) }) // ==================== 方法存在性 ==================== describe('方法完整性', () => { it('所有 WM 核心方法存在', () => { expect(typeof wm.setTitle).toBe('function') expect(typeof wm.minimize).toBe('function') expect(typeof wm.restore).toBe('function') expect(typeof wm.toggleZoom).toBe('function') expect(typeof wm.toggleFullscreen).toBe('function') expect(typeof wm.focus).toBe('function') expect(typeof wm.close).toBe('function') }) it('clampAll 不抛错', () => { expect(() => wm.clampAll()).not.toThrow() }) }) })