macos-web/tests/helpers.ts
2026-07-23 15:16:57 +08:00

88 lines
3.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* tests/helpers.ts — vitest 测试辅助函数
*
* 提供:
* - setupDOM() 设置完整 DOM 结构
* - createMockWin() 创建 Vue 组件的 win prop 对象
* - tick() / waitFor() 时间工具
* - 兼容旧 APIgetWM/getFS/getApps/getSys/getNotify/openApp/closeActiveWindow/等)
*/
import { setupTestDOM, createMockWin as _createMockWin } from './mocks'
// ---- 重新导出 mocks 中的函数 ----
export { createMockFS, createMockWM, createMockSys, createMockNotify, createMockStore } from './mocks'
export type { MockWin } from './mocks'
// ============================================================
// DOM 设置(兼容旧 API
// ============================================================
export const setupDOM = setupTestDOM
// ============================================================
// Mock Window 工厂(兼容旧 API
// ============================================================
export function createMockWin(opts?: { id?: string; appId?: string; data?: any }) {
return _createMockWin(opts)
}
// ============================================================
// 时间工具
// ============================================================
/** 模拟时间流逝(真实 setTimeout */
export function tick(ms: number): Promise<void> {
return new Promise(r => setTimeout(r, ms))
}
/** 等待元素可见 */
export async function waitFor(selector: string, timeout = 5000): Promise<HTMLElement> {
const start = Date.now()
while (Date.now() - start < timeout) {
const el = document.querySelector(selector) as HTMLElement
if (el && !el.classList.contains('hidden')) return el
await tick(50)
}
throw new Error(`Timeout waiting for: ${selector}`)
}
// ============================================================
// 兼容旧 APIwindow 全局对象访问器)
// ============================================================
export function getWM(): any { return (window as any).WM }
export function getFS(): any { return (window as any).FS }
export function getApps(): any { return (window as any).Apps }
export function getSys(): any { return (window as any).Sys }
export function getNotify(): any { return (window as any).Notify }
// ============================================================
// 兼容旧 API应用操作
// ============================================================
export async function openApp(appId: string, args?: any) {
const Apps = getApps(); const WM = getWM()
Apps.open(appId, args); await tick(200)
return WM.windows.length
}
export async function closeActiveWindow() {
const WM = getWM(); const win = WM.activeWin
if (win) await WM.close(win); await tick(200)
}
export function winCount(): number { return getWM().windows.length }
export function winTitles(): string[] { return getWM().windows.map((w: any) => w.appId + ':' + w.title) }
export async function unlockSystem() {
const Sys = getSys(); Sys.unlock(); await tick(500)
}
// ============================================================
// 启动序列(用于 Launchpad 等需要全系统初始化的测试)
// ============================================================
export async function bootSystem() {
setupDOM()
const { initSystem } = await import('../src/system')
initSystem()
const { Sys } = await import('../src/composables/useSys')
Sys.boot()
await tick(3100)
}