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

DeviceMultiHost.test.ts

149 lines5.7 KB

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