| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import * as Cause from "effect/Cause"; |
| 3 | 4 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 4 | 5 | import * as Deferred from "effect/Deferred"; |
| 5 | 6 | import * as Duration from "effect/Duration"; |
| ... | ... | @@ -15,6 +16,8 @@ import * as TestClock from "effect/testing/TestClock"; |
| 15 | 16 | import * as HttpClient from "effect/http/HttpClient"; |
| 16 | 17 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 17 | 18 | |
| 19 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 20 | import * as ExternalLauncher from "../process/externalLauncher.ts"; |
| 18 | 21 | import * as CliTokenManager from "./CliTokenManager.ts"; |
| 19 | 22 | |
| 20 | 23 | // pk_test_<base64 of "clerk.example.test$"> |
| ... | ... | @@ -164,6 +167,57 @@ const layerDeviceFlow = (server: DeviceFlowServer) => |
| 164 | 167 | const tokenRequests = (requests: ReadonlyArray<RecordedTokenRequest>) => |
| 165 | 168 | requests.filter((request) => request.url.endsWith("/oauth/token")); |
| 166 | 169 | |
| 170 | // The endpoint sends 200 headers, then the JSON body never finishes. |
| 171 | const layerStalledBody = Layer.succeed( |
| 172 | HttpClient.HttpClient, |
| 173 | HttpClient.make((request) => |
| 174 | Effect.succeed( |
| 175 | HttpClientResponse.fromWeb( |
| 176 | request, |
| 177 | new Response( |
| 178 | new ReadableStream({ |
| 179 | start: (controller) => controller.enqueue(new TextEncoder().encode("{")), |
| 180 | }), |
| 181 | { status: 200, headers: { "content-type": "application/json" } }, |
| 182 | ), |
| 183 | ), |
| 184 | ), |
| 185 | ), |
| 186 | ); |
| 187 | |
| 188 | const expiredStoredToken = new TextEncoder().encode( |
| 189 | JSON.stringify({ accessToken: "expired", refreshToken: "refresh-token-1", expiresAtEpochMs: 0 }), |
| 190 | ); |
| 191 | |
| 192 | it.effect("a stalled token refresh fails and releases the credential lock", () => |
| 193 | Effect.gen(function* () { |
| 194 | const manager = yield* CliTokenManager.CloudCliTokenManager; |
| 195 | const refresh = yield* manager.getExisting.pipe(Effect.flip, Effect.forkChild); |
| 196 | const hasCredential = yield* manager.hasCredential.pipe(Effect.forkChild); |
| 197 | |
| 198 | yield* TestClock.adjust(Duration.seconds(10)); |
| 199 | |
| 200 | assert.instanceOf(yield* Fiber.join(refresh), CliTokenManager.CloudCliCredentialRefreshError); |
| 201 | assert.isTrue(yield* Fiber.join(hasCredential)); |
| 202 | }).pipe( |
| 203 | Effect.provide( |
| 204 | CliTokenManager.layer.pipe( |
| 205 | Layer.provide( |
| 206 | Layer.mergeAll( |
| 207 | layerStalledBody, |
| 208 | Layer.mock(ServerSecretStore.ServerSecretStore)({ |
| 209 | get: () => Effect.succeedSome(expiredStoredToken), |
| 210 | }), |
| 211 | Layer.mock(ExternalLauncher.ExternalLauncher)({}), |
| 212 | NodeServices.layer, |
| 213 | ), |
| 214 | ), |
| 215 | ), |
| 216 | ), |
| 217 | provideTestEnv, |
| 218 | ), |
| 219 | ); |
| 220 | |
| 167 | 221 | it.layer(NodeServices.layer)("CliTokenManager.deviceAuthorizationLogin", (it) => { |
| 168 | 222 | it.effect("requests a device code, shows it, and polls until Clerk grants the token", () => |
| 169 | 223 | Effect.gen(function* () { |
| ... | ... | @@ -322,4 +376,18 @@ it.layer(NodeServices.layer)("CliTokenManager.deviceAuthorizationLogin", (it) => |
| 322 | 376 | assert.isTrue(isAuthorizationError(result)); |
| 323 | 377 | }), |
| 324 | 378 | ); |
| 379 | |
| 380 | it.effect("fails instead of hanging when the device authorization response stalls", () => |
| 381 | Effect.gen(function* () { |
| 382 | const fiber = yield* CliTokenManager.deviceAuthorizationLogin(() => Effect.void).pipe( |
| 383 | Effect.provide(layerStalledBody), |
| 384 | provideTestEnv, |
| 385 | Effect.flip, |
| 386 | Effect.forkChild, |
| 387 | ); |
| 388 | yield* TestClock.adjust(Duration.seconds(10)); |
| 389 | |
| 390 | assert.isTrue(Cause.isTimeoutError(yield* Fiber.join(fiber))); |
| 391 | }), |
| 392 | ); |
| 325 | 393 | }); |