Spaces:
Configuration error
Configuration error
File size: 5,451 Bytes
a37e1b9 | 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 | // @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { api, fetchJSON } from "./api";
const reloadMocks = vi.hoisted(() => ({
attemptDashboardTokenReloadOnce: vi.fn(() => false),
clearDashboardTokenReloadAttempt: vi.fn(),
}));
vi.mock("./dashboard-auth-reload", () => ({
attemptDashboardTokenReloadOnce: reloadMocks.attemptDashboardTokenReloadOnce,
clearDashboardTokenReloadAttempt: reloadMocks.clearDashboardTokenReloadAttempt,
}));
const SESSION_HEADER = "X-Hermes-Session-Token";
beforeEach(() => {
reloadMocks.attemptDashboardTokenReloadOnce.mockReset();
reloadMocks.attemptDashboardTokenReloadOnce.mockReturnValue(false);
reloadMocks.clearDashboardTokenReloadAttempt.mockReset();
Object.defineProperty(window, "__HERMES_SESSION_TOKEN__", {
configurable: true,
value: "stale-token",
writable: true,
});
Object.defineProperty(window, "__HERMES_AUTH_REQUIRED__", {
configurable: true,
value: false,
writable: true,
});
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
function jsonFetchMock(body: unknown = { ok: true }) {
return vi.fn<typeof fetch>(
async () =>
new Response(JSON.stringify(body), {
headers: { "Content-Type": "application/json" },
status: 200,
}),
);
}
describe("fetchJSON", () => {
it("tries the one-shot reload path for loopback 401s", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => ({
clone: () => ({
json: async () => ({}),
}),
ok: false,
status: 401,
statusText: "Unauthorized",
text: async () => "Unauthorized",
})),
);
reloadMocks.attemptDashboardTokenReloadOnce.mockReturnValue(true);
const pending = fetchJSON("/api/status");
await expect(Promise.race([pending, Promise.resolve("pending")])).resolves.toBe(
"pending",
);
expect(reloadMocks.attemptDashboardTokenReloadOnce).toHaveBeenCalledTimes(1);
expect(reloadMocks.clearDashboardTokenReloadAttempt).not.toHaveBeenCalled();
});
it("clears the reload latch after a successful response", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => ({
json: async () => ({ ok: true }),
ok: true,
status: 200,
})),
);
await expect(fetchJSON("/api/status")).resolves.toEqual({ ok: true });
expect(reloadMocks.clearDashboardTokenReloadAttempt).toHaveBeenCalledTimes(1);
});
});
describe("api.getModelOptions", () => {
it("requests a live model refresh when asked", async () => {
vi.stubGlobal("window", {});
const fetchMock = jsonFetchMock({ providers: [] });
vi.stubGlobal("fetch", fetchMock);
await api.getModelOptions({ refresh: true });
expect(fetchMock).toHaveBeenCalledWith(
"/api/model/options?refresh=1&include_unconfigured=1",
expect.objectContaining({ credentials: "include" }),
);
});
it("keeps explicit profile scoping when refreshing", async () => {
vi.stubGlobal("window", {});
const fetchMock = jsonFetchMock({ providers: [] });
vi.stubGlobal("fetch", fetchMock);
await api.getModelOptions({ profile: "default", refresh: true });
expect(fetchMock).toHaveBeenCalledWith(
"/api/model/options?profile=default&refresh=1&include_unconfigured=1",
expect.objectContaining({ credentials: "include" }),
);
});
});
describe("api OAuth helpers", () => {
it("starts OAuth login in gated mode without requiring an injected session token", async () => {
vi.stubGlobal("window", { __HERMES_AUTH_REQUIRED__: true });
const fetchMock = jsonFetchMock({
flow: "device_code",
session_id: "oauth-session",
});
vi.stubGlobal("fetch", fetchMock);
await api.startOAuthLogin("openai-codex");
expect(fetchMock).toHaveBeenCalledWith(
"/api/providers/oauth/openai-codex/start",
expect.objectContaining({
body: "{}",
credentials: "include",
method: "POST",
}),
);
const headers = fetchMock.mock.calls[0][1]?.headers as Headers;
expect(headers.get("Content-Type")).toBe("application/json");
expect(headers.has(SESSION_HEADER)).toBe(false);
});
it("still sends the injected session token for OAuth login in loopback mode", async () => {
vi.stubGlobal("window", { __HERMES_SESSION_TOKEN__: "loopback-token" });
const fetchMock = jsonFetchMock({
flow: "device_code",
session_id: "oauth-session",
});
vi.stubGlobal("fetch", fetchMock);
await api.startOAuthLogin("openai-codex");
const headers = fetchMock.mock.calls[0][1]?.headers as Headers;
expect(headers.get(SESSION_HEADER)).toBe("loopback-token");
});
it("runs provider auth mutations in gated mode via cookie auth", async () => {
vi.stubGlobal("window", { __HERMES_AUTH_REQUIRED__: true });
const fetchMock = jsonFetchMock({ ok: true });
vi.stubGlobal("fetch", fetchMock);
await api.disconnectOAuthProvider("anthropic");
await api.submitOAuthCode("anthropic", "oauth-session", "code-123");
await api.cancelOAuthSession("oauth-session");
await api.revealEnvVar("OPENAI_API_KEY");
for (const call of fetchMock.mock.calls) {
const init = call[1] as RequestInit;
expect(init.credentials).toBe("include");
expect((init.headers as Headers).has(SESSION_HEADER)).toBe(false);
}
});
});
|