|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Stream from "effect/Stream"; |
| 6 | +import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 7 | +import { Atom, AtomRegistry } from "effect/reactivity"; |
| 8 | + |
| 9 | +import { |
| 10 | + AVAILABLE_CONNECTION_STATE, |
| 11 | + ConnectionTransientError, |
| 12 | + type SupervisorConnectionState, |
| 13 | +} from "../connection/model.ts"; |
| 14 | +import * as EnvironmentRegistry from "../connection/registry.ts"; |
| 15 | +import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 16 | +import { createEnvironmentCatalogAtoms } from "./connections.ts"; |
| 17 | + |
| 18 | +const ENVIRONMENT_ID = EnvironmentId.make("catalog-environment"); |
| 19 | + |
| 20 | +const connectionState = ( |
| 21 | + overrides: Partial<SupervisorConnectionState>, |
| 22 | +): SupervisorConnectionState => ({ |
| 23 | + ...AVAILABLE_CONNECTION_STATE, |
| 24 | + desired: true, |
| 25 | + network: "online", |
| 26 | + ...overrides, |
| 27 | +}); |
| 28 | + |
| 29 | +describe("environment catalog atoms", () => { |
| 30 | + it.effect("changes the session generation only when a new session comes up", () => |
| 31 | + Effect.scoped( |
| 32 | + Effect.gen(function* () { |
| 33 | + const state = yield* SubscriptionRef.make( |
| 34 | + connectionState({ phase: "connected", attempt: 1, generation: 1 }), |
| 35 | + ); |
| 36 | + const supervisor = { |
| 37 | + state, |
| 38 | + } as unknown as EnvironmentSupervisor.EnvironmentSupervisor["Service"]; |
| 39 | + const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = ( |
| 40 | + _environmentId, |
| 41 | + stream, |
| 42 | + ) => Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor); |
| 43 | + const registry = EnvironmentRegistry.EnvironmentRegistry.of({ |
| 44 | + followStream, |
| 45 | + } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]); |
| 46 | + const catalog = createEnvironmentCatalogAtoms( |
| 47 | + Atom.runtime(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, registry)), |
| 48 | + ); |
| 49 | + const atom = catalog.sessionGenerationAtom(ENVIRONMENT_ID); |
| 50 | + const atoms = AtomRegistry.make(); |
| 51 | + const unmount = atoms.mount(atom); |
| 52 | + yield* Effect.addFinalizer(() => |
| 53 | + Effect.sync(() => { |
| 54 | + unmount(); |
| 55 | + atoms.dispose(); |
| 56 | + }), |
| 57 | + ); |
| 58 | + const lastFailure = new ConnectionTransientError({ reason: "transport", detail: "Down." }); |
| 59 | + const generationAfter = (next: SupervisorConnectionState) => |
| 60 | + SubscriptionRef.set(state, next).pipe( |
| 61 | + Effect.andThen(Effect.yieldNow), |
| 62 | + Effect.map(() => atoms.get(atom)), |
| 63 | + ); |
| 64 | + |
| 65 | + yield* Effect.yieldNow; |
| 66 | + expect(atoms.get(atom)).toBe(1); |
| 67 | + // A retry reports the generation it is attempting before it is established. |
| 68 | + expect( |
| 69 | + yield* generationAfter( |
| 70 | + connectionState({ phase: "connecting", stage: "opening", generation: 2, lastFailure }), |
| 71 | + ), |
| 72 | + ).toBe(1); |
| 73 | + expect( |
| 74 | + yield* generationAfter( |
| 75 | + connectionState({ phase: "backoff", generation: 1, lastFailure, retryAt: 1 }), |
| 76 | + ), |
| 77 | + ).toBe(1); |
| 78 | + expect( |
| 79 | + yield* generationAfter( |
| 80 | + connectionState({ phase: "connected", attempt: 2, generation: 2 }), |
| 81 | + ), |
| 82 | + ).toBe(2); |
| 83 | + }), |
| 84 | + ), |
| 85 | + ); |
| 86 | +}); |
0 commit comments