feat: mac语音转换silk格式修复

This commit is contained in:
Wxw-Gu
2026-08-31 10:55:52 +08:00
parent 5acfa14475
commit 6cbf9af662
14 changed files with 970 additions and 12 deletions
+50 -3
View File
@@ -14,6 +14,10 @@ const getSettings = vi.fn()
const saveSettings = vi.fn()
const listVoices = vi.fn()
const openApiKeys = vi.fn()
const checkVoiceEncodingEnvironment = vi.fn()
const installPilk = vi.fn()
const openPythonDownload = vi.fn()
const openFfmpegDownload = vi.fn()
const response = {
success: true,
@@ -70,13 +74,21 @@ describe('TextToSpeechPage', () => {
}
}))
openApiKeys.mockReset().mockResolvedValue({ success: true })
checkVoiceEncodingEnvironment.mockReset()
installPilk.mockReset()
openPythonDownload.mockReset().mockResolvedValue({ success: true })
openFfmpegDownload.mockReset().mockResolvedValue({ success: true })
Object.defineProperty(window, 'api', {
configurable: true,
value: {
getTextToSpeechSettings: getSettings,
saveTextToSpeechSettings: saveSettings,
listTextToSpeechVoices: listVoices,
openFishAudioApiKeys: openApiKeys
openFishAudioApiKeys: openApiKeys,
checkPersonalWechatVoiceEncodingEnvironment: checkVoiceEncodingEnvironment,
installPersonalWechatPilk: installPilk,
openPersonalWechatVoicePythonDownload: openPythonDownload,
openPersonalWechatVoiceFfmpegDownload: openFfmpegDownload
}
})
})
@@ -136,11 +148,46 @@ describe('TextToSpeechPage', () => {
await waitFor(() => expect(saveSettings).toHaveBeenCalledWith({ clearApiKey: true }))
})
it('keeps the speech page free of personal WeChat runtime controls', async () => {
it('shows the macOS voice encoding environment entry without exposing OneBot controls', async () => {
render(<TextToSpeechPage onNotice={vi.fn()} />)
expect(await screen.findByText('API 设置')).toBeVisible()
expect(screen.queryByText(/微信|OneBot/i)).not.toBeInTheDocument()
expect(screen.getByText('语音编码环境')).toBeVisible()
expect(screen.queryByText(/OneBot/i)).not.toBeInTheDocument()
expect(screen.queryByText('支持的微信版本')).not.toBeInTheDocument()
})
it('checks the actual encoding environment and offers pilk repair', async () => {
const onNotice = vi.fn()
const incompleteEnvironment = {
state: 'incomplete' as const,
ready: false,
runtimeReady: true,
python: { ready: true, executable: '/usr/local/bin/python3', version: '3.12.11' },
pilk: { ready: false, error: '未安装' },
ffmpeg: { ready: true, executable: '/runtime/ffmpeg', version: '6.1' },
encoder: 'go-silk' as const,
message: '语音编码环境不完整,OneBot 可能回退到 go-silk'
}
const readyEnvironment = {
...incompleteEnvironment,
state: 'ready' as const,
ready: true,
pilk: { ready: true, version: '0.2.4', path: '/user/pilk/__init__.py' },
encoder: 'pilk' as const,
message: '语音编码环境正常,可以使用 pilk 编码'
}
checkVoiceEncodingEnvironment.mockResolvedValue(incompleteEnvironment)
installPilk.mockResolvedValue({ success: true, environment: readyEnvironment })
render(<TextToSpeechPage onNotice={onNotice} />)
fireEvent.click(await screen.findByRole('button', { name: '检查编码环境' }))
expect(await screen.findByText('⚠ 编码环境不完整')).toBeVisible()
expect(screen.getByRole('button', { name: '安装 pilk' })).toBeEnabled()
fireEvent.click(screen.getByRole('button', { name: '安装 pilk' }))
await waitFor(() => expect(installPilk).toHaveBeenCalledTimes(1))
expect(onNotice).toHaveBeenCalledWith('语音编码环境已修复。重新启动语音发送服务后生效。')
expect(await screen.findByText('✓ 编码环境正常')).toBeVisible()
})
})
+36
View File
@@ -739,6 +739,42 @@ for (const appearanceTheme of ['light', 'dark'] as const) {
})
}
for (const appearanceTheme of ['light', 'dark'] as const) {
test(`SETTINGS-06A voice encoding environment ${appearanceTheme} visual @visual`, async () => {
const fixture = await launchTestApp({ now: visualNow, appearanceTheme })
const pageErrors: Error[] = []
fixture.page.on('pageerror', (error) => pageErrors.push(error))
try {
await fixture.setWindowContentSize(visualViewport)
await fixture.page
.getByRole('navigation', { name: '一级导航' })
.getByRole('button', { name: '设置' })
.click()
await fixture.page.getByRole('button', { name: '文字转语音' }).click()
const environment = fixture.page.locator('.voice-encoding-environment')
await environment.scrollIntoViewIfNeeded()
await expect(environment.getByRole('heading', { name: '语音编码环境' })).toBeVisible()
await expect(environment.getByRole('button', { name: '检查编码环境' })).toBeVisible()
await expect(environment.getByRole('status')).toContainText('尚未检查')
await expect(fixture.page.locator('html')).toHaveAttribute('data-theme', appearanceTheme)
expect(
await fixture.page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth)
).toBe(true)
expect(pageErrors).toEqual([])
await clearScreenshotFocus(fixture.page)
await expect(environment).toHaveScreenshot(
`settings-text-to-speech-voice-environment-${appearanceTheme}.png`,
{
animations: 'disabled',
caret: 'hide'
}
)
} finally {
await fixture.close()
}
})
}
for (const appearanceTheme of ['light', 'dark'] as const) {
test(`SETTINGS-07 voice recognition controls ${appearanceTheme} visual @visual`, async () => {
const fixture = await launchTestApp({ now: visualNow, appearanceTheme })
@@ -98,6 +98,14 @@ describe('preload IPC contract', () => {
await api.getPersonalWechatSenderStatus()
expect(invoke).toHaveBeenLastCalledWith('wechat-personal:getStatus')
await api.checkPersonalWechatVoiceEncodingEnvironment()
expect(invoke).toHaveBeenLastCalledWith('wechat-personal:checkVoiceEnvironment')
await api.installPersonalWechatPilk()
expect(invoke).toHaveBeenLastCalledWith('wechat-personal:installPilk')
await api.openPersonalWechatVoicePythonDownload()
expect(invoke).toHaveBeenLastCalledWith('wechat-personal:openVoicePythonDownload')
await api.openPersonalWechatVoiceFfmpegDownload()
expect(invoke).toHaveBeenLastCalledWith('wechat-personal:openVoiceFfmpegDownload')
await api.checkPersonalWechatSenderStatus('4567')
expect(invoke).toHaveBeenLastCalledWith('wechat-personal:checkStatus', '4567')
await api.rebindPersonalWechatSender()
@@ -0,0 +1,139 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { findRuntime, buildEnvironment, writeLog } = vi.hoisted(() => ({
findRuntime: vi.fn(),
buildEnvironment: vi.fn(),
writeLog: vi.fn()
}))
vi.mock('../../src/main/services/personal-wechat-send-service', () => ({
findPersonalWechatRuntime: findRuntime,
buildPersonalWechatRuntimeEnvironment: buildEnvironment
}))
vi.mock('../../src/main/app-logger', () => ({ appLogger: { write: writeLog } }))
import { PersonalWechatVoiceEnvironmentService } from '../../src/main/services/personal-wechat-voice-environment-service'
const runtime = {
root: '/runtime',
executable: '/runtime/onebot/onebot',
workingDirectory: '/runtime/onebot',
configDirectory: '/runtime/wechat_version',
logPath: '/runtime/onebot/log/macos.log'
}
describe('PersonalWechatVoiceEnvironmentService', () => {
let pilkAvailable = true
const commands: Array<{ executable: string; args: string[]; env: NodeJS.ProcessEnv }> = []
beforeEach(() => {
pilkAvailable = true
commands.length = 0
findRuntime.mockReset().mockReturnValue(runtime)
buildEnvironment.mockReset().mockReturnValue({
PATH: '/runtime/bin',
PYTHONPATH: '/runtime/python'
})
writeLog.mockReset()
})
function createService(): PersonalWechatVoiceEnvironmentService {
return new PersonalWechatVoiceEnvironmentService({
platform: 'darwin',
architecture: 'arm64',
findRuntime,
buildEnvironment,
now: () => new Date('2026-08-31T00:00:00.000Z'),
runCommand: async (executable, args, options) => {
commands.push({ executable, args, env: options.env })
if (executable === 'python3') {
return {
stdout: JSON.stringify({ executable: '/runtime/python3', version: '3.12.11' }),
stderr: ''
}
}
if (executable === '/runtime/python3' && args[0] === '-c') {
if (!pilkAvailable) throw new Error("ModuleNotFoundError: No module named 'pilk'")
return {
stdout: JSON.stringify({ version: '0.2.4', path: '/runtime/python/pilk/__init__.py' }),
stderr: ''
}
}
if (executable === '/usr/bin/which') {
return { stdout: '/runtime/bin/ffmpeg\n', stderr: '' }
}
if (executable === 'ffmpeg') {
return { stdout: 'ffmpeg version 7.0 fixture\n', stderr: '' }
}
if (executable === '/runtime/python3' && args[0] === '-m') {
pilkAvailable = true
return { stdout: 'installed\n', stderr: '' }
}
throw new Error(`unexpected command: ${executable} ${args.join(' ')}`)
}
})
}
it('checks pilk with the Python and environment used by OneBot', async () => {
const environment = await createService().check()
expect(environment).toMatchObject({
state: 'ready',
ready: true,
runtimeReady: true,
encoder: 'pilk',
python: { ready: true, executable: '/runtime/python3', version: '3.12.11' },
pilk: { ready: true, version: '0.2.4', path: '/runtime/python/pilk/__init__.py' },
ffmpeg: { ready: true, executable: '/runtime/bin/ffmpeg', version: '7.0' }
})
expect(commands.find((command) => command.executable === 'python3')?.env).toEqual({
PATH: '/runtime/bin',
PYTHONPATH: '/runtime/python'
})
expect(commands.some((command) => command.executable === '/runtime/python3')).toBe(true)
expect(writeLog).toHaveBeenCalledWith(
expect.objectContaining({
scope: 'VoiceRuntime',
message: 'Voice encoding environment is ready'
})
)
})
it('reports go-silk fallback and installs the exact pilk version with the detected Python', async () => {
pilkAvailable = false
const service = createService()
const before = await service.check()
expect(before).toMatchObject({ state: 'incomplete', ready: false, encoder: 'go-silk' })
expect(before.pilk.ready).toBe(false)
const result = await service.installPilk()
expect(result.success).toBe(true)
expect(result.environment.pilk).toMatchObject({ ready: true, version: '0.2.4' })
expect(commands).toContainEqual(
expect.objectContaining({
executable: '/runtime/python3',
args: ['-m', 'pip', 'install', '--user', 'pilk==0.2.4']
})
)
})
it('keeps the UI contract explicit when Python cannot be started', async () => {
const service = new PersonalWechatVoiceEnvironmentService({
platform: 'darwin',
architecture: 'arm64',
findRuntime,
buildEnvironment,
runCommand: async () => {
throw new Error('spawn python3 ENOENT')
}
})
const environment = await service.check()
expect(environment).toMatchObject({ state: 'incomplete', ready: false, encoder: 'unavailable' })
expect(environment.python).toMatchObject({ ready: false })
expect(environment.pilk).toMatchObject({ ready: false, error: 'Python 未找到,无法检查 pilk' })
})
})
+1 -1
View File
@@ -95,6 +95,6 @@ describe('personal WeChat runtime security invariants', () => {
resolve('src/main/services/personal-wechat-send-service.ts'),
'utf8'
)
expect(senderSource).toContain('buildRuntimePythonPath(preflight.runtime.root)')
expect(senderSource).toContain('buildPersonalWechatRuntimeEnvironment(preflight.runtime.root)')
})
})