Files
oh-my-pi/packages/coding-agent/test/oauth-discovery.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

53 lines
1.8 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import {
analyzeAuthError,
discoverOAuthEndpoints,
extractMcpAuthServerUrl,
} from "@oh-my-pi/pi-coding-agent/mcp/oauth-discovery";
import { hookFetch } from "@oh-my-pi/pi-utils";
describe("mcp oauth discovery", () => {
it("extracts Mcp-Auth-Server from transport error headers", () => {
const error = new Error(
'HTTP 401: unauthorized [WWW-Authenticate: Bearer resource_metadata="https://mcp.figma.com/.well-known/oauth-protected-resource"; Mcp-Auth-Server: https://www.figma.com]',
);
expect(extractMcpAuthServerUrl(error)).toBe("https://www.figma.com/");
const auth = analyzeAuthError(error);
expect(auth.requiresAuth).toBe(true);
expect(auth.authServerUrl).toBe("https://www.figma.com/");
});
it("discovers oauth endpoints from auth server metadata", async () => {
const calls: string[] = [];
using _hook = hookFetch(input => {
const url = String(input);
calls.push(url);
if (url === "https://www.figma.com/.well-known/oauth-authorization-server") {
return new Response(
JSON.stringify({
authorization_endpoint: "https://www.figma.com/oauth",
token_endpoint: "https://api.figma.com/v1/oauth/token",
client_id: "figma-client-id",
scopes_supported: ["file_read", "file_write"],
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
);
}
return new Response("not found", { status: 404 });
});
const oauth = await discoverOAuthEndpoints("https://mcp.figma.com/mcp", "https://www.figma.com");
expect(oauth).toEqual({
authorizationUrl: "https://www.figma.com/oauth",
tokenUrl: "https://api.figma.com/v1/oauth/token",
clientId: "figma-client-id",
scopes: "file_read file_write",
});
expect(calls[0]).toBe("https://www.figma.com/.well-known/oauth-authorization-server");
});
});