feat: 拓展中心加入机器人

This commit is contained in:
Wxw-Gu committed 2026-11-18 17:42:17 +08:00
1 parent 116989a091
commit f16067d6d0
31 files changed
+3510 -566

No files matched your search

@@ -3,12 +3,7 @@ import path from 'node:path'
import { describe, expect, it } from 'vitest'
import { ExtensionTemplateCatalog } from '../../src/main/extensions/extension-template-catalog'
/**
* 打包产物验证:只读 `Resources/extension-templates`,不碰源码树。
*
* 这一步回答的是「删掉 examples 之后用户还能不能看到模板」——如果目录不存在
* (例如只跑了单元测试),整组跳过,不影响常规 CI。
*/
/** 打包产物验证:只读 `Resources/extension-templates`,不碰源码树。 */
const CANDIDATES = [
'dist/mac-arm64/TraceMemo.app/Contents/Resources/extension-templates',
'dist/mac/TraceMemo.app/Contents/Resources/extension-templates',
@@ -21,7 +16,7 @@ const packagedRoot = CANDIDATES.map((relative) => path.join(process.cwd(), relat
)
describe.skipIf(!packagedRoot)('打包后的模板资源', () => {
it('产物目录自带两个模板与索引', async () => {
it('产物目录自带三个模板与索引', async () => {
const catalog = new ExtensionTemplateCatalog({
templateRoot: packagedRoot as string,
getRecord: () => undefined,
@@ -34,6 +29,7 @@ describe.skipIf(!packagedRoot)('打包后的模板资源', () => {
const list = await catalog.list()
expect(list.map((item) => item.template.templateId).sort()).toEqual([
'douyin-sender',
'group-chat-bot',
'keyword-bot'
])
// 资源自包含:索引里的 zip 就在同一目录,且哈希匹配(damaged 会暴露不一致)。
@@ -49,7 +45,7 @@ describe.skipIf(!packagedRoot)('打包后的模板资源', () => {
probePython: () => '/usr/bin/python3',
probeChrome: () => '/usr/bin/chrome'
})
for (const templateId of ['keyword-bot', 'douyin-sender']) {
for (const templateId of ['keyword-bot', 'group-chat-bot', 'douyin-sender']) {
const resolved = await catalog.resolveZip(templateId)
expect(fs.existsSync(resolved.path)).toBe(true)
expect(resolved.template.sha256).toMatch(/^[a-f0-9]{64}$/)
+72
View File
@@ -336,3 +336,75 @@ describe('extension send gateway / video', () => {
).rejects.toMatchObject({ code: 'CAPABILITY_UNAVAILABLE' })
})
})
describe('extension send gateway / voice', () => {
const cleanup: string[] = []
function tempDir(prefix: string): string {
const root = fs.mkdtempSync(path.join(os.tmpdir(), prefix))
cleanup.push(root)
return root
}
afterEach(() => {
for (const root of cleanup.splice(0)) fs.rmSync(root, { recursive: true, force: true })
})
const voiceContext = {
extensionId: 'com.example.group-chat-bot',
permissions: ['wechat.send.voice' as const],
conversationScope: [] as string[]
}
it('缺少 wechat.send.voice 权限时拒绝', async () => {
const { instance } = gateway({ dataRoot: tempDir('tm-ext-voice-') })
await expect(
instance.sendVoice(
{ ...voiceContext, permissions: [] },
{ to: 'fixture@chatroom', text: '你好' }
)
).rejects.toMatchObject({ code: 'PERMISSION_DENIED' })
})
it('传入 text 时委托宿主 TTS 合成、经 Gateway 发送并在结束后清理临时音频', async () => {
const dataRoot = tempDir('tm-ext-voice-')
const generatedAudio = path.join(dataRoot, 'tts-sample.mp3')
fs.writeFileSync(generatedAudio, Buffer.from('fixture-audio'))
const synthesizeVoice = vi.fn(async () => ({ filePath: generatedAudio }))
const cleanupVoice = vi.fn()
const executeAction = vi.fn(async () => sentResult)
const instance = new ExtensionSendGateway({
getCapability: async () => ({
...readyCapability,
capabilities: { text: true, image: true, voice: true, video: true }
}),
checkEntitlement: async () => ({ ok: true, message: '授权有效', source: 'mac_license' }),
executeAction: executeAction as never,
getExtensionDataRoot: () => dataRoot,
synthesizeVoice,
cleanupVoice,
getFromId: () => 'wxid_self',
now: () => 1_000_000
})
const result = await instance.sendVoice(voiceContext, {
to: 'fixture@chatroom',
text: '大家晚上好',
voiceId: 'voice-123',
dedupeKey: 'evt-voice-1'
})
expect(result).toEqual({ actionId: 'action-1', status: 'sent' })
expect(synthesizeVoice).toHaveBeenCalledWith({ text: '大家晚上好', voiceId: 'voice-123' })
expect(executeAction).toHaveBeenCalledWith(
expect.objectContaining({
origin: 'extension',
purpose: 'extension_send',
content: { type: 'voice', path: fs.realpathSync(generatedAudio) },
metadata: { extensionId: 'com.example.group-chat-bot', fromId: 'wxid_self' }
})
)
expect(cleanupVoice).toHaveBeenCalledWith(generatedAudio)
})
})
@@ -75,11 +75,12 @@ function record(overrides: Partial<ExtensionRegistryRecord>): ExtensionRegistryR
}
describe('官方模板目录', () => {
it('列出两个模板,未安装时状态为 not-installed', async () => {
it('列出三个模板,未安装时状态为 not-installed', async () => {
const { catalog } = setup()
const list = await catalog.list()
expect(list.map((item) => item.template.templateId).sort()).toEqual([
'douyin-sender',
'group-chat-bot',
'keyword-bot'
])
expect(list.every((item) => item.state === 'not-installed')).toBe(true)
+3 -2
View File
@@ -39,18 +39,19 @@ function stageExamples(): { sourceRoot: string; lockFile: string } {
}
describe('官方模板构建', () => {
it('产出两个模板与索引,索引指向真实文件', () => {
it('产出三个模板与索引,索引指向真实文件', () => {
const { sourceRoot, lockFile } = stageExamples()
const outputDir = path.join(tempRoot(), 'out')
const result = buildTemplates({ sourceRoot, outputDir, lockFile })
expect(result.templates.map((item) => item.templateId).sort()).toEqual([
'douyin-sender',
'group-chat-bot',
'keyword-bot'
])
const index = fs.readJsonSync(path.join(outputDir, 'index.json'))
expect(index.version).toBe(1)
expect(index.templates).toHaveLength(2)
expect(index.templates).toHaveLength(3)
for (const template of index.templates) {
expect(fs.existsSync(path.join(outputDir, template.file))).toBe(true)
expect(template.sha256).toMatch(/^[a-f0-9]{64}$/)