Open History to see the last commit
1import type { DesktopDiscoveredSshHost } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import { AtomRegistry } from "effect/unstable/reactivity";
4import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
5import { describe, expect, it, vi } from "vite-plus/test";
6
7import { createDesktopSshHostsStateAtom } 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("desktopSshHostsState", () => {
20 it("retains discovered hosts when the settings screen remounts", async () => {
21 const discoverSshHosts = vi.fn(async () => hosts);
22 const atom = createDesktopSshHostsStateAtom(() => ({ discoverSshHosts }));
23 const registry = AtomRegistry.make();
24
25 const unmount = registry.mount(atom);
26 await vi.waitFor(() => {
27 expect(AsyncResult.value(registry.get(atom))).toEqual(
28 expect.objectContaining({ _tag: "Some", value: hosts }),
29 );
30 });
31 unmount();
32
33 const remount = registry.mount(atom);
34 expect(AsyncResult.value(registry.get(atom))).toEqual(
35 expect.objectContaining({ _tag: "Some", value: hosts }),
36 );
37 expect(discoverSshHosts).toHaveBeenCalledTimes(1);
38
39 remount();
40 registry.dispose();
41 });
42
43 it("retains the desktop bridge failure as the discovery error cause", async () => {
44 const cause = new Error("ssh config unavailable");
45 const atom = createDesktopSshHostsStateAtom(() => ({
46 discoverSshHosts: async () => Promise.reject(cause),
47 }));
48 const registry = AtomRegistry.make();
49 registry.mount(atom);
50
51 await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
52 const result = registry.get(atom);
53 if (!AsyncResult.isFailure(result)) throw new Error("Expected SSH host discovery to fail.");
54
55 expect(Cause.squash(result.cause)).toEqual(
56 expect.objectContaining({
57 _tag: "DesktopSshDiscoveryError",
58 cause,
59 }),
60 );
61 registry.dispose();
62 });
63});