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

DeviceMultiHost.test.ts

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