Some checks failed
CI / docs-scope (push) Has been cancelled
CI / secrets (push) Has been cancelled
CI / ios (push) Has been cancelled
Docker Release / validate_manual_backfill (push) Has been cancelled
Install Smoke / docs-scope (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / config-docs-drift (push) Has been cancelled
CI / changed-scope (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / release-check (push) Has been cancelled
CI / checks (pnpm canvas:a2ui:bundle && bunx vitest run --config vitest.unit.config.ts, bun, test) (push) Has been cancelled
CI / checks (pnpm canvas:a2ui:bundle && pnpm test, node, 2, 1, test) (push) Has been cancelled
CI / checks (pnpm canvas:a2ui:bundle && pnpm test, node, 2, 2, test) (push) Has been cancelled
CI / checks (pnpm protocol:check, node, protocol) (push) Has been cancelled
CI / checks (pnpm test:channels, node, channels) (push) Has been cancelled
CI / checks (pnpm test:extensions, node, extensions) (push) Has been cancelled
CI / check (push) Has been cancelled
CI / startup-memory (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / compat-node22 (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / checks-windows (pnpm test, node, 6, 1, test) (push) Has been cancelled
CI / checks-windows (pnpm test, node, 6, 2, test) (push) Has been cancelled
CI / checks-windows (pnpm test, node, 6, 3, test) (push) Has been cancelled
CI / checks-windows (pnpm test, node, 6, 4, test) (push) Has been cancelled
CI / checks-windows (pnpm test, node, 6, 5, test) (push) Has been cancelled
CI / checks-windows (pnpm test, node, 6, 6, test) (push) Has been cancelled
CI / macos (push) Has been cancelled
CI / android (./gradlew --no-daemon :app:assembleDebug, build) (push) Has been cancelled
CI / android (./gradlew --no-daemon :app:testDebugUnitTest, test) (push) Has been cancelled
Docker Release / approve_manual_backfill (push) Has been cancelled
Docker Release / build-amd64 (push) Has been cancelled
Docker Release / build-arm64 (push) Has been cancelled
Docker Release / create-manifest (push) Has been cancelled
Install Smoke / install-smoke (push) Has been cancelled
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { deriveCopilotApiBaseUrlFromToken, resolveCopilotApiToken } from "./token.js";
|
|
|
|
describe("github-copilot token", () => {
|
|
const loadJsonFile = vi.fn();
|
|
const saveJsonFile = vi.fn();
|
|
const cachePath = "/tmp/openclaw-state/credentials/github-copilot.token.json";
|
|
|
|
beforeEach(() => {
|
|
loadJsonFile.mockClear();
|
|
saveJsonFile.mockClear();
|
|
});
|
|
|
|
it("derives baseUrl from token", async () => {
|
|
expect(deriveCopilotApiBaseUrlFromToken("token;proxy-ep=proxy.example.com;")).toBe(
|
|
"https://api.example.com",
|
|
);
|
|
expect(deriveCopilotApiBaseUrlFromToken("token;proxy-ep=https://proxy.foo.bar;")).toBe(
|
|
"https://api.foo.bar",
|
|
);
|
|
});
|
|
|
|
it("uses cache when token is still valid", async () => {
|
|
const now = Date.now();
|
|
loadJsonFile.mockReturnValue({
|
|
token: "cached;proxy-ep=proxy.example.com;",
|
|
expiresAt: now + 60 * 60 * 1000,
|
|
updatedAt: now,
|
|
});
|
|
|
|
const fetchImpl = vi.fn();
|
|
const res = await resolveCopilotApiToken({
|
|
githubToken: "gh",
|
|
cachePath,
|
|
loadJsonFileImpl: loadJsonFile,
|
|
saveJsonFileImpl: saveJsonFile,
|
|
fetchImpl: fetchImpl as unknown as typeof fetch,
|
|
});
|
|
|
|
expect(res.token).toBe("cached;proxy-ep=proxy.example.com;");
|
|
expect(res.baseUrl).toBe("https://api.example.com");
|
|
expect(String(res.source)).toContain("cache:");
|
|
expect(fetchImpl).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("fetches and stores token when cache is missing", async () => {
|
|
loadJsonFile.mockReturnValue(undefined);
|
|
|
|
const fetchImpl = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({
|
|
token: "fresh;proxy-ep=https://proxy.contoso.test;",
|
|
expires_at: Math.floor(Date.now() / 1000) + 3600,
|
|
}),
|
|
});
|
|
|
|
const { resolveCopilotApiToken } = await import("./token.js");
|
|
|
|
const res = await resolveCopilotApiToken({
|
|
githubToken: "gh",
|
|
cachePath,
|
|
loadJsonFileImpl: loadJsonFile,
|
|
saveJsonFileImpl: saveJsonFile,
|
|
fetchImpl: fetchImpl as unknown as typeof fetch,
|
|
});
|
|
|
|
expect(res.token).toBe("fresh;proxy-ep=https://proxy.contoso.test;");
|
|
expect(res.baseUrl).toBe("https://api.contoso.test");
|
|
expect(saveJsonFile).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|