Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import { ThreadId } from "@t3tools/contracts";
3import * as Deferred from "effect/Deferred";
4import * as Fiber from "effect/Fiber";
5import * as Effect from "effect/Effect";
6import { HttpClient, HttpClientResponse } from "effect/unstable/http";
7import { ServerSettingsService } from "../serverSettings.ts";
8import { DeviceHostError, DeviceHost } from "./DeviceHost.ts";
9import { makeWithHosts } from "./DeviceService.ts";
10
11it.effect("keeps hosts independent when serials collide and another host fails", () =>
12 Effect.gen(function* () {
13 const host = (id: string, failed = false): DeviceHost["Service"] => {
14 const ready = {
15 nodePath: process.execPath,
16 hub: { origin: `http://${id}` },
17 agentDevice: { baseUrl: `http://${id}`, token: "test", entryPath: "/agent-device" },
18 run: () => Effect.succeed({ stdout: "", stderr: "", code: 0 }),
19 helpers: { serveSimAxSettings: null, serveSimCli: null },
20 };
21 return {
22 id,
23 summary: Effect.succeed({
24 id,
25 label: id,
26 kind: id === "b" ? "ssh" : "local",
27 hubInstalled: true,
28 agentDeviceInstalled: true,
29 platforms: id === "b" ? [] : [{ platform: "android", available: true }],
30 }),
31 platformAvailability: (platform) => Effect.succeed({ platform, available: true }),
32 ensureReady: () =>
33 failed
34 ? Effect.fail(
35 new DeviceHostError({ hostId: id, step: "connect", cause: new Error("offline") }),
36 )
37 : Effect.succeed(ready),
38 ensureAgentReady: () => Effect.succeed(ready),
39 current: Effect.succeed(ready),
40 stopAgent: Effect.void,
41 stop: Effect.void,
42 };
43 };
44 const http = HttpClient.make((request) =>
45 Effect.succeed(
46 HttpClientResponse.fromWeb(
47 request,
48 Response.json({
49 simulators: [],
50 emulators: [
51 {
52 id: "emulator-5554",
53 name: "Pixel",
54 version: "36",
55 platform: "android",
56 booted: true,
57 physical: false,
58 },
59 ],
60 }),
61 ),
62 ),
63 );
64 const hosts = new Map(["a", "b", "offline"].map((id) => [id, host(id, id === "offline")]));
65 const writeStarted = yield* Deferred.make<void>();
66 const finishWrite = yield* Deferred.make<void>();
67 const order: string[] = [];
68 const service = yield* makeWithHosts(hosts, undefined, () =>
69 Effect.gen(function* () {
70 order.push("write started");
71 yield* Deferred.succeed(writeStarted, undefined);
72 yield* Deferred.await(finishWrite);
73 order.push("write finished");
74 return "/host-config.json";
75 }),
76 ).pipe(Effect.provideService(HttpClient.HttpClient, http));
77 expect(yield* service.agentReadinessIfSupported("b")).not.toBeNull();
78 const listed = yield* service.list;
79 expect(listed.devices.map((device) => device.hostId).sort()).toEqual(["a", "b"]);
80 expect(listed.hostStatuses.offline?.status).toBe("failed");
81 const threadId = ThreadId.make("thread");
82 for (const hostId of ["a", "b"])
83 yield* service.open({ threadId, hostId, deviceId: "emulator-5554", platform: "android" });
84 yield* service.close({ threadId, hostId: "a", deviceId: "emulator-5554" });
85 const state = yield* service.state;
86 expect(state.devices).toHaveLength(2);
87 expect(state.sessions.map((session) => session.hostId)).toEqual(["b"]);
88 expect(state.hostStatuses.a?.status).toBe("ready");
89 expect(state.hostStatuses.offline?.status).toBe("failed");
90 const targeting = yield* service
91 .agentTarget({ threadId, hostId: "b", deviceId: "emulator-5554" })
92 .pipe(Effect.forkChild);
93 yield* Deferred.await(writeStarted);
94 const replacing = yield* service
95 .withLifecycleLock(
96 Effect.gen(function* () {
97 order.push("replace");
98 hosts.set("b", host("b"));
99 yield* service.refreshHosts;
100 }),
101 )
102 .pipe(Effect.forkChild);
103 yield* Deferred.succeed(finishWrite, undefined);
104 yield* Fiber.join(targeting);
105 yield* Fiber.join(replacing);
106 expect(order).toEqual(["write started", "write finished", "replace"]);
107 const replaced = yield* service.state;
108 expect(replaced.sessions).toEqual([]);
109 expect(replaced.devices.map((device) => device.hostId)).toEqual(["a"]);
110 expect(replaced.hostStatuses.b).toBeUndefined();
111 yield* service.open({ threadId, hostId: "b", deviceId: "emulator-5554", platform: "android" });
112 hosts.delete("b");
113 yield* service.refreshHosts;
114 yield* service.setHostStatus("b", { status: "ready" });
115 expect((yield* service.state).hostStatuses.b).toBeUndefined();
116 expect((yield* service.state).sessions).toEqual([]);
117 yield* service.agentReadinessIfSupported("a");
118 expect((yield* service.state).hostStatuses.a?.status).toBe("ready");
119 yield* service.configure({ enabled: false });
120 expect((yield* service.state).hostStatuses).toEqual({});
121 }).pipe(
122 Effect.provide(
123 ServerSettingsService.layerTest({ enableDeviceSupport: true, enableAgentDeviceAccess: true }),
124 ),
125 ),
126);