11import fs from "node:fs/promises" ;
22import { describe , expect , it , vi } from "vitest" ;
33import { createDeferred } from "../../test/helpers/promise.js" ;
4+ import type { ChannelPlugin } from "../channels/plugins/types.public.js" ;
45import { flushDiagnosticsTimeline } from "../infra/diagnostics-timeline.js" ;
56import { createPluginRecord } from "../plugins/loader-records.js" ;
67import { createEmptyPluginRegistry } from "../plugins/registry-empty.js" ;
@@ -20,6 +21,105 @@ import { createGatewayKernel } from "./server-kernel.js";
2021import { createSyntheticPluginRuntimeClient } from "./server-plugin-runtime-client.js" ;
2122
2223describe ( "createGatewayKernel" , ( ) => {
24+ it ( "does not start recovered channels after close prelude begins" , async ( ) => {
25+ const port = await getFreePort ( ) ;
26+ const state = await createOpenClawTestState ( {
27+ label : "gateway-kernel-breaker-recovery-close" ,
28+ layout : "home" ,
29+ env : {
30+ OPENCLAW_GATEWAY_PASSWORD : undefined ,
31+ OPENCLAW_GATEWAY_TOKEN : undefined ,
32+ OPENCLAW_SKIP_BROWSER_CONTROL_SERVER : "1" ,
33+ OPENCLAW_SKIP_CANVAS_HOST : "1" ,
34+ OPENCLAW_SKIP_CHANNELS : undefined ,
35+ OPENCLAW_SKIP_CRON : "1" ,
36+ OPENCLAW_SKIP_GMAIL_WATCHER : "1" ,
37+ OPENCLAW_SKIP_PROVIDERS : undefined ,
38+ OPENCLAW_TEST_MINIMAL_GATEWAY : "0" ,
39+ VITEST : "1" ,
40+ } ,
41+ } ) ;
42+ const originalPluginRegistry = captureActivePluginRegistrySnapshot ( ) ;
43+ const startAccount = vi . fn ( async ( ) => { } ) ;
44+ const channelPlugin : ChannelPlugin = {
45+ ...createChannelTestPluginBase ( {
46+ id : "telegram" ,
47+ config : {
48+ listAccountIds : ( config ) => Object . keys ( config . channels ?. telegram ?. accounts ?? { } ) ,
49+ resolveAccount : ( config , accountId ) =>
50+ config . channels ?. telegram ?. accounts ?. [ accountId ?? "default" ] ?? { } ,
51+ isConfigured : ( account ) =>
52+ typeof ( account as { botToken ?: unknown } ) . botToken === "string" ,
53+ } ,
54+ } ) ,
55+ gateway : { startAccount } ,
56+ } ;
57+ const registry = createTestRegistry ( [
58+ {
59+ pluginId : channelPlugin . id ,
60+ plugin : channelPlugin ,
61+ source : "gateway-kernel-test" ,
62+ } ,
63+ ] ) ;
64+ registry . plugins . push (
65+ createPluginRecord ( {
66+ id : channelPlugin . id ,
67+ source : "gateway-kernel-test" ,
68+ origin : "bundled" ,
69+ enabled : true ,
70+ configSchema : false ,
71+ } ) ,
72+ ) ;
73+ let kernel : Awaited < ReturnType < typeof createGatewayKernel > > | undefined ;
74+ try {
75+ stageActivePluginRegistry ( registry , null , "default" ) ;
76+ const token = "gateway-kernel-breaker-recovery-token" ;
77+ await state . writeConfig ( {
78+ gateway : {
79+ auth : { mode : "token" , token } ,
80+ controlUi : { enabled : false } ,
81+ port,
82+ } ,
83+ channels : {
84+ telegram : {
85+ accounts : {
86+ default : { botToken : "telegram-breaker-recovery-token" } ,
87+ } ,
88+ } ,
89+ } ,
90+ } ) ;
91+ state . applyEnv ( ) ;
92+ kernel = await createGatewayKernel ( port , {
93+ auth : { mode : "token" , token } ,
94+ bind : "loopback" ,
95+ channelAutostartSuppression : {
96+ reason : "crash-loop-breaker" ,
97+ message : "safe mode" ,
98+ } ,
99+ controlUiEnabled : false ,
100+ sidecarStartup : "defer" ,
101+ tryRecoverChannelAutostartSuppression : ( ) => true ,
102+ } ) ;
103+
104+ await expect ( kernel . channelManager . recoverAutostartSuppression ( ) ) . resolves . toBe ( true ) ;
105+ expect ( startAccount ) . not . toHaveBeenCalled ( ) ;
106+
107+ await kernel . beginClosePrelude ( ) ;
108+ kernel . releaseStartupAccountStarts ( ) ;
109+ await new Promise < void > ( ( resolve ) => setImmediate ( resolve ) ) ;
110+
111+ expect ( startAccount ) . not . toHaveBeenCalled ( ) ;
112+ expect ( kernel . channelManager . isAutoRestartScheduled ( "telegram" , "default" ) ) . toBe ( false ) ;
113+ } finally {
114+ try {
115+ await kernel ?. closeOnStartupFailure ( ) ;
116+ } finally {
117+ restoreActivePluginRegistrySnapshot ( originalPluginRegistry ) ;
118+ await state . cleanup ( ) ;
119+ }
120+ }
121+ } ) ;
122+
23123 it ( "reports startup and readiness as draining during a direct close" , async ( ) => {
24124 const port = 19_789 ;
25125 const state = await createOpenClawTestState ( {
0 commit comments