1import { describe, expect, it } from "vite-plus/test";
2import { updateDeviceHosts } from "./deviceHostsSettings.logic";
3
4describe("device host changes across environments", () => {
5 const shared = { id: "shared", label: "Mac mini", target: "julius@macmini" };
6 const local = { id: "other", label: "Android", target: "julius@android" };
7
8 it("adds to differing host lists without losing environment-specific hosts, including on retry", () => {
9 const environments = [[], [local], [shared, local]];
10 const saved = environments.map((hosts) => updateDeviceHosts(hosts, shared, false));
11 expect(saved).toEqual([[shared], [local, shared], [shared, local]]);
12 expect(saved.map((hosts) => updateDeviceHosts(hosts, shared, false))).toEqual(saved);
13 });
14
15 it("edits and removes the shared host while preserving unrelated hosts", () => {
16 const edited = { ...shared, target: "julius@new-address" };
17 const saved = [[shared], [local, shared]].map((hosts) =>
18 updateDeviceHosts(hosts, edited, false),
19 );
20 expect(saved).toEqual([[edited], [local, edited]]);
21 expect(saved.map((hosts) => updateDeviceHosts(hosts, edited, true))).toEqual([[], [local]]);
22 });
23
24 it("recognizes a host added separately on another environment and preserves its local ID", () => {
25 const remote = { ...shared, id: "remote-id" };
26 const edited = { ...shared, target: "julius@new-address" };
27 expect(updateDeviceHosts([remote, local], shared, false)).toEqual([remote, local]);
28 const saved = updateDeviceHosts([remote, local], edited, false, shared);
29 expect(saved).toEqual([{ ...edited, id: remote.id }, local]);
30 expect(updateDeviceHosts(saved, edited, false, shared)).toEqual(saved);
31 expect(updateDeviceHosts([remote, local], shared, true)).toEqual([local]);
32 });
33
34 it("keeps distinct SSH connections to the same target separate", () => {
35 const anotherPort = { ...shared, id: "another-port", port: 2222 };
36 const anotherIdentity = { ...shared, id: "another-key", identityFile: "~/.ssh/another" };
37 expect(updateDeviceHosts([anotherPort, anotherIdentity], shared, false)).toEqual([
38 anotherPort,
39 anotherIdentity,
40 shared,
41 ]);
42 });
43
44 it("prefers the selected ID over a sibling with the same destination", () => {
45 const sibling = { ...shared, id: "sibling", label: "Another entry" };
46 const edited = { ...shared, label: "Renamed" };
47 expect(updateDeviceHosts([sibling, shared], edited, false, shared)).toEqual([sibling, edited]);
48 expect(updateDeviceHosts([sibling, shared], shared, true)).toEqual([sibling]);
49 });
50
51 it("refuses an ambiguous destination on another environment instead of changing a sibling", () => {
52 const remote = { ...shared, id: "remote" };
53 const sibling = { ...shared, id: "sibling" };
54 expect(() => updateDeviceHosts([remote, sibling], shared, true)).toThrow("Multiple hosts");
55 expect(() => updateDeviceHosts([remote, sibling], shared, false)).toThrow("Multiple hosts");
56 });
57});