@@ -3,9 +3,11 @@ import fs from "node:fs/promises";
33import os from "node:os" ;
44import path from "node:path" ;
55import { 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" ;
79import { 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
1012describe ( "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} ) ;
0 commit comments