@@ -3,6 +3,7 @@
|
3 | 3 | import { describe, expect, it } from "vitest"; |
4 | 4 | import { resolveGatewayProbeAuthResolution } from "../commands/status.gateway-probe.js"; |
5 | 5 | import type { OpenClawConfig } from "../config/config.js"; |
| 6 | +import { withEnv } from "../test-utils/env.js"; |
6 | 7 | import { resolveGatewayAuth } from "./auth.js"; |
7 | 8 | import { resolveGatewayCredentialsFromConfig } from "./credentials.js"; |
8 | 9 | import { resolveGatewayProbeAuth } from "./probe-auth.js"; |
@@ -40,33 +41,14 @@ function makeRemoteGatewayConfig(remote: { token?: string; password?: string }):
|
40 | 41 | } |
41 | 42 | |
42 | 43 | function withGatewayAuthEnv<T>(env: NodeJS.ProcessEnv, fn: () => T): T { |
43 | | -const keys = [ |
44 | | -"OPENCLAW_GATEWAY_TOKEN", |
45 | | -"OPENCLAW_GATEWAY_PASSWORD", |
46 | | -"OPENCLAW_SERVICE_KIND", |
47 | | -] as const; |
48 | | -const previous = new Map<string, string | undefined>(); |
49 | | -for (const key of keys) { |
50 | | -previous.set(key, process.env[key]); |
51 | | -const nextValue = env[key]; |
52 | | -if (typeof nextValue === "string") { |
53 | | -process.env[key] = nextValue; |
54 | | -} else { |
55 | | -delete process.env[key]; |
56 | | -} |
57 | | -} |
58 | | -try { |
59 | | -return fn(); |
60 | | -} finally { |
61 | | -for (const key of keys) { |
62 | | -const value = previous.get(key); |
63 | | -if (typeof value === "string") { |
64 | | -process.env[key] = value; |
65 | | -} else { |
66 | | -delete process.env[key]; |
67 | | -} |
68 | | -} |
69 | | -} |
| 44 | +return withEnv( |
| 45 | +{ |
| 46 | +OPENCLAW_GATEWAY_TOKEN: env.OPENCLAW_GATEWAY_TOKEN, |
| 47 | +OPENCLAW_GATEWAY_PASSWORD: env.OPENCLAW_GATEWAY_PASSWORD, |
| 48 | +OPENCLAW_SERVICE_KIND: env.OPENCLAW_SERVICE_KIND, |
| 49 | +}, |
| 50 | +fn, |
| 51 | +); |
70 | 52 | } |
71 | 53 | |
72 | 54 | describe("gateway credential precedence coverage", () => { |
@@ -156,9 +138,8 @@ describe("gateway credential precedence coverage", () => {
|
156 | 138 | mode, |
157 | 139 | env, |
158 | 140 | }); |
159 | | -const status = ( |
160 | | -await withGatewayAuthEnv(env, () => resolveGatewayProbeAuthResolution(cfg)) |
161 | | -).auth; |
| 141 | +const status = (await withGatewayAuthEnv(env, () => resolveGatewayProbeAuthResolution(cfg))) |
| 142 | +.auth; |
162 | 143 | const auth = resolveGatewayAuth({ |
163 | 144 | authConfig: cfg.gateway?.auth, |
164 | 145 | env, |
|