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

simlockDeviceHostsSettings.logic.test.ts

82 lines3.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { SecretProviderId, type EnvironmentId } from "@t3tools/contracts";
4​
5import {
6 newSimlockDeviceHost,
7 planSimlockDeviceHostSave,
8 simlockDeviceHostProblem,
9 updateSimlockDeviceHosts,
10} from "./simlockDeviceHostsSettings.logic";
11​
12const host = {
13 ...newSimlockDeviceHost("simlock"),
14 gatewayUrl: "https://gateway.example.ts.net:4700",
15 agentToken: "agent",
16};
17​
18describe("Simlock device host settings", () => {
19 it("names what keeps a draft from saving", () => {
20 expect(simlockDeviceHostProblem(host)).toBeNull();
21 expect(simlockDeviceHostProblem({ ...host, label: " " })).toBe("Give the host a label.");
22 expect(simlockDeviceHostProblem({ ...host, gatewayUrl: "gateway:4700" })).toMatch(/origin/);
23 expect(
24 simlockDeviceHostProblem({ ...host, gatewayUrl: "https://gateway.example/api" }),
25 ).toMatch(/origin/);
26 expect(simlockDeviceHostProblem({ ...host, maxLeases: 21 })).toMatch(/ranges/);
27 expect(simlockDeviceHostProblem({ ...host, maxLeaseMinutes: 4 })).toMatch(/ranges/);
28 });
29​
30 it("adds, replaces, and removes one host", () => {
31 const other = { ...host, id: "other", label: "Other" };
32 const added = updateSimlockDeviceHosts([other], host, false);
33 expect(added.map((candidate) => candidate.id)).toEqual(["other", "simlock"]);
34 const renamed = updateSimlockDeviceHosts(added, { ...host, label: "Renamed" }, false);
35 expect(renamed.map((candidate) => candidate.label)).toEqual(["Other", "Renamed"]);
36 expect(updateSimlockDeviceHosts(renamed, host, true)).toEqual([other]);
37 });
38​
39 it("saves the shared fields everywhere and each environment's own token, if it has one", () => {
40 const mbp = "mbp" as EnvironmentId;
41 const cups = "cups" as EnvironmentId;
42 const environments = [
43 { environmentId: mbp, label: "mbp", hosts: [{ ...host, agentToken: "<redacted>" }] },
44 { environmentId: cups, label: "cups", hosts: [] },
45 ];
46 const draft = { ...host, label: "Pool", agentToken: "" };
47​
48 expect(
49 planSimlockDeviceHostSave(environments, draft, new Map([[mbp, { kind: "saved" }]])),
50 ).toEqual({
51 patches: [
52 { environmentId: mbp, hosts: [{ ...draft, agentToken: "<redacted>" }] },
53 { environmentId: cups, hosts: [{ ...draft, agentToken: "" }] },
54 ],
55 });
56 expect(
57 planSimlockDeviceHostSave(
58 environments,
59 draft,
60 new Map([
61 [mbp, { kind: "saved" }],
62 [cups, { kind: "new", value: " cups-token " }],
63 ]),
64 ),
65 ).toEqual({
66 patches: [
67 { environmentId: mbp, hosts: [{ ...draft, agentToken: "<redacted>" }] },
68 { environmentId: cups, hosts: [{ ...draft, agentToken: "cups-token" }] },
69 ],
70 });
71 const reference = { provider: SecretProviderId.make("homelab"), reference: "simlock#token" };
72 expect(
73 planSimlockDeviceHostSave(
74 environments,
75 draft,
76 new Map([[cups, { kind: "new", value: reference }]]),
77 ),
78 ).toMatchObject({
79 patches: [{}, { environmentId: cups, hosts: [{ ...draft, agentToken: reference }] }],
80 });
81 });
82});