|
1 | 1 | // Google tests cover realtime voice provider plugin behavior. |
2 | | -import { REALTIME_VOICE_AUDIO_FORMAT_PCM16_24KHZ } from "openclaw/plugin-sdk/realtime-voice"; |
| 2 | +import { |
| 3 | + REALTIME_VOICE_AUDIO_FORMAT_PCM16_24KHZ, |
| 4 | + resamplePcm, |
| 5 | +} from "openclaw/plugin-sdk/realtime-voice"; |
3 | 6 | import type { RealtimeVoiceTool } from "openclaw/plugin-sdk/realtime-voice"; |
4 | 7 | import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
5 | 8 | import { buildGoogleRealtimeVoiceProvider } from "./realtime-voice-provider.js"; |
@@ -1213,6 +1216,106 @@ describe("buildGoogleRealtimeVoiceProvider", () => { |
1213 | 1216 | expect(connectedSession.sendRealtimeInput).toHaveBeenCalledTimes(1); |
1214 | 1217 | }); |
1215 | 1218 |
|
| 1219 | + it("copies and bounds pending audio by aggregate bytes before activation", async () => { |
| 1220 | + const connectedSession = createMockGoogleLiveSession(); |
| 1221 | + connectMock.mockResolvedValueOnce(connectedSession); |
| 1222 | + const provider = buildGoogleRealtimeVoiceProvider(); |
| 1223 | + const bridge = provider.createBridge({ |
| 1224 | + providerConfig: { apiKey: "gemini-key" }, |
| 1225 | + audioFormat: REALTIME_VOICE_AUDIO_FORMAT_PCM16_24KHZ, |
| 1226 | + onAudio: vi.fn(), |
| 1227 | + onClearAudio: vi.fn(), |
| 1228 | + }); |
| 1229 | + const backing = Buffer.alloc(2 * 1024 * 1024); |
| 1230 | + const firstChunk = backing.subarray(0, 512 * 1024); |
| 1231 | + firstChunk.writeInt16LE(513); |
| 1232 | + const expectedFirstSample = resamplePcm(Buffer.from(firstChunk), 24_000, 16_000).readInt16LE(0); |
| 1233 | + |
| 1234 | + bridge.sendAudio(firstChunk); |
| 1235 | + bridge.sendAudio(Buffer.alloc(512 * 1024, 0x7f)); |
| 1236 | + bridge.sendAudio(Buffer.from([0x01])); |
| 1237 | + firstChunk.fill(0); |
| 1238 | + |
| 1239 | + await bridge.connect(); |
| 1240 | + lastConnectParams().callbacks.onopen(); |
| 1241 | + lastConnectParams().callbacks.onmessage({ setupComplete: { sessionId: "session-1" } }); |
| 1242 | + |
| 1243 | + expect(connectedSession.sendRealtimeInput).toHaveBeenCalledTimes(2); |
| 1244 | + const firstAudio = connectedSession.sendRealtimeInput.mock.calls[0]?.[0]?.audio as |
| 1245 | + | { data?: unknown } |
| 1246 | + | undefined; |
| 1247 | + expect(Buffer.from(String(firstAudio?.data), "base64").readInt16LE(0)).toBe( |
| 1248 | + expectedFirstSample, |
| 1249 | + ); |
| 1250 | + }); |
| 1251 | + |
| 1252 | + it("bounds pending audio by chunk count before activation", async () => { |
| 1253 | + const connectedSession = createMockGoogleLiveSession(); |
| 1254 | + connectMock.mockResolvedValueOnce(connectedSession); |
| 1255 | + const provider = buildGoogleRealtimeVoiceProvider(); |
| 1256 | + const bridge = provider.createBridge({ |
| 1257 | + providerConfig: { apiKey: "gemini-key" }, |
| 1258 | + audioFormat: REALTIME_VOICE_AUDIO_FORMAT_PCM16_24KHZ, |
| 1259 | + onAudio: vi.fn(), |
| 1260 | + onClearAudio: vi.fn(), |
| 1261 | + }); |
| 1262 | + |
| 1263 | + for (let index = 0; index < 321; index += 1) { |
| 1264 | + bridge.sendAudio(Buffer.alloc(2, index & 0xff)); |
| 1265 | + } |
| 1266 | + |
| 1267 | + await bridge.connect(); |
| 1268 | + lastConnectParams().callbacks.onopen(); |
| 1269 | + lastConnectParams().callbacks.onmessage({ setupComplete: { sessionId: "session-1" } }); |
| 1270 | + |
| 1271 | + expect(connectedSession.sendRealtimeInput).toHaveBeenCalledTimes(320); |
| 1272 | + }); |
| 1273 | + |
| 1274 | + it("drops reconnect audio on terminal exhaustion until an explicit reconnect owns admission", async () => { |
| 1275 | + vi.useFakeTimers(); |
| 1276 | + const reconnectedSession = createMockGoogleLiveSession(); |
| 1277 | + const provider = buildGoogleRealtimeVoiceProvider(); |
| 1278 | + const onClose = vi.fn(); |
| 1279 | + const bridge = provider.createBridge({ |
| 1280 | + providerConfig: { apiKey: "gemini-key" }, |
| 1281 | + audioFormat: REALTIME_VOICE_AUDIO_FORMAT_PCM16_24KHZ, |
| 1282 | + onAudio: vi.fn(), |
| 1283 | + onClearAudio: vi.fn(), |
| 1284 | + onClose, |
| 1285 | + }); |
| 1286 | + |
| 1287 | + await bridge.connect(); |
| 1288 | + const firstSession = lastConnectParams().callbacks; |
| 1289 | + firstSession.onopen(); |
| 1290 | + firstSession.onmessage({ setupComplete: { sessionId: "session-1" } }); |
| 1291 | + connectMock |
| 1292 | + .mockRejectedValueOnce(new Error("connect failed 1")) |
| 1293 | + .mockRejectedValueOnce(new Error("connect failed 2")) |
| 1294 | + .mockRejectedValueOnce(new Error("connect failed 3")) |
| 1295 | + .mockResolvedValueOnce(reconnectedSession); |
| 1296 | + firstSession.onclose({ code: 1011, reason: "temporary" }); |
| 1297 | + bridge.sendAudio(Buffer.from([0x01, 0x00])); |
| 1298 | + |
| 1299 | + await vi.advanceTimersByTimeAsync(1_750); |
| 1300 | + bridge.sendAudio(Buffer.from([0x02, 0x00])); |
| 1301 | + |
| 1302 | + expect(onClose).toHaveBeenCalledOnce(); |
| 1303 | + expect(onClose).toHaveBeenCalledWith("error"); |
| 1304 | + |
| 1305 | + await bridge.connect(); |
| 1306 | + const reconnected = lastConnectParams().callbacks; |
| 1307 | + reconnected.onopen(); |
| 1308 | + reconnected.onmessage({ setupComplete: { sessionId: "session-2" } }); |
| 1309 | + bridge.sendAudio(Buffer.alloc(480, 0x03)); |
| 1310 | + |
| 1311 | + expect(reconnectedSession.sendRealtimeInput).toHaveBeenCalledOnce(); |
| 1312 | + const sent = reconnectedSession.sendRealtimeInput.mock.calls[0]?.[0]?.audio as |
| 1313 | + | { data?: unknown } |
| 1314 | + | undefined; |
| 1315 | + expect(sent?.data).toBeTypeOf("string"); |
| 1316 | + bridge.close(); |
| 1317 | + }); |
| 1318 | + |
1216 | 1319 | it("does not activate a late session after close during setup", async () => { |
1217 | 1320 | const pendingSession = createDeferred<MockGoogleLiveSession>(); |
1218 | 1321 | const lateSession = createMockGoogleLiveSession(); |
|
0 commit comments