Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SshDeviceHost.test.ts

139 lines5.5 KB

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 const owners: string[] = [];
24 let forwards = 0;
25 let failForward = true;
26 let rejectConfig = true;
27 const spawner = ChildProcessSpawner.make((command) =>
28 Effect.gen(function* () {
29 if (command._tag !== "StandardCommand") return yield* Effect.die("Unexpected command");
30 const forwarding = command.args.includes("-N");
31 let output = "";
32 if (forwarding) {
33 if (failForward) {
34 failForward = false;
35 return yield* PlatformError.systemError({
36 _tag: "AlreadyExists",
37 module: "ChildProcess",
38 method: "spawn",
39 description: "Port already bound",
40 });
41 }
42 forwards++;
43 yield* Effect.addFinalizer(() =>
44 Effect.sync(() => {
45 forwards--;
46 }),
47 );
48 } else {
49 const stdin = command.options.stdin;
50 if (
51 !stdin ||
52 typeof stdin !== "object" ||
53 !("stream" in stdin) ||
54 !Stream.isStream(stdin.stream)
55 )
56 return yield* Effect.die("Missing script");
57 const script = yield* stdin.stream.pipe(
58 Stream.decodeText(),
59 Stream.runFold(
60 () => "",
61 (a, b) => a + b,
62 ),
63 );
64 const mode = /const mode = "([^"]+)"/.exec(script)?.[1] ?? "";
65 modes.push(mode);
66 owners.push(/const owner = "([^"]+)"/.exec(script)?.[1] ?? "");
67 output = JSON.stringify({
68 nodePath: "/node",
69 platforms: [{ platform: "ios", available: true }],
70 hubPort: 1234,
71 helpers: { serveSimAxSettings: null, serveSimCli: null },
72 ...(mode === "agent-start"
73 ? { daemonPort: 1235, token: "fixture", entryPath: "/agent.mjs" }
74 : {}),
75 });
76 }
77 return ChildProcessSpawner.makeHandle({
78 pid: ChildProcessSpawner.ProcessId(123),
79 stdout: Stream.make(new TextEncoder().encode(output)),
80 stderr: Stream.empty,
81 all: Stream.empty,
82 exitCode: forwarding ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(0)),
83 isRunning: Effect.succeed(forwarding),
84 kill: () => Effect.void,
85 stdin: Sink.drain,
86 getInputFd: () => Sink.drain,
87 getOutputFd: () => Stream.empty,
88 unref: Effect.succeed(Effect.void),
89 });
90 }),
91 );
92 const host = yield* SshDeviceHost.make(
93 { id: "test", label: "Test", target: "test.example" },
94 () =>
95 rejectConfig
96 ? Effect.fail(
97 new DeviceHost.DeviceHostError({
98 hostId: "test",
99 step: "configuring agent access",
100 cause: new Error("fixture failure"),
101 }),
102 )
103 : Effect.void,
104 ).pipe(
105 Effect.provide(Layer.mergeAll(ServerConfig.layerTest(home, home), Net.layer)),
106 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
107 Effect.provideService(
108 HttpClient.HttpClient,
109 HttpClient.make((request) =>
110 Effect.succeed(HttpClientResponse.fromWeb(request, new Response("ok"))),
111 ),
112 ),
113 );
114 yield* host.ensureReady(() => Effect.void);
115 yield* SshDeviceHost.probe({ id: "test", label: "Test", target: "test.example" }).pipe(
116 Effect.provide(ServerConfig.layerTest(home, home)),
117 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
118 );
119 expect(new Set(owners).size).toBe(1);
120 expect(owners[0]).toMatch(/^[a-f0-9]{24}$/);
121 expect(forwards).toBe(1);
122 expect(modes.filter((mode) => mode === "start")).toHaveLength(2);
123 yield* host.platformAvailability("ios");
124 expect((yield* host.summary).hubInstalled).toBe(true);
125 const failed = yield* host.ensureAgentReady(() => Effect.void).pipe(Effect.result);
126 expect(failed._tag).toBe("Failure");
127 expect(forwards).toBe(0);
128 expect(modes.at(-1)).toBe("stop-agent");
129 expect(yield* host.current).toBeNull();
130 rejectConfig = false;
131 yield* host.ensureAgentReady(() => Effect.void);
132 yield* host.platformAvailability("ios");
133 expect((yield* host.summary).agentDeviceInstalled).toBe(true);
134 yield* host.stopAgent;
135 expect(forwards).toBe(1);
136 yield* host.stop;
137 expect(forwards).toBe(0);
138 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
139);