/**
* Vitest helpers for macOS-web tests.
* Mirrors the original tests/helpers.js but uses Vitest + happy-dom.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { initSystem } from '../src/system'
/** Set up the full DOM structure needed by the app (mirrors index.html) */
export function setupDOM() {
document.body.innerHTML = `
客人用户
点按或按 Enter 键解锁
密码不正确,请重试
已关机 点按屏幕以开机
`
}
/** Boot sequence: simulate what Sys.boot() does but skip animations */
export async function bootSystem() {
setupDOM()
// Init the system (this registers apps, sets up FS, etc.)
initSystem()
// Simulate boot completion by forcing immediate unlock
const { Sys } = await import('../src/composables/useSys')
Sys.boot()
// Fast-forward boot animation
await tick(3100)
}
/** Simulate time passage */
export function tick(ms: number): Promise {
return new Promise(r => setTimeout(r, ms))
}
/** Wait for an element to be visible */
export async function waitFor(selector: string, timeout = 5000): Promise {
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}`)
}
/** Get global WM */
export function getWM(): any {
return (window as any).WM
}
/** Get global FS */
export function getFS(): any {
return (window as any).FS
}
/** Get global Apps */
export function getApps(): any {
return (window as any).Apps
}
/** Get global Sys */
export function getSys(): any {
return (window as any).Sys
}
/** Get global Notify */
export function getNotify(): any {
return (window as any).Notify
}
/** Open an app and wait for window */
export async function openApp(appId: string, args?: any) {
const Apps = getApps()
const WM = getWM()
Apps.open(appId, args)
await tick(200)
// Return window count
return WM.windows.length
}
/** Close active window */
export async function closeActiveWindow() {
const WM = getWM()
const win = WM.activeWin
if (win) await WM.close(win)
await tick(200)
}
/** Get window count */
export function winCount(): number {
return getWM().windows.length
}
/** Get window titles */
export function winTitles(): string[] {
return getWM().windows.map((w: any) => w.appId + ':' + w.title)
}
/** Unlock the system */
export async function unlockSystem() {
const Sys = getSys()
// Simulate clicking the lockscreen
Sys.unlock()
await tick(500)
}