mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 17:25:15 +08:00
196 lines
7.6 KiB
TypeScript
196 lines
7.6 KiB
TypeScript
import fs from 'fs-extra'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
|
import { createExtensionPaths } from '../../src/main/extensions/extension-paths'
|
|
import { ExtensionManager } from '../../src/main/extensions/extension-manager'
|
|
import type { PersonalWechatSendCapability } from '../../src/shared/personal-wechat'
|
|
import type { WechatActionResult } from '../../src/shared/wechat-action'
|
|
|
|
const tempDirs: string[] = []
|
|
|
|
function makeTempDir(): string {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'tm-ext-notify-'))
|
|
tempDirs.push(dir)
|
|
return dir
|
|
}
|
|
|
|
afterEach(() => {
|
|
for (const dir of tempDirs.splice(0)) fs.removeSync(dir)
|
|
})
|
|
|
|
function writeExtension(dir: string, id: string, name: string, permissions: string[]): void {
|
|
fs.ensureDirSync(path.join(dir, 'ui'))
|
|
fs.writeFileSync(path.join(dir, 'ui', 'index.html'), '<!doctype html><html><body>ok</body></html>')
|
|
fs.writeJsonSync(path.join(dir, 'manifest.json'), {
|
|
manifestVersion: 1,
|
|
id,
|
|
name,
|
|
version: '1.0.0',
|
|
apiVersion: '1',
|
|
permissions,
|
|
entrypoints: { page: 'ui/index.html' },
|
|
contributions: {
|
|
pages: [{ id: 'main', title: name }],
|
|
navigation: { label: name, icon: 'bell', pageId: 'main' }
|
|
}
|
|
})
|
|
}
|
|
|
|
function makeManager(userData: string): ExtensionManager {
|
|
return new ExtensionManager({
|
|
paths: createExtensionPaths(userData),
|
|
appVersion: '2.7.0',
|
|
getCapability: async () =>
|
|
({
|
|
supported: true,
|
|
ready: true,
|
|
status: 'ready',
|
|
capabilities: { text: true, image: true, voice: true },
|
|
senderStatus: {},
|
|
message: ''
|
|
}) as unknown as PersonalWechatSendCapability,
|
|
checkEntitlement: async () => ({ ok: true, message: '授权有效', source: 'mac_license' }),
|
|
executeAction: async (): Promise<WechatActionResult> => ({
|
|
actionId: 'a1',
|
|
status: 'sent',
|
|
decision: 'allow',
|
|
startedAt: '2026-01-01T00:00:00.000Z',
|
|
finishedAt: '2026-01-01T00:00:01.000Z'
|
|
}),
|
|
queryMessages: async () => [],
|
|
listGroups: async () => [],
|
|
aiChat: async () => ({ text: 'ok' }),
|
|
getAccountId: () => 'wxid_fixture',
|
|
getSelfUsernames: () => ['wxid_fixture'],
|
|
isDatabaseReady: () => true,
|
|
secretCipher: {
|
|
isAvailable: () => false,
|
|
encrypt: () => Buffer.alloc(0),
|
|
decrypt: () => ''
|
|
}
|
|
})
|
|
}
|
|
|
|
describe('ExtensionNotificationCenter 持久化、去重与生命周期', () => {
|
|
it('校验权限、拒绝停用扩展写入、支持 dedupeKey 去重与重启恢复,并在卸载后清理', async () => {
|
|
const root = makeTempDir()
|
|
const userData = path.join(root, 'userData')
|
|
const extA = path.join(root, 'ext-a')
|
|
const extB = path.join(root, 'ext-b')
|
|
writeExtension(extA, 'com.example.digest', '晨报', ['notifications.create'])
|
|
writeExtension(extB, 'com.example.bot', '机器人', ['notifications.create'])
|
|
|
|
const manager = makeManager(userData)
|
|
await manager.initialize()
|
|
await manager.installFromDirectory(extA)
|
|
await manager.installFromDirectory(extB)
|
|
|
|
// 未启用扩展必须拒绝写入新通知。
|
|
expect(() =>
|
|
manager.createNotification('com.example.digest', { title: '不应写入' })
|
|
).toThrowError(/停用/)
|
|
|
|
// 启用后若撤销 notifications.create 权限,主进程必须独立拒绝写入。
|
|
await manager.setEnabled('com.example.digest', true)
|
|
await manager.revokePermission('com.example.digest', 'notifications.create')
|
|
expect(() =>
|
|
manager.createNotification('com.example.digest', { title: '无权限' })
|
|
).toThrowError(/notifications\.create/)
|
|
|
|
// 重新授权后正常写入,并支持 dedupeKey 去重。
|
|
await manager.grantPermissions('com.example.digest', ['notifications.create'])
|
|
await manager.setEnabled('com.example.bot', true)
|
|
|
|
const first = manager.createNotification('com.example.digest', {
|
|
title: '今日晨报已生成',
|
|
body: '覆盖 3 个群聊',
|
|
level: 'success',
|
|
dedupeKey: 'morning-2026-12-01'
|
|
})
|
|
expect(first.deduped).toBe(false)
|
|
|
|
const duplicate = manager.createNotification('com.example.digest', {
|
|
title: '今日晨报已更新',
|
|
body: '覆盖 4 个群聊',
|
|
level: 'info',
|
|
dedupeKey: 'morning-2026-12-01'
|
|
})
|
|
expect(duplicate.deduped).toBe(true)
|
|
expect(duplicate.id).toBe(first.id)
|
|
expect(manager.listNotifications('com.example.digest')).toHaveLength(1)
|
|
expect(manager.listNotifications('com.example.digest')[0].title).toBe('今日晨报已更新')
|
|
|
|
// 另一个扩展使用相同 dedupeKey 不会跨扩展碰撞。
|
|
const other = manager.createNotification('com.example.bot', {
|
|
title: '机器人提醒',
|
|
level: 'warning',
|
|
dedupeKey: 'morning-2026-12-01'
|
|
})
|
|
expect(other.deduped).toBe(false)
|
|
|
|
// 固定 digest 到一级菜单后,digest 的未读在 unreadByExtension,而 bot 的未读聚合到 unpinnedUnread。
|
|
manager.pinNavigation('com.example.digest')
|
|
const summary = manager.getNotificationSummary()
|
|
expect(summary.totalUnread).toBe(2)
|
|
expect(summary.unpinnedUnread).toBe(1)
|
|
expect(summary.unreadByExtension['com.example.digest']).toBe(1)
|
|
expect(summary.unreadByExtension['com.example.bot']).toBe(1)
|
|
|
|
// 标记单条已读。
|
|
expect(manager.markNotificationsRead({ id: first.id })).toBe(1)
|
|
expect(manager.getNotificationSummary().totalUnread).toBe(1)
|
|
|
|
// 停用扩展后拒绝新通知。
|
|
await manager.setEnabled('com.example.bot', false)
|
|
expect(() =>
|
|
manager.createNotification('com.example.bot', { title: '停用后新通知' })
|
|
).toThrowError(/停用/)
|
|
|
|
await manager.dispose()
|
|
|
|
// 模拟重启恢复后通知与未读计数保持一致。
|
|
const manager2 = makeManager(userData)
|
|
await manager2.initialize()
|
|
expect(manager2.listNotifications()).toHaveLength(2)
|
|
expect(manager2.getNotificationSummary().unpinnedUnread).toBe(1)
|
|
|
|
// 卸载扩展后清理该扩展所有宿主通知状态。
|
|
await manager2.uninstall('com.example.bot', true)
|
|
expect(manager2.listNotifications('com.example.bot')).toEqual([])
|
|
expect(manager2.getNotificationSummary().totalUnread).toBe(0)
|
|
await manager2.dispose()
|
|
})
|
|
|
|
it('单扩展高频写入触发防刷屏限频,且标记已读严格按扩展隔离', async () => {
|
|
const root = makeTempDir()
|
|
const userData = path.join(root, 'userData')
|
|
const extA = path.join(root, 'ext-flood')
|
|
const extB = path.join(root, 'ext-peer')
|
|
writeExtension(extA, 'com.example.flood', '刷屏测试', ['notifications.create'])
|
|
writeExtension(extB, 'com.example.peer', '相邻扩展', ['notifications.create'])
|
|
const manager = makeManager(userData)
|
|
await manager.initialize()
|
|
await manager.installFromDirectory(extA)
|
|
await manager.installFromDirectory(extB)
|
|
await manager.setEnabled('com.example.flood', true)
|
|
await manager.setEnabled('com.example.peer', true)
|
|
|
|
const peerNotice = manager.createNotification('com.example.peer', { title: 'B 扩展通知' })
|
|
// 使用 flood 的 extensionId 尝试标记 peer 的通知已读不得生效。
|
|
expect(
|
|
manager.markNotificationsRead({ extensionId: 'com.example.flood', id: peerNotice.id })
|
|
).toBe(0)
|
|
expect(manager.listNotifications('com.example.peer')[0].read).toBe(false)
|
|
|
|
// 连续创建 20 条新通知成功,第 21 条触发 CONCURRENCY_LIMITED。
|
|
for (let i = 0; i < 20; i += 1) {
|
|
manager.createNotification('com.example.flood', { title: `通知 ${i}` })
|
|
}
|
|
expect(() =>
|
|
manager.createNotification('com.example.flood', { title: '第 21 条刷屏通知' })
|
|
).toThrowError(/过于频繁/)
|
|
await manager.dispose()
|
|
})
|
|
})
|