Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import { LOCAL_DEVICE_HOST_ID } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import { HttpClient, HttpClientResponse } from "effect/http";
5
6import { makeDeviceHubOperations } from "./DeviceHubOperations.ts";
7
8const used = {
9 id: "used",
10 name: "Used iPhone",
11 version: "iOS 27.0",
12 platform: "ios",
13 booted: true,
14 physical: false,
15} as const;
16
17it.effect("adds never-used iOS simulators from the hub's grid", () =>
18 Effect.gen(function* () {
19 const operations = makeDeviceHubOperations({
20 hostId: LOCAL_DEVICE_HOST_ID,
21 origin: "http://device.test",
22 http: HttpClient.make((request) =>
23 Effect.succeed(
24 HttpClientResponse.fromWeb(
25 request,
26 Response.json(
27 request.url.endsWith("/api/devices")
28 ? // The hub's recent-device inventory omits simulators without usage timestamps.
29 { simulators: [used], emulators: [] }
30 : {
31 devices: [
32 { device: "USED", name: "Grid name", runtime: "iOS-27-0", state: "Booted" },
33 {
34 device: "fresh",
35 name: "Fresh iPhone",
36 runtime: "iOS-27-0",
37 state: "Shutdown",
38 },
39 {
40 device: "watch",
41 name: "Watch",
42 runtime: "watchOS-27-0",
43 state: "Shutdown",
44 },
45 ],
46 },
47 ),
48 ),
49 ),
50 ),
51 run: () => Effect.die("iOS discovery uses the hub"),
52 availablePlatforms: Effect.succeed({ ios: true, android: false }),
53 });
54 expect((yield* operations.list).devices).toEqual([
55 used,
56 {
57 id: "fresh",
58 name: "Fresh iPhone",
59 version: "iOS 27.0",
60 platform: "ios",
61 booted: false,
62 physical: false,
63 },
64 ]);
65 }),
66);
67
68it.effect.each(["unavailable", "http-error", "invalid-json"])(
69 "keeps hub inventory and diagnostics when iOS grid discovery is %s",
70 (scenario) =>
71 Effect.gen(function* () {
72 const operations = makeDeviceHubOperations({
73 hostId: LOCAL_DEVICE_HOST_ID,
74 origin: "http://device.test",
75 http: HttpClient.make((request) =>
76 Effect.sync(() => {
77 if (request.url.endsWith("/api/devices")) {
78 return HttpClientResponse.fromWeb(
79 request,
80 Response.json({
81 simulators: [used],
82 emulators: [],
83 errors: [{ message: "Existing discovery warning" }],
84 }),
85 );
86 }
87 if (scenario === "unavailable") throw new Error("iOS must not be probed here");
88 return HttpClientResponse.fromWeb(
89 request,
90 scenario === "http-error"
91 ? new Response("private diagnostics", { status: 500 })
92 : Response.json({ unexpected: "private diagnostics" }),
93 );
94 }),
95 ),
96 run: () => Effect.die("Discovery must not run a command"),
97 availablePlatforms: Effect.succeed({ ios: scenario !== "unavailable", android: false }),
98 });
99 const listed = yield* operations.list;
100 expect(listed.devices).toEqual([used]);
101 expect(listed.detail).toBe(
102 scenario === "unavailable"
103 ? "Existing discovery warning"
104 : "Existing discovery warning\nCould not list all iOS simulators. Recently used simulators remain available.",
105 );
106 }),
107);