|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + EnvironmentId, |
| 4 | + type PeerEnvironmentOperation, |
| 5 | + type PeerEnvironmentResponse, |
| 6 | + type PeerEnvironmentSummary, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 9 | +import * as Deferred from "effect/Deferred"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | +import * as Fiber from "effect/Fiber"; |
| 12 | +import * as Stream from "effect/Stream"; |
| 13 | +import * as TestClock from "effect/testing/TestClock"; |
| 14 | + |
| 15 | +import * as PeerEnvironmentBroker from "./PeerEnvironmentBroker.ts"; |
| 16 | + |
| 17 | +const makeBroker = PeerEnvironmentBroker.make.pipe(Effect.provide(NodeServices.layer)); |
| 18 | +const r2d2 = EnvironmentId.make("environment-r2d2"); |
| 19 | +const summary = (status: PeerEnvironmentSummary["status"]): PeerEnvironmentSummary => ({ |
| 20 | + environmentId: r2d2, |
| 21 | + label: "r2d2", |
| 22 | + status, |
| 23 | +}); |
| 24 | + |
| 25 | +/** A connected app that answers every request with `answer`. */ |
| 26 | +const connectApp = ( |
| 27 | + broker: PeerEnvironmentBroker.PeerEnvironmentBroker["Service"], |
| 28 | + clientId: string, |
| 29 | + answer: (operation: PeerEnvironmentOperation) => PeerEnvironmentResponse["outcome"] | null, |
| 30 | +) => |
| 31 | + Effect.gen(function* () { |
| 32 | + const events = yield* broker.connect({ clientId }); |
| 33 | + const connected = yield* Deferred.make<void>(); |
| 34 | + yield* events.pipe( |
| 35 | + Stream.runForEach((event) => { |
| 36 | + if (event.type === "connected") return Deferred.succeed(connected, undefined); |
| 37 | + const outcome = answer(event.request.operation); |
| 38 | + return outcome === null |
| 39 | + ? Effect.void |
| 40 | + : broker.respond({ |
| 41 | + clientId, |
| 42 | + connectionId: event.connectionId, |
| 43 | + requestId: event.request.requestId, |
| 44 | + outcome, |
| 45 | + }); |
| 46 | + }), |
| 47 | + Effect.forkScoped, |
| 48 | + ); |
| 49 | + yield* Deferred.await(connected); |
| 50 | + }); |
| 51 | + |
| 52 | +it.effect("refuses to reach another environment when no app is connected", () => |
| 53 | + Effect.gen(function* () { |
| 54 | + const broker = yield* makeBroker; |
| 55 | + const error = yield* Effect.flip(broker.invoke({ operation: "catalog", environmentId: r2d2 })); |
| 56 | + assert.equal(error.code, "host_unavailable"); |
| 57 | + assert.include(error.message, "connected to both"); |
| 58 | + }), |
| 59 | +); |
| 60 | + |
| 61 | +it.effect("carries a request through the connected app and returns its answer", () => |
| 62 | + Effect.gen(function* () { |
| 63 | + const broker = yield* makeBroker; |
| 64 | + const seen: Array<PeerEnvironmentOperation> = []; |
| 65 | + yield* connectApp(broker, "desktop", (operation) => { |
| 66 | + seen.push(operation); |
| 67 | + return { ok: true, result: { operation: "list", environments: [summary("connected")] } }; |
| 68 | + }); |
| 69 | + assert.deepEqual(yield* broker.list, [summary("connected")]); |
| 70 | + assert.deepEqual(seen, [{ operation: "list" }]); |
| 71 | + }).pipe(Effect.scoped), |
| 72 | +); |
| 73 | + |
| 74 | +it.effect("tries another app when the newest one cannot reach the target", () => |
| 75 | + Effect.gen(function* () { |
| 76 | + const broker = yield* makeBroker; |
| 77 | + yield* connectApp(broker, "desktop", () => ({ |
| 78 | + ok: true, |
| 79 | + result: { |
| 80 | + operation: "catalog", |
| 81 | + label: "r2d2", |
| 82 | + serverVersion: "1.0.0", |
| 83 | + providers: [], |
| 84 | + projects: [], |
| 85 | + }, |
| 86 | + })); |
| 87 | + yield* connectApp(broker, "phone", () => ({ |
| 88 | + ok: false, |
| 89 | + code: "environment_not_connected", |
| 90 | + message: "not saved on this phone", |
| 91 | + })); |
| 92 | + const result = yield* broker.invoke({ operation: "catalog", environmentId: r2d2 }); |
| 93 | + assert.equal(result.label, "r2d2"); |
| 94 | + }).pipe(Effect.scoped), |
| 95 | +); |
| 96 | + |
| 97 | +it.effect("does not retry elsewhere once the target itself refused the request", () => |
| 98 | + Effect.gen(function* () { |
| 99 | + const broker = yield* makeBroker; |
| 100 | + let olderAppAsked = false; |
| 101 | + yield* connectApp(broker, "desktop", () => { |
| 102 | + olderAppAsked = true; |
| 103 | + return null; |
| 104 | + }); |
| 105 | + yield* connectApp(broker, "phone", () => ({ |
| 106 | + ok: false, |
| 107 | + code: "environment_unauthorized", |
| 108 | + message: "r2d2 refused the request", |
| 109 | + })); |
| 110 | + const error = yield* Effect.flip(broker.invoke({ operation: "catalog", environmentId: r2d2 })); |
| 111 | + assert.equal(error.code, "environment_unauthorized"); |
| 112 | + assert.isFalse(olderAppAsked); |
| 113 | + }).pipe(Effect.scoped), |
| 114 | +); |
| 115 | + |
| 116 | +it.effect("ignores an answer that does not come from the app that was asked", () => |
| 117 | + Effect.gen(function* () { |
| 118 | + const broker = yield* makeBroker; |
| 119 | + const events = yield* broker.connect({ clientId: "desktop" }); |
| 120 | + const fiber = yield* broker |
| 121 | + .invoke({ operation: "catalog", environmentId: r2d2 }, 1_000) |
| 122 | + .pipe(Effect.flip, Effect.forkScoped); |
| 123 | + const request = yield* events.pipe( |
| 124 | + Stream.filter((event) => event.type === "request"), |
| 125 | + Stream.runHead, |
| 126 | + ); |
| 127 | + assert.isTrue(request._tag === "Some"); |
| 128 | + if (request._tag !== "Some") return; |
| 129 | + yield* broker.respond({ |
| 130 | + clientId: "someone-else", |
| 131 | + connectionId: request.value.connectionId, |
| 132 | + requestId: request.value.request.requestId, |
| 133 | + outcome: { ok: true, result: { operation: "list", environments: [] } }, |
| 134 | + }); |
| 135 | + yield* TestClock.adjust(3_000); |
| 136 | + const error = yield* Fiber.join(fiber); |
| 137 | + assert.equal(error.code, "host_unavailable"); |
| 138 | + }).pipe(Effect.scoped), |
| 139 | +); |
| 140 | + |
| 141 | +it.effect("reports the best status when several apps know the same environment", () => |
| 142 | + Effect.gen(function* () { |
| 143 | + const broker = yield* makeBroker; |
| 144 | + yield* connectApp(broker, "desktop", () => ({ |
| 145 | + ok: true, |
| 146 | + result: { operation: "list", environments: [summary("connected")] }, |
| 147 | + })); |
| 148 | + yield* connectApp(broker, "phone", () => ({ |
| 149 | + ok: true, |
| 150 | + result: { operation: "list", environments: [summary("offline")] }, |
| 151 | + })); |
| 152 | + assert.deepEqual(yield* broker.list, [summary("connected")]); |
| 153 | + }).pipe(Effect.scoped), |
| 154 | +); |
0 commit comments