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

EnvironmentsMap.browser.test.tsx

276 lines9.8 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection";
3import * as DateTime from "effect/DateTime";
4import { Option } from "effect";
5import { describe, expect, it, vi } from "vite-plus/test";
6import { page, userEvent } from "vite-plus/test/browser";
7​
8import "~/index.css";
9import { act, render } from "../../../test/render";
10import { AppRouteModal } from "../AppRouteModal";
11import { EnvironmentsMap } from "./EnvironmentsMap";
12import { FleetDetails } from "./EnvironmentsMapDetails";
13import { buildFleetGraph, type FleetEnvironmentInput, fleetStacks } from "./environmentsMap.logic";
14​
15vi.mock("../chat/GlobalVoiceInputIndicator", () => ({ GlobalVoiceInputIndicator: () => null }));
16// One linked machine on the map, so a search can reach a node that is not this browser.
17vi.mock(import("../../state/environments"), async (original) => ({
18 ...(await original()),
19 useEnvironments: () => ({
20 isReady: true,
21 networkStatus: "online" as const,
22 environments: [
23 {
24 environmentId: EnvironmentId.make("desk"),
25 label: "Desk",
26 displayUrl: "http://localhost:12345",
27 relayManaged: false,
28 entry: {
29 target: new PrimaryConnectionTarget({
30 environmentId: EnvironmentId.make("desk"),
31 label: "Desk",
32 httpBaseUrl: "http://localhost:12345",
33 wsBaseUrl: "ws://localhost:12345/ws",
34 }),
35 profile: Option.none(),
36 enabled: true,
37 },
38 connection: { phase: "connected", error: null, traceId: null },
39 refusedRouteIds: [],
40 serverConfig: null,
41 },
42 ],
43 presentationById: new Map(),
44 }),
45}));
46​
47const linkedAt = DateTime.makeUnsafe("2026-11-19T12:00:00.000Z");
48const hub: FleetEnvironmentInput = {
49 environmentId: EnvironmentId.make("hub"),
50 label: "hub",
51 machine: "server",
52 phase: "connected",
53 enabled: true,
54 routes: [],
55 activeRouteId: null,
56 refusedRouteIds: [],
57 offersLinks: true,
58 canWriteAccess: true,
59 hasLinkAddress: true,
60 outgoing: {
61 state: "loaded",
62 links: Array.from({ length: 10 }, (_, index) => ({
63 environmentId: EnvironmentId.make(`vm-${index + 1}`),
64 label: `vm-${index + 1}`,
65 urls: [],
66 access: "approval-required",
67 linkedAt,
68 expiresAt: DateTime.makeUnsafe("2027-01-01T00:00:00.000Z"),
69 lastReachedAt: null,
70 lastError: null,
71 status: "reachable",
72 })),
73 },
74};
75​
76const renderSettingsMap = () =>
77 render(
78 <AppRouteModal label="Settings">
79 <EnvironmentsMap
80 onAddEnvironment={() => {}}
81 renderSavedEnvironment={() => null}
82 renderConnectMachine={() => null}
83 />
84 </AppRouteModal>,
85 { router: "/settings/connections" },
86 );
87​
88describe("EnvironmentsMap", () => {
89 it("clears the selection on Esc before Settings closes", async () => {
90 await using _screen = await renderSettingsMap();
91 const settings = page.getByRole("dialog", { name: "Settings" });
92 const browser = page.getByRole("button", { name: "This browser", exact: true });
93​
94 await expect.element(settings).toHaveFocus();
95 await browser.click();
96 await expect.element(browser).toHaveAttribute("aria-pressed", "true");
97 await userEvent.keyboard("{Escape}");
98​
99 await expect.element(browser).toHaveAttribute("aria-pressed", "false");
100 // The close would render in the same commit that cleared the selection.
101 expect(settings.element()).toHaveAttribute("data-open");
102​
103 await userEvent.keyboard("{Escape}");
104 await expect.element(settings).not.toBeInTheDocument();
105 });
106​
107 it("clears the selection on Esc when a click does not focus the machine", async () => {
108 await using _screen = await renderSettingsMap();
109 // Safari leaves focus where it was when a button is clicked.
110 const keepFocus = (event: MouseEvent) => event.preventDefault();
111 document.addEventListener("mousedown", keepFocus, true);
112 using _keepFocus = {
113 [Symbol.dispose]: () => document.removeEventListener("mousedown", keepFocus, true),
114 };
115 const settings = page.getByRole("dialog", { name: "Settings" });
116 const browser = page.getByRole("button", { name: "This browser", exact: true });
117​
118 await expect.element(settings).toHaveFocus();
119 await browser.click();
120 await expect.element(browser).toHaveAttribute("aria-pressed", "true");
121 await userEvent.keyboard("{Escape}");
122​
123 await expect.element(browser).toHaveAttribute("aria-pressed", "false");
124 expect(settings.element()).toHaveAttribute("data-open");
125 });
126​
127 it("clears the selection on Esc from the details before Settings closes", async () => {
128 await using _screen = await renderSettingsMap();
129 const settings = page.getByRole("dialog", { name: "Settings" });
130 const browser = page.getByRole("button", { name: "This browser", exact: true });
131 const details = page.getByRole("group", { name: "Details" });
132​
133 await expect.element(settings).toHaveFocus();
134 await browser.click();
135 await expect.element(browser).toHaveAttribute("aria-pressed", "true");
136 // Choosing a machine or link inside the details leaves focus on the panel.
137 (details.element() as HTMLElement).focus();
138 await userEvent.keyboard("{Escape}");
139​
140 await expect.element(browser).toHaveAttribute("aria-pressed", "false");
141 expect(settings.element()).toHaveAttribute("data-open");
142​
143 await userEvent.keyboard("{Escape}");
144 await expect.element(settings).not.toBeInTheDocument();
145 });
146​
147 it("clears a stack's filter on Esc before Settings closes", async () => {
148 const graph = buildFleetGraph({
149 clientLabel: "This browser",
150 environments: [hub],
151 connectMachines: [],
152 now: linkedAt,
153 });
154 const [stack] = fleetStacks(graph, new Set());
155 const onSelect = vi.fn();
156 await using _screen = await render(
157 <AppRouteModal label="Settings">
158 <FleetDetails
159 graph={graph}
160 stacks={[stack!]}
161 environments={[]}
162 connectProblem={null}
163 selection={{ kind: "stack", id: stack!.id }}
164 found={null}
165 onSelect={onSelect}
166 onLinkFrom={() => {}}
167 onLinkBack={() => {}}
168 onToggleStack={() => {}}
169 renderSavedEnvironment={() => null}
170 renderConnectMachine={() => null}
171 onAddEnvironment={() => {}}
172 />
173 </AppRouteModal>,
174 { router: "/settings/connections" },
175 );
176 const settings = page.getByRole("dialog", { name: "Settings" });
177 const filter = page.getByRole("searchbox", { name: "Filter these machines by name" });
178​
179 await filter.fill("vm-1");
180 await expect.element(page.getByRole("status")).toHaveTextContent("2 of 10");
181 await userEvent.keyboard("{Escape}");
182​
183 await expect.element(filter).toHaveValue("");
184 expect(settings.element()).toHaveAttribute("data-open");
185 expect(onSelect).not.toHaveBeenCalled();
186​
187 await filter.fill("vm-1");
188 // An Escape that cancels an IME composition (Safari reports keyCode 229) belongs to the IME.
189 for (const composition of [
190 { isComposing: true, keyCode: 27 },
191 { isComposing: false, keyCode: 229 },
192 ]) {
193 await act(() => {
194 filter
195 .element()
196 .dispatchEvent(
197 new KeyboardEvent("keydown", { key: "Escape", ...composition, bubbles: true }),
198 );
199 });
200 await expect.element(filter).toHaveValue("vm-1");
201 expect(settings.element()).toHaveAttribute("data-open");
202 }
203​
204 await userEvent.keyboard("{Escape}");
205 await expect.element(filter).toHaveValue("");
206 await userEvent.keyboard("{Escape}");
207 await expect.element(settings).not.toBeInTheDocument();
208 });
209​
210 it("keeps the map search on Escape and Enter that belong to an IME", async () => {
211 await using _screen = await renderSettingsMap();
212 const settings = page.getByRole("dialog", { name: "Settings" });
213 const desk = page.getByRole("button", { name: /^Desk/ });
214 const mapSearch = page.getByRole("searchbox", { name: "Find a machine on the map" });
215​
216 await expect.element(mapSearch).toHaveAttribute("type", "text");
217 await mapSearch.fill("Desk");
218​
219 // An Escape that cancels an IME composition belongs to the IME, not to the search.
220 for (const composition of [
221 { isComposing: true, keyCode: 27 },
222 { isComposing: false, keyCode: 229 },
223 ]) {
224 await act(() => {
225 mapSearch
226 .element()
227 .dispatchEvent(
228 new KeyboardEvent("keydown", { key: "Escape", ...composition, bubbles: true }),
229 );
230 });
231 await expect.element(mapSearch).toHaveValue("Desk");
232 expect(settings.element()).toHaveAttribute("data-open");
233 }
234​
235 // An Enter that confirms an IME composition belongs to the IME too.
236 for (const composition of [
237 { isComposing: true, keyCode: 13 },
238 { isComposing: false, keyCode: 229 },
239 ]) {
240 await act(() => {
241 mapSearch.element().dispatchEvent(
242 new KeyboardEvent("keydown", {
243 key: "Enter",
244 ...composition,
245 bubbles: true,
246 cancelable: true,
247 }),
248 );
249 });
250 await expect.element(desk).toHaveAttribute("aria-pressed", "false");
251 }
252​
253 // The same Enter outside a composition still jumps to the first match.
254 await act(() => {
255 mapSearch.element().dispatchEvent(
256 new KeyboardEvent("keydown", {
257 key: "Enter",
258 isComposing: false,
259 keyCode: 13,
260 bubbles: true,
261 cancelable: true,
262 }),
263 );
264 });
265 await expect.element(desk).toHaveAttribute("aria-pressed", "true");
266​
267 // Jumping to the match moved focus to the node; take it back for the Escape below.
268 await mapSearch.click();
269 await userEvent.keyboard("{Escape}");
270 await expect.element(mapSearch).toHaveValue("");
271 expect(settings.element()).toHaveAttribute("data-open");
272​
273 await userEvent.keyboard("{Escape}");
274 await expect.element(settings).not.toBeInTheDocument();
275 });
276});