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

EnvironmentsMap.browser.test.tsx

177 lines6.5 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5​
6import "~/index.css";
7import { act, render } from "../../../test/render";
8import { AppRouteModal } from "../AppRouteModal";
9import { EnvironmentsMap } from "./EnvironmentsMap";
10import { FleetDetails } from "./EnvironmentsMapDetails";
11import { buildFleetGraph, type FleetEnvironmentInput, fleetStacks } from "./environmentsMap.logic";
12​
13vi.mock("../chat/GlobalVoiceInputIndicator", () => ({ GlobalVoiceInputIndicator: () => null }));
14​
15const linkedAt = DateTime.makeUnsafe("2026-11-19T12:00:00.000Z");
16const hub: FleetEnvironmentInput = {
17 environmentId: EnvironmentId.make("hub"),
18 label: "hub",
19 machine: "server",
20 phase: "connected",
21 enabled: true,
22 routes: [],
23 activeRouteId: null,
24 refusedRouteIds: [],
25 offersLinks: true,
26 canWriteAccess: true,
27 hasLinkAddress: true,
28 outgoing: {
29 state: "loaded",
30 links: Array.from({ length: 10 }, (_, index) => ({
31 environmentId: EnvironmentId.make(`vm-${index + 1}`),
32 label: `vm-${index + 1}`,
33 urls: [],
34 access: "approval-required",
35 linkedAt,
36 expiresAt: DateTime.makeUnsafe("2027-01-01T00:00:00.000Z"),
37 lastReachedAt: null,
38 lastError: null,
39 status: "reachable",
40 })),
41 },
42};
43​
44const renderSettingsMap = () =>
45 render(
46 <AppRouteModal label="Settings">
47 <EnvironmentsMap
48 onAddEnvironment={() => {}}
49 renderSavedEnvironment={() => null}
50 renderConnectMachine={() => null}
51 />
52 </AppRouteModal>,
53 { router: "/settings/connections" },
54 );
55​
56describe("EnvironmentsMap", () => {
57 it("clears the selection on Esc before Settings closes", async () => {
58 await using _screen = await renderSettingsMap();
59 const settings = page.getByRole("dialog", { name: "Settings" });
60 const browser = page.getByRole("button", { name: "This browser", exact: true });
61​
62 await expect.element(settings).toHaveFocus();
63 await browser.click();
64 await expect.element(browser).toHaveAttribute("aria-pressed", "true");
65 await userEvent.keyboard("{Escape}");
66​
67 await expect.element(browser).toHaveAttribute("aria-pressed", "false");
68 // The close would render in the same commit that cleared the selection.
69 expect(settings.element()).toHaveAttribute("data-open");
70​
71 await userEvent.keyboard("{Escape}");
72 await expect.element(settings).not.toBeInTheDocument();
73 });
74​
75 it("clears the selection on Esc when a click does not focus the machine", async () => {
76 await using _screen = await renderSettingsMap();
77 // Safari leaves focus where it was when a button is clicked.
78 const keepFocus = (event: MouseEvent) => event.preventDefault();
79 document.addEventListener("mousedown", keepFocus, true);
80 using _keepFocus = {
81 [Symbol.dispose]: () => document.removeEventListener("mousedown", keepFocus, true),
82 };
83 const settings = page.getByRole("dialog", { name: "Settings" });
84 const browser = page.getByRole("button", { name: "This browser", exact: true });
85​
86 await expect.element(settings).toHaveFocus();
87 await browser.click();
88 await expect.element(browser).toHaveAttribute("aria-pressed", "true");
89 await userEvent.keyboard("{Escape}");
90​
91 await expect.element(browser).toHaveAttribute("aria-pressed", "false");
92 expect(settings.element()).toHaveAttribute("data-open");
93 });
94​
95 it("clears the selection on Esc from the details before Settings closes", async () => {
96 await using _screen = await renderSettingsMap();
97 const settings = page.getByRole("dialog", { name: "Settings" });
98 const browser = page.getByRole("button", { name: "This browser", exact: true });
99 const details = page.getByRole("group", { name: "Details" });
100​
101 await expect.element(settings).toHaveFocus();
102 await browser.click();
103 await expect.element(browser).toHaveAttribute("aria-pressed", "true");
104 // Choosing a machine or link inside the details leaves focus on the panel.
105 (details.element() as HTMLElement).focus();
106 await userEvent.keyboard("{Escape}");
107​
108 await expect.element(browser).toHaveAttribute("aria-pressed", "false");
109 expect(settings.element()).toHaveAttribute("data-open");
110​
111 await userEvent.keyboard("{Escape}");
112 await expect.element(settings).not.toBeInTheDocument();
113 });
114​
115 it("clears a stack's filter on Esc before Settings closes", async () => {
116 const graph = buildFleetGraph({
117 clientLabel: "This browser",
118 environments: [hub],
119 connectMachines: [],
120 now: linkedAt,
121 });
122 const [stack] = fleetStacks(graph, new Set());
123 const onSelect = vi.fn();
124 await using _screen = await render(
125 <AppRouteModal label="Settings">
126 <FleetDetails
127 graph={graph}
128 stacks={[stack!]}
129 environments={[]}
130 connectProblem={null}
131 selection={{ kind: "stack", id: stack!.id }}
132 found={null}
133 onSelect={onSelect}
134 onLinkFrom={() => {}}
135 onLinkBack={() => {}}
136 onToggleStack={() => {}}
137 renderSavedEnvironment={() => null}
138 renderConnectMachine={() => null}
139 onAddEnvironment={() => {}}
140 />
141 </AppRouteModal>,
142 { router: "/settings/connections" },
143 );
144 const settings = page.getByRole("dialog", { name: "Settings" });
145 const filter = page.getByRole("searchbox", { name: "Filter these machines by name" });
146​
147 await filter.fill("vm-1");
148 await expect.element(page.getByRole("status")).toHaveTextContent("2 of 10");
149 await userEvent.keyboard("{Escape}");
150​
151 await expect.element(filter).toHaveValue("");
152 expect(settings.element()).toHaveAttribute("data-open");
153 expect(onSelect).not.toHaveBeenCalled();
154​
155 await filter.fill("vm-1");
156 // An Escape that cancels an IME composition (Safari reports keyCode 229) belongs to the IME.
157 for (const composition of [
158 { isComposing: true, keyCode: 27 },
159 { isComposing: false, keyCode: 229 },
160 ]) {
161 await act(() => {
162 filter
163 .element()
164 .dispatchEvent(
165 new KeyboardEvent("keydown", { key: "Escape", ...composition, bubbles: true }),
166 );
167 });
168 await expect.element(filter).toHaveValue("vm-1");
169 expect(settings.element()).toHaveAttribute("data-open");
170 }
171​
172 await userEvent.keyboard("{Escape}");
173 await expect.element(filter).toHaveValue("");
174 await userEvent.keyboard("{Escape}");
175 await expect.element(settings).not.toBeInTheDocument();
176 });
177});