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

desktopSshHosts.test.ts

144 lines4.0 KB

1import type { DesktopDiscoveredSshHost } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import { AtomRegistry } from "effect/reactivity";
4import * as AsyncResult from "effect/reactivity/AsyncResult";
5import { describe, expect, it, vi } from "vite-plus/test";
6​
7import { createDesktopSshHostsStateAtom, filterDiscoveredSshHosts } from "./desktopSshHosts";
8​
9const hosts: ReadonlyArray<DesktopDiscoveredSshHost> = [
10 {
11 alias: "devbox",
12 hostname: "devbox.local",
13 port: null,
14 source: "ssh-config",
15 username: null,
16 },
17];
18​
19describe("filterDiscoveredSshHosts", () => {
20 const suggestions: ReadonlyArray<DesktopDiscoveredSshHost> = [
21 {
22 alias: "grape",
23 hostname: "grape",
24 port: null,
25 source: "known-hosts",
26 username: null,
27 },
28 {
29 alias: "Pinot",
30 hostname: "Pinot",
31 port: null,
32 source: "ssh-config",
33 username: null,
34 },
35 {
36 alias: "preprod",
37 hostname: "preprod",
38 port: 2222,
39 source: "ssh-config",
40 username: "deploy",
41 },
42 {
43 alias: "prod-z",
44 hostname: "prod-z",
45 port: null,
46 source: "ssh-config",
47 username: null,
48 },
49 {
50 alias: "prod-a",
51 hostname: "prod-a",
52 port: null,
53 source: "ssh-config",
54 username: null,
55 },
56 {
57 alias: "prod-m",
58 hostname: "prod-m",
59 port: null,
60 source: "ssh-config",
61 username: null,
62 },
63 ];
64​
65 it.each(["", " "])("returns every host for an empty query %j", (query) => {
66 expect(filterDiscoveredSshHosts(suggestions, query)).toEqual(suggestions);
67 });
68​
69 it("trims the query", () => {
70 expect(filterDiscoveredSshHosts(suggestions, " pi ")).toEqual([suggestions[1]]);
71 });
72​
73 it("ranks alias prefix matches before substring matches", () => {
74 expect(filterDiscoveredSshHosts(suggestions, "prod")).toEqual([
75 suggestions[3],
76 suggestions[4],
77 suggestions[5],
78 suggestions[2],
79 ]);
80 });
81​
82 it("preserves the original order within a match tier", () => {
83 expect(filterDiscoveredSshHosts(suggestions, "prod").slice(0, 3)).toEqual([
84 suggestions[3],
85 suggestions[4],
86 suggestions[5],
87 ]);
88 });
89​
90 it("matches aliases and hostnames case-insensitively", () => {
91 expect(filterDiscoveredSshHosts(suggestions, "PINOT")).toEqual([suggestions[1]]);
92 expect(filterDiscoveredSshHosts(hosts, "box.LOCAL")).toEqual(hosts);
93 });
94​
95 it("returns an empty array when no hosts match", () => {
96 expect(filterDiscoveredSshHosts(suggestions, "merlot")).toEqual([]);
97 });
98});
99​
100describe("desktopSshHostsState", () => {
101 it("retains discovered hosts when the settings screen remounts", async () => {
102 const discoverSshHosts = vi.fn(async () => hosts);
103 const atom = createDesktopSshHostsStateAtom(() => ({ discoverSshHosts }));
104 const registry = AtomRegistry.make();
105​
106 const unmount = registry.mount(atom);
107 await vi.waitFor(() => {
108 expect(AsyncResult.value(registry.get(atom))).toEqual(
109 expect.objectContaining({ _tag: "Some", value: hosts }),
110 );
111 });
112 unmount();
113​
114 const remount = registry.mount(atom);
115 expect(AsyncResult.value(registry.get(atom))).toEqual(
116 expect.objectContaining({ _tag: "Some", value: hosts }),
117 );
118 expect(discoverSshHosts).toHaveBeenCalledTimes(1);
119​
120 remount();
121 registry.dispose();
122 });
123​
124 it("retains the desktop bridge failure as the discovery error cause", async () => {
125 const cause = new Error("ssh config unavailable");
126 const atom = createDesktopSshHostsStateAtom(() => ({
127 discoverSshHosts: async () => Promise.reject(cause),
128 }));
129 const registry = AtomRegistry.make();
130 registry.mount(atom);
131​
132 await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
133 const result = registry.get(atom);
134 if (!AsyncResult.isFailure(result)) throw new Error("Expected SSH host discovery to fail.");
135​
136 expect(Cause.squash(result.cause)).toEqual(
137 expect.objectContaining({
138 _tag: "DesktopSshDiscoveryError",
139 cause,
140 }),
141 );
142 registry.dispose();
143 });
144});