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

device.test.ts

81 lines2.6 KB

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