|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { maybeApplyTtsToPayload } from "../../../../packages/speech-core/src/tts.ts"; |
| 7 | +import { buildWebchatAudioContentBlocksFromReplyPayloads } from "../../../../src/gateway/server-methods/chat-webchat-media.ts"; |
| 8 | +import { createPluginRecord } from "../../../../src/plugins/loader-records.ts"; |
| 9 | +import { createPluginRegistry } from "../../../../src/plugins/registry.ts"; |
| 10 | +import { |
| 11 | + resetPluginRuntimeStateForTest, |
| 12 | + setActivePluginRegistry, |
| 13 | +} from "../../../../src/plugins/runtime.ts"; |
| 14 | + |
| 15 | +const noopLogger = { |
| 16 | + info() {}, |
| 17 | + warn() {}, |
| 18 | + error() {}, |
| 19 | + debug() {}, |
| 20 | +}; |
| 21 | + |
| 22 | +function installMockTtsProvider() { |
| 23 | + const registry = createPluginRegistry({ |
| 24 | + logger: noopLogger, |
| 25 | + runtime: {}, |
| 26 | + activateGlobalSideEffects: false, |
| 27 | + }); |
| 28 | + const record = createPluginRecord({ |
| 29 | + id: "qa-webchat-auto-tts", |
| 30 | + name: "QA WebChat Auto TTS", |
| 31 | + source: "test/e2e/qa-lab/media/webchat-auto-tts.e2e.test.ts", |
| 32 | + origin: "global", |
| 33 | + enabled: true, |
| 34 | + configSchema: false, |
| 35 | + }); |
| 36 | + const synthesizeCalls: string[] = []; |
| 37 | + |
| 38 | + registry.registerSpeechProvider(record, { |
| 39 | + id: "mock", |
| 40 | + label: "Mock", |
| 41 | + autoSelectOrder: 1, |
| 42 | + isConfigured: () => true, |
| 43 | + synthesize: async (request) => { |
| 44 | + synthesizeCalls.push(request.text); |
| 45 | + return { |
| 46 | + audioBuffer: Buffer.from("voice"), |
| 47 | + fileExtension: ".ogg", |
| 48 | + outputFormat: "ogg", |
| 49 | + voiceCompatible: request.target === "voice-note", |
| 50 | + }; |
| 51 | + }, |
| 52 | + }); |
| 53 | + setActivePluginRegistry(registry.registry); |
| 54 | + return synthesizeCalls; |
| 55 | +} |
| 56 | + |
| 57 | +describe("QA WebChat auto TTS", () => { |
| 58 | + afterEach(() => { |
| 59 | + resetPluginRuntimeStateForTest(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("synthesizes only the final WebChat tail and exposes trusted local audio", async () => { |
| 63 | + resetPluginRuntimeStateForTest(); |
| 64 | + const synthesizeCalls = installMockTtsProvider(); |
| 65 | + const prefsPath = path.join(os.tmpdir(), `openclaw-webchat-tts-${process.pid}.json`); |
| 66 | + let mediaPath: string | undefined; |
| 67 | + |
| 68 | + try { |
| 69 | + const text = "WebChat streams block text; dispatch synthesizes one TTS tail with kind final."; |
| 70 | + const cfg = { |
| 71 | + messages: { |
| 72 | + tts: { |
| 73 | + enabled: true, |
| 74 | + provider: "mock", |
| 75 | + prefsPath, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } satisfies OpenClawConfig; |
| 79 | + |
| 80 | + const blockResult = await maybeApplyTtsToPayload({ |
| 81 | + payload: { text }, |
| 82 | + cfg, |
| 83 | + channel: "webchat", |
| 84 | + kind: "block", |
| 85 | + }); |
| 86 | + expect(blockResult.mediaUrl).toBeUndefined(); |
| 87 | + expect(blockResult.text).toBe(text); |
| 88 | + expect(synthesizeCalls).toEqual([]); |
| 89 | + |
| 90 | + const tailResult = await maybeApplyTtsToPayload({ |
| 91 | + payload: { text }, |
| 92 | + cfg, |
| 93 | + channel: "webchat", |
| 94 | + kind: "final", |
| 95 | + }); |
| 96 | + mediaPath = tailResult.mediaUrl; |
| 97 | + |
| 98 | + expect(synthesizeCalls).toEqual([text]); |
| 99 | + expect(mediaPath).toMatch(/voice-\d+\.ogg$/); |
| 100 | + if (!mediaPath || !fs.existsSync(mediaPath)) { |
| 101 | + throw new Error("expected final WebChat TTS to write local audio"); |
| 102 | + } |
| 103 | + expect(tailResult).toMatchObject({ |
| 104 | + spokenText: text, |
| 105 | + trustedLocalMedia: true, |
| 106 | + }); |
| 107 | + |
| 108 | + const trustedBlocks = await buildWebchatAudioContentBlocksFromReplyPayloads( |
| 109 | + [ |
| 110 | + { |
| 111 | + mediaUrl: mediaPath, |
| 112 | + audioAsVoice: tailResult.audioAsVoice, |
| 113 | + spokenText: text, |
| 114 | + trustedLocalMedia: true, |
| 115 | + }, |
| 116 | + ], |
| 117 | + { localRoots: [path.dirname(mediaPath)] }, |
| 118 | + ); |
| 119 | + const untrustedBlocks = await buildWebchatAudioContentBlocksFromReplyPayloads( |
| 120 | + [{ mediaUrl: mediaPath }], |
| 121 | + { localRoots: [path.dirname(mediaPath)] }, |
| 122 | + ); |
| 123 | + |
| 124 | + expect(trustedBlocks).toHaveLength(1); |
| 125 | + expect(trustedBlocks[0]).toMatchObject({ |
| 126 | + type: "attachment", |
| 127 | + attachment: { |
| 128 | + kind: "audio", |
| 129 | + label: path.basename(mediaPath), |
| 130 | + mimeType: "audio/ogg", |
| 131 | + url: fs.realpathSync(mediaPath), |
| 132 | + }, |
| 133 | + }); |
| 134 | + expect(untrustedBlocks).toHaveLength(0); |
| 135 | + } finally { |
| 136 | + if (mediaPath) { |
| 137 | + fs.rmSync(path.dirname(mediaPath), { recursive: true, force: true }); |
| 138 | + } |
| 139 | + fs.rmSync(prefsPath, { force: true }); |
| 140 | + } |
| 141 | + }); |
| 142 | +}); |
0 commit comments