74 lines
2.8 KiB
TypeScript
74 lines
2.8 KiB
TypeScript
/**
|
||
* 11-clock: 时钟——Notify breakthrough、秒表计时器通知、防重复
|
||
* 原测试: Playwright browser → 改为 Notify + Mock Sys composable 层测试
|
||
*/
|
||
import { describe, it, expect, beforeAll, beforeEach } from 'vitest'
|
||
import { Notify, setSysForNotify } from '../../src/composables/useNotify'
|
||
|
||
// Mock Sys
|
||
const mockSys = {
|
||
settings: { notificationsEnabled: true, notifAllow: {} as Record<string, boolean>, focus: false }
|
||
}
|
||
setSysForNotify(mockSys)
|
||
|
||
describe('11-clock — 时钟通知', () => {
|
||
beforeAll(() => {
|
||
document.body.innerHTML = '<div id="notification-center" class="hidden"></div><div id="banner-container"></div><nav id="dock"></nav>'
|
||
})
|
||
beforeEach(() => { Notify.list = [] })
|
||
|
||
// ===== breakthrough 突破专注模式 =====
|
||
it('breakthrough 在专注模式下仍发送通知', () => {
|
||
mockSys.settings.focus = true
|
||
const n = Notify.send({ appId: 'clock', title: '计时器', body: '时间到!', breakthrough: true, silent: true })
|
||
expect(n).not.toBeNull()
|
||
expect(Notify.list.length).toBe(1)
|
||
mockSys.settings.focus = false
|
||
})
|
||
|
||
it('非 breakthrough 在专注模式下仍保存记录(silent 不弹横幅)', () => {
|
||
mockSys.settings.focus = true
|
||
const n = Notify.send({ appId: 'clock', title: '普通', body: '', silent: true })
|
||
expect(n).not.toBeNull()
|
||
expect(Notify.list.length).toBe(1)
|
||
mockSys.settings.focus = false
|
||
})
|
||
|
||
// ===== 防重复通知 =====
|
||
it('badgeCount 正确统计未读', () => {
|
||
Notify.send({ appId: 'clock', title: 'A', body: '1', silent: true })
|
||
Notify.send({ appId: 'clock', title: 'B', body: '2', silent: true })
|
||
expect(Notify.badgeCount('clock')).toBe(2)
|
||
})
|
||
|
||
it('markRead 减少未读计数', () => {
|
||
const n = Notify.send({ appId: 'clock', title: 'T', body: 'B', silent: true })
|
||
expect(Notify.badgeCount('clock')).toBe(1)
|
||
Notify.markRead(n!.id)
|
||
expect(Notify.badgeCount('clock')).toBe(0)
|
||
})
|
||
|
||
// ===== 通知开关 =====
|
||
it('notificationsEnabled=false 时 send 返回 null', () => {
|
||
mockSys.settings.notificationsEnabled = false
|
||
expect(Notify.send({ appId: 'clock', title: 'T', body: 'B', silent: true })).toBeNull()
|
||
mockSys.settings.notificationsEnabled = true
|
||
})
|
||
|
||
it('per-app notifAllow=false 时 send 返回 null', () => {
|
||
mockSys.settings.notifAllow['clock'] = false
|
||
expect(Notify.send({ appId: 'clock', title: 'T', body: 'B', silent: true })).toBeNull()
|
||
mockSys.settings.notifAllow['clock'] = true
|
||
})
|
||
|
||
it('allowed 判断总开关 + per-app 开关', () => {
|
||
expect(Notify.allowed('clock')).toBe(true)
|
||
mockSys.settings.notifAllow['clock'] = false
|
||
expect(Notify.allowed('clock')).toBe(false)
|
||
mockSys.settings.notifAllow['clock'] = true
|
||
mockSys.settings.notificationsEnabled = false
|
||
expect(Notify.allowed('clock')).toBe(false)
|
||
mockSys.settings.notificationsEnabled = true
|
||
})
|
||
})
|