@@ -102,6 +102,15 @@ function createBacklogEvent(index: number, type = "channel.updated") {
102102 } ;
103103}
104104
105+ function emitMessageEvent ( socket : FakeSocket , index : number ) {
106+ socket . emit (
107+ "message" ,
108+ Buffer . from (
109+ JSON . stringify ( { ...createBacklogEvent ( index , "message.created" ) , seq : index + 1 } ) ,
110+ ) ,
111+ ) ;
112+ }
113+
105114describe ( "ClickClack gateway" , ( ) => {
106115 beforeEach ( ( ) => {
107116 vi . clearAllMocks ( ) ;
@@ -335,6 +344,130 @@ describe("ClickClack gateway", () => {
335344 expect ( runError ) . toBeUndefined ( ) ;
336345 } ) ;
337346
347+ it ( "processes websocket events in order before reconnecting from their cursor" , async ( ) => {
348+ const firstSocket = new FakeSocket ( ) ;
349+ const secondSocket = new FakeSocket ( ) ;
350+ mocks . client . websocket . mockReturnValueOnce ( firstSocket ) . mockReturnValueOnce ( secondSocket ) ;
351+ let finishFirstEvent : ( ( ) => void ) | undefined ;
352+ mocks . handleClickClackInbound . mockImplementationOnce (
353+ ( ) =>
354+ new Promise < void > ( ( resolve ) => {
355+ finishFirstEvent = resolve ;
356+ } ) ,
357+ ) ;
358+ const abort = new AbortController ( ) ;
359+ const ctx = createGatewayContext ( abort . signal ) ;
360+ const run = startClickClackGatewayAccount ( ctx ) ;
361+
362+ await vi . waitFor ( ( ) => expect ( mocks . client . websocket ) . toHaveBeenCalledTimes ( 1 ) ) ;
363+
364+ for ( const index of [ 1 , 2 ] ) {
365+ firstSocket . emit (
366+ "message" ,
367+ Buffer . from (
368+ JSON . stringify ( {
369+ id : `evt-${ index } ` ,
370+ cursor : `cursor-${ index } ` ,
371+ type : "message.created" ,
372+ workspace_id : "workspace-1" ,
373+ channel_id : "chan-1" ,
374+ seq : index + 1 ,
375+ created_at : "2026-01-01T00:00:00.000Z" ,
376+ payload : { message_id : "msg-1" , author_id : "human-1" } ,
377+ } ) ,
378+ ) ,
379+ ) ;
380+ }
381+ firstSocket . emit ( "close" ) ;
382+
383+ await vi . waitFor ( ( ) => expect ( finishFirstEvent ) . toBeTypeOf ( "function" ) ) ;
384+ expect ( mocks . handleClickClackInbound ) . toHaveBeenCalledTimes ( 1 ) ;
385+ expect ( mocks . client . websocket ) . toHaveBeenCalledTimes ( 1 ) ;
386+
387+ finishFirstEvent ?.( ) ;
388+ await vi . waitFor ( ( ) => expect ( mocks . client . websocket ) . toHaveBeenCalledTimes ( 2 ) ) ;
389+
390+ expect ( mocks . handleClickClackInbound ) . toHaveBeenCalledTimes ( 2 ) ;
391+ expect ( mocks . client . eventPage ) . toHaveBeenLastCalledWith ( "workspace-1" , {
392+ afterCursor : "cursor-2" ,
393+ limit : 500 ,
394+ } ) ;
395+
396+ abort . abort ( ) ;
397+ await run ;
398+ } ) ;
399+
400+ it ( "replays a failed websocket event on reconnect without processing later queued events" , async ( ) => {
401+ const firstSocket = new FakeSocket ( ) ;
402+ const secondSocket = new FakeSocket ( ) ;
403+ const events = [
404+ createBacklogEvent ( 1 , "message.created" ) ,
405+ createBacklogEvent ( 2 , "message.created" ) ,
406+ ] ;
407+ mocks . client . eventPage
408+ . mockResolvedValueOnce ( { events : [ ] , tailCursor : "" } )
409+ . mockResolvedValueOnce ( { events } )
410+ . mockResolvedValueOnce ( { events : [ ] } ) ;
411+ mocks . client . websocket . mockReturnValueOnce ( firstSocket ) . mockReturnValueOnce ( secondSocket ) ;
412+ mocks . handleClickClackInbound . mockRejectedValueOnce ( new Error ( "dispatch failed" ) ) ;
413+ const abort = new AbortController ( ) ;
414+ const ctx = createGatewayContext ( abort . signal ) ;
415+ const run = startClickClackGatewayAccount ( ctx ) ;
416+
417+ await vi . waitFor ( ( ) => expect ( mocks . client . websocket ) . toHaveBeenCalledTimes ( 1 ) ) ;
418+
419+ for ( const index of [ 1 , 2 ] ) {
420+ emitMessageEvent ( firstSocket , index ) ;
421+ }
422+
423+ await vi . waitFor ( ( ) => expect ( mocks . client . websocket ) . toHaveBeenCalledTimes ( 2 ) ) ;
424+
425+ expect ( mocks . handleClickClackInbound ) . toHaveBeenCalledTimes ( 3 ) ;
426+ expect ( firstSocket . close ) . toHaveBeenCalledOnce ( ) ;
427+ expect ( mocks . client . eventPage ) . toHaveBeenNthCalledWith ( 2 , "workspace-1" , {
428+ afterCursor : "" ,
429+ limit : 500 ,
430+ } ) ;
431+ expect ( mocks . client . websocket ) . toHaveBeenLastCalledWith ( "workspace-1" , "cursor-2" ) ;
432+ expect ( ctx . log ?. warn ) . toHaveBeenCalledWith (
433+ "[default] ClickClack event processing failed; reconnecting: dispatch failed" ,
434+ ) ;
435+
436+ abort . abort ( ) ;
437+ await run ;
438+ } ) ;
439+
440+ it ( "does not serialize unrelated gateway streams" , async ( ) => {
441+ const slowSocket = new FakeSocket ( ) ;
442+ const fastSocket = new FakeSocket ( ) ;
443+ mocks . client . websocket . mockReturnValueOnce ( slowSocket ) . mockReturnValueOnce ( fastSocket ) ;
444+ let finishSlowEvent : ( ( ) => void ) | undefined ;
445+ mocks . handleClickClackInbound . mockImplementationOnce (
446+ ( ) =>
447+ new Promise < void > ( ( resolve ) => {
448+ finishSlowEvent = resolve ;
449+ } ) ,
450+ ) ;
451+ const slowAbort = new AbortController ( ) ;
452+ const fastAbort = new AbortController ( ) ;
453+ const slowRun = startClickClackGatewayAccount ( createGatewayContext ( slowAbort . signal ) ) ;
454+ const fastRun = startClickClackGatewayAccount ( createGatewayContext ( fastAbort . signal ) ) ;
455+
456+ await vi . waitFor ( ( ) => expect ( mocks . client . websocket ) . toHaveBeenCalledTimes ( 2 ) ) ;
457+
458+ emitMessageEvent ( slowSocket , 1 ) ;
459+ await vi . waitFor ( ( ) => expect ( finishSlowEvent ) . toBeTypeOf ( "function" ) ) ;
460+ emitMessageEvent ( fastSocket , 2 ) ;
461+
462+ await vi . waitFor ( ( ) => expect ( mocks . handleClickClackInbound ) . toHaveBeenCalledTimes ( 2 ) ) ;
463+ expect ( finishSlowEvent ) . toBeTypeOf ( "function" ) ;
464+
465+ finishSlowEvent ?.( ) ;
466+ slowAbort . abort ( ) ;
467+ fastAbort . abort ( ) ;
468+ await Promise . all ( [ slowRun , fastRun ] ) ;
469+ } ) ;
470+
338471 it ( "drops messages denied by ClickClack sender access before inbound handling" , async ( ) => {
339472 const socket = new FakeSocket ( ) ;
340473 mocks . client . websocket . mockReturnValue ( socket ) ;
@@ -542,41 +675,32 @@ describe("ClickClack gateway", () => {
542675 } ) ;
543676 } ) ;
544677
545- it ( "wraps non-Error ws message rejections with the original value in the message" , async ( ) => {
546- const socket = new FakeSocket ( ) ;
547- mocks . client . websocket . mockReturnValue ( socket ) ;
678+ it ( "logs non-Error websocket failures before replaying them" , async ( ) => {
679+ const firstSocket = new FakeSocket ( ) ;
680+ const secondSocket = new FakeSocket ( ) ;
681+ const event = createBacklogEvent ( 1 , "message.created" ) ;
682+ mocks . client . eventPage
683+ . mockResolvedValueOnce ( { events : [ ] , tailCursor : "" } )
684+ . mockResolvedValueOnce ( { events : [ event ] } )
685+ . mockResolvedValueOnce ( { events : [ ] } ) ;
686+ mocks . client . websocket . mockReturnValueOnce ( firstSocket ) . mockReturnValueOnce ( secondSocket ) ;
548687 const rejection = { code : "ECONNRESET" , retryable : true } ;
549- mocks . resolveClickClackInboundAccess . mockRejectedValue ( rejection ) ;
688+ mocks . resolveClickClackInboundAccess . mockRejectedValueOnce ( rejection ) ;
550689 const abort = new AbortController ( ) ;
551690 const ctx = createGatewayContext ( abort . signal ) ;
552691 const run = startClickClackGatewayAccount ( ctx ) ;
553692
554693 await vi . waitFor ( ( ) => expect ( mocks . client . websocket ) . toHaveBeenCalledTimes ( 1 ) ) ;
555694
556- socket . emit (
557- "message" ,
558- Buffer . from (
559- JSON . stringify ( {
560- id : "evt-1" ,
561- cursor : "cursor-1" ,
562- type : "message.created" ,
563- workspace_id : "workspace-1" ,
564- channel_id : "chan-1" ,
565- seq : 2 ,
566- created_at : "2026-01-01T00:00:00.000Z" ,
567- payload : { message_id : "msg-1" , author_id : "human-1" } ,
568- } ) ,
569- ) ,
695+ emitMessageEvent ( firstSocket , 1 ) ;
696+
697+ await vi . waitFor ( ( ) => expect ( mocks . client . websocket ) . toHaveBeenCalledTimes ( 2 ) ) ;
698+ expect ( ctx . log ?. warn ) . toHaveBeenCalledWith (
699+ '[default] ClickClack event processing failed; reconnecting: {"code":"ECONNRESET","retryable":true}' ,
570700 ) ;
701+ expect ( mocks . client . websocket ) . toHaveBeenLastCalledWith ( "workspace-1" , "cursor-1" ) ;
571702
572- await expect ( run ) . rejects . toMatchObject ( {
573- message : 'ClickClack ws message failed: {"code":"ECONNRESET","retryable":true}' ,
574- cause : rejection ,
575- } ) ;
576- expect ( socket . close ) . toHaveBeenCalledOnce ( ) ;
577- expect ( ctx . setStatus ) . toHaveBeenLastCalledWith ( {
578- accountId : "default" ,
579- running : false ,
580- } ) ;
703+ abort . abort ( ) ;
704+ await run ;
581705 } ) ;
582706} ) ;
0 commit comments