Open History to see the last commit
1// @effect-diagnostics preferSchemaOverJson:off - the external process fixture emits raw JSON over SSH stdout.
2import { expect, it } from "@effect/vitest";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import * as Net from "@t3tools/shared/Net";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as PlatformError from "effect/PlatformError";
9import * as Sink from "effect/Sink";
10import * as Stream from "effect/Stream";
11import * as HttpClient from "effect/unstable/http/HttpClient";
12import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
13import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
14import * as ServerConfig from "../config.ts";
15import * as DeviceHost from "./DeviceHost.ts";
16import * as SshDeviceHost from "./SshDeviceHost.ts";
17
18it.effect("preserves installed status after probes and cleans failed agent activation", () =>
19 Effect.gen(function* () {
20 const fs = yield* FileSystem.FileSystem;
21 const home = yield* fs.makeTempDirectoryScoped();
22 const modes: string[] = [];
23 let forwards = 0;
24 let failForward = true;
25 let rejectConfig = true;
26 const spawner = ChildProcessSpawner.make((command) =>
27 Effect.gen(function* () {
28 if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
29 const forwarding = command.args.includes("-N");
30 let output = "";
31 if (forwarding) {
32 if (failForward) {
33 failForward = false;
34 return yield* PlatformError.systemError({
35 _tag: "AlreadyExists",
36 module: "ChildProcess",
37 method: "spawn",
38 description: "Port already bound",
39 });
40 }
41 forwards++;
42 yield* Effect.addFinalizer(() =>
43 Effect.sync(() => {
44 forwards--;
45 }),
46 );
47 } else {
48 const stdin = command.options.stdin;
49 if (
50 !stdin ||
51 typeof stdin !== "object" ||
52 !("stream" in stdin) ||
53 !Stream.isStream(stdin.stream)
54 )
55 return yield* Effect.die("Missing script");
56 const script = yield* stdin.stream.pipe(
57 Stream.decodeText(),
58 Stream.runFold(
59 () => "",
60 (a, b) => a + b,
61 ),
62 );
63 const mode = /const mode = "([^"]+)"/.exec(script)?.[1] ?? "";
64 modes.push(mode);
65 output = JSON.stringify({
66 nodePath: "/node",
67 platforms: [{ platform: "ios", available: true }],
68 hubPort: 1234,
69 helpers: { serveSimAxSettings: null, serveSimCli: null },
70 ...(mode === "agent-start"
71 ? { daemonPort: 1235, token: "fixture", entryPath: "/agent.mjs" }
72 : {}),
73 });
74 }
75 return ChildProcessSpawner.makeHandle({
76 pid: ChildProcessSpawner.ProcessId(123),
77 stdout: Stream.make(new TextEncoder().encode(output)),
78 stderr: Stream.empty,
79 all: Stream.empty,
80 exitCode: forwarding ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(0)),
81 isRunning: Effect.succeed(forwarding),
82 kill: () => Effect.void,
83 stdin: Sink.drain,
84 getInputFd: () => Sink.drain,
85 getOutputFd: () => Stream.empty,
86 unref: Effect.succeed(Effect.void),
87 });
88 }),
89 );
90 const host = yield* SshDeviceHost.make(
91 { id: "test", label: "Test", target: "test.example" },
92 () =>
93 rejectConfig
94 ? Effect.fail(
95 new DeviceHost.DeviceHostError({
96 hostId: "test",
97 step: "configuring agent access",
98 cause: new Error("fixture failure"),
99 }),
100 )
101 : Effect.void,
102 ).pipe(
103 Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
104 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
105 Effect.provideService(
106 HttpClient.HttpClient,
107 HttpClient.make((request) =>
108 Effect.succeed(HttpClientResponse.fromWeb(request, new Response("ok"))),
109 ),
110 ),
111 );
112 yield* host.ensureReady(() => Effect.void);
113 expect(forwards).toBe(1);
114 expect(modes.filter((mode) => mode === "start")).toHaveLength(2);
115 yield* host.platformAvailability("ios");
116 expect((yield* host.summary).hubInstalled).toBe(true);
117 const failed = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.result);
118 expect(failed._tag).toBe("Failure");
119 expect(forwards).toBe(0);
120 expect(modes.at(-1)).toBe("stop-agent");
121 expect(yield* host.current).toBeNull();
122 rejectConfig = false;
123 yield* host.ensureAgentReady(() => Effect.void);
124 yield* host.platformAvailability("ios");
125 expect((yield* host.summary).agentDeviceInstalled).toBe(true);
126 yield* host.stopAgent;
127 expect(forwards).toBe(1);
128 yield* host.stop;
129 expect(forwards).toBe(0);
130 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
131);