22import { createServer } from "node:http" ;
33import type { AddressInfo } from "node:net" ;
44import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts" ;
5+ import { createDeferred } from "openclaw/plugin-sdk/extension-shared" ;
56import { afterEach , describe , expect , it , vi } from "vitest" ;
67import type WebSocket from "ws" ;
78import type { RawData } from "ws" ;
@@ -186,16 +187,21 @@ describe("buildDeepgramRealtimeTranscriptionProvider", () => {
186187 } ,
187188 } ) ;
188189 const onPartial = vi . fn ( ) ;
189- const onTranscript = vi . fn ( ) ;
190+ const transcriptReceived = createDeferred < string > ( ) ;
191+ const onTranscript = vi . fn ( transcriptReceived . resolve ) ;
190192 const session = buildDeepgramRealtimeTranscriptionProvider ( ) . createSession ( {
191193 providerConfig : { apiKey : "dummy" , baseUrl : server . baseUrl , endpointingMs : 1000 } ,
192194 onPartial,
193195 onTranscript,
194196 } ) ;
195197
196- await session . connect ( ) ;
197- await vi . waitFor ( ( ) => expect ( onTranscript ) . toHaveBeenCalledWith ( "hello world" ) ) ;
198- session . close ( ) ;
198+ try {
199+ await session . connect ( ) ;
200+ await vi . waitFor ( ( ) => transcriptReceived . promise ) ;
201+ expect ( onTranscript ) . toHaveBeenCalledWith ( "hello world" ) ;
202+ } finally {
203+ session . close ( ) ;
204+ }
199205
200206 expect ( onPartial ) . toHaveBeenCalledWith ( "hello" ) ;
201207 expect ( onTranscript ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -209,63 +215,101 @@ describe("buildDeepgramRealtimeTranscriptionProvider", () => {
209215 sendResult ( ws , { text : "hello" , isFinal : true , speechFinal : true } ) ;
210216 } ,
211217 } ) ;
212- const onTranscript = vi . fn ( ) ;
218+ const transcriptReceived = createDeferred < string > ( ) ;
219+ const onTranscript = vi . fn ( transcriptReceived . resolve ) ;
213220 const session = buildDeepgramRealtimeTranscriptionProvider ( ) . createSession ( {
214221 providerConfig : { apiKey : "dummy" , baseUrl : server . baseUrl , endpointingMs : 1000 } ,
215222 onTranscript,
216223 } ) ;
217224
218- await session . connect ( ) ;
219- await vi . waitFor ( ( ) => expect ( onTranscript ) . toHaveBeenCalledWith ( "hello" ) ) ;
220- session . close ( ) ;
225+ try {
226+ await session . connect ( ) ;
227+ await vi . waitFor ( ( ) => transcriptReceived . promise ) ;
228+ expect ( onTranscript ) . toHaveBeenCalledWith ( "hello" ) ;
229+ } finally {
230+ session . close ( ) ;
231+ }
221232
222233 expect ( onTranscript ) . toHaveBeenCalledTimes ( 1 ) ;
223234 } ) ;
224235
225236 it ( "does not promote a rejected provisional tail on an empty speech-final result" , async ( ) => {
237+ const deliveryMarker = "rejected-tail frames delivered" ;
226238 const server = await createDeepgramRealtimeServer ( {
227239 onRequest : ( ) => undefined ,
228240 onConnection : ( ws ) => {
229241 sendResult ( ws , { text : "delete everything" } ) ;
230242 sendResult ( ws , { text : "" , isFinal : true , speechFinal : true } ) ;
231243 ws . send ( JSON . stringify ( { type : "SpeechStarted" } ) ) ;
244+ // An error marker observes delivery without creating another speech turn.
245+ ws . send ( JSON . stringify ( { type : "Error" , message : deliveryMarker } ) ) ;
232246 } ,
233247 } ) ;
234248 const onPartial = vi . fn ( ) ;
235249 const onSpeechStart = vi . fn ( ) ;
250+ const framesDelivered = createDeferred < void > ( ) ;
251+ const onError = vi . fn ( ( error : Error ) => {
252+ if ( error . message === deliveryMarker ) {
253+ framesDelivered . resolve ( ) ;
254+ }
255+ } ) ;
236256 const onTranscript = vi . fn ( ) ;
237257 const session = buildDeepgramRealtimeTranscriptionProvider ( ) . createSession ( {
238258 providerConfig : { apiKey : "dummy" , baseUrl : server . baseUrl , endpointingMs : 1000 } ,
239259 onPartial,
260+ onError,
240261 onSpeechStart,
241262 onTranscript,
242263 } ) ;
243264
244- await session . connect ( ) ;
245- await vi . waitFor ( ( ) => expect ( onSpeechStart ) . toHaveBeenCalledTimes ( 2 ) ) ;
246- session . close ( ) ;
265+ try {
266+ await session . connect ( ) ;
267+ await vi . waitFor ( ( ) => framesDelivered . promise ) ;
268+ expect ( onError ) . toHaveBeenCalledExactlyOnceWith (
269+ expect . objectContaining ( { message : deliveryMarker } ) ,
270+ ) ;
271+ expect ( onSpeechStart ) . toHaveBeenCalledTimes ( 2 ) ;
272+ } finally {
273+ session . close ( ) ;
274+ }
247275
248276 expect ( onPartial ) . toHaveBeenCalledWith ( "delete everything" ) ;
249277 expect ( onTranscript ) . not . toHaveBeenCalled ( ) ;
250278 } ) ;
251279
252280 it ( "preserves identical transcripts from consecutive utterances" , async ( ) => {
281+ const deliveryMarker = "consecutive-utterance frames delivered" ;
253282 const server = await createDeepgramRealtimeServer ( {
254283 onRequest : ( ) => undefined ,
255284 onConnection : ( ws ) => {
256285 sendResult ( ws , { text : "yes" , isFinal : true , speechFinal : true } ) ;
257286 sendResult ( ws , { text : "yes" , isFinal : true , speechFinal : true } ) ;
287+ ws . send ( JSON . stringify ( { type : "Error" , message : deliveryMarker } ) ) ;
258288 } ,
259289 } ) ;
260290 const onTranscript = vi . fn ( ) ;
291+ const framesDelivered = createDeferred < void > ( ) ;
292+ const onError = vi . fn ( ( error : Error ) => {
293+ if ( error . message === deliveryMarker ) {
294+ framesDelivered . resolve ( ) ;
295+ }
296+ } ) ;
261297 const session = buildDeepgramRealtimeTranscriptionProvider ( ) . createSession ( {
262298 providerConfig : { apiKey : "dummy" , baseUrl : server . baseUrl , endpointingMs : 1000 } ,
263299 onTranscript,
300+ onError,
264301 } ) ;
265302
266- await session . connect ( ) ;
267- await vi . waitFor ( ( ) => expect ( onTranscript ) . toHaveBeenCalledTimes ( 2 ) ) ;
268- session . close ( ) ;
303+ try {
304+ await session . connect ( ) ;
305+ await vi . waitFor ( ( ) => framesDelivered . promise ) ;
306+ expect ( onError ) . toHaveBeenCalledExactlyOnceWith (
307+ expect . objectContaining ( { message : deliveryMarker } ) ,
308+ ) ;
309+ expect ( onTranscript ) . toHaveBeenCalledTimes ( 2 ) ;
310+ } finally {
311+ session . close ( ) ;
312+ }
269313
270314 expect ( onTranscript . mock . calls ) . toEqual ( [ [ "yes" ] , [ "yes" ] ] ) ;
271315 } ) ;
@@ -287,17 +331,22 @@ describe("buildDeepgramRealtimeTranscriptionProvider", () => {
287331 } ) ;
288332 } ,
289333 } ) ;
290- const onTranscript = vi . fn ( ) ;
334+ const transcriptReceived = createDeferred < string > ( ) ;
335+ const onTranscript = vi . fn ( transcriptReceived . resolve ) ;
291336 const session = buildDeepgramRealtimeTranscriptionProvider ( ) . createSession ( {
292337 providerConfig : { apiKey : "dummy" , baseUrl : server . baseUrl , endpointingMs : 10_000 } ,
293338 onTranscript,
294339 } ) ;
295340
296- await session . connect ( ) ;
297- session . close ( ) ;
298- await vi . waitFor ( ( ) => expect ( onTranscript ) . toHaveBeenCalledWith ( "good bye" ) ) ;
299-
300- expect ( onTranscript ) . toHaveBeenCalledTimes ( 1 ) ;
341+ try {
342+ await session . connect ( ) ;
343+ session . close ( ) ;
344+ await vi . waitFor ( ( ) => transcriptReceived . promise ) ;
345+ expect ( onTranscript ) . toHaveBeenCalledWith ( "good bye" ) ;
346+ expect ( onTranscript ) . toHaveBeenCalledTimes ( 1 ) ;
347+ } finally {
348+ session . close ( ) ;
349+ }
301350 } ) ;
302351
303352 it ( "flushes finalized text once when finalize produces no result" , async ( ) => {
@@ -314,24 +363,41 @@ describe("buildDeepgramRealtimeTranscriptionProvider", () => {
314363 } ) ;
315364 } ,
316365 } ) ;
317- const onPartial = vi . fn ( ) ;
366+ const partialReceived = createDeferred < void > ( ) ;
367+ const onPartial = vi . fn ( ( text : string ) => {
368+ if ( text === "good bye" ) {
369+ partialReceived . resolve ( ) ;
370+ }
371+ } ) ;
318372 const onTranscript = vi . fn ( ) ;
319373 const session = buildDeepgramRealtimeTranscriptionProvider ( ) . createSession ( {
320374 providerConfig : { apiKey : "dummy" , baseUrl : server . baseUrl , endpointingMs : 10_000 } ,
321375 onPartial,
322376 onTranscript,
323377 } ) ;
324378
325- await session . connect ( ) ;
326- await vi . waitFor ( ( ) => expect ( onPartial ) . toHaveBeenCalledWith ( "good bye" ) ) ;
327- vi . useFakeTimers ( ) ;
328- session . close ( ) ;
329- session . close ( ) ;
330- await vi . advanceTimersByTimeAsync ( 5_000 ) ;
331-
332- expect ( finalizeRequests ) . toBe ( 1 ) ;
333- expect ( onTranscript ) . toHaveBeenCalledTimes ( 1 ) ;
334- expect ( onTranscript ) . toHaveBeenCalledWith ( "good" ) ;
379+ try {
380+ await session . connect ( ) ;
381+ await vi . waitFor ( ( ) => partialReceived . promise ) ;
382+ expect ( onPartial ) . toHaveBeenCalledWith ( "good bye" ) ;
383+ vi . useFakeTimers ( ) ;
384+ session . close ( ) ;
385+ session . close ( ) ;
386+ await vi . advanceTimersByTimeAsync ( 5_000 ) ;
387+
388+ expect ( finalizeRequests ) . toBe ( 1 ) ;
389+ expect ( onTranscript ) . toHaveBeenCalledTimes ( 1 ) ;
390+ expect ( onTranscript ) . toHaveBeenCalledWith ( "good" ) ;
391+ } finally {
392+ if ( vi . isFakeTimers ( ) ) {
393+ session . close ( ) ;
394+ } else {
395+ // A failed observation can leave finalized text; close must own its fallback clock.
396+ vi . useFakeTimers ( ) ;
397+ session . close ( ) ;
398+ await vi . advanceTimersByTimeAsync ( 5_000 ) . catch ( ( ) => undefined ) ;
399+ }
400+ }
335401 } ) ;
336402
337403 it ( "does not commit a turn on an utterance-end gap before speech-final" , async ( ) => {
@@ -343,15 +409,20 @@ describe("buildDeepgramRealtimeTranscriptionProvider", () => {
343409 sendResult ( ws , { text : "speaking" , isFinal : true , speechFinal : true } ) ;
344410 } ,
345411 } ) ;
346- const onTranscript = vi . fn ( ) ;
412+ const transcriptReceived = createDeferred < string > ( ) ;
413+ const onTranscript = vi . fn ( transcriptReceived . resolve ) ;
347414 const session = buildDeepgramRealtimeTranscriptionProvider ( ) . createSession ( {
348415 providerConfig : { apiKey : "dummy" , baseUrl : server . baseUrl , endpointingMs : 25 } ,
349416 onTranscript,
350417 } ) ;
351418
352- await session . connect ( ) ;
353- await vi . waitFor ( ( ) => expect ( onTranscript ) . toHaveBeenCalledWith ( "still speaking" ) ) ;
354- session . close ( ) ;
419+ try {
420+ await session . connect ( ) ;
421+ await vi . waitFor ( ( ) => transcriptReceived . promise ) ;
422+ expect ( onTranscript ) . toHaveBeenCalledWith ( "still speaking" ) ;
423+ } finally {
424+ session . close ( ) ;
425+ }
355426
356427 expect ( onTranscript ) . toHaveBeenCalledTimes ( 1 ) ;
357428 } ) ;
@@ -457,20 +528,24 @@ describe("buildDeepgramRealtimeTranscriptionProvider", () => {
457528 sendResult ( ws , { text : "y" } ) ;
458529 } ,
459530 } ) ;
460- const onError = vi . fn ( ) ;
531+ const errorReceived = createDeferred < Error > ( ) ;
532+ const onError = vi . fn ( errorReceived . resolve ) ;
461533 const session = buildDeepgramRealtimeTranscriptionProvider ( ) . createSession ( {
462534 providerConfig : { apiKey : "dummy" , baseUrl : server . baseUrl , endpointingMs : 1000 } ,
463535 onError,
464536 } ) ;
465537
466- await session . connect ( ) ;
467- await vi . waitFor ( ( ) =>
538+ try {
539+ await session . connect ( ) ;
540+ await vi . waitFor ( ( ) => errorReceived . promise ) ;
468541 expect ( onError ) . toHaveBeenCalledWith (
469542 expect . objectContaining ( {
470543 message : expect . stringContaining ( "retained transcript exceeded" ) ,
471544 } ) ,
472- ) ,
473- ) ;
474- session . close ( ) ;
545+ ) ;
546+ expect ( session . isConnected ( ) ) . toBe ( false ) ;
547+ } finally {
548+ session . close ( ) ;
549+ }
475550 } ) ;
476551} ) ;
0 commit comments