macos-web/tests/cases/12-settings.test.ts
2026-07-20 14:30:44 +08:00

112 lines
4.1 KiB
TypeScript

/**
* 12-settings: 系统设置——Store 持久化、时区、WiFi/BT 状态恢复、switch ARIA
* 原测试: Playwright browser → 改为 Store + DEFAULT_SETTINGS composable 层测试
*/
import { describe, it, expect, beforeEach } from 'vitest'
import { store } from '../../src/composables/useStore'
import { WALLPAPERS, DEFAULT_SETTINGS } from '../../src/composables/useSettings'
import { tzValid, fmtMenuClock, fmtTime } from '../../src/utils'
describe('12-settings — 系统设置', () => {
beforeEach(() => { localStorage.clear() })
// ===== DEFAULT_SETTINGS 完整性 =====
it('DEFAULT_SETTINGS 包含所有关键字段', () => {
expect(DEFAULT_SETTINGS.appearance).toBe('light')
expect(DEFAULT_SETTINGS.wallpaper).toBe('monterey')
expect(DEFAULT_SETTINGS.dockSize).toBe(54)
expect(DEFAULT_SETTINGS.dockMagnify).toBe(true)
expect(DEFAULT_SETTINGS.dockPosition).toBe('bottom')
expect(DEFAULT_SETTINGS.dockAutohide).toBe(false)
expect(DEFAULT_SETTINGS.notificationsEnabled).toBe(true)
expect(DEFAULT_SETTINGS.h24).toBe(false)
expect(DEFAULT_SETTINGS.timezone).toBe('local')
expect(DEFAULT_SETTINGS.volume).toBe(0.6)
expect(DEFAULT_SETTINGS.muted).toBe(false)
expect(DEFAULT_SETTINGS.wifi).toBe(true)
expect(DEFAULT_SETTINGS.bluetooth).toBe(true)
expect(DEFAULT_SETTINGS.computerName).toBe('MacBook Pro')
})
// ===== WALLPAPERS =====
it('WALLPAPERS 有 6 张', () => { expect(WALLPAPERS.length).toBe(6) })
it('每张壁纸有 id/name/src', () => {
WALLPAPERS.forEach(w => {
expect(w.id).toBeTruthy()
expect(w.name).toBeTruthy()
expect(w.src).toContain('/assets/wallpapers/')
})
})
// ===== Store 持久化 =====
it('settings 持久化: wifiNetwork + kbRepeat', () => {
const s = { ...DEFAULT_SETTINGS, wifiNetwork: 'CoffeeShop_Guest', kbRepeat: 3 }
store.set('settings', s)
const r = store.get('settings', DEFAULT_SETTINGS)
expect(r.wifiNetwork).toBe('CoffeeShop_Guest')
expect(r.kbRepeat).toBe(3)
})
it('蓝牙连接状态持久化', () => {
const s = { ...DEFAULT_SETTINGS, btDevices: { 'MX Master 3S': true, 'AirPods Pro': false } }
store.set('settings', s)
const r = store.get('settings', DEFAULT_SETTINGS)
expect(r.btDevices['MX Master 3S']).toBe(true)
})
it('时区持久化: Asia/Tokyo', () => {
const s = { ...DEFAULT_SETTINGS, timezone: 'Asia/Tokyo' }
store.set('settings', s)
expect(store.get('settings', DEFAULT_SETTINGS).timezone).toBe('Asia/Tokyo')
})
it('Store remove 后恢复默认', () => {
store.set('settings', { wallpaper: 'bigsur' })
store.remove('settings')
expect(store.get('settings', DEFAULT_SETTINGS).wallpaper).toBe('monterey')
})
// ===== 时区工具 =====
it('tzValid 验证有效时区', () => {
expect(tzValid('Asia/Shanghai')).toBe(true)
expect(tzValid('Asia/Tokyo')).toBe(true)
expect(tzValid('America/New_York')).toBe(true)
})
it('tzValid 拒绝无效时区', () => {
expect(tzValid('')).toBe(false)
expect(tzValid('local')).toBe(false)
expect(tzValid('Fake/Zone')).toBe(false)
})
it('fmtTime 不同时区返回不同时间', () => {
const d = new Date('2024-01-15T12:00:00Z')
const tokyo = fmtTime(d, true, 'Asia/Tokyo')
const ny = fmtTime(d, true, 'America/New_York')
expect(tokyo).not.toBe(ny) // 不同时区时间不同
})
// ===== switch ARIA (在 dom 环境下验证) =====
it('Store 损坏数据回退默认', () => {
localStorage.setItem(store.PREFIX + 'settings', '{bad json')
const r = store.get('settings', DEFAULT_SETTINGS)
expect(r.appearance).toBe('light')
expect(r.wallpaper).toBe('monterey')
})
// ===== 暗色壁纸变体 =====
it('bigsur 壁纸有暗色变体', () => {
const bigsur = WALLPAPERS.find(w => w.id === 'bigsur')
expect(bigsur).toBeDefined()
expect(bigsur!.dark).toBeTruthy()
expect(bigsur!.dark).toContain('big-sur-night')
})
// ===== 搜索相关设置 =====
it('siri 搜索开关默认开启', () => {
expect(DEFAULT_SETTINGS.siriApps).toBe(true)
expect(DEFAULT_SETTINGS.siriFiles).toBe(true)
expect(DEFAULT_SETTINGS.siriSettings).toBe(true)
})
})