mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 01:05:30 +08:00
feat: 拓展中心加入机器人
This commit is contained in:
1 parent
4eaddc2d75
commit
a81b3733fa
31 files changed
+3510
-566
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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}$/)
|
||||
|
||||
Reference in new issue
Block a user