Skip to content

Commit 7a3996a

Browse files
committed
test(cli): decouple state-dir proof from guard timeout
1 parent 9030d86 commit 7a3996a

2 files changed

Lines changed: 69 additions & 40 deletions

File tree

‎src/cli/state-dir-gateway-check.server.test.ts‎

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import fs from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
55
import { fileURLToPath } from "node:url";
6-
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
6+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
7+
import { callGateway } from "../gateway/call.js";
8+
import { ADMIN_SCOPE } from "../gateway/method-scopes.js";
79
import { getFreePort } from "../test-utils/ports.js";
8-
import { checkCliGatewayStateDir } from "./state-dir-gateway-check.js";
10+
import { compareCliGatewayStateDirs, type GatewayHello } from "./state-dir-gateway-check.js";
911

1012
describe("state-dir guard with a real token Gateway", () => {
1113
const token = "state-dir-test-token";
@@ -15,11 +17,6 @@ describe("state-dir guard with a real token Gateway", () => {
1517
let gatewayStateDir: string;
1618
let cliStateDir: string;
1719

18-
const setCliStateDir = (stateDir: string) => {
19-
vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);
20-
vi.stubEnv("OPENCLAW_CONFIG_PATH", path.join(stateDir, "openclaw.json"));
21-
};
22-
2320
beforeAll(async () => {
2421
root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-state-dir-server-"));
2522
gatewayStateDir = path.join(root, "gateway");
@@ -69,14 +66,9 @@ describe("state-dir guard with a real token Gateway", () => {
6966
reject(new Error(`Gateway fixture exited early: ${code}\n${childStderr}`));
7067
});
7168
});
72-
vi.stubEnv("HOME", path.join(root, "cli-home"));
73-
setCliStateDir(cliStateDir);
74-
vi.stubEnv("OPENCLAW_GATEWAY_PORT", String(port));
75-
vi.stubEnv("OPENCLAW_SYSTEMD_UNIT", `openclaw-state-dir-server-${process.pid}`);
7669
}, 120_000);
7770

7871
afterAll(async () => {
79-
vi.unstubAllEnvs();
8072
const exited = new Promise<void>((resolve) => {
8173
child.once("exit", () => {
8274
resolve();
@@ -87,37 +79,53 @@ describe("state-dir guard with a real token Gateway", () => {
8779
await fs.rm(root, { recursive: true, force: true });
8880
});
8981

90-
it("allows matching authenticated hello paths", async () => {
91-
setCliStateDir(gatewayStateDir);
92-
await expect(
93-
checkCliGatewayStateDir({
94-
command: "openclaw channels add",
95-
config: { gateway: { mode: "local", port, auth: { mode: "token", token } } },
96-
}),
97-
).resolves.toEqual({ kind: "allow" });
98-
setCliStateDir(cliStateDir);
99-
});
100-
101-
it("refuses mismatched authenticated hello paths", async () => {
102-
const outcome = await checkCliGatewayStateDir({
103-
command: "openclaw channels add",
82+
it("compares paths from an authenticated Gateway hello", async () => {
83+
let hello: GatewayHello | undefined;
84+
await callGateway({
10485
config: { gateway: { mode: "local", port, auth: { mode: "token", token } } },
86+
method: "status",
87+
params: { includeChannelSummary: false },
88+
scopes: [ADMIN_SCOPE],
89+
sharedStateMode: "read-only",
90+
// This bounds the live integration proof without changing the guard's 3s offline policy.
91+
timeoutMs: 60_000,
92+
onHelloOk: (value) => {
93+
hello = value;
94+
},
10595
});
10696

107-
expect(outcome).toMatchObject({ kind: "refuse" });
108-
if (outcome.kind !== "refuse") {
109-
throw new Error("expected authenticated path mismatch refusal");
97+
if (!hello?.snapshot.stateDir) {
98+
throw new Error("expected authenticated hello state paths");
11099
}
111-
expect(outcome.message).toContain(gatewayStateDir);
112-
expect(outcome.message).toContain("live Gateway");
113-
});
114-
115-
it("warns when a tokenless CLI can prove only the Gateway protocol", async () => {
116-
await expect(
117-
checkCliGatewayStateDir({
118-
command: "openclaw channels add",
119-
config: { gateway: { mode: "local", port, auth: { mode: "token" } } },
100+
expect(hello.snapshot).toMatchObject({
101+
stateDir: gatewayStateDir,
102+
configPath: path.join(gatewayStateDir, "openclaw.json"),
103+
});
104+
const gatewayPaths = {
105+
gatewayStateDir: hello.snapshot.stateDir,
106+
gatewayConfigPath: hello.snapshot.configPath,
107+
source: "live Gateway" as const,
108+
mode: "refuse" as const,
109+
command: "openclaw channels add",
110+
};
111+
expect(
112+
compareCliGatewayStateDirs({
113+
cliStateDir: gatewayStateDir,
114+
cliConfigPath: path.join(gatewayStateDir, "openclaw.json"),
115+
...gatewayPaths,
120116
}),
121-
).resolves.toMatchObject({ kind: "warn" });
122-
});
117+
).toEqual({ kind: "allow" });
118+
119+
const mismatch = compareCliGatewayStateDirs({
120+
cliStateDir,
121+
cliConfigPath: path.join(cliStateDir, "openclaw.json"),
122+
...gatewayPaths,
123+
});
124+
expect(mismatch).toMatchObject({ kind: "refuse" });
125+
if (mismatch.kind !== "refuse") {
126+
throw new Error("expected authenticated path mismatch refusal");
127+
}
128+
expect(mismatch.message).toContain(gatewayStateDir);
129+
expect(mismatch.message).toContain("live Gateway");
130+
}, 120_000);
123131
});

‎src/cli/state-dir-gateway-check.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ describe("state-dir-gateway-check", () => {
116116
expect(mocks.probeGateway).not.toHaveBeenCalled();
117117
});
118118

119+
it("refuses paths from an authenticated hello without service fallback", async () => {
120+
const gatewayStateDir = path.join(root, "gateway");
121+
const gatewayConfigPath = path.join(gatewayStateDir, "openclaw.json");
122+
await fs.mkdir(gatewayStateDir);
123+
mocks.callGateway.mockImplementation(
124+
async (options: {
125+
onHelloOk?: (hello: { snapshot: { stateDir?: string; configPath?: string } }) => void;
126+
}) => {
127+
options.onHelloOk?.({
128+
snapshot: { stateDir: gatewayStateDir, configPath: gatewayConfigPath },
129+
});
130+
return {};
131+
},
132+
);
133+
134+
await expect(
135+
checkCliGatewayStateDir({ command: "openclaw channels add", config: {} }),
136+
).resolves.toMatchObject({ kind: "refuse" });
137+
expect(mocks.readGatewayServiceState).not.toHaveBeenCalled();
138+
});
139+
119140
it("warns only when a credential-blocked protocol probe reaches an unowned Gateway", async () => {
120141
mocks.callGateway.mockRejectedValue(
121142
new GatewayCredentialsRequiredError({ method: "status", configPath: cliConfigPath }),

0 commit comments

Comments
 (0)