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

device.test.ts

101 lines3.3 KB

1import { describe, expect, it } from "@effect/vitest";
2import { Schema } from "effect";
3import {
4 DeviceServiceState,
5 EasDeviceHostConfig,
6 EasDeviceHostConfigs,
7 SshDeviceHostConfigs,
8 deviceToolInstallMessage,
9} from "./device.ts";
10import { secretSettingsFields } from "./settings.ts";
11​
12describe("device tool install progress", () => {
13 it("distinguishes a new install from an upgrade and chooses versions numerically", () => {
14 expect(
15 deviceToolInstallMessage("device hub", {
16 requiredVersion: "0.11.0",
17 installedVersions: [],
18 runningVersion: null,
19 }),
20 ).toBe("Installing device hub 0.11.0…");
21 expect(
22 deviceToolInstallMessage("device hub", {
23 requiredVersion: "0.11.0",
24 installedVersions: ["0.9.0", "0.10.0"],
25 runningVersion: null,
26 }),
27 ).toBe("Updating device hub from 0.10.0 to 0.11.0…");
28 });
29});
30​
31const decodeSshHosts = Schema.decodeUnknownSync(SshDeviceHostConfigs);
32const decodeEas = Schema.decodeUnknownSync(EasDeviceHostConfigs);
33const encodeState = Schema.encodeSync(DeviceServiceState);
34const decodeState = Schema.decodeUnknownSync(DeviceServiceState);
35// The host summary as clients decoded it before EAS hosts existed.
36const decodeOlderState = Schema.decodeUnknownSync(
37 Schema.Struct({
38 hosts: Schema.Array(
39 Schema.Struct({ id: Schema.String, kind: Schema.Literals(["local", "ssh"]) }),
40 ),
41 }),
42);
43​
44describe("configured device hosts", () => {
45 it("reads SSH hosts saved before hosts had a kind, and bounds EAS sessions", () => {
46 expect(decodeSshHosts([{ id: "mac", label: "Mac mini", target: "julius@mac" }])).toEqual([
47 { kind: "ssh", id: "mac", label: "Mac mini", target: "julius@mac" },
48 ]);
49 expect(decodeEas([{ kind: "eas", id: "eas", label: "EAS Simulator" }])).toEqual([
50 {
51 kind: "eas",
52 id: "eas",
53 label: "EAS Simulator",
54 accessToken: "",
55 maxSessionMinutes: 30,
56 idleMinutes: 10,
57 maxConcurrentSessions: 2,
58 },
59 ]);
60 expect(() =>
61 decodeEas([{ kind: "eas", id: "eas", label: "EAS", maxSessionMinutes: 600 }]),
62 ).toThrow();
63 // The default idle limit of 10 is not smaller than a session limit of 10.
64 expect(() =>
65 decodeEas([{ kind: "eas", id: "eas", label: "EAS", maxSessionMinutes: 10 }]),
66 ).toThrow();
67 });
68​
69 it("marks the EAS access token as a secret setting", () => {
70 expect(secretSettingsFields(EasDeviceHostConfig)).toEqual(["accessToken"]);
71 });
72});
73​
74describe("device state for clients from before EAS hosts", () => {
75 const summary = {
76 label: "Host",
77 platforms: [],
78 hubInstalled: true,
79 agentDeviceInstalled: true,
80 };
81 const state = {
82 hosts: [
83 { ...summary, id: "local", kind: "local" as const },
84 { ...summary, id: "eas", kind: "eas" as const },
85 ],
86 hostStatus: "ready" as const,
87 hostStatuses: {},
88 devices: [],
89 sessions: [],
90 onboardingCompleted: true,
91 agentAccessEnabled: true,
92 hubBasePath: "/api/device-hub",
93 revision: 1,
94 };
95​
96 it("sends EAS hosts where an older client does not look, and reads them back", () => {
97 const wire = encodeState(state);
98 expect(decodeOlderState(wire).hosts.map((host) => host.id)).toEqual(["local"]);
99 expect(decodeState(wire).hosts).toEqual(state.hosts);
100 });
101});