Files
oh-my-pi/packages/coding-agent/test/oauth-flow.test.ts
T
can1357 005401e82a refactor: extracted fetch mocking into reusable hookFetch utility
- Extracted fetch mocking logic into reusable `hookFetch()` utility function with middleware-style handler pattern.
- Replaced manual `globalThis.fetch` assignment and restoration across 10 test files with `hookFetch()` calls using `using` statement for automatic cleanup.
- Implemented Disposable pattern with Symbol.dispose for fetch hook resource management, eliminating try-finally blocks.
- Exported `hookFetch` from utils public API to enable consistent fetch mocking across packages.
2026-03-08 16:16:54 +01:00

49 lines
1.7 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { MCPOAuthFlow } from "@oh-my-pi/pi-coding-agent/mcp/oauth-flow";
import { hookFetch } from "@oh-my-pi/pi-utils";
describe("mcp oauth flow", () => {
it("uses Codex client name for dynamic client registration", async () => {
let registrationPayload: Record<string, unknown> | null = null;
using _hook = hookFetch((input, init) => {
const url = String(input);
if (url === "https://www.figma.com/.well-known/oauth-authorization-server") {
return new Response(
JSON.stringify({ registration_endpoint: "https://api.figma.com/v1/oauth/mcp/register" }),
{ status: 200, headers: { "Content-Type": "application/json" } },
);
}
if (url === "https://api.figma.com/v1/oauth/mcp/register") {
registrationPayload = JSON.parse(String(init?.body ?? "{}")) as Record<string, unknown>;
return new Response(
JSON.stringify({
client_id: "registered-client-id",
client_secret: "registered-client-secret",
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
);
}
return new Response("not found", { status: 404 });
});
const flow = new MCPOAuthFlow(
{
authorizationUrl: "https://www.figma.com/oauth/mcp",
tokenUrl: "https://api.figma.com/v1/oauth/token",
},
{},
);
const { url } = await flow.generateAuthUrl("test-state", "http://127.0.0.1:53172/callback");
const authUrl = new URL(url);
expect(registrationPayload).not.toBeNull();
expect((registrationPayload as { client_name?: string } | null)?.client_name).toBe("Codex");
expect(authUrl.searchParams.get("client_id")).toBe("registered-client-id");
expect(authUrl.searchParams.get("state")).toBe("test-state");
});
});