Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import { Schema } from "effect";
3import {
4 DeviceBootError,
5 DeviceServiceState,
6 EasDeviceHostConfig,
7 EasDeviceHostConfigs,
8 SshDeviceHostConfigs,
9} from "./device.ts";
10import { secretSettingsFields } from "./settings.ts";
11
12const decodeSshHosts = Schema.decodeUnknownSync(SshDeviceHostConfigs);
13const decodeEas = Schema.decodeUnknownSync(EasDeviceHostConfigs);
14const encodeState = Schema.encodeSync(DeviceServiceState);
15const decodeState = Schema.decodeUnknownSync(DeviceServiceState);
16// The host summary as clients decoded it before EAS hosts existed.
17const decodeOlderState = Schema.decodeUnknownSync(
18 Schema.Struct({
19 hosts: Schema.Array(
20 Schema.Struct({ id: Schema.String, kind: Schema.Literals(["local", "ssh"]) }),
21 ),
22 }),
23);
24
25describe("configured device hosts", () => {
26 it("reads SSH hosts saved before hosts had a kind, and bounds EAS sessions", () => {
27 expect(decodeSshHosts([{ id: "mac", label: "Mac mini", target: "julius@mac" }])).toEqual([
28 { kind: "ssh", id: "mac", label: "Mac mini", target: "julius@mac" },
29 ]);
30 expect(decodeEas([{ kind: "eas", id: "eas", label: "EAS Simulator" }])).toEqual([
31 {
32 kind: "eas",
33 id: "eas",
34 label: "EAS Simulator",
35 accessToken: "",
36 maxSessionMinutes: 30,
37 idleMinutes: 10,
38 maxConcurrentSessions: 2,
39 },
40 ]);
41 expect(() =>
42 decodeEas([{ kind: "eas", id: "eas", label: "EAS", maxSessionMinutes: 600 }]),
43 ).toThrow();
44 // The default idle limit of 10 is not smaller than a session limit of 10.
45 expect(() =>
46 decodeEas([{ kind: "eas", id: "eas", label: "EAS", maxSessionMinutes: 10 }]),
47 ).toThrow();
48 });
49
50 it("marks the EAS access token as a secret setting", () => {
51 expect(secretSettingsFields(EasDeviceHostConfig)).toEqual(["accessToken"]);
52 });
53});
54
55describe("device state for clients from before EAS hosts", () => {
56 const summary = {
57 label: "Host",
58 platforms: [],
59 hubInstalled: true,
60 agentDeviceInstalled: true,
61 };
62 const state = {
63 hosts: [
64 { ...summary, id: "local", kind: "local" as const },
65 { ...summary, id: "eas", kind: "eas" as const },
66 ],
67 hostStatus: "ready" as const,
68 hostStatuses: {},
69 devices: [],
70 sessions: [],
71 onboardingCompleted: true,
72 agentAccessEnabled: true,
73 hubBasePath: "/api/device-hub",
74 revision: 1,
75 };
76
77 it("sends EAS hosts where an older client does not look, and reads them back", () => {
78 const wire = encodeState(state);
79 expect(decodeOlderState(wire).hosts.map((host) => host.id)).toEqual(["local"]);
80 expect(decodeState(wire).hosts).toEqual(state.hosts);
81 });
82});
83
84describe("device boot errors", () => {
85 it("gives a next step instead of blaming the environment server when the host sent no details", () => {
86 const message = new DeviceBootError({
87 hostId: "mac",
88 deviceId: "Pixel_10",
89 reason: "launch_failed",
90 cause: null,
91 }).message;
92 expect(message).not.toContain("environment server");
93 expect(message).toContain("Try again or open another device");
94 });
95
96 it("keeps the host's diagnostics when it sent them", () => {
97 const message = new DeviceBootError({
98 hostId: "mac",
99 deviceId: "Pixel_10",
100 reason: "launch_failed",
101 diagnostics: "emulator: PANIC: Missing emulator engine program for 'x86_64' CPU.",
102 cause: null,
103 }).message;
104 expect(message).toContain("PANIC: Missing emulator engine program");
105 expect(message).not.toContain("Try again");
106 });
107});