Skip to content

Commit ff73e14

Browse files
committed
test(github-copilot): mock guarded device flow fetch
1 parent e9cf3e3 commit ff73e14

1 file changed

Lines changed: 30 additions & 27 deletions

File tree

extensions/github-copilot/index.test.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
1818
const mocks = vi.hoisted(() => ({
1919
githubCopilotLoginCommand: vi.fn(),
2020
resolveCopilotApiToken: vi.fn(),
21+
fetchWithSsrFGuard: vi.fn(),
2122
}));
2223

2324
vi.mock("./register.runtime.js", () => ({
@@ -27,6 +28,11 @@ vi.mock("./register.runtime.js", () => ({
2728
fetchCopilotUsage: vi.fn(),
2829
}));
2930

31+
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => ({
32+
...(await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>()),
33+
fetchWithSsrFGuard: mocks.fetchWithSsrFGuard,
34+
}));
35+
3036
import plugin from "./index.js";
3137

3238
const tempDirs: string[] = [];
@@ -313,36 +319,33 @@ describe("github-copilot plugin", () => {
313319
},
314320
}),
315321
);
316-
const fetchMock = vi.fn(async (input: unknown) => {
317-
const target =
318-
typeof input === "string"
319-
? input
320-
: input instanceof URL
321-
? input.toString()
322-
: input instanceof Request
323-
? input.url
324-
: String(input);
325-
if (target === "https://github.com/login/device/code") {
326-
return new Response(
327-
JSON.stringify({
328-
device_code: "device-code-stub",
329-
user_code: "ABCD-1234",
330-
verification_uri: "https://github.com/login/device",
331-
expires_in: 900,
332-
interval: 0,
333-
}),
334-
{ status: 200, headers: { "Content-Type": "application/json" } },
335-
);
322+
mocks.fetchWithSsrFGuard.mockImplementation(async ({ url }: { url: string }) => {
323+
if (url === "https://github.com/login/device/code") {
324+
return {
325+
response: new Response(
326+
JSON.stringify({
327+
device_code: "device-code-stub",
328+
user_code: "ABCD-1234",
329+
verification_uri: "https://github.com/login/device",
330+
expires_in: 900,
331+
interval: 0,
332+
}),
333+
{ status: 200, headers: { "Content-Type": "application/json" } },
334+
),
335+
release: vi.fn(async () => {}),
336+
};
336337
}
337-
if (target === "https://github.com/login/oauth/access_token") {
338-
return new Response(
339-
JSON.stringify({ access_token: "refreshed-token", token_type: "bearer" }),
340-
{ status: 200, headers: { "Content-Type": "application/json" } },
341-
);
338+
if (url === "https://github.com/login/oauth/access_token") {
339+
return {
340+
response: new Response(
341+
JSON.stringify({ access_token: "refreshed-token", token_type: "bearer" }),
342+
{ status: 200, headers: { "Content-Type": "application/json" } },
343+
),
344+
release: vi.fn(async () => {}),
345+
};
342346
}
343-
throw new Error(`unexpected fetch in github-copilot refresh test: ${target}`);
347+
throw new Error(`unexpected fetch in github-copilot refresh test: ${url}`);
344348
});
345-
vi.stubGlobal("fetch", fetchMock);
346349
const prompter = {
347350
confirm: vi.fn(async () => true),
348351
note: vi.fn(),

0 commit comments

Comments
 (0)