The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 157 lines 6.09 KB
Raw
1 import { Atom, AsyncResult } from "effect/reactivity";
2 import * as Cause from "effect/Cause";
3 import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
4 import { useState } from "react";
5 import { page, userEvent } from "vite-plus/test/browser";
6 import { describe, expect, it, vi } from "vite-plus/test";
7
8 import { render, stubGlobal } from "../../../test/render";
9 import { AppAtomRegistryProvider } from "../../rpc/atomRegistry";
10
11 const mocks = vi.hoisted(() => ({
12 connectSshEnvironment: vi.fn<() => Promise<AtomCommandResult<void, Error>>>(async () =>
13 AsyncResult.success(undefined),
14 ),
15 refresh: () => {},
16 }));
17
18 vi.mock(import("../../state/use-atom-command"), async (original) => ({
19 ...(await original()),
20 useAtomCommand: ((command: unknown) => command) as never,
21 }));
22 vi.mock(import("~/connection/onboarding"), async (original) => ({
23 ...(await original()),
24 connectSshEnvironment: mocks.connectSshEnvironment as never,
25 }));
26 vi.mock(import("~/state/query"), async (original) => ({
27 ...(await original()),
28 useEnvironmentQuery: (() => ({
29 data: [],
30 isPending: false,
31 error: null,
32 refresh: mocks.refresh,
33 })) as never,
34 }));
35 vi.mock(import("~/state/environments"), async (original) => ({
36 ...(await original()),
37 useRelayEnvironmentDiscovery: (() => ({ environments: new Map() })) as never,
38 }));
39 vi.mock(import("~/state/server"), async (original) => ({
40 ...(await original()),
41 primaryServerKeybindingsAtom: Atom.make([]) as never,
42 }));
43
44 import { AddEnvironmentDialog } from "./AddEnvironmentDialog";
45
46 function Owner() {
47 const [open, setOpen] = useState(false);
48 const [error, setError] = useState<string | null>(null);
49 return (
50 <AppAtomRegistryProvider>
51 <AddEnvironmentDialog
52 open={open}
53 routeTarget={null}
54 error={error}
55 savedEnvironments={[]}
56 onOpenChange={setOpen}
57 onErrorChange={setError}
58 />
59 </AppAtomRegistryProvider>
60 );
61 }
62
63 function setup() {
64 mocks.connectSshEnvironment.mockClear();
65 return stubGlobal("desktopBridge", {});
66 }
67
68 const remoteCard = () => page.getByRole("button", { name: /Remote link/ });
69 const sshCard = () => page.getByRole("button", { name: /^SSH/ });
70
71 async function openDialogOnSsh() {
72 await userEvent.click(page.getByRole("button", { name: "Add environment" }));
73 await userEvent.click(sshCard());
74 await expect.element(sshCard()).toHaveAttribute("aria-pressed", "true");
75 }
76
77 async function expectDefaultModeAfterReopen() {
78 await expect.element(page.getByRole("dialog")).not.toBeInTheDocument();
79 await userEvent.click(page.getByRole("button", { name: "Add environment" }));
80 await expect.element(remoteCard()).toHaveAttribute("aria-pressed", "true");
81 await expect.element(sshCard()).toHaveAttribute("aria-pressed", "false");
82 await expect.element(page.getByPlaceholder("backend.example.com")).toBeInTheDocument();
83 }
84
85 describe("AddEnvironmentDialog", () => {
86 it("reopens on Remote link after adding an SSH environment", async () => {
87 using _bridge = setup();
88 await using _screen = await render(<Owner />);
89
90 await openDialogOnSsh();
91 await userEvent.fill(page.getByPlaceholder("Search hosts or type devbox"), "devbox");
92 await userEvent.click(
93 page.getByRole("dialog").getByRole("button", { name: "Add environment" }),
94 );
95 expect(mocks.connectSshEnvironment).toHaveBeenCalledOnce();
96
97 await expectDefaultModeAfterReopen();
98 });
99
100 it("drops an SSH error when switching to Remote link", async () => {
101 using _bridge = setup();
102 await using _screen = await render(<Owner />);
103
104 mocks.connectSshEnvironment.mockResolvedValueOnce(
105 AsyncResult.failure(Cause.fail(new Error("ssh: connection refused"))),
106 );
107 await openDialogOnSsh();
108 await userEvent.type(page.getByPlaceholder("Search hosts or type devbox"), "devbox");
109 await userEvent.click(
110 page.getByRole("dialog").getByRole("button", { name: "Add environment" }),
111 );
112 await expect.element(page.getByText(/connection refused/)).toBeInTheDocument();
113
114 await userEvent.click(remoteCard());
115 await expect.element(page.getByText(/connection refused/)).not.toBeInTheDocument();
116 });
117
118 it("clears the form after dismissing it on SSH", async () => {
119 using _bridge = setup();
120 await using _screen = await render(<Owner />);
121
122 await openDialogOnSsh();
123 await userEvent.click(remoteCard());
124 await userEvent.fill(page.getByPlaceholder("backend.example.com"), "devbox.example.com");
125 await userEvent.fill(page.getByPlaceholder("PAIRCODE"), "PAIRCODE");
126 await userEvent.click(sshCard());
127 await userEvent.fill(page.getByPlaceholder("Search hosts or type devbox"), "devbox");
128 await userEvent.fill(page.getByPlaceholder("root"), "maria");
129 await userEvent.fill(page.getByPlaceholder("22"), "2222");
130 await userEvent.keyboard("{Escape}");
131
132 await expectDefaultModeAfterReopen();
133 await expect.element(page.getByPlaceholder("backend.example.com")).toHaveValue("");
134 await expect.element(page.getByPlaceholder("PAIRCODE")).toHaveValue("");
135 await userEvent.click(sshCard());
136 await expect.element(page.getByPlaceholder("Search hosts or type devbox")).toHaveValue("");
137 await expect.element(page.getByPlaceholder("root")).toHaveValue("");
138 await expect.element(page.getByPlaceholder("22")).toHaveValue("");
139
140 let finishConnection!: (result: AtomCommandResult<void, Error>) => void;
141 const pending = new Promise<AtomCommandResult<void, Error>>((resolve) => {
142 finishConnection = resolve;
143 });
144 mocks.connectSshEnvironment.mockImplementationOnce(() => pending);
145 await userEvent.fill(page.getByPlaceholder("Search hosts or type devbox"), "devbox");
146 await userEvent.click(
147 page.getByRole("dialog").getByRole("button", { name: "Add environment" }),
148 );
149 await userEvent.keyboard("{Escape}");
150 await expectDefaultModeAfterReopen();
151 finishConnection(AsyncResult.failure(Cause.fail(new Error("dismissed SSH attempt"))));
152 await expect
153 .element(page.getByRole("dialog").getByRole("button", { name: "Add environment" }))
154 .toBeEnabled();
155 await expect.element(page.getByText(/dismissed SSH attempt/)).not.toBeInTheDocument();
156 });
157 });