Skip to content

Commit 7818341

Browse files
authored
Merge pull request #188 from kartikkabadi/fix/pronunciation-dict-shape
fix(speech): serialize pronunciation dictionary correctly
2 parents bd52360 + 76e7869 commit 7818341

3 files changed

Lines changed: 103 additions & 5 deletions

File tree

src/commands/speech/synthesize.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ export default defineCommand({
9292
if (flags.subtitles) body.subtitle_enable = true; // Correct API parameter name
9393

9494
if (flags.pronunciation) {
95-
body.pronunciation_dict = (flags.pronunciation as string[]).map(p => {
96-
const [from, to] = p.split('/');
97-
return { tone: to || from!, text: from! };
98-
});
95+
body.pronunciation_dict = {
96+
tone: flags.pronunciation as string[],
97+
};
9998
}
10099

101100
if (dryRun(config, body)) return;

src/types/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export interface SpeechRequest {
105105
channel?: number;
106106
};
107107
language_boost?: string;
108-
pronunciation_dict?: Array<{ tone: string; text: string }>;
108+
pronunciation_dict?: { tone: string[] };
109109
output_format?: 'url' | 'hex';
110110
stream?: boolean;
111111
subtitle_enable?: boolean; // Correct API parameter name (not 'subtitle')

test/commands/speech/synthesize.test.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,105 @@ describe('speech synthesize command', () => {
202202
console.log = originalLog;
203203
}
204204
});
205+
206+
it('--pronunciation serializes multiple values with the API-compatible shape', async () => {
207+
const config = {
208+
apiKey: 'test-key',
209+
region: 'global' as const,
210+
baseUrl: 'https://api.mmx.io',
211+
output: 'json' as const,
212+
timeout: 10,
213+
verbose: false,
214+
quiet: false,
215+
noColor: true,
216+
yes: false,
217+
dryRun: true,
218+
nonInteractive: true,
219+
async: false,
220+
};
221+
222+
const originalLog = console.log;
223+
let output = '';
224+
console.log = (msg: string) => { output += msg; };
225+
226+
try {
227+
await synthesizeCommand.execute(config, {
228+
text: '处理这个危险的情况。',
229+
pronunciation: [
230+
'处理/(chu3)(li3)',
231+
'危险/dangerous',
232+
],
233+
quiet: false,
234+
verbose: false,
235+
noColor: true,
236+
yes: false,
237+
dryRun: true,
238+
help: false,
239+
nonInteractive: true,
240+
async: false,
241+
});
242+
243+
const parsed = JSON.parse(output);
244+
245+
// pronunciation_dict is an object with a tone string array — not the
246+
// old array-of-{text,tone} shape — and each value is preserved verbatim
247+
// (no splitting, trimming, or rewriting), covering both the pinyin form
248+
// and the plain-replacement form shown in the official API docs.
249+
expect(Array.isArray(parsed.request.pronunciation_dict)).toBe(false);
250+
expect(parsed.request.pronunciation_dict).toEqual({
251+
tone: [
252+
'处理/(chu3)(li3)',
253+
'危险/dangerous',
254+
],
255+
});
256+
} finally {
257+
console.log = originalLog;
258+
}
259+
});
260+
261+
it('--pronunciation with a single value serializes under tone', async () => {
262+
const config = {
263+
apiKey: 'test-key',
264+
region: 'global' as const,
265+
baseUrl: 'https://api.mmx.io',
266+
output: 'json' as const,
267+
timeout: 10,
268+
verbose: false,
269+
quiet: false,
270+
noColor: true,
271+
yes: false,
272+
dryRun: true,
273+
nonInteractive: true,
274+
async: false,
275+
};
276+
277+
const originalLog = console.log;
278+
let output = '';
279+
console.log = (msg: string) => { output += msg; };
280+
281+
try {
282+
await synthesizeCommand.execute(config, {
283+
text: 'Omg, the real danger is not that computers start thinking.',
284+
pronunciation: ['Omg/Oh my god'],
285+
quiet: false,
286+
verbose: false,
287+
noColor: true,
288+
yes: false,
289+
dryRun: true,
290+
help: false,
291+
nonInteractive: true,
292+
async: false,
293+
});
294+
295+
const parsed = JSON.parse(output);
296+
297+
expect(parsed.request.pronunciation_dict).toEqual({
298+
tone: ['Omg/Oh my god'],
299+
});
300+
} finally {
301+
console.log = originalLog;
302+
}
303+
});
205304
});
206305

207306
describe('speech synthesize format validation', () => {

0 commit comments

Comments
 (0)