@@ -13,9 +13,13 @@ import { |
13 | 13 | type OrchestrationV2ThreadHistoryPage, |
14 | 14 | } from "@t3tools/contracts"; |
15 | 15 | import * as Effect from "effect/Effect"; |
| 16 | +import * as Exit from "effect/Exit"; |
| 17 | +import * as Fiber from "effect/Fiber"; |
16 | 18 | import * as Layer from "effect/Layer"; |
17 | 19 | import * as Option from "effect/Option"; |
| 20 | +import * as Queue from "effect/Queue"; |
18 | 21 | import * as Schema from "effect/Schema"; |
| 22 | +import * as TestClock from "effect/testing/TestClock"; |
19 | 23 | import type { HttpClient } from "effect/http"; |
20 | 24 |
|
21 | 25 | import * as RemoteEnvironmentAuthorization from "../authorization/service.ts"; |
@@ -170,6 +174,14 @@ function makeHarness(reply: (requestNumber: number) => Response | Promise<Respon |
170 | 174 | }; |
171 | 175 | } |
172 | 176 |
|
| 177 | +// Resolves "settled" instead of hanging when the load finishes without sending another request. |
| 178 | +function nextRequestOrSettled(requests: Queue.Queue<number>, load: Fiber.Fiber<unknown, unknown>) { |
| 179 | + return Effect.raceFirst( |
| 180 | + Queue.take(requests).pipe(Effect.as("sent" as const)), |
| 181 | + Fiber.await(load).pipe(Effect.as("settled" as const)), |
| 182 | + ); |
| 183 | +} |
| 184 | + |
173 | 185 | type HttpInput = ReturnType<typeof makeHarness>["input"]; |
174 | 186 | const LOADERS: ReadonlyArray<{ |
175 | 187 | readonly name: string; |
@@ -521,6 +533,69 @@ describe("authenticated environment HTTP requests", () => { |
521 | 533 | }), |
522 | 534 | ); |
523 | 535 |
|
| 536 | + it.effect( |
| 537 | + "waits longer for session permissions after the environment is too slow to answer", |
| 538 | + () => |
| 539 | + Effect.gen(function* () { |
| 540 | + const requests = yield* Queue.unbounded<number>(); |
| 541 | + let answer: () => void = () => {}; |
| 542 | + const answered = new Promise<void>((resolve) => { |
| 543 | + answer = resolve; |
| 544 | + }); |
| 545 | + const harness = makeHarness((requestNumber) => { |
| 546 | + Queue.offerUnsafe(requests, requestNumber); |
| 547 | + return requestNumber === 1 |
| 548 | + ? new Promise<Response>(() => {}) |
| 549 | + : answered.then(() => Response.json(SESSION)); |
| 550 | + }); |
| 551 | + const load = yield* fetchEnvironmentSessionState({ |
| 552 | + prepared: { ...PREPARED, httpAuthorization: { _tag: "Bearer", token: "bearer-token" } }, |
| 553 | + signer: Option.none(), |
| 554 | + timeoutMs: 1_000, |
| 555 | + }).pipe(Effect.provide(harness.httpLayer), Effect.forkChild({ startImmediately: true })); |
| 556 | + |
| 557 | + yield* Queue.take(requests); |
| 558 | + yield* TestClock.adjust("1 second"); |
| 559 | + expect(yield* nextRequestOrSettled(requests, load)).toBe("sent"); |
| 560 | + yield* TestClock.adjust("1500 millis"); |
| 561 | + answer(); |
| 562 | + |
| 563 | + expect(yield* Fiber.await(load)).toStrictEqual(Exit.succeed(SESSION)); |
| 564 | + expect(harness.calls).toHaveLength(2); |
| 565 | + }), |
| 566 | + ); |
| 567 | + |
| 568 | + it.effect("gives up on session permissions once a 60 second wait also times out", () => |
| 569 | + Effect.gen(function* () { |
| 570 | + const requests = yield* Queue.unbounded<number>(); |
| 571 | + const harness = makeHarness((requestNumber) => { |
| 572 | + Queue.offerUnsafe(requests, requestNumber); |
| 573 | + return new Promise<Response>(() => {}); |
| 574 | + }); |
| 575 | + const load = yield* fetchEnvironmentSessionState({ |
| 576 | + prepared: { ...PREPARED, httpAuthorization: { _tag: "Bearer", token: "bearer-token" } }, |
| 577 | + signer: Option.none(), |
| 578 | + timeoutMs: 15_000, |
| 579 | + }).pipe(Effect.provide(harness.httpLayer), Effect.forkChild({ startImmediately: true })); |
| 580 | + |
| 581 | + yield* Queue.take(requests); |
| 582 | + yield* TestClock.adjust("15 seconds"); |
| 583 | + expect(yield* nextRequestOrSettled(requests, load)).toBe("sent"); |
| 584 | + yield* TestClock.adjust("30 seconds"); |
| 585 | + expect(yield* nextRequestOrSettled(requests, load)).toBe("sent"); |
| 586 | + yield* TestClock.adjust("60 seconds"); |
| 587 | + const exit = yield* Fiber.await(load); |
| 588 | + |
| 589 | + expect(exit).toMatchObject({ |
| 590 | + _tag: "Failure", |
| 591 | + cause: { |
| 592 | + reasons: [{ error: { _tag: "RemoteEnvironmentAuthTimeoutError", timeoutMs: 60_000 } }], |
| 593 | + }, |
| 594 | + }); |
| 595 | + expect(harness.calls).toHaveLength(3); |
| 596 | + }), |
| 597 | + ); |
| 598 | + |
524 | 599 | it.effect("reports persistent session rejection instead of showing missing permissions", () => |
525 | 600 | Effect.gen(function* () { |
526 | 601 | const harness = makeHarness(() => Response.json(UNAUTHENTICATED_SESSION)); |
|