File size: 7,722 Bytes
f778c12 | 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 | import { beforeEach, describe, expect, it, vi } from "vitest";
import { GatewayClientRequestError } from "../../../packages/gateway-client/src/request-error.js";
import { GatewayLocalBackendSharedAuthUnavailableError } from "../../gateway/call.js";
import { GatewayTransportError } from "../../gateway/transport-error.js";
const mocks = vi.hoisted(() => ({
callGateway: vi.fn(),
isImplicitLocalGatewayTarget: vi.fn(),
}));
vi.mock("../../gateway/call.js", async () => {
const requestErrors = await import("../../../packages/gateway-client/src/request-error.js");
return {
callGateway: mocks.callGateway,
GatewayLocalBackendSharedAuthUnavailableError: class extends Error {},
isGatewayClientRequestError: (error: unknown) =>
error instanceof requestErrors.GatewayClientRequestError,
isImplicitLocalGatewayTarget: mocks.isImplicitLocalGatewayTarget,
};
});
const { refreshRunningGatewayAuthState } = await import("./auth-refresh.js");
describe("refreshRunningGatewayAuthState", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.callGateway.mockReset();
mocks.isImplicitLocalGatewayTarget.mockResolvedValue(true);
});
it.each(["login", "logout", "update"] as const)(
"acknowledges a published %s",
async (operation) => {
mocks.callGateway.mockResolvedValueOnce({ refreshed: true });
const warn = vi.fn();
await expect(
refreshRunningGatewayAuthState("main", operation, { error: warn }),
).resolves.toBe("refreshed");
expect(mocks.callGateway).toHaveBeenCalledWith(
expect.objectContaining({
method: "models.authRefresh",
params: { operation, agentId: "main" },
requireLocalBackendSharedAuth: true,
}),
);
expect(warn).not.toHaveBeenCalled();
},
);
it.each([
{
name: "legacy status refresh succeeds",
error: new GatewayClientRequestError({
code: "INVALID_REQUEST",
message: "unknown method: models.authRefresh",
}),
fallback: "success",
},
{
name: "legacy status refresh fails",
error: new GatewayClientRequestError({
code: "INVALID_REQUEST",
message: "unknown method: models.authRefresh",
}),
fallback: "failure",
},
{
name: "the Gateway reports a different error code",
error: new GatewayClientRequestError({
code: "UNAVAILABLE",
message: "unknown method: models.authRefresh",
}),
fallback: "none",
},
{
name: "the Gateway rejects admin scope",
error: new GatewayClientRequestError({
code: "INVALID_REQUEST",
message: "missing scope: operator.admin",
}),
fallback: "none",
},
{
name: "another method is unknown",
error: new GatewayClientRequestError({
code: "INVALID_REQUEST",
message: "unknown method: models.authStatus",
}),
fallback: "none",
},
{
name: "a local error has the same message",
error: new Error("unknown method: models.authRefresh"),
fallback: "none",
},
])("keeps restart guidance when $name", async ({ error, fallback }) => {
mocks.callGateway.mockImplementation(
async (options: { method: string; onHelloOk?: () => void }) => {
options.onHelloOk?.();
if (options.method === "models.authRefresh") {
throw error;
}
if (fallback === "failure") {
throw new Error("legacy refresh failed");
}
return { providers: [] };
},
);
const warn = vi.fn();
await expect(refreshRunningGatewayAuthState("main", "login", { error: warn })).resolves.toBe(
"gateway-rejected",
);
expect(mocks.callGateway).toHaveBeenCalledTimes(fallback === "none" ? 1 : 2);
if (fallback !== "none") {
expect(mocks.callGateway).toHaveBeenLastCalledWith(
expect.objectContaining({
method: "models.authStatus",
params: { refresh: true, agentId: "main" },
timeoutMs: 3000,
requireLocalBackendSharedAuth: true,
}),
);
}
expect(warn).toHaveBeenCalledWith(
"Warning: Model auth changes were saved, but the running Gateway could not refresh them. Run `openclaw gateway restart` to apply the saved changes.",
);
});
it("stays silent when no gateway is listening", async () => {
mocks.callGateway.mockRejectedValueOnce(
new GatewayTransportError({
kind: "closed",
message: "gateway unreachable",
reason: "connect ECONNREFUSED 127.0.0.1:18789",
connectionDetails: {
url: "ws://127.0.0.1:18789",
urlSource: "local loopback",
message: "Local target",
},
}),
);
const warn = vi.fn();
await expect(refreshRunningGatewayAuthState("main", "login", { error: warn })).resolves.toBe(
"gateway-unreachable",
);
expect(mocks.callGateway).toHaveBeenCalledWith(
expect.objectContaining({ requireLocalBackendSharedAuth: true }),
);
expect(warn).not.toHaveBeenCalled();
});
it("warns when a running gateway rejects the auth refresh", async () => {
mocks.callGateway.mockImplementationOnce(async (options: { onHelloOk?: () => void }) => {
options.onHelloOk?.();
throw new Error("refresh rejected");
});
const warn = vi.fn();
await expect(refreshRunningGatewayAuthState("main", "login", { error: warn })).resolves.toBe(
"gateway-rejected",
);
expect(warn).toHaveBeenCalledWith(
"Warning: Model auth changes were saved, but the running Gateway could not refresh them. Run `openclaw gateway restart` to apply the saved changes.",
);
});
it("warns when the gateway cannot publish refreshed auth state", async () => {
mocks.callGateway.mockImplementationOnce(async (options: { onHelloOk?: () => void }) => {
options.onHelloOk?.();
return { refreshed: false };
});
const warn = vi.fn();
await expect(refreshRunningGatewayAuthState("main", "login", { error: warn })).resolves.toBe(
"gateway-rejected",
);
expect(warn).toHaveBeenCalledWith(
"Warning: Model auth changes were saved, but the running Gateway could not refresh them. Run `openclaw gateway restart` to apply the saved changes.",
);
});
it("directs remote clients to run auth changes on the gateway host", async () => {
mocks.isImplicitLocalGatewayTarget.mockResolvedValueOnce(false);
mocks.callGateway.mockRejectedValueOnce(
new GatewayLocalBackendSharedAuthUnavailableError(
"local backend shared auth is limited to the configured local gateway",
),
);
const warn = vi.fn();
await expect(refreshRunningGatewayAuthState("main", "login", { error: warn })).resolves.toBe(
"gateway-rejected",
);
expect(warn).toHaveBeenCalledWith(
"Warning: Model auth changes were saved on this host, but the configured Gateway does not share this auth state. Run the auth command on the Gateway host (the far end of any SSH tunnel).",
);
});
it("does not guess a restart host when target classification fails", async () => {
mocks.isImplicitLocalGatewayTarget.mockRejectedValueOnce(new Error("invalid config"));
const warn = vi.fn();
await expect(refreshRunningGatewayAuthState("main", "login", { error: warn })).resolves.toBe(
"gateway-unreachable",
);
expect(mocks.callGateway).not.toHaveBeenCalled();
expect(warn).toHaveBeenCalledWith(
"Warning: Model auth changes were saved, but the configured Gateway could not be identified or refreshed. Apply the auth change on the Gateway host, or restart it there.",
);
});
});
|