anthropic-oauth.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { loginAnthropic, refreshAnthropicToken } from "../src/utils/oauth/anthropic.ts";
  3. function jsonResponse(body: unknown, status: number = 200): Response {
  4. return new Response(JSON.stringify(body), {
  5. status,
  6. headers: {
  7. "Content-Type": "application/json",
  8. },
  9. });
  10. }
  11. function getUrl(input: unknown): string {
  12. if (typeof input === "string") {
  13. return input;
  14. }
  15. if (input instanceof URL) {
  16. return input.toString();
  17. }
  18. if (input instanceof Request) {
  19. return input.url;
  20. }
  21. throw new Error(`Unsupported fetch input: ${String(input)}`);
  22. }
  23. function getJsonBody(init?: RequestInit): Record<string, string> {
  24. if (typeof init?.body !== "string") {
  25. throw new Error(`Expected string request body, got ${typeof init?.body}`);
  26. }
  27. return JSON.parse(init.body) as Record<string, string>;
  28. }
  29. describe.sequential("Anthropic OAuth", () => {
  30. afterEach(() => {
  31. vi.unstubAllGlobals();
  32. });
  33. it("keeps the localhost redirect_uri for manual callback login", async () => {
  34. let authUrl = "";
  35. const fetchMock = vi.fn(async (input: unknown, init?: RequestInit): Promise<Response> => {
  36. expect(getUrl(input)).toBe("https://platform.claude.com/v1/oauth/token");
  37. expect(init?.method).toBe("POST");
  38. const body = getJsonBody(init);
  39. expect(body.grant_type).toBe("authorization_code");
  40. expect(body.code).toBe("manual-code");
  41. expect(body.redirect_uri).toBe("http://localhost:53692/callback");
  42. return jsonResponse({
  43. access_token: "access-token",
  44. refresh_token: "refresh-token",
  45. expires_in: 3600,
  46. });
  47. });
  48. vi.stubGlobal("fetch", fetchMock);
  49. const credentials = await loginAnthropic({
  50. onAuth: (info) => {
  51. authUrl = info.url;
  52. },
  53. onPrompt: async () => "",
  54. onManualCodeInput: async () => {
  55. const url = new URL(authUrl);
  56. const state = url.searchParams.get("state");
  57. const redirectUri = url.searchParams.get("redirect_uri");
  58. if (!state || !redirectUri) {
  59. throw new Error("Missing OAuth state or redirect_uri in auth URL");
  60. }
  61. return `${redirectUri}?code=manual-code&state=${state}`;
  62. },
  63. });
  64. expect(credentials.access).toBe("access-token");
  65. expect(credentials.refresh).toBe("refresh-token");
  66. expect(fetchMock).toHaveBeenCalledOnce();
  67. });
  68. it("omits scope from refresh token requests", async () => {
  69. const fetchMock = vi.fn(async (input: unknown, init?: RequestInit): Promise<Response> => {
  70. expect(getUrl(input)).toBe("https://platform.claude.com/v1/oauth/token");
  71. expect(init?.method).toBe("POST");
  72. const body = getJsonBody(init);
  73. expect(body.grant_type).toBe("refresh_token");
  74. expect(body.client_id).toBeTruthy();
  75. expect(body.refresh_token).toBe("refresh-token");
  76. expect(body).not.toHaveProperty("scope");
  77. return jsonResponse({
  78. access_token: "new-access-token",
  79. refresh_token: "new-refresh-token",
  80. expires_in: 3600,
  81. });
  82. });
  83. vi.stubGlobal("fetch", fetchMock);
  84. const credentials = await refreshAnthropicToken("refresh-token");
  85. expect(credentials.access).toBe("new-access-token");
  86. expect(credentials.refresh).toBe("new-refresh-token");
  87. expect(fetchMock).toHaveBeenCalledOnce();
  88. });
  89. });