@@ -2,7 +2,9 @@ import { expect, it } from "@effect/vitest"; |
2 | 2 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
3 | 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
4 | 4 | import * as Net from "@t3tools/shared/Net"; |
| 5 | +import * as Deferred from "effect/Deferred"; |
5 | 6 | import * as Effect from "effect/Effect"; |
| 7 | +import * as Fiber from "effect/Fiber"; |
6 | 8 | import * as FileSystem from "effect/FileSystem"; |
7 | 9 | import * as Layer from "effect/Layer"; |
8 | 10 | import * as PlatformError from "effect/PlatformError"; |
@@ -196,6 +198,97 @@ it.effect("preserves installed status after probes and cleans failed agent activ |
196 | 198 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
197 | 199 | ); |
198 | 200 |
|
| 201 | +it.effect("releases the forward of a bootstrap interrupted before its hub answered", () => |
| 202 | + Effect.gen(function* () { |
| 203 | + const fs = yield* FileSystem.FileSystem; |
| 204 | + const home = yield* fs.makeTempDirectoryScoped(); |
| 205 | + const probing = yield* Deferred.make<void>(); |
| 206 | + let holdReadiness = true; |
| 207 | + let forwards = 0; |
| 208 | + const spawner = ChildProcessSpawner.make((command) => |
| 209 | + Effect.gen(function* () { |
| 210 | + const forwarding = command._tag === "StandardCommand" && command.args.includes("-L"); |
| 211 | + if (forwarding) { |
| 212 | + forwards++; |
| 213 | + yield* Effect.addFinalizer(() => |
| 214 | + Effect.sync(() => { |
| 215 | + forwards--; |
| 216 | + }), |
| 217 | + ); |
| 218 | + } |
| 219 | + const output = forwarding |
| 220 | + ? "" |
| 221 | + : JSON.stringify({ |
| 222 | + nodePath: "/node", |
| 223 | + platforms: [{ platform: "ios", available: true }], |
| 224 | + hubPort: 1234, |
| 225 | + helpers: { serveSimAxSettings: null, serveSimCli: null }, |
| 226 | + }); |
| 227 | + return ChildProcessSpawner.makeHandle({ |
| 228 | + pid: ChildProcessSpawner.ProcessId(123), |
| 229 | + stdout: Stream.make(new TextEncoder().encode(output)), |
| 230 | + stderr: Stream.empty, |
| 231 | + all: Stream.empty, |
| 232 | + exitCode: forwarding ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(0)), |
| 233 | + isRunning: Effect.succeed(forwarding), |
| 234 | + kill: () => Effect.void, |
| 235 | + stdin: Sink.drain, |
| 236 | + getInputFd: () => Sink.drain, |
| 237 | + getOutputFd: () => Stream.empty, |
| 238 | + unref: Effect.succeed(Effect.void), |
| 239 | + }); |
| 240 | + }), |
| 241 | + ); |
| 242 | + const publishing = yield* Deferred.make<void>(); |
| 243 | + let holdReadyStatus = false; |
| 244 | + const host = yield* SshDeviceHost.make( |
| 245 | + { |
| 246 | + kind: "ssh", |
| 247 | + id: "test", |
| 248 | + label: "Test", |
| 249 | + target: "test.example", |
| 250 | + }, |
| 251 | + undefined, |
| 252 | + (status) => |
| 253 | + status === "ready" && holdReadyStatus |
| 254 | + ? Deferred.succeed(publishing, undefined).pipe(Effect.andThen(Effect.never)) |
| 255 | + : Effect.void, |
| 256 | + ).pipe( |
| 257 | + Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)), |
| 258 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 259 | + Effect.provideService( |
| 260 | + HttpClient.HttpClient, |
| 261 | + HttpClient.make((request) => |
| 262 | + Effect.gen(function* () { |
| 263 | + if (holdReadiness) { |
| 264 | + yield* Deferred.succeed(probing, undefined); |
| 265 | + return yield* Effect.never; |
| 266 | + } |
| 267 | + return HttpClientResponse.fromWeb(request, new Response("ok")); |
| 268 | + }), |
| 269 | + ), |
| 270 | + ), |
| 271 | + ); |
| 272 | + const first = yield* host.ensureReady(() => Effect.void).pipe(Effect.forkChild); |
| 273 | + yield* Deferred.await(probing); |
| 274 | + yield* Fiber.interrupt(first); |
| 275 | + expect(forwards).toBe(0); |
| 276 | + holdReadiness = false; |
| 277 | + yield* host.ensureReady(() => Effect.void); |
| 278 | + yield* host.stop; |
| 279 | + expect(forwards).toBe(0); |
| 280 | + holdReadyStatus = true; |
| 281 | + const second = yield* host.ensureReady(() => Effect.void).pipe(Effect.forkChild); |
| 282 | + yield* Deferred.await(publishing); |
| 283 | + yield* Fiber.interrupt(second); |
| 284 | + holdReadyStatus = false; |
| 285 | + yield* host.ensureReady(() => Effect.void); |
| 286 | + expect(forwards).toBe(1); |
| 287 | + yield* host.stop; |
| 288 | + expect(forwards).toBe(0); |
| 289 | + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 290 | +); |
| 291 | + |
199 | 292 | it.effect("lists Android devices without probing the host again once it reports Android", () => |
200 | 293 | Effect.gen(function* () { |
201 | 294 | const fs = yield* FileSystem.FileSystem; |
|