File size: 7,663 Bytes
f778c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createNonExitingRuntimeEnv } from "../../test-utils/plugin-runtime-env.js";
import { runGatewayResume, runGatewaySuspend } from "./suspend-cli.js";
const readyResult = {
status: "ready" as const,
suspensionId: "suspension-1",
expiresAtMs: Date.parse("2026-08-11T12:00:00.000Z"),
activeCount: 0,
blockers: [],
};
const busyResult = {
status: "busy" as const,
reason: "active-work" as const,
retryAfterMs: 200,
activeCount: 1,
blockers: [{ kind: "root-request" as const, count: 1, message: "1 active request" }],
};
describe("gateway suspend CLI", () => {
beforeEach(() => vi.clearAllMocks());
afterEach(() => vi.unstubAllEnvs());
it.each([undefined, 0, "0", " 0 ", "0.25", "1e1"])(
"prints a ready lease for wait %j",
async (waitSeconds) => {
const callGateway = vi.fn(async () => readyResult);
const runtime = createNonExitingRuntimeEnv();
await runGatewaySuspend({ rpcOpts: {}, waitSeconds }, { callGateway, runtime });
expect(callGateway).toHaveBeenCalledWith(
"gateway.suspend.prepare",
{},
{ requestId: expect.stringMatching(/^cli-[0-9a-f]{8}$/u) },
);
expect(callGateway).toHaveBeenCalledOnce();
expect(runtime.log).toHaveBeenCalledWith("Gateway suspension prepared.");
expect(runtime.log).toHaveBeenCalledWith("Suspension ID: suspension-1");
expect(runtime.log).toHaveBeenCalledWith(
`Expires: 2026-08-11T12:00:00.000Z (${readyResult.expiresAtMs} ms)`,
);
expect(runtime.log).toHaveBeenCalledWith("Resume with: openclaw gateway resume suspension-1");
},
);
it.each(["", " ", "\t\n"])(
"rejects blank wait %j before acquiring a lease",
async (waitSeconds) => {
const callGateway = vi.fn(async () => readyResult);
await expect(
runGatewaySuspend(
{ rpcOpts: {}, waitSeconds },
{ callGateway, runtime: createNonExitingRuntimeEnv() },
),
).rejects.toThrow("--wait must be a non-negative number of seconds");
expect(callGateway).not.toHaveBeenCalled();
},
);
it.each([
{
name: "default gateway",
rpcOpts: {},
profile: "",
container: "",
command: "openclaw gateway resume suspension-1",
},
{
name: "custom local port",
rpcOpts: { localPortOverride: 18999 },
profile: "",
container: "",
command: "openclaw gateway resume suspension-1 --port 18999",
},
{
name: "named profile and custom local port",
rpcOpts: { localPortOverride: 18999 },
profile: "work",
container: "",
command: "openclaw --profile work gateway resume suspension-1 --port 18999",
},
{
name: "container takes precedence over profile",
rpcOpts: { localPortOverride: 18999 },
profile: "work",
container: "demo",
command: "openclaw --container demo gateway resume suspension-1 --port 18999",
},
{
name: "explicit URL and credentials remain private",
rpcOpts: { url: "wss://gateway.example:19444", token: "opaque-credential" },
profile: "",
container: "",
command: "openclaw gateway resume suspension-1",
},
])(
"prints a correctly scoped resume hint for $name",
async ({ rpcOpts, profile, container, command }) => {
vi.stubEnv("OPENCLAW_PROFILE", profile);
vi.stubEnv("OPENCLAW_CONTAINER_HINT", container);
const runtime = createNonExitingRuntimeEnv();
await runGatewaySuspend(
{ rpcOpts },
{ callGateway: vi.fn(async () => readyResult), runtime },
);
expect(runtime.log).toHaveBeenCalledWith(`Resume with: ${command}`);
},
);
it("reports blockers without polling when --wait is omitted", async () => {
const callGateway = vi.fn(async () => busyResult);
await expect(
runGatewaySuspend(
{ rpcOpts: {}, requestId: "host-operation" },
{ callGateway, runtime: createNonExitingRuntimeEnv() },
),
).rejects.toThrow(
"Gateway suspension is busy (active-work; 1 active).\nBlockers:\n- 1 active request\nRetry later or use --wait <seconds>.",
);
expect(callGateway).toHaveBeenCalledOnce();
});
it("polls with one stable request id until the Gateway is ready", async () => {
const callGateway = vi
.fn()
.mockResolvedValueOnce(busyResult)
.mockResolvedValueOnce(readyResult);
let now = 1_000;
const sleep = vi.fn(async (delayMs: number) => {
now += delayMs;
});
await runGatewaySuspend(
{ rpcOpts: {}, requestId: "host-operation", waitSeconds: "2" },
{ callGateway, runtime: createNonExitingRuntimeEnv(), nowMs: () => now, sleep },
);
expect(sleep).toHaveBeenCalledExactlyOnceWith(200);
expect(callGateway).toHaveBeenCalledTimes(2);
expect(callGateway.mock.calls.map((call) => call[2])).toEqual([
{ requestId: "host-operation" },
{ requestId: "host-operation" },
]);
});
it("emits the latest busy result and exits nonzero in JSON mode", async () => {
const runtime = createNonExitingRuntimeEnv();
await runGatewaySuspend(
{ rpcOpts: { json: true }, requestId: "host-operation", json: true },
{ callGateway: vi.fn(async () => busyResult), runtime },
);
expect(runtime.writeJson).toHaveBeenCalledWith({
...busyResult,
requestId: "host-operation",
});
expect(runtime.exit).toHaveBeenCalledWith(1);
});
it("never issues another prepare after a sleep overshoots the deadline", async () => {
let now = 1_000;
const callGateway = vi.fn(async () => busyResult);
await expect(
runGatewaySuspend(
{ rpcOpts: {}, requestId: "host-operation", waitSeconds: "0.2" },
{
callGateway,
runtime: createNonExitingRuntimeEnv(),
nowMs: () => now,
sleep: async () => {
// A lagging clock can wake far past the advertised --wait window.
now += 10_000;
},
},
),
).rejects.toThrow("Timed out waiting for the Gateway to become idle.");
expect(callGateway).toHaveBeenCalledOnce();
});
it("reports the latest blockers when the wait deadline expires", async () => {
let now = 1_000;
await expect(
runGatewaySuspend(
{ rpcOpts: {}, requestId: "host-operation", waitSeconds: "0.1" },
{
callGateway: vi.fn(async () => busyResult),
runtime: createNonExitingRuntimeEnv(),
nowMs: () => now,
sleep: async (delayMs) => {
now += delayMs;
},
},
),
).rejects.toThrow(
"Gateway suspension is busy (active-work; 1 active).\nBlockers:\n- 1 active request\nTimed out waiting for the Gateway to become idle.",
);
});
});
describe("gateway resume CLI", () => {
it.each([
{ resumed: true, message: "Gateway resumed." },
{
resumed: false,
message:
"No matching suspension was held (lease already expired or resumed); gateway is running.",
},
])("prints the resumed=$resumed outcome", async ({ resumed, message }) => {
const runtime = createNonExitingRuntimeEnv();
const callGateway = vi.fn(async () => ({ ok: true, status: "running", resumed }));
await runGatewayResume({ rpcOpts: {}, suspensionId: "suspension-1" }, { callGateway, runtime });
expect(callGateway).toHaveBeenCalledExactlyOnceWith(
"gateway.suspend.resume",
{},
{ suspensionId: "suspension-1" },
);
expect(runtime.log).toHaveBeenCalledExactlyOnceWith(message);
});
});
|