File size: 9,957 Bytes
3201ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { afterEach, describe, expect, it, vi } from "vitest";
import {
  DEFAULT_WORKING_DIR,
  buildConversationWorkingDir,
  getAgentServerBaseUrl,
  getAgentServerFormDefaults,
  getAgentServerSessionApiKey,
  getAgentServerWorkingDir,
  getCookieAuthCloudHost,
  getLockedCloudAuthMode,
  getLockedCloudHost,
  isAuthRequired,
  isSameCloudHost,
  isAuthRequiredAndMissing,
} from "#/api/agent-server-config";

const ORIGINAL_LOCATION = window.location;

function mockWindowLocation(url: string) {
  Object.defineProperty(window, "location", {
    configurable: true,
    value: new URL(url),
  });
}

afterEach(() => {
  window.localStorage.clear();
  vi.unstubAllEnvs();
  delete (window as unknown as Record<string, unknown>)
    .__AGENT_CANVAS_SESSION_API_KEY__;
  delete (window as unknown as Record<string, unknown>)
    .__AGENT_CANVAS_LOCK_TO_CLOUD__;
  Object.defineProperty(window, "location", {
    configurable: true,
    value: ORIGINAL_LOCATION,
  });
});

describe("agent server config", () => {
  it("uses VITE_BACKEND_BASE_URL when it is provided", () => {
    mockWindowLocation("https://work-1.example.dev/settings");
    vi.stubEnv("VITE_BACKEND_BASE_URL", "https://agent.example.com/");

    expect(getAgentServerBaseUrl()).toBe("https://agent.example.com");
  });

  it("uses the browser origin when no backend URL is configured", () => {
    mockWindowLocation("https://work-1.example.dev/settings");
    vi.stubEnv("VITE_BACKEND_BASE_URL", "");

    expect(getAgentServerBaseUrl()).toBe("https://work-1.example.dev");
  });

  it("does not rewrite localhost backend URLs to the browser origin", () => {
    mockWindowLocation("https://work-1.example.dev/settings");
    vi.stubEnv("VITE_BACKEND_BASE_URL", "http://127.0.0.1:8000");

    expect(getAgentServerBaseUrl()).toBe("http://127.0.0.1:8000");
  });

  it("prefills the settings form from environment defaults", () => {
    vi.stubEnv("VITE_BACKEND_BASE_URL", "https://env-agent.example.com/");
    vi.stubEnv("VITE_SESSION_API_KEY", "env-session-key");

    expect(getAgentServerFormDefaults()).toEqual({
      baseUrl: "https://env-agent.example.com",
      sessionApiKey: "env-session-key",
    });
    expect(getAgentServerSessionApiKey()).toBe("env-session-key");
  });

  it("defaults the working dir to the relative workspace path", () => {
    expect(getAgentServerWorkingDir()).toBe(DEFAULT_WORKING_DIR);
  });

  it("nests each conversation's working dir under the configured base using the hex id", () => {
    vi.stubEnv("VITE_WORKING_DIR", "/srv/workspaces/");

    expect(
      buildConversationWorkingDir("4a8dca37-3bf0-48de-a0af-949d711c3d48"),
    ).toBe("/srv/workspaces/4a8dca373bf048dea0af949d711c3d48");
  });
});

function setInjectedCloudHost(value: unknown) {
  (
    window as unknown as Record<string, unknown>
  ).__AGENT_CANVAS_LOCK_TO_CLOUD__ = value;
}

describe("getLockedCloudHost", () => {
  it("returns null when no Cloud lock is configured", () => {
    expect(getLockedCloudHost()).toBeNull();
  });

  it("uses VITE_LOCK_TO_CLOUD when it is provided", () => {
    vi.stubEnv("VITE_LOCK_TO_CLOUD", "https://cloud.example.com/");
    setInjectedCloudHost("https://runtime.example.com");

    expect(getLockedCloudHost()).toBe("https://cloud.example.com");
  });

  it("falls back to the runtime-injected Cloud URL", () => {
    setInjectedCloudHost("https://runtime.example.com/");

    expect(getLockedCloudHost()).toBe("https://runtime.example.com");
  });

  it("adds https:// to hostnames without an explicit scheme", () => {
    setInjectedCloudHost("cloud.example.com/");

    expect(getLockedCloudHost()).toBe("https://cloud.example.com");
  });

  it("ignores blank and non-string runtime values", () => {
    setInjectedCloudHost("   ");
    expect(getLockedCloudHost()).toBeNull();

    setInjectedCloudHost(12345);
    expect(getLockedCloudHost()).toBeNull();
  });
});

describe("isSameCloudHost", () => {
  it.each([
    ["https://staging.openhands.dev", "https://staging.all-hands.dev"],
    [
      "https://pr-254.staging.openhands.dev",
      "https://pr-254.staging.all-hands.dev",
    ],
    ["https://openhands.dev", "https://app.all-hands.dev"],
  ])("treats transition domains as equivalent (%s, %s)", (a, b) => {
    expect(isSameCloudHost(a, b)).toBe(true);
    expect(isSameCloudHost(b, a)).toBe(true);
  });

  it("does not treat different preview numbers as equivalent", () => {
    expect(
      isSameCloudHost(
        "https://pr-254.staging.openhands.dev",
        "https://pr-255.staging.all-hands.dev",
      ),
    ).toBe(false);
  });
});

describe("getLockedCloudAuthMode", () => {
  it("uses cookie auth when the locked Cloud host matches the current origin", () => {
    mockWindowLocation("https://app.all-hands.dev/canvas");
    setInjectedCloudHost("https://app.all-hands.dev");

    expect(getLockedCloudAuthMode()).toBe("cookie");
  });

  it("uses cookie auth for equivalent openhands.dev and all-hands.dev preview hosts", () => {
    mockWindowLocation("https://pr-254.staging.openhands.dev/canvas");
    setInjectedCloudHost("https://pr-254.staging.all-hands.dev");

    expect(getLockedCloudAuthMode()).toBe("cookie");
    expect(getCookieAuthCloudHost()).toBe(
      "https://pr-254.staging.openhands.dev",
    );
  });

  it("uses cookie auth when production moves from app.all-hands.dev to openhands.dev", () => {
    mockWindowLocation("https://openhands.dev/canvas");
    setInjectedCloudHost("https://app.all-hands.dev");

    expect(getLockedCloudAuthMode()).toBe("cookie");
    expect(getCookieAuthCloudHost()).toBe("https://openhands.dev");
  });

  it("uses API-key auth when the locked Cloud host is cross-origin", () => {
    mockWindowLocation("https://canvas.example.dev/");
    setInjectedCloudHost("https://app.all-hands.dev");

    expect(getLockedCloudAuthMode()).toBe("api-key");
  });
});

describe("isAuthRequired", () => {
  afterEach(() => {
    delete (window as unknown as Record<string, unknown>)
      .__AGENT_CANVAS_AUTH_REQUIRED__;
  });

  it("returns false when neither env var nor window flag is set", () => {
    expect(isAuthRequired()).toBe(false);
  });

  it("returns true when VITE_AUTH_REQUIRED is 'true'", () => {
    vi.stubEnv("VITE_AUTH_REQUIRED", "true");
    expect(isAuthRequired()).toBe(true);
  });

  it("returns true when window.__AGENT_CANVAS_AUTH_REQUIRED__ is set", () => {
    (
      window as unknown as Record<string, unknown>
    ).__AGENT_CANVAS_AUTH_REQUIRED__ = true;
    expect(isAuthRequired()).toBe(true);
  });

  it("returns false when window flag is a non-true value", () => {
    (
      window as unknown as Record<string, unknown>
    ).__AGENT_CANVAS_AUTH_REQUIRED__ = "true";
    expect(isAuthRequired()).toBe(false);
  });
});

describe("isAuthRequiredAndMissing", () => {
  afterEach(() => {
    delete (window as unknown as Record<string, unknown>)
      .__AGENT_CANVAS_AUTH_REQUIRED__;
  });

  it("returns false when VITE_AUTH_REQUIRED is not set", () => {
    expect(isAuthRequiredAndMissing()).toBe(false);
  });

  it("returns true when VITE_AUTH_REQUIRED is true and no key is baked in", () => {
    vi.stubEnv("VITE_AUTH_REQUIRED", "true");
    vi.stubEnv("VITE_SESSION_API_KEY", "");

    expect(isAuthRequiredAndMissing()).toBe(true);
  });

  it("returns true via window flag when no key is baked in", () => {
    vi.stubEnv("VITE_SESSION_API_KEY", "");
    (
      window as unknown as Record<string, unknown>
    ).__AGENT_CANVAS_AUTH_REQUIRED__ = true;

    expect(isAuthRequiredAndMissing()).toBe(true);
  });

  it("returns false when VITE_AUTH_REQUIRED is true but VITE_SESSION_API_KEY is baked in", () => {
    vi.stubEnv("VITE_AUTH_REQUIRED", "true");
    vi.stubEnv("VITE_SESSION_API_KEY", "baked-key");

    expect(isAuthRequiredAndMissing()).toBe(false);
  });

  it("returns false for non-'true' values of VITE_AUTH_REQUIRED", () => {
    vi.stubEnv("VITE_AUTH_REQUIRED", "false");

    expect(isAuthRequiredAndMissing()).toBe(false);
  });
});

// Covers the published `agent-canvas` binary path: the prebuilt bundle has
// no VITE_SESSION_API_KEY baked in, but `scripts/static-server.mjs` injects
// the runtime key into `window.__AGENT_CANVAS_SESSION_API_KEY__`. Without
// this fallback, `makeDefaultLocalBackend()` returns null on a fresh install
// and the user sees the Manage Backends modal instead of the onboarding flow.
describe("getAgentServerSessionApiKey runtime-injection fallback", () => {
  function setInjectedKey(value: unknown) {
    (
      window as unknown as Record<string, unknown>
    ).__AGENT_CANVAS_SESSION_API_KEY__ = value;
  }

  it("returns the env-baked key when VITE_SESSION_API_KEY is set", () => {
    vi.stubEnv("VITE_SESSION_API_KEY", "env-key");
    setInjectedKey("window-key");

    expect(getAgentServerSessionApiKey()).toBe("env-key");
  });

  it("falls back to the runtime-injected window global when no env key is baked in", () => {
    vi.stubEnv("VITE_SESSION_API_KEY", "");
    setInjectedKey("runtime-injected-key");

    expect(getAgentServerSessionApiKey()).toBe("runtime-injected-key");
  });

  it("includes the injected key in form defaults so the registry can seed", () => {
    vi.stubEnv("VITE_SESSION_API_KEY", "");
    setInjectedKey("runtime-injected-key");

    expect(getAgentServerFormDefaults().sessionApiKey).toBe(
      "runtime-injected-key",
    );
  });

  it("returns null when neither env nor window provides a key", () => {
    vi.stubEnv("VITE_SESSION_API_KEY", "");

    expect(getAgentServerSessionApiKey()).toBeNull();
  });

  it("treats a whitespace-only injected key as missing", () => {
    vi.stubEnv("VITE_SESSION_API_KEY", "");
    setInjectedKey("   ");

    expect(getAgentServerSessionApiKey()).toBeNull();
  });

  it("ignores non-string injected values", () => {
    vi.stubEnv("VITE_SESSION_API_KEY", "");
    setInjectedKey(12345);

    expect(getAgentServerSessionApiKey()).toBeNull();
  });
});