mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 17:25:15 +08:00
203 lines
6.7 KiB
TypeScript
203 lines
6.7 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 { parseExtensionManifest } from '../../src/shared/extensions/manifest'
|
|
import { ExtensionNavigationStore } from '../../src/main/extensions/extension-navigation-store'
|
|
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-nav-'))
|
|
tempDirs.push(dir)
|
|
return dir
|
|
}
|
|
|
|
afterEach(() => {
|
|
for (const dir of tempDirs.splice(0)) fs.removeSync(dir)
|
|
})
|
|
|
|
function writeSampleExtension(
|
|
dir: string,
|
|
id: string,
|
|
name: string,
|
|
navigation?: { label: string; icon?: string; pageId?: string }
|
|
): void {
|
|
fs.ensureDirSync(path.join(dir, 'ui'))
|
|
fs.writeFileSync(path.join(dir, 'ui', 'index.html'), '<!doctype html><html><body>hi</body></html>')
|
|
fs.writeJsonSync(path.join(dir, 'manifest.json'), {
|
|
manifestVersion: 1,
|
|
id,
|
|
name,
|
|
version: '1.0.0',
|
|
apiVersion: '1',
|
|
permissions: ['storage'],
|
|
entrypoints: { page: 'ui/index.html' },
|
|
contributions: {
|
|
pages: [{ id: 'main', title: name }],
|
|
automationActions: [],
|
|
...(navigation ? { navigation } : {})
|
|
}
|
|
})
|
|
}
|
|
|
|
describe('动态一级导航声明与持久化', () => {
|
|
it('manifest 校验 contributions.navigation 并兼容旧版无 navigation 清单', () => {
|
|
const valid = parseExtensionManifest({
|
|
manifestVersion: 1,
|
|
id: 'com.example.digest',
|
|
name: 'AI 晨报',
|
|
version: '1.0.0',
|
|
apiVersion: '1',
|
|
permissions: ['storage'],
|
|
entrypoints: { page: 'ui/index.html' },
|
|
contributions: {
|
|
pages: [{ id: 'main', title: '晨报' }],
|
|
navigation: { label: '晨报', icon: 'digest', pageId: 'main', order: 10 }
|
|
}
|
|
})
|
|
expect(valid.ok).toBe(true)
|
|
expect(valid.manifest?.contributions.navigation).toEqual({
|
|
label: '晨报',
|
|
icon: 'digest',
|
|
pageId: 'main',
|
|
order: 10
|
|
})
|
|
|
|
const legacy = parseExtensionManifest({
|
|
manifestVersion: 1,
|
|
id: 'com.example.legacy',
|
|
name: '旧扩展',
|
|
version: '1.0.0',
|
|
apiVersion: '1',
|
|
permissions: ['storage'],
|
|
entrypoints: { page: 'ui/index.html' }
|
|
})
|
|
expect(legacy.ok).toBe(true)
|
|
expect(legacy.manifest?.contributions.navigation).toBeUndefined()
|
|
|
|
const badIcon = parseExtensionManifest({
|
|
manifestVersion: 1,
|
|
id: 'com.example.bad',
|
|
name: '坏图标',
|
|
version: '1.0.0',
|
|
apiVersion: '1',
|
|
permissions: [],
|
|
entrypoints: { page: 'ui/index.html' },
|
|
contributions: {
|
|
pages: [{ id: 'main', title: '首页' }],
|
|
navigation: { label: '测试', icon: 'evil-svg' }
|
|
}
|
|
})
|
|
expect(badIcon.ok).toBe(false)
|
|
|
|
const missingPage = parseExtensionManifest({
|
|
manifestVersion: 1,
|
|
id: 'com.example.nopage',
|
|
name: '无页面',
|
|
version: '1.0.0',
|
|
apiVersion: '1',
|
|
permissions: [],
|
|
entrypoints: { background: 'bg.js' },
|
|
contributions: {
|
|
navigation: { label: '测试', icon: 'spark' }
|
|
}
|
|
})
|
|
expect(missingPage.ok).toBe(false)
|
|
})
|
|
|
|
it('固定、排序、重启恢复与卸载自动清理完整生效', async () => {
|
|
const root = makeTempDir()
|
|
const extADir = path.join(root, 'src-a')
|
|
const extBDir = path.join(root, 'src-b')
|
|
writeSampleExtension(extADir, 'com.example.alpha', '晨报助手', {
|
|
label: '晨报',
|
|
icon: 'digest',
|
|
pageId: 'main'
|
|
})
|
|
writeSampleExtension(extBDir, 'com.example.beta', '群聊机器人', {
|
|
label: '机器人',
|
|
icon: 'bot',
|
|
pageId: 'main'
|
|
})
|
|
|
|
const paths = createExtensionPaths(path.join(root, 'userData'))
|
|
const createManager = () =>
|
|
new ExtensionManager({
|
|
paths,
|
|
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: () => ''
|
|
}
|
|
})
|
|
|
|
const manager1 = createManager()
|
|
await manager1.initialize()
|
|
expect((await manager1.installFromDirectory(extADir)).success).toBe(true)
|
|
expect((await manager1.installFromDirectory(extBDir)).success).toBe(true)
|
|
|
|
expect(manager1.pinNavigation('com.example.alpha').success).toBe(true)
|
|
expect(manager1.pinNavigation('com.example.beta').success).toBe(true)
|
|
expect(manager1.listPinnedNavigation().map((item) => item.extensionId)).toEqual([
|
|
'com.example.alpha',
|
|
'com.example.beta'
|
|
])
|
|
|
|
// 调整排序。
|
|
manager1.reorderPinnedNavigation(['com.example.beta', 'com.example.alpha'])
|
|
expect(manager1.listPinnedNavigation().map((item) => item.extensionId)).toEqual([
|
|
'com.example.beta',
|
|
'com.example.alpha'
|
|
])
|
|
await manager1.dispose()
|
|
|
|
// 模拟应用重启:新建 Manager,固定项与顺序必须恢复。
|
|
const manager2 = createManager()
|
|
await manager2.initialize()
|
|
expect(manager2.listPinnedNavigation().map((item) => [item.extensionId, item.label, item.icon])).toEqual([
|
|
['com.example.beta', '机器人', 'bot'],
|
|
['com.example.alpha', '晨报', 'digest']
|
|
])
|
|
|
|
// 卸载扩展后自动从一级导航移除。
|
|
await manager2.uninstall('com.example.beta', true)
|
|
expect(manager2.listPinnedNavigation().map((item) => item.extensionId)).toEqual([
|
|
'com.example.alpha'
|
|
])
|
|
|
|
// 底层存储同步已清理。
|
|
const store = new ExtensionNavigationStore(paths.pinnedNavigationFile)
|
|
expect(store.list().map((item) => item.extensionId)).toEqual(['com.example.alpha'])
|
|
await manager2.dispose()
|
|
})
|
|
})
|