// @vitest-environment jsdom import { HexclaveClientInterface } from "@stackframe/stack-shared"; import { describe, expect, it, vi } from "vitest"; import { getNewOAuthProviderOrScopeUrl } from "./auth"; vi.mock("./cookie", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, saveVerifierAndState: async () => ({ codeChallenge: "", state: "", }), }; }); describe("getNewOAuthProviderOrScopeUrl", () => { it("returns the OAuth URL without performing navigation", async () => { window.history.replaceState({}, "", "/account?after_auth_return_to=%2Fsettings"); const iface = new HexclaveClientInterface({ clientVersion: "test", getBaseUrl: () => "https://api.example.com", getApiUrls: () => ["https://api.example.com"], extraRequestHeaders: {}, projectId: "00000000-0000-4000-8000-000000000000", publishableClientKey: "pck_test", }); const session = iface.createSession({ refreshToken: null, accessToken: null }); const location = await getNewOAuthProviderOrScopeUrl( iface, { provider: "github", redirectUrl: "/handler/oauth-callback", errorRedirectUrl: "/handler/error", providerScope: "repo user", }, session, ); const url = new URL(location); expect(`${url.origin}${url.pathname}`).toBe("https://api.example.com/api/v1/auth/oauth/authorize/github"); expect(Object.fromEntries(url.searchParams.entries())).toMatchInlineSnapshot(` { "after_callback_redirect_url": "http://localhost:3000/account?after_auth_return_to=%2Fsettings", "client_id": "00000000-0000-4000-8000-000000000000", "client_secret": "pck_test", "code_challenge": "", "code_challenge_method": "S256", "error_redirect_url": "http://localhost:3000/handler/error?after_auth_return_to=%2Fsettings", "grant_type": "authorization_code", "provider_scope": "repo user", "redirect_uri": "http://localhost:3000/handler/oauth-callback?after_auth_return_to=%2Fsettings", "response_type": "code", "scope": "legacy", "state": "", "type": "link", } `); }); });