|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { EnvironmentId, type AuthSessionState } from "@t3tools/contracts"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Option from "effect/Option"; |
| 6 | +import * as Stream from "effect/Stream"; |
| 7 | +import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 8 | +import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; |
| 9 | + |
| 10 | +import { RelayConnectionTarget, type PreparedConnection } from "../connection/model.ts"; |
| 11 | +import { EnvironmentRegistry } from "../connection/registry.ts"; |
| 12 | +import { EnvironmentSupervisor } from "../connection/supervisor.ts"; |
| 13 | +import * as RpcHttp from "../rpc/http.ts"; |
| 14 | +import { createEnvironmentSessionAtoms } from "./session.ts"; |
| 15 | + |
| 16 | +const TARGET = new RelayConnectionTarget({ |
| 17 | + environmentId: EnvironmentId.make("environment-1"), |
| 18 | + label: "Environment", |
| 19 | +}); |
| 20 | +const PREPARED: PreparedConnection = { |
| 21 | + environmentId: TARGET.environmentId, |
| 22 | + label: TARGET.label, |
| 23 | + httpBaseUrl: "https://environment.example.test", |
| 24 | + socketUrl: "wss://environment.example.test/ws", |
| 25 | + httpAuthorization: { _tag: "Bearer", token: "bearer-token" }, |
| 26 | + target: TARGET, |
| 27 | +}; |
| 28 | +const SESSION = { |
| 29 | + authenticated: true, |
| 30 | + auth: { |
| 31 | + policy: "remote-reachable", |
| 32 | + bootstrapMethods: ["desktop-bootstrap"], |
| 33 | + sessionMethods: ["bearer-access-token"], |
| 34 | + sessionCookieName: "t3_session", |
| 35 | + }, |
| 36 | + scopes: ["orchestration:read", "orchestration:operate"], |
| 37 | +} satisfies AuthSessionState; |
| 38 | + |
| 39 | +function makeSessionAtom(reply: (requestNumber: number) => Promise<Response>) { |
| 40 | + let calls = 0; |
| 41 | + const fetchFn: typeof fetch = () => reply(++calls); |
| 42 | + const layer = Effect.gen(function* () { |
| 43 | + const supervisor = { |
| 44 | + target: TARGET, |
| 45 | + prepared: yield* SubscriptionRef.make(Option.some(PREPARED)), |
| 46 | + } as unknown as EnvironmentSupervisor["Service"]; |
| 47 | + const registry = { |
| 48 | + followStream: <A, E, R>(_environmentId: EnvironmentId, stream: Stream.Stream<A, E, R>) => |
| 49 | + stream.pipe(Stream.provideService(EnvironmentSupervisor, supervisor)), |
| 50 | + } as unknown as EnvironmentRegistry["Service"]; |
| 51 | + return Layer.merge( |
| 52 | + Layer.succeed(EnvironmentRegistry, registry), |
| 53 | + RpcHttp.layerRemoteHttpClient(fetchFn), |
| 54 | + ); |
| 55 | + }).pipe(Layer.unwrap); |
| 56 | + const atom = createEnvironmentSessionAtoms(Atom.runtime(layer)).sessionStateAtom( |
| 57 | + TARGET.environmentId, |
| 58 | + ); |
| 59 | + return { atom, calls: () => calls }; |
| 60 | +} |
| 61 | + |
| 62 | +const settle = ( |
| 63 | + registry: AtomRegistry.AtomRegistry, |
| 64 | + atom: ReturnType<typeof makeSessionAtom>["atom"], |
| 65 | +) => AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true }).pipe(Effect.result); |
| 66 | + |
| 67 | +describe("environment session state", () => { |
| 68 | + it.effect("keeps a confirmed grant through a failed refresh and retries it", () => |
| 69 | + Effect.gen(function* () { |
| 70 | + const session = makeSessionAtom(async (requestNumber) => { |
| 71 | + if (requestNumber === 2) throw new TypeError("Failed to fetch"); |
| 72 | + return Response.json(SESSION); |
| 73 | + }); |
| 74 | + const registry = AtomRegistry.make(); |
| 75 | + yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); |
| 76 | + const observed: Array<string> = []; |
| 77 | + registry.mount(session.atom); |
| 78 | + registry.subscribe(session.atom, (result) => observed.push(result._tag)); |
| 79 | + |
| 80 | + expect(yield* settle(registry, session.atom)).toMatchObject({ success: SESSION }); |
| 81 | + registry.refresh(session.atom); |
| 82 | + expect(yield* settle(registry, session.atom)).toMatchObject({ success: SESSION }); |
| 83 | + |
| 84 | + expect(session.calls()).toBe(3); |
| 85 | + expect(observed).not.toContain("Failure"); |
| 86 | + }).pipe(Effect.scoped), |
| 87 | + ); |
| 88 | + |
| 89 | + it.effect("reports a failed first load without retrying", () => |
| 90 | + Effect.gen(function* () { |
| 91 | + const session = makeSessionAtom(async () => { |
| 92 | + throw new TypeError("Failed to fetch"); |
| 93 | + }); |
| 94 | + const registry = AtomRegistry.make(); |
| 95 | + yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); |
| 96 | + registry.mount(session.atom); |
| 97 | + |
| 98 | + const result = yield* settle(registry, session.atom); |
| 99 | + expect(result._tag).toBe("Failure"); |
| 100 | + expect(AsyncResult.isFailure(registry.get(session.atom))).toBe(true); |
| 101 | + expect(session.calls()).toBe(1); |
| 102 | + }).pipe(Effect.scoped), |
| 103 | + ); |
| 104 | +}); |
0 commit comments