| ... | @@ -1,29 +1,35 @@ | ... | @@ -1,29 +1,35 @@ |
| 1 | import { assert, describe, it } from "@effect/vitest"; | 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; | 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; | | |
| 4 | import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"; | | |
| 5 | import { afterEach } from "vite-plus/test"; | 3 | import { afterEach } from "vite-plus/test"; |
| 6 | | 4 | |
| 7 | import { fetchCloudAuth, validateClerkFrontendApiUrl } from "./cloudAuth.ts"; | 5 | import { fetchCloudAuth, validateClerkFrontendApiUrl } from "./cloudAuth.ts"; |
| 8 | | 6 | |
| 9 | const originalClerkPublishableKey = process.env.T3CODE_CLERK_PUBLISHABLE_KEY; | 7 | const originalClerkPublishableKey = process.env.T3CODE_CLERK_PUBLISHABLE_KEY; |
| | 8 | const originalFetch = globalThis.fetch; |
| 10 | | 9 | |
| 11 | const clerkPublishableKey = (hostname: string): string => | 10 | const clerkPublishableKey = (hostname: string): string => |
| 12 | `pk_test_${Buffer.from(`${hostname}$`).toString("base64")}`; | 11 | `pk_test_${Buffer.from(`${hostname}$`).toString("base64")}`; |
| 13 | | 12 | |
| 14 | function makeHttpClientLayer( | 13 | type FetchCall = readonly [input: RequestInfo | URL, init: RequestInit]; |
| 15 | handler: ( | 14 | |
| 16 | request: HttpClientRequest.HttpClientRequest, | 15 | const recordedFetch = (...responses: ReadonlyArray<Response>) => { |
| 17 | ) => Effect.Effect<HttpClientResponse.HttpClientResponse, never>, | 16 | const calls: Array<FetchCall> = []; |
| 18 | ) { | 17 | let responseIndex = 0; |
| 19 | return Layer.succeed( | 18 | const fetchFn = ((input, init) => { |
| 20 | HttpClient.HttpClient, | 19 | calls.push([input, init ?? {}]); |
| 21 | HttpClient.make((request) => handler(request)), | 20 | const response = responses[responseIndex++]; |
| 22 | ); | 21 | if (!response) { |
| 23 | } | 22 | return Promise.reject(new Error("Unexpected fetch call")); |
| | 23 | } |
| | 24 | return Promise.resolve(response); |
| | 25 | }) satisfies typeof fetch; |
| | 26 | |
| | 27 | return { fetchFn, calls }; |
| | 28 | }; |
| 24 | | 29 | |
| 25 | describe("Desktop cloud auth IPC", () => { | 30 | describe("Desktop cloud auth IPC", () => { |
| 26 | afterEach(() => { | 31 | afterEach(() => { |
| | 32 | globalThis.fetch = originalFetch; |
| 27 | if (originalClerkPublishableKey === undefined) { | 33 | if (originalClerkPublishableKey === undefined) { |
| 28 | delete process.env.T3CODE_CLERK_PUBLISHABLE_KEY; | 34 | delete process.env.T3CODE_CLERK_PUBLISHABLE_KEY; |
| 29 | } else { | 35 | } else { |
| ... | @@ -33,16 +39,8 @@ describe("Desktop cloud auth IPC", () => { | ... | @@ -33,16 +39,8 @@ describe("Desktop cloud auth IPC", () => { |
| 33 | | 39 | |
| 34 | it.effect("preserves Clerk's URL-encoded OAuth form content type", () => { | 40 | it.effect("preserves Clerk's URL-encoded OAuth form content type", () => { |
| 35 | const body = "strategy=oauth_google&redirect_url=t3code%3A%2F%2Fauth%2Fcallback"; | 41 | const body = "strategy=oauth_google&redirect_url=t3code%3A%2F%2Fauth%2Fcallback"; |
| 36 | let forwardedRequest: HttpClientRequest.HttpClientRequest | null = null; | 42 | const fetch = recordedFetch(Response.json({ response: { object: "sign_in_attempt" } })); |
| 37 | const layer = makeHttpClientLayer((request) => | 43 | globalThis.fetch = fetch.fetchFn; |
| 38 | Effect.sync(() => { | | |
| 39 | forwardedRequest = request; | | |
| 40 | return HttpClientResponse.fromWeb( | | |
| 41 | request, | | |
| 42 | Response.json({ response: { object: "sign_in_attempt" } }), | | |
| 43 | ); | | |
| 44 | }), | | |
| 45 | ); | | |
| 46 | | 44 | |
| 47 | return Effect.gen(function* () { | 45 | return Effect.gen(function* () { |
| 48 | yield* fetchCloudAuth.handler({ | 46 | yield* fetchCloudAuth.handler({ |
| ... | @@ -55,32 +53,25 @@ describe("Desktop cloud auth IPC", () => { | ... | @@ -55,32 +53,25 @@ describe("Desktop cloud auth IPC", () => { |
| 55 | body, | 53 | body, |
| 56 | }); | 54 | }); |
| 57 | | 55 | |
| 58 | assert(forwardedRequest !== null); | 56 | const forwardedRequest = fetch.calls[0]; |
| | 57 | assert(forwardedRequest !== undefined); |
| | 58 | const [url, init] = forwardedRequest; |
| | 59 | assert.equal(String(url), "https://example.clerk.accounts.dev/v1/client/sign_ins"); |
| | 60 | assert.equal(init.method, "POST"); |
| 59 | assert.equal( | 61 | assert.equal( |
| 60 | forwardedRequest.headers["content-type"], | 62 | new Headers(init.headers).get("content-type"), |
| 61 | "application/x-www-form-urlencoded;charset=UTF-8", | 63 | "application/x-www-form-urlencoded;charset=UTF-8", |
| 62 | ); | 64 | ); |
| 63 | assert.equal(forwardedRequest.body._tag, "Uint8Array"); | 65 | assert.equal(new TextDecoder().decode(init.body as Uint8Array), body); |
| 64 | if (forwardedRequest.body._tag === "Uint8Array") { | 66 | }); |
| 65 | assert.equal(new TextDecoder().decode(forwardedRequest.body.body), body); | | |
| 66 | } | | |
| 67 | }).pipe(Effect.provide(layer)); | | |
| 68 | }); | 67 | }); |
| 69 | | 68 | |
| 70 | it.effect( | 69 | it.effect( |
| 71 | "allows the custom Clerk Frontend API host encoded by the configured publishable key", | 70 | "allows the custom Clerk Frontend API host encoded by the configured publishable key", |
| 72 | () => { | 71 | () => { |
| 73 | process.env.T3CODE_CLERK_PUBLISHABLE_KEY = clerkPublishableKey("clerk.t3.codes"); | 72 | process.env.T3CODE_CLERK_PUBLISHABLE_KEY = clerkPublishableKey("clerk.t3.codes"); |
| 74 | let forwardedRequest: HttpClientRequest.HttpClientRequest | null = null; | 73 | const fetch = recordedFetch(Response.json({ response: { object: "client" } })); |
| 75 | const layer = makeHttpClientLayer((request) => | 74 | globalThis.fetch = fetch.fetchFn; |
| 76 | Effect.sync(() => { | | |
| 77 | forwardedRequest = request; | | |
| 78 | return HttpClientResponse.fromWeb( | | |
| 79 | request, | | |
| 80 | Response.json({ response: { object: "client" } }), | | |
| 81 | ); | | |
| 82 | }), | | |
| 83 | ); | | |
| 84 | | 75 | |
| 85 | return Effect.gen(function* () { | 76 | return Effect.gen(function* () { |
| 86 | yield* fetchCloudAuth.handler({ | 77 | yield* fetchCloudAuth.handler({ |
| ... | @@ -89,9 +80,10 @@ describe("Desktop cloud auth IPC", () => { | ... | @@ -89,9 +80,10 @@ describe("Desktop cloud auth IPC", () => { |
| 89 | headers: {}, | 80 | headers: {}, |
| 90 | }); | 81 | }); |
| 91 | | 82 | |
| 92 | assert(forwardedRequest !== null); | 83 | const forwardedRequest = fetch.calls[0]; |
| 93 | assert.equal(forwardedRequest.url.toString(), "https://clerk.t3.codes/v1/client"); | 84 | assert(forwardedRequest !== undefined); |
| 94 | }).pipe(Effect.provide(layer)); | 85 | assert.equal(String(forwardedRequest[0]), "https://clerk.t3.codes/v1/client"); |
| | 86 | }); |
| 95 | }, | 87 | }, |
| 96 | ); | 88 | ); |
| 97 | | 89 | |