Skip to content

Commit 61bb7d5

Browse files
committed
fix(test): clean up cli backend live startup failures
1 parent 3060ebf commit 61bb7d5

1 file changed

Lines changed: 35 additions & 26 deletions

File tree

src/gateway/gateway-cli-backend.live.test.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -475,27 +475,30 @@ describeLive("gateway live (cli backend)", () => {
475475
await fs.writeFile(tempConfigPath, `${JSON.stringify(nextCfg, null, 2)}\n`);
476476
process.env.OPENCLAW_CONFIG_PATH = tempConfigPath;
477477
const deviceIdentity = await ensurePairedTestGatewayClientIdentity();
478+
let server: Awaited<ReturnType<typeof startGatewayServer>> | undefined;
479+
let client: Awaited<ReturnType<typeof connectTestGatewayClient>> | undefined;
478480
logCliBackendLiveStep("config-written", {
479481
tempConfigPath,
480482
stateDir,
481483
cliCommand,
482484
cliArgs,
483485
});
484486

485-
const server = await startGatewayServer(port, {
486-
bind: "loopback",
487-
auth: { mode: "token", token },
488-
controlUiEnabled: false,
489-
});
490-
logCliBackendLiveStep("server-started");
491-
const client = await connectTestGatewayClient({
492-
url: `ws://127.0.0.1:${port}`,
493-
token,
494-
deviceIdentity,
495-
});
496-
logCliBackendLiveStep("client-connected");
497-
498487
try {
488+
server = await startGatewayServer(port, {
489+
bind: "loopback",
490+
auth: { mode: "token", token },
491+
controlUiEnabled: false,
492+
});
493+
logCliBackendLiveStep("server-started");
494+
client = await connectTestGatewayClient({
495+
url: `ws://127.0.0.1:${port}`,
496+
token,
497+
deviceIdentity,
498+
});
499+
logCliBackendLiveStep("client-connected");
500+
const activeClient = client;
501+
499502
const sessionKey = "agent:dev:live-cli-backend";
500503
const nonce = randomBytes(3).toString("hex").toUpperCase();
501504
const memoryNonce = randomBytes(3).toString("hex").toUpperCase();
@@ -505,7 +508,7 @@ describeLive("gateway live (cli backend)", () => {
505508
providerId,
506509
"agent request",
507510
(timeouts) =>
508-
client.request(
511+
activeClient.request(
509512
"agent",
510513
{
511514
sessionKey,
@@ -562,7 +565,7 @@ describeLive("gateway live (cli backend)", () => {
562565
switchNonce,
563566
memoryToken,
564567
});
565-
const patchPayload = await client.request("sessions.patch", {
568+
const patchPayload = await activeClient.request("sessions.patch", {
566569
key: sessionKey,
567570
model: modelSwitchTarget,
568571
});
@@ -575,7 +578,7 @@ describeLive("gateway live (cli backend)", () => {
575578
providerId,
576579
"agent model-switch request",
577580
(timeouts) =>
578-
client.request(
581+
activeClient.request(
579582
"agent",
580583
{
581584
sessionKey,
@@ -611,7 +614,7 @@ describeLive("gateway live (cli backend)", () => {
611614
providerId,
612615
"agent resume request",
613616
(timeouts) =>
614-
client.request(
617+
activeClient.request(
615618
"agent",
616619
{
617620
sessionKey,
@@ -650,7 +653,7 @@ describeLive("gateway live (cli backend)", () => {
650653
: sessionKey;
651654
logCliBackendLiveStep("image-probe:start", { sessionKey: imageSessionKey });
652655
await verifyCliBackendImageProbe({
653-
client,
656+
client: activeClient,
654657
providerId,
655658
sessionKey: imageSessionKey,
656659
tempDir,
@@ -681,7 +684,7 @@ describeLive("gateway live (cli backend)", () => {
681684
} else {
682685
logCliBackendLiveStep("cron-mcp-probe:start", { sessionKey });
683686
await verifyCliCronMcpProbe({
684-
client,
687+
client: activeClient,
685688
providerId,
686689
sessionKey,
687690
port,
@@ -692,13 +695,19 @@ describeLive("gateway live (cli backend)", () => {
692695
}
693696
}
694697
} finally {
695-
logCliBackendLiveStep("cleanup:start");
696-
clearRuntimeConfigSnapshot();
697-
await client.stopAndWait();
698-
await server.close();
699-
await fs.rm(tempDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
700-
restoreCliBackendLiveEnv(previousEnv);
701-
logCliBackendLiveStep("cleanup:done");
698+
try {
699+
logCliBackendLiveStep("cleanup:start");
700+
clearRuntimeConfigSnapshot();
701+
try {
702+
await client?.stopAndWait();
703+
} finally {
704+
await server?.close();
705+
}
706+
} finally {
707+
await fs.rm(tempDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
708+
restoreCliBackendLiveEnv(previousEnv);
709+
logCliBackendLiveStep("cleanup:done");
710+
}
702711
}
703712
},
704713
CLI_BACKEND_LIVE_TIMEOUT_MS,

0 commit comments

Comments
 (0)