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

DeviceSourcesSettings.browser.test.tsx

103 lines3.4 KB

1import { EnvironmentId, type DeviceServiceState } from "@t3tools/contracts";
2import { AsyncResult, Atom } from "effect/reactivity";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5​
6import "~/index.css";
7import { render } from "../../../test/render";
8​
9const testState = vi.hoisted(() => ({
10 command: vi.fn(),
11 states: new Map<string, unknown>(),
12 scope: null as unknown,
13}));
14​
15vi.mock(import("~/state/device"), () => ({
16 deviceEnvironment: {
17 state: ({ environmentId }: { environmentId: string }) =>
18 Atom.make(AsyncResult.success(testState.states.get(environmentId))),
19 } as never,
20}));
21vi.mock(import("~/state/use-atom-command"), async (original) => ({
22 ...(await original()),
23 useAtomCommand: (() => testState.command) as never,
24}));
25vi.mock(import("./SettingsScopeContext"), async (original) => ({
26 ...(await original()),
27 useSettingsScope: (() => testState.scope) as never,
28}));
29vi.mock(import("./useHostConnectionChecks"), () => ({
30 useHostConnectionChecks: () => ({ checks: {}, testConnection: () => undefined }),
31}));
32​
33import { DeviceSourcesSettings } from "./DeviceSourcesSettings";
34​
35type HostStatus = DeviceServiceState["hostStatuses"][string];
36​
37const deviceState = (statuses: Record<string, HostStatus>): DeviceServiceState => ({
38 hosts: Object.keys(statuses).map((id) => ({
39 kind: id === "local" ? "local" : "ssh",
40 id,
41 label: id,
42 platforms: [{ platform: "ios", available: true }],
43 hubInstalled: true,
44 agentDeviceInstalled: true,
45 })),
46 hostStatus: "ready",
47 hostStatuses: statuses,
48 devices: [],
49 sessions: [],
50 onboardingCompleted: true,
51 agentAccessEnabled: true,
52 hubBasePath: "/api/device-hub",
53 revision: 1,
54});
55​
56function setup(
57 environments: ReadonlyArray<{
58 id: string;
59 label: string;
60 state: DeviceServiceState;
61 }>,
62) {
63 testState.command.mockReset();
64 testState.states.clear();
65 testState.command.mockResolvedValue({ _tag: "Success", value: undefined });
66 const scoped = environments.map((environment) => {
67 testState.states.set(environment.id, environment.state);
68 return {
69 environmentId: EnvironmentId.make(environment.id),
70 label: environment.label,
71 displayUrl: "",
72 connection: { phase: "connected" },
73 serverConfig: {
74 settings: { enableDeviceSupport: true, deviceHosts: [] },
75 },
76 };
77 });
78 testState.scope = {
79 scope: { kind: "environments" },
80 environments: scoped,
81 connectedEnvironments: scoped,
82 environment: scoped[0],
83 };
85​
86const failed = (detail: string) => deviceState({ local: { status: "failed", detail } });
87​
88describe("DeviceSourcesSettings", () => {
89 it("reveals each failed environment's error through Details when several are listed", async () => {
90 setup([
91 { id: "studio", label: "Mac Studio", state: failed("hub exited with code 1") },
92 { id: "mini", label: "Mac Mini", state: failed("xcrun: not found") },
93 ]);
94 await using _screen = await render(
95 <DeviceSourcesSettings supportsEas={false} supportsSimlock={false} />,
96 );
97​
98 const studio = page.getByRole("status").filter({ hasText: "Mac Studio" });
99 await expect.element(page.getByText("hub exited with code 1")).not.toBeInTheDocument();
100 await userEvent.click(studio.getByRole("button", { name: "Details" }));
101 await expect.element(page.getByText("hub exited with code 1")).toBeVisible();
102 });
103});