|
1 | | -import type { AdvertisedEndpoint } from "@t3tools/contracts"; |
| 1 | +import { type AdvertisedEndpoint, EnvironmentId } from "@t3tools/contracts"; |
2 | 2 | import { describe, expect, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Option from "effect/Option"; |
| 6 | +import * as TestClock from "effect/testing/TestClock"; |
3 | 7 |
|
| 8 | +import * as ManagedRelay from "../relay/managedRelay.ts"; |
| 9 | +import { remoteHttpClientLayer } from "../rpc/http.ts"; |
| 10 | +import type { PreparedConnection } from "./model.ts"; |
| 11 | +import * as ConnectionPromotion from "./promotion.ts"; |
4 | 12 | import { selectPromotionCandidates } from "./promotion.ts"; |
5 | 13 |
|
6 | 14 | function endpoint(input: { |
@@ -94,3 +102,93 @@ describe("selectPromotionCandidates", () => { |
94 | 102 | expect(candidates).toEqual([]); |
95 | 103 | }); |
96 | 104 | }); |
| 105 | + |
| 106 | +const ENVIRONMENT_ID = EnvironmentId.make("environment-1"); |
| 107 | +const LAN_ENDPOINT = endpoint({ |
| 108 | + id: "server-lan:http://192.168.1.20:3773", |
| 109 | + httpBaseUrl: "http://192.168.1.20:3773/", |
| 110 | + reachability: "lan", |
| 111 | +}); |
| 112 | +const TAILNET_ENDPOINT = endpoint({ |
| 113 | + id: "tailscale-ip:http://100.100.100.100:3773", |
| 114 | + httpBaseUrl: "http://100.100.100.100:3773/", |
| 115 | + reachability: "private-network", |
| 116 | +}); |
| 117 | +const RELAY_PREPARED: PreparedConnection = { |
| 118 | + environmentId: ENVIRONMENT_ID, |
| 119 | + label: "Remote", |
| 120 | + httpBaseUrl: RELAY_BASE_URL, |
| 121 | + socketUrl: "wss://tunnel.example.test/ws", |
| 122 | + httpAuthorization: { _tag: "Dpop", accessToken: "access-token" }, |
| 123 | + target: { _tag: "RelayConnectionTarget", environmentId: ENVIRONMENT_ID, label: "Remote" }, |
| 124 | +}; |
| 125 | + |
| 126 | +// Fake fetch: the relay origin answers the advertised endpoint list, and every |
| 127 | +// direct candidate answers the descriptor probe as the same environment. |
| 128 | +const promotionHarnessLayer = (endpoints: readonly AdvertisedEndpoint[]) => { |
| 129 | + const fetchFn = ((input) => { |
| 130 | + const url = String(input instanceof Request ? input.url : input); |
| 131 | + if (url.endsWith("/api/remote-access/endpoints")) { |
| 132 | + return Promise.resolve(Response.json(endpoints)); |
| 133 | + } |
| 134 | + if (url.endsWith("/.well-known/t3/environment")) { |
| 135 | + return Promise.resolve( |
| 136 | + Response.json({ |
| 137 | + environmentId: ENVIRONMENT_ID, |
| 138 | + label: "Remote", |
| 139 | + platform: { os: "linux", arch: "x64" }, |
| 140 | + serverVersion: "0.0.0-test", |
| 141 | + capabilities: { repositoryIdentity: true }, |
| 142 | + }), |
| 143 | + ); |
| 144 | + } |
| 145 | + return Promise.reject(new Error(`Unexpected fetch call to ${url}`)); |
| 146 | + }) satisfies typeof fetch; |
| 147 | + return ConnectionPromotion.layer.pipe( |
| 148 | + Layer.provide( |
| 149 | + Layer.mergeAll( |
| 150 | + remoteHttpClientLayer(fetchFn), |
| 151 | + Layer.succeed( |
| 152 | + ManagedRelay.ManagedRelayDpopSigner, |
| 153 | + ManagedRelay.ManagedRelayDpopSigner.of({ |
| 154 | + thumbprint: Effect.succeed("thumbprint-1"), |
| 155 | + createProof: (proofInput) => Effect.succeed(`proof:${proofInput.url}`), |
| 156 | + }), |
| 157 | + ), |
| 158 | + ), |
| 159 | + ), |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +describe("ConnectionPromotion cooldowns", () => { |
| 164 | + it.effect("keeps every failed endpoint cooling down, not just the latest one", () => |
| 165 | + Effect.gen(function* () { |
| 166 | + const promotion = yield* ConnectionPromotion.ConnectionPromotion; |
| 167 | + |
| 168 | + // LAN wins first (ranked above the tailnet), then fails. |
| 169 | + const first = yield* promotion.discover(RELAY_PREPARED); |
| 170 | + expect(Option.map(first, (route) => route.endpointId)).toEqual(Option.some(LAN_ENDPOINT.id)); |
| 171 | + yield* promotion.reportOverrideFailed(ENVIRONMENT_ID); |
| 172 | + |
| 173 | + // The tailnet route is promoted next and fails within the cooldown. |
| 174 | + const second = yield* promotion.discover(RELAY_PREPARED); |
| 175 | + expect(Option.map(second, (route) => route.endpointId)).toEqual( |
| 176 | + Option.some(TAILNET_ENDPOINT.id), |
| 177 | + ); |
| 178 | + yield* TestClock.adjust("1 minute"); |
| 179 | + yield* promotion.reportOverrideFailed(ENVIRONMENT_ID); |
| 180 | + |
| 181 | + // Both cooldowns are still active: the LAN route must not be re-selected |
| 182 | + // just because the tailnet failure came later. |
| 183 | + expect(yield* promotion.discover(RELAY_PREPARED)).toEqual(Option.none()); |
| 184 | + |
| 185 | + // The LAN cooldown started first, so it expires first. |
| 186 | + yield* TestClock.adjust("4 minutes"); |
| 187 | + const third = yield* promotion.discover(RELAY_PREPARED); |
| 188 | + expect(Option.map(third, (route) => route.endpointId)).toEqual(Option.some(LAN_ENDPOINT.id)); |
| 189 | + }).pipe( |
| 190 | + Effect.provide(promotionHarnessLayer([LAN_ENDPOINT, TAILNET_ENDPOINT])), |
| 191 | + Effect.provide(TestClock.layer()), |
| 192 | + ), |
| 193 | + ); |
| 194 | +}); |
0 commit comments