|
1 | 1 | // @vitest-environment node |
2 | | -import { describe, expect, it } from "vitest"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
3 | 3 | import { ConnectErrorDetailCodes } from "../../../../packages/gateway-protocol/src/connect-error-details.js"; |
| 4 | +import { createStorageMock } from "../../test-helpers/storage.ts"; |
| 5 | +import { resolveGatewayTokenForUrlEdit } from "../storage.ts"; |
4 | 6 | import { |
5 | 7 | resolveAuthHintKind, |
6 | 8 | resolvePairingHint, |
7 | 9 | shouldShowInsecureContextHint, |
8 | 10 | shouldShowPairingHint, |
9 | 11 | } from "./overview-hints.ts"; |
10 | 12 |
|
| 13 | +afterEach(() => { |
| 14 | + vi.unstubAllGlobals(); |
| 15 | +}); |
| 16 | + |
| 17 | +describe("resolveGatewayTokenForUrlEdit", () => { |
| 18 | + it("preserves the current token for same normalized gateway endpoint edits", () => { |
| 19 | + expect( |
| 20 | + resolveGatewayTokenForUrlEdit( |
| 21 | + "wss://gateway.example/openclaw", |
| 22 | + " wss://gateway.example/openclaw/ ", |
| 23 | + "abc123", |
| 24 | + ), |
| 25 | + ).toBe("abc123"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("loads a scoped token when the normalized gateway endpoint changes", () => { |
| 29 | + vi.stubGlobal("sessionStorage", createStorageMock()); |
| 30 | + sessionStorage.setItem( |
| 31 | + "openclaw.control.token.v1:wss://other-gateway.example/openclaw", |
| 32 | + "other-token", |
| 33 | + ); |
| 34 | + |
| 35 | + expect( |
| 36 | + resolveGatewayTokenForUrlEdit( |
| 37 | + "wss://gateway.example/openclaw", |
| 38 | + "wss://other-gateway.example/openclaw/", |
| 39 | + "abc123", |
| 40 | + ), |
| 41 | + ).toBe("other-token"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("clears the token when the changed gateway endpoint has no scoped token", () => { |
| 45 | + vi.stubGlobal("sessionStorage", createStorageMock()); |
| 46 | + |
| 47 | + expect( |
| 48 | + resolveGatewayTokenForUrlEdit( |
| 49 | + "wss://gateway.example/openclaw", |
| 50 | + "wss://other-gateway.example/openclaw", |
| 51 | + "abc123", |
| 52 | + ), |
| 53 | + ).toBe(""); |
| 54 | + }); |
| 55 | + |
| 56 | + it("does not restore legacy durable tokens when the gateway endpoint changes", () => { |
| 57 | + vi.stubGlobal("localStorage", createStorageMock()); |
| 58 | + vi.stubGlobal("sessionStorage", createStorageMock()); |
| 59 | + localStorage.setItem( |
| 60 | + "openclaw.control.settings.v1", |
| 61 | + JSON.stringify({ |
| 62 | + gatewayUrl: "wss://other-gateway.example/openclaw", |
| 63 | + token: "legacy-durable-token", |
| 64 | + }), |
| 65 | + ); |
| 66 | + |
| 67 | + expect( |
| 68 | + resolveGatewayTokenForUrlEdit( |
| 69 | + "wss://gateway.example/openclaw", |
| 70 | + "wss://other-gateway.example/openclaw", |
| 71 | + "abc123", |
| 72 | + ), |
| 73 | + ).toBe(""); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
11 | 77 | describe("shouldShowPairingHint", () => { |
12 | 78 | it("returns true for 'pairing required' close reason", () => { |
13 | 79 | expect(shouldShowPairingHint(false, "disconnected (1008): pairing required")).toBe(true); |
|
0 commit comments