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

DeviceMultiHost.test.ts

133 lines5.2 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 Effect from "effect/Effect";
6import { HttpClient, HttpClientResponse } from "effect/unstable/http";
7import * as ServerSettings from "../serverSettings.ts";
8import * as DeviceHost from "./DeviceHost.ts";
9import * as DeviceService 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.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 DeviceHost.DeviceHostError({
36 hostId: id,
37 step: "connect",
38 cause: new Error("offline"),
39 }),
40 )
41 : Effect.succeed(ready),
42 ensureAgentReady: () => Effect.succeed(ready),
43 current: Effect.succeed(ready),
44 stopAgent: Effect.void,
45 stop: Effect.void,
46 };
47 };
48 const http = HttpClient.make((request) =>
49 Effect.succeed(
50 HttpClientResponse.fromWeb(
51 request,
52 Response.json({
53 simulators: [],
54 emulators: [
55 {
56 id: "emulator-5554",
57 name: "Pixel",
58 version: "36",
59 platform: "android",
60 booted: true,
61 physical: false,
62 },
63 ],
64 }),
65 ),
66 ),
67 );
68 const hosts = new Map(["a", "b", "offline"].map((id) => [id, host(id, id === "offline")]));
69 const writeStarted = yield* Deferred.make<void>();
70 const finishWrite = yield* Deferred.make<void>();
71 const order: string[] = [];
72 const service = yield* DeviceService.makeWithHosts(hosts, undefined, () =>
73 Effect.gen(function* () {
74 order.push("write started");
75 yield* Deferred.succeed(writeStarted, undefined);
76 yield* Deferred.await(finishWrite);
77 order.push("write finished");
78 return "/host-config.json";
79 }),
80 ).pipe(Effect.provideService(HttpClient.HttpClient, http));
81 expect(yield* service.agentReadinessIfSupported("b")).not.toBeNull();
82 const listed = yield* service.list;
83 expect(listed.devices.map((device) => device.hostId).sort()).toEqual(["a", "b"]);
84 expect(listed.hostStatuses.offline?.status).toBe("failed");
85 const threadId = ThreadId.make("thread");
86 for (const hostId of ["a", "b"])
87 yield* service.open({ threadId, hostId, deviceId: "emulator-5554", platform: "android" });
88 yield* service.close({ threadId, hostId: "a", deviceId: "emulator-5554" });
89 const state = yield* service.state;
90 expect(state.devices).toHaveLength(2);
91 expect(state.sessions.map((session) => session.hostId)).toEqual(["b"]);
92 expect(state.hostStatuses.a?.status).toBe("ready");
93 expect(state.hostStatuses.offline?.status).toBe("failed");
94 const targeting = yield* service
95 .agentTarget({ threadId, hostId: "b", deviceId: "emulator-5554" })
96 .pipe(Effect.forkChild);
97 yield* Deferred.await(writeStarted);
98 const replacing = yield* service
99 .withLifecycleLock(
100 Effect.gen(function* () {
101 order.push("replace");
102 hosts.set("b", host("b"));
103 yield* service.refreshHosts;
104 }),
105 )
106 .pipe(Effect.forkChild);
107 yield* Deferred.succeed(finishWrite, undefined);
108 yield* Fiber.join(targeting);
109 yield* Fiber.join(replacing);
110 expect(order).toEqual(["write started", "write finished", "replace"]);
111 const replaced = yield* service.state;
112 expect(replaced.sessions).toEqual([]);
113 expect(replaced.devices.map((device) => device.hostId)).toEqual(["a"]);
114 expect(replaced.hostStatuses.b).toBeUndefined();
115 yield* service.open({ threadId, hostId: "b", deviceId: "emulator-5554", platform: "android" });
116 hosts.delete("b");
117 yield* service.refreshHosts;
118 yield* service.setHostStatus("b", { status: "ready" });
119 expect((yield* service.state).hostStatuses.b).toBeUndefined();
120 expect((yield* service.state).sessions).toEqual([]);
121 yield* service.agentReadinessIfSupported("a");
122 expect((yield* service.state).hostStatuses.a?.status).toBe("ready");
123 yield* service.configure({ enabled: false });
124 expect((yield* service.state).hostStatuses).toEqual({});
125 }).pipe(
126 Effect.provide(
127 ServerSettings.layerTest({
128 enableDeviceSupport: true,
129 enableAgentDeviceAccess: true,
130 }),
131 ),
132 ),
133);