@myclaw163/clawclaw-cli 0.6.101 → 0.6.107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,387 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
3
+ import { tmpdir } from 'os';
4
+ import { join } from 'path';
5
+ import {
6
+ CustomTtsError,
7
+ loadCustomTtsConfig,
8
+ parseCustomTtsConfig,
9
+ synthesizeCustomTts,
10
+ type CustomTtsConfig,
11
+ } from './custom-tts.js';
12
+ import { getWorkspaceConfigFile } from './init-command.js';
13
+
14
+ const tempDirs: string[] = [];
15
+
16
+ afterEach(() => {
17
+ vi.unstubAllGlobals();
18
+ vi.restoreAllMocks();
19
+ for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
20
+ });
21
+
22
+ function config(overrides: Partial<CustomTtsConfig['response']> = {}): CustomTtsConfig {
23
+ return {
24
+ customize_tts: true,
25
+ request: {
26
+ url: 'https://tts.example/v1/speech',
27
+ method: 'POST',
28
+ headers: {
29
+ Authorization: 'Bearer secret',
30
+ 'Content-Type': 'application/json',
31
+ },
32
+ text_field: 'input',
33
+ body: {
34
+ nested: ['fixed value'],
35
+ voice: 'female-01',
36
+ },
37
+ timeout_ms: 5_000,
38
+ },
39
+ response: {
40
+ type: 'binary',
41
+ content_type: 'audio/mpeg',
42
+ filename: 'tts.mp3',
43
+ ...overrides,
44
+ },
45
+ };
46
+ }
47
+
48
+ describe('parseCustomTtsConfig', () => {
49
+ it('treats a missing or false switch as disabled', () => {
50
+ expect(parseCustomTtsConfig({})).toBeNull();
51
+ expect(parseCustomTtsConfig({ customize_tts: false })).toBeNull();
52
+ });
53
+
54
+ it('validates enabled custom TTS settings', () => {
55
+ expect(() => parseCustomTtsConfig({ customize_tts: 'true' })).toThrow(CustomTtsError);
56
+ expect(() => parseCustomTtsConfig({
57
+ customize_tts: true,
58
+ request: { url: 'file:///tmp/tts', text_field: 'input' },
59
+ })).toThrow(/http:\/\/ or https:\/\//);
60
+ expect(() => parseCustomTtsConfig({
61
+ customize_tts: true,
62
+ request: { url: 'https://tts.example/v1/speech' },
63
+ })).toThrow(/request\.text_field or place \{\{text\}\}/);
64
+ expect(() => parseCustomTtsConfig({
65
+ customize_tts: true,
66
+ request: {
67
+ url: 'https://tts.example/v1/speech',
68
+ text_field: '',
69
+ body: { messages: [{ content: '{{text}}' }] },
70
+ },
71
+ })).toThrow(/request\.text_field must be a non-empty string/);
72
+ expect(() => parseCustomTtsConfig({
73
+ customize_tts: true,
74
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input', body: [] },
75
+ })).toThrow(/request\.body must be a JSON object/);
76
+ expect(() => parseCustomTtsConfig({
77
+ customize_tts: true,
78
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input' },
79
+ response: { format: 'png' },
80
+ })).toThrow(/unsupported audio format "png"/);
81
+ expect(() => parseCustomTtsConfig({
82
+ customize_tts: true,
83
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input' },
84
+ response: { type: 'binary', format_path: 'format' },
85
+ })).toThrow(/format_path is only supported/);
86
+ });
87
+
88
+ it('applies request and binary response defaults', () => {
89
+ expect(parseCustomTtsConfig({
90
+ customize_tts: true,
91
+ request: { url: 'https://tts.example/v1/speech', text_field: 'text' },
92
+ })).toEqual({
93
+ customize_tts: true,
94
+ request: {
95
+ url: 'https://tts.example/v1/speech',
96
+ method: 'POST',
97
+ headers: { 'Content-Type': 'application/json' },
98
+ text_field: 'text',
99
+ body: {},
100
+ timeout_ms: 10_000,
101
+ },
102
+ response: { type: 'binary' },
103
+ });
104
+
105
+ expect(parseCustomTtsConfig({
106
+ customize_tts: true,
107
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input' },
108
+ response: { filename: 'voice.mp3' },
109
+ })?.response).toEqual({ type: 'binary', filename: 'voice.mp3' });
110
+
111
+ expect(parseCustomTtsConfig({
112
+ customize_tts: true,
113
+ request: { url: 'https://tts.example/v1/speech', text_field: 'input' },
114
+ response: {
115
+ type: 'json_base64',
116
+ path: 'data.audio',
117
+ format_path: 'data.format',
118
+ },
119
+ })?.response).toEqual({
120
+ type: 'json_base64',
121
+ path: 'data.audio',
122
+ format_path: 'data.format',
123
+ });
124
+ });
125
+
126
+ it('accepts a nested text placeholder without request.text_field', () => {
127
+ expect(parseCustomTtsConfig({
128
+ customize_tts: true,
129
+ request: {
130
+ url: 'https://tts.example/v1/speech',
131
+ body: {
132
+ model: 'mimo-v2.5-tts',
133
+ messages: [{ role: 'assistant', content: '{{text}}' }],
134
+ },
135
+ },
136
+ response: { type: 'json_base64', path: 'choices.0.message.audio.data', format: 'wav' },
137
+ })?.request).toMatchObject({
138
+ body: {
139
+ model: 'mimo-v2.5-tts',
140
+ messages: [{ role: 'assistant', content: '{{text}}' }],
141
+ },
142
+ });
143
+ });
144
+ });
145
+
146
+ describe('loadCustomTtsConfig', () => {
147
+ it('reads only the tts child from workspace config.json without modifying other fields', () => {
148
+ const workspace = mkdtempSync(join(tmpdir(), 'ccl-workspace-config-'));
149
+ tempDirs.push(workspace);
150
+ const path = getWorkspaceConfigFile(workspace);
151
+ const workspaceConfig = {
152
+ future_feature: { enabled: true },
153
+ tts: config(),
154
+ };
155
+ writeFileSync(path, JSON.stringify(workspaceConfig, null, 2), 'utf8');
156
+
157
+ expect(loadCustomTtsConfig(workspace)).toEqual(config());
158
+ expect(JSON.parse(readFileSync(path, 'utf8'))).toEqual(workspaceConfig);
159
+ });
160
+
161
+ it('treats a workspace config without tts as disabled', () => {
162
+ const workspace = mkdtempSync(join(tmpdir(), 'ccl-workspace-config-'));
163
+ tempDirs.push(workspace);
164
+ writeFileSync(getWorkspaceConfigFile(workspace), JSON.stringify({ future_feature: true }), 'utf8');
165
+
166
+ expect(loadCustomTtsConfig(workspace)).toBeNull();
167
+ });
168
+ });
169
+
170
+ describe('synthesizeCustomTts', () => {
171
+ it('injects text into the configured field and returns a binary audio response', async () => {
172
+ let captured: RequestInit | undefined;
173
+ vi.stubGlobal('fetch', vi.fn(async (_url: string, init: RequestInit) => {
174
+ captured = init;
175
+ return new Response(new Uint8Array([1, 2, 3]), {
176
+ status: 200,
177
+ headers: { 'Content-Type': 'audio/mpeg' },
178
+ });
179
+ }));
180
+
181
+ const fieldConfig = config();
182
+ fieldConfig.request.text_field = 'content';
183
+ const result = await synthesizeCustomTts('你好', fieldConfig);
184
+
185
+ expect(JSON.parse(String(captured?.body))).toEqual({
186
+ nested: ['fixed value'],
187
+ voice: 'female-01',
188
+ content: '你好',
189
+ });
190
+ expect((captured?.headers as Record<string, string>).Authorization).toBe('Bearer secret');
191
+ expect([...result.audio]).toEqual([1, 2, 3]);
192
+ expect(result).toMatchObject({ contentType: 'audio/mpeg', filename: 'tts.mp3' });
193
+ });
194
+
195
+ it('replaces nested text placeholders for MIMO-style request bodies', async () => {
196
+ let captured: RequestInit | undefined;
197
+ vi.stubGlobal('fetch', vi.fn(async (_url: string, init: RequestInit) => {
198
+ captured = init;
199
+ return new Response(new Uint8Array([1, 2, 3]), {
200
+ status: 200,
201
+ headers: { 'Content-Type': 'audio/wav' },
202
+ });
203
+ }));
204
+
205
+ const mimoConfig = config({ content_type: undefined, filename: undefined });
206
+ delete mimoConfig.request.text_field;
207
+ mimoConfig.request.body = {
208
+ model: 'mimo-v2.5-tts',
209
+ messages: [{ role: 'assistant', content: '{{text}}' }],
210
+ metadata: { repeated: 'speech={{text}}' },
211
+ };
212
+
213
+ const result = await synthesizeCustomTts('你好,MIMO', mimoConfig);
214
+
215
+ expect(JSON.parse(String(captured?.body))).toEqual({
216
+ model: 'mimo-v2.5-tts',
217
+ messages: [{ role: 'assistant', content: '你好,MIMO' }],
218
+ metadata: { repeated: 'speech=你好,MIMO' },
219
+ });
220
+ expect(mimoConfig.request.body).toEqual({
221
+ model: 'mimo-v2.5-tts',
222
+ messages: [{ role: 'assistant', content: '{{text}}' }],
223
+ metadata: { repeated: 'speech={{text}}' },
224
+ });
225
+ expect(result).toMatchObject({ contentType: 'audio/wav', filename: 'tts.wav' });
226
+ });
227
+
228
+ it('fills both text_field and nested placeholders when both are configured', async () => {
229
+ let captured: RequestInit | undefined;
230
+ vi.stubGlobal('fetch', vi.fn(async (_url: string, init: RequestInit) => {
231
+ captured = init;
232
+ return new Response(new Uint8Array([1]), {
233
+ status: 200,
234
+ headers: { 'Content-Type': 'audio/mpeg' },
235
+ });
236
+ }));
237
+
238
+ const mixedConfig = config();
239
+ mixedConfig.request.body = { messages: [{ content: '{{text}}' }] };
240
+ await synthesizeCustomTts('same speech', mixedConfig);
241
+
242
+ expect(JSON.parse(String(captured?.body))).toEqual({
243
+ messages: [{ content: 'same speech' }],
244
+ input: 'same speech',
245
+ });
246
+ });
247
+
248
+ it('reads base64 audio from a configured JSON path', async () => {
249
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
250
+ data: { audio: Buffer.from('audio-data').toString('base64') },
251
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
252
+
253
+ const result = await synthesizeCustomTts('hello', config({
254
+ type: 'json_base64',
255
+ path: 'data.audio',
256
+ content_type: 'audio/wav',
257
+ filename: 'voice.wav',
258
+ }));
259
+
260
+ expect(result.audio.toString()).toBe('audio-data');
261
+ expect(result).toMatchObject({ contentType: 'audio/wav', filename: 'voice.wav' });
262
+ });
263
+
264
+ it('gets base64 audio format from a configured response path', async () => {
265
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
266
+ data: {
267
+ audio: Buffer.from('audio-data').toString('base64'),
268
+ format: 'ogg',
269
+ },
270
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
271
+
272
+ const result = await synthesizeCustomTts('hello', config({
273
+ type: 'json_base64',
274
+ path: 'data.audio',
275
+ format_path: 'data.format',
276
+ content_type: undefined,
277
+ filename: undefined,
278
+ }));
279
+
280
+ expect(result.audio.toString()).toBe('audio-data');
281
+ expect(result).toMatchObject({ contentType: 'audio/ogg', filename: 'tts.ogg' });
282
+ });
283
+
284
+ it('gets base64 audio format from a data URL', async () => {
285
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
286
+ audio: `data:audio/wav;base64,${Buffer.from('audio-data').toString('base64')}`,
287
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
288
+
289
+ const result = await synthesizeCustomTts('hello', config({
290
+ type: 'json_base64',
291
+ path: 'audio',
292
+ content_type: undefined,
293
+ filename: undefined,
294
+ }));
295
+
296
+ expect(result).toMatchObject({ contentType: 'audio/wav', filename: 'tts.wav' });
297
+ });
298
+
299
+ it('uses a configured format when the TTS response has no format metadata', async () => {
300
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
301
+ audio: Buffer.from('audio-data').toString('base64'),
302
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
303
+
304
+ const result = await synthesizeCustomTts('hello', config({
305
+ type: 'json_base64',
306
+ path: 'audio',
307
+ format: 'webm',
308
+ content_type: undefined,
309
+ filename: undefined,
310
+ }));
311
+
312
+ expect(result).toMatchObject({ contentType: 'audio/webm', filename: 'tts.webm' });
313
+ });
314
+
315
+ it('rejects base64 audio when neither the response nor config provides a format', async () => {
316
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
317
+ audio: Buffer.from('audio-data').toString('base64'),
318
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
319
+
320
+ await expect(synthesizeCustomTts('hello', config({
321
+ type: 'json_base64',
322
+ path: 'audio',
323
+ content_type: undefined,
324
+ filename: undefined,
325
+ }))).rejects.toThrow(/Cannot determine custom TTS audio format/);
326
+ });
327
+
328
+ it('rejects an unsupported format returned by the TTS service', async () => {
329
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
330
+ base64: Buffer.from('not-audio').toString('base64'),
331
+ format: 'png',
332
+ }), { status: 200, headers: { 'Content-Type': 'application/json' } })));
333
+
334
+ await expect(synthesizeCustomTts('hello', config({
335
+ type: 'json_base64',
336
+ path: 'base64',
337
+ format_path: 'format',
338
+ content_type: undefined,
339
+ filename: undefined,
340
+ }))).rejects.toThrow(/unsupported audio format "png"/);
341
+ });
342
+
343
+ it('rejects a binary response that is actually JSON', async () => {
344
+ vi.stubGlobal('fetch', vi.fn(async () => new Response('{"error":"not audio"}', {
345
+ status: 200,
346
+ headers: { 'Content-Type': 'application/json' },
347
+ })));
348
+
349
+ await expect(synthesizeCustomTts('hello', config())).rejects.toThrow(/unsupported audio format "application\/json"/);
350
+ });
351
+
352
+ it('requires config format when a binary response omits Content-Type', async () => {
353
+ vi.stubGlobal('fetch', vi.fn(async () => new Response(new Uint8Array([1, 2, 3]), { status: 200 })));
354
+
355
+ await expect(synthesizeCustomTts('hello', config({
356
+ content_type: undefined,
357
+ filename: undefined,
358
+ }))).rejects.toThrow(/Cannot determine custom TTS audio format/);
359
+ });
360
+
361
+ it('downloads audio returned by a configured JSON URL path', async () => {
362
+ const fetchMock = vi.fn(async (url: string) => {
363
+ if (url === 'https://tts.example/v1/speech') {
364
+ return new Response(JSON.stringify({ result: { audio_url: 'https://cdn.example/voice.ogg' } }), {
365
+ status: 200,
366
+ headers: { 'Content-Type': 'application/json' },
367
+ });
368
+ }
369
+ return new Response(new Uint8Array([4, 5, 6]), {
370
+ status: 200,
371
+ headers: { 'Content-Type': 'audio/ogg' },
372
+ });
373
+ });
374
+ vi.stubGlobal('fetch', fetchMock);
375
+
376
+ const result = await synthesizeCustomTts('hello', config({
377
+ type: 'json_url',
378
+ path: 'result.audio_url',
379
+ content_type: undefined,
380
+ filename: undefined,
381
+ }));
382
+
383
+ expect(fetchMock).toHaveBeenCalledTimes(2);
384
+ expect(result).toMatchObject({ contentType: 'audio/ogg', filename: 'tts.ogg' });
385
+ expect([...result.audio]).toEqual([4, 5, 6]);
386
+ });
387
+ });