22import { EventEmitter } from "node:events" ;
33import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env" ;
44import { beforeAll , beforeEach , describe , expect , it , vi , type Mock } from "vitest" ;
5- import type { GatewayPlugin } from "../internal/gateway.js" ;
5+ import { GatewayCloseCodes , type GatewayPlugin } from "../internal/gateway.js" ;
66import type { waitForDiscordGatewayStop } from "../monitor.gateway.js" ;
77import {
88 DISCORD_GATEWAY_TRANSPORT_ACTIVITY_EVENT ,
@@ -194,6 +194,8 @@ describe("runDiscordGatewayLifecycle", () => {
194194
195195 type StatusPatch = {
196196 connected ?: boolean ;
197+ lifecycle ?: "ready" | "recovering" | "blocked" ;
198+ terminalDisconnect ?: boolean ;
197199 lastDisconnect ?: null | Record < string , unknown > ;
198200 lastError ?: string | null ;
199201 } ;
@@ -268,7 +270,8 @@ describe("runDiscordGatewayLifecycle", () => {
268270
269271 expectStatusPatch (
270272 statusSink ,
271- ( patch ) => patch . connected === true && patch . lastDisconnect === null ,
273+ ( patch ) =>
274+ patch . connected === true && patch . lifecycle === "ready" && patch . lastDisconnect === null ,
272275 ) ;
273276 } ) ;
274277
@@ -363,7 +366,14 @@ describe("runDiscordGatewayLifecycle", () => {
363366 expectStatusPatch (
364367 statusSink ,
365368 ( patch ) =>
366- patch . connected === true && patch . lastDisconnect === null && patch . lastError === null ,
369+ patch . connected === true &&
370+ patch . lifecycle === "ready" &&
371+ patch . lastDisconnect === null &&
372+ patch . lastError === null ,
373+ ) ;
374+ expectStatusPatch (
375+ statusSink ,
376+ ( patch ) => patch . connected === false && patch . lifecycle === "recovering" ,
367377 ) ;
368378 } finally {
369379 vi . useRealTimers ( ) ;
@@ -618,11 +628,38 @@ describe("runDiscordGatewayLifecycle", () => {
618628 statusSink ,
619629 ( patch ) =>
620630 patch . connected === false &&
631+ patch . lifecycle === "recovering" &&
621632 patch . lastDisconnect !== null &&
622633 patch . lastDisconnect ?. status === 1006 ,
623634 ) ;
624635 } ) ;
625636
637+ it . each ( [ GatewayCloseCodes . AuthenticationFailed , GatewayCloseCodes . InvalidIntents ] ) (
638+ "publishes blocked lifecycle for fatal gateway close code %s" ,
639+ async ( closeCode ) => {
640+ const { emitter, gateway } = createGatewayHarness ( ) ;
641+ gateway . isConnected = true ;
642+ getDiscordGatewayEmitterMock . mockReturnValueOnce ( emitter ) ;
643+ waitForDiscordGatewayStopMock . mockImplementationOnce ( async ( ) => {
644+ emitter . emit ( "debug" , `Gateway websocket closed: ${ closeCode } ` ) ;
645+ } ) ;
646+
647+ const { lifecycleParams, statusSink } = createLifecycleHarness ( { gateway } ) ;
648+
649+ await expect ( runDiscordGatewayLifecycle ( lifecycleParams ) ) . resolves . toBeUndefined ( ) ;
650+
651+ expectStatusPatch (
652+ statusSink ,
653+ ( patch ) =>
654+ patch . connected === false &&
655+ patch . lifecycle === "blocked" &&
656+ patch . terminalDisconnect === true &&
657+ patch . lastError === `Gateway websocket closed: ${ closeCode } ` &&
658+ patch . lastDisconnect ?. status === closeCode ,
659+ ) ;
660+ } ,
661+ ) ;
662+
626663 it ( "pushes disconnected status when the gateway schedules a reconnect" , async ( ) => {
627664 const { emitter, gateway } = createGatewayHarness ( ) ;
628665 gateway . isConnected = true ;
@@ -639,6 +676,7 @@ describe("runDiscordGatewayLifecycle", () => {
639676 statusSink ,
640677 ( patch ) =>
641678 patch . connected === false &&
679+ patch . lifecycle === "recovering" &&
642680 patch . lastError === "Gateway reconnect scheduled in 1000ms (zombie, resume=true)" ,
643681 ) ;
644682 } ) ;
@@ -665,7 +703,8 @@ describe("runDiscordGatewayLifecycle", () => {
665703 expectStatusPatch ( statusSink , ( patch ) => patch . connected === false ) ;
666704 expectStatusPatch (
667705 statusSink ,
668- ( patch ) => patch . connected === true && patch . lastDisconnect === null ,
706+ ( patch ) =>
707+ patch . connected === true && patch . lifecycle === "ready" && patch . lastDisconnect === null ,
669708 ) ;
670709 } finally {
671710 vi . useRealTimers ( ) ;
0 commit comments