| 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('explicit canonical model wins, while actual voice remains in Core', () => { |
| 17 | assert.equal(metadata(options({ model: 'mimo-v2-tts', voice_prompt: 'Design prompt' })).model, 'mimo-v2-tts') |
| 18 | assert.equal(metadata(options({ model: 'mimo-v2.5-tts-voicedesign', has_voice: true, voice_nonempty: true, instruction: 'warm' })).voice, 'omit') |
| 19 | assert.equal(metadata(options({ model: 'mimo-v2.5-tts-voicedesign', has_clone_path: true, instruction: 'warm' })).voice, 'clone') |
| 20 | assert.deepEqual(Object.keys(metadata(options({ has_voice: true, voice_nonempty: true }))).sort(), ['instruction', 'model', 'voice']) |
| 21 | }) |
| 22 | test('Rust whitespace including NEL is trimmed but BOM remains significant', () => { |
| 23 | assert.equal(metadata(options({ instruction: '\u0085\u2007calm\u3000' })).instruction, 'calm') |
| 24 | assert.equal(metadata(options({ instruction: '\ufeffcalm\ufeff' })).instruction, '\ufeffcalm\ufeff') |
| 25 | assert.equal(metadata(options({ voice_prompt: '\u0085', instruction: '\u2007', model: 'mimo-v2-tts' })).instruction, null) |
| 26 | assert.equal(metadata(options({ voice_prompt: '', instruction: ' warm ' })).model, 'mimo-v2.5-tts-voicedesign') |
| 27 | }) |
| 28 | test('ASCII format normalization and pcm alias match the Rust contract', () => { |
| 29 | for (const [value, expected] of [['WAV', 'wav'], [' pcm ', 'pcm16'], ['\u0085MP3\u0085', 'mp3'], ['pcm16', 'pcm16']]) assert.deepEqual(metadata(format(value)), { format: expected }) |
| 30 | assert.equal(transformStockSnapshot(format('\ufeffwav')).result.success, false) |
| 31 | assert.equal(metadata(format('flac')).error, "unsupported speech format 'flac' (allowed: wav, mp3, pcm16)") |
| 32 | assert.equal(metadata(format('flac', 'cli')).error, "Unsupported speech format 'flac' (allowed: wav, mp3, pcm16)") |
| 33 | }) |
| 34 | test('surface-specific failures preserve original CLI and tool messages', () => { |
| 35 | for (const surface of ['cli', 'tool']) { |
| 36 | const tool = surface === 'tool' |
| 37 | 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') |
| 38 | 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>') |
| 39 | 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') |
| 40 | assert.equal(transformStockSnapshot(options({ surface, model: 'mimo-chat' })).result.success, false) |
| 41 | } |
| 42 | }) |
| 43 | test('empty CLI voice presence still conflicts with clone and otherwise defaults', () => { |
| 44 | assert.equal(metadata(options({ surface: 'cli', has_voice: true })).voice, 'default') |
| 45 | assert.equal(transformStockSnapshot(options({ surface: 'cli', has_voice: true, has_clone_path: true })).result.success, false) |
| 46 | }) |
| 47 | test('malformed, contradictory and oversized snapshots refuse', () => { |
| 48 | 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)) |
| 49 | }) |
| 50 | |
| 51 | test('actual pinned harness redeems one captured speech plan without sample/path authority', async () => { |
| 52 | const owner = { plugin_id: 'host:harness', generation: 8, owner_token: 'fixture-owner' } |
| 53 | const calls = [] |
| 54 | 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) |
| 55 | const plan = await runner.run({ owner, execution_id: 'captured-speech', ticket: 'fixture-ticket', deadline_ms: 1000 }, new AbortController().signal) |
| 56 | assert.deepEqual(plan.result.metadata, { model: 'mimo-v2.5-tts-voiceclone', instruction: null, voice: 'raw' }) |
| 57 | assert.deepEqual(calls, [['exec/redeem', { owner, execution_id: 'captured-speech', ticket: 'fixture-ticket' }]]) |
| 58 | assert.equal(JSON.stringify(plan).includes('base64'), false) |
| 59 | await runner.dispose() |
| 60 | }) |
| 61 | test('speech runner cancellation settles before a late reply and never retries', async () => { |
| 62 | const owner = { plugin_id: 'host:harness', generation: 8, owner_token: 'fixture-owner' } |
| 63 | const abort = new AbortController() |
| 64 | let finish, calls = 0 |
| 65 | const runner = createHarnessModule({ request() { calls++; return new Promise(resolve => { finish = resolve }) } }, owner) |
| 66 | const pending = runner.run({ owner, execution_id: 'captured-speech', ticket: 'fixture-ticket', deadline_ms: 1000 }, abort.signal) |
| 67 | abort.abort(); await assert.rejects(pending, /cancelled/) |
| 68 | finish(options({})); assert.equal(calls, 1); await runner.dispose() |
| 69 | }) |
| 70 |