返回 CodeWhale
speech-adapters.test.mjs
根目录 / crates / engine / extension-host / test / speech-adapters.test.mjs
1 import test from 'node:test'
2 import assert from 'node:assert/strict'
3 import { transformStockSnapshot } from '../dist/stock-adapters.mjs'
4 import { createHarnessModule } from '../dist/builtin/harness.mjs'
5 const project = (operation, input) => ({ kind: 'stock_adapter', operation, input })
6 const options = extra => project('speech_options', { surface: 'tool', model: null, instruction: null, voice_prompt: null, has_clone_path: false, has_voice: false, voice_nonempty: false, voice_is_data_uri: false, ...extra })
7 const metadata = input => transformStockSnapshot(input).result.metadata
8 const format = (value, surface = 'tool') => project('speech_format', { surface, format: value })
9
10 test('speech default, design and clone selection use captured presence only', () => {
11 assert.deepEqual(metadata(options({})), { model: 'mimo-v2.5-tts', instruction: null, voice: 'default' })
12 assert.deepEqual(metadata(options({ voice_prompt: 'Warm', instruction: ' Slow ' })), { model: 'mimo-v2.5-tts-voicedesign', instruction: 'Warm\n\nSlow', voice: 'omit' })
13 assert.deepEqual(metadata(options({ has_clone_path: true })), { model: 'mimo-v2.5-tts-voiceclone', instruction: null, voice: 'clone' })
14 assert.deepEqual(metadata(options({ has_voice: true, voice_nonempty: true, voice_is_data_uri: true })), { model: 'mimo-v2.5-tts-voiceclone', instruction: null, voice: 'raw' })
15 })
16 test('MiniMax speech preserves the selected model, voice mode and wire format', () => {
17 assert.deepEqual(metadata(options({ minimax: true, model: 'speech-2.8-hd' })), { model: 'speech-2.8-hd', instruction: null, voice: 'default' })
18 assert.equal(metadata(options({ minimax: true, model: 'speech-2.8-turbo', has_voice: true, voice_nonempty: true })).voice, 'raw')
19 for (const [format, expected] of [['FLAC', 'flac'], ['pcm16', 'pcm']]) {
20 assert.equal(metadata(project('speech_format', { surface: 'tool', minimax: true, format })).format, expected)
21 }
22 })
23 test('explicit canonical model wins, while actual voice remains in Core', () => {
24 assert.equal(metadata(options({ model: 'mimo-v2-tts', voice_prompt: 'Design prompt' })).model, 'mimo-v2-tts')
25 assert.equal(metadata(options({ model: 'mimo-v2.5-tts-voicedesign', has_voice: true, voice_nonempty: true, instruction: 'warm' })).voice, 'omit')
26 assert.equal(metadata(options({ model: 'mimo-v2.5-tts-voicedesign', has_clone_path: true, instruction: 'warm' })).voice, 'clone')
27 assert.deepEqual(Object.keys(metadata(options({ has_voice: true, voice_nonempty: true }))).sort(), ['instruction', 'model', 'voice'])
28 })
29 test('Rust whitespace including NEL is trimmed but BOM remains significant', () => {
30 assert.equal(metadata(options({ instruction: '\u0085\u2007calm\u3000' })).instruction, 'calm')
31 assert.equal(metadata(options({ instruction: '\ufeffcalm\ufeff' })).instruction, '\ufeffcalm\ufeff')
32 assert.equal(metadata(options({ voice_prompt: '\u0085', instruction: '\u2007', model: 'mimo-v2-tts' })).instruction, null)
33 assert.equal(metadata(options({ voice_prompt: '', instruction: ' warm ' })).model, 'mimo-v2.5-tts-voicedesign')
34 })
35 test('ASCII format normalization and pcm alias match the Rust contract', () => {
36 for (const [value, expected] of [['WAV', 'wav'], [' pcm ', 'pcm16'], ['\u0085MP3\u0085', 'mp3'], ['pcm16', 'pcm16']]) assert.deepEqual(metadata(format(value)), { format: expected })
37 assert.equal(transformStockSnapshot(format('\ufeffwav')).result.success, false)
38 assert.equal(metadata(format('flac')).error, "unsupported speech format 'flac' (allowed: wav, mp3, pcm16)")
39 assert.equal(metadata(format('flac', 'cli')).error, "Unsupported speech format 'flac' (allowed: wav, mp3, pcm16)")
40 })
41 test('surface-specific failures preserve original CLI and tool messages', () => {
42 for (const surface of ['cli', 'tool']) {
43 const tool = surface === 'tool'
44 assert.equal(metadata(options({ surface, has_clone_path: true, has_voice: true })).error, tool ? 'use either clone_voice or voice for cloned voice data, not both' : 'Use either --clone-voice or --voice for cloned voice data, not both')
45 assert.equal(metadata(options({ surface, model: 'mimo-v2.5-tts-voiceclone' })).error, tool ? 'mimo-v2.5-tts-voiceclone requires clone_voice <mp3|wav> or voice <data-uri>' : 'mimo-v2.5-tts-voiceclone requires --clone-voice <mp3|wav> or --voice <data-uri>')
46 assert.equal(metadata(options({ surface, voice_prompt: '' })).error, tool ? 'mimo-v2.5-tts-voicedesign requires voice_prompt or instruction' : 'mimo-v2.5-tts-voicedesign requires --voice-prompt or --instruction to describe the voice')
47 assert.equal(transformStockSnapshot(options({ surface, model: 'mimo-chat' })).result.success, false)
48 }
49 })
50 test('empty CLI voice presence still conflicts with clone and otherwise defaults', () => {
51 assert.equal(metadata(options({ surface: 'cli', has_voice: true })).voice, 'default')
52 assert.equal(transformStockSnapshot(options({ surface: 'cli', has_voice: true, has_clone_path: true })).result.success, false)
53 })
54 test('malformed, contradictory and oversized snapshots refuse', () => {
55 for (const input of [options({ surface: 'daemon' }), options({ has_voice: false, voice_is_data_uri: true }), options({ voice_nonempty: true }), options({ has_voice: 'true' }), options({ model: 42 }), options({ instruction: 'x'.repeat(1024 * 1024) }), format(null)]) assert.throws(() => transformStockSnapshot(input))
56 })
57
58 test('actual pinned harness redeems one captured speech plan without sample/path authority', async () => {
59 const owner = { plugin_id: 'host:harness', generation: 8, owner_token: 'fixture-owner' }
60 const calls = []
61 const runner = createHarnessModule({ async request(method, input) { calls.push([method, input]); return options({ has_voice: true, voice_nonempty: true, voice_is_data_uri: true }) } }, owner)
62 const plan = await runner.run({ owner, execution_id: 'captured-speech', ticket: 'fixture-ticket', deadline_ms: 1000 }, new AbortController().signal)
63 assert.deepEqual(plan.result.metadata, { model: 'mimo-v2.5-tts-voiceclone', instruction: null, voice: 'raw' })
64 assert.deepEqual(calls, [['exec/redeem', { owner, execution_id: 'captured-speech', ticket: 'fixture-ticket' }]])
65 assert.equal(JSON.stringify(plan).includes('base64'), false)
66 await runner.dispose()
67 })
68 test('speech runner cancellation settles before a late reply and never retries', async () => {
69 const owner = { plugin_id: 'host:harness', generation: 8, owner_token: 'fixture-owner' }
70 const abort = new AbortController()
71 let finish, calls = 0
72 const runner = createHarnessModule({ request() { calls++; return new Promise(resolve => { finish = resolve }) } }, owner)
73 const pending = runner.run({ owner, execution_id: 'captured-speech', ticket: 'fixture-ticket', deadline_ms: 1000 }, abort.signal)
74 abort.abort(); await assert.rejects(pending, /cancelled/)
75 finish(options({})); assert.equal(calls, 1); await runner.dispose()
76 })
77
77 lines Plain Text