Open History to see the last commit
1import {
2 DEFAULT_CLIENT_SETTINGS,
3 DEFAULT_UNIFIED_SETTINGS,
4 type DeviceServiceState,
5} from "@t3tools/contracts";
6import {
7 createMemoryHistory,
8 createRootRoute,
9 createRouter,
10 RouterProvider,
11} from "@tanstack/react-router";
12import { act, StrictMode, type ReactNode } from "react";
13import { create, type ReactTestRenderer } from "react-test-renderer";
14import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
15
16const { listBrowserImportSources } = vi.hoisted(() => ({
17 listBrowserImportSources: vi.fn().mockResolvedValue([]),
18}));
19
20vi.mock("../ui/tooltip", () => ({
21 Tooltip: ({ children }: { children: ReactNode }) => children,
22 TooltipTrigger: ({ render, children }: { render: ReactNode; children: ReactNode }) => (
23 <>
24 {render}
25 {children}
26 </>
27 ),
28 TooltipPopup: () => null,
29}));
30vi.mock("../preview/previewBridge", () => ({
31 previewBridge: { listBrowserImportSources },
32}));
33vi.mock("../../env", () => ({ isElectron: true }));
34vi.mock("../../state/environments", () => ({
35 useEnvironments: () => ({ environments: [], isReady: true }),
36 usePrimaryEnvironment: () => null,
37 usePrimaryEnvironmentId: () => null,
38}));
39vi.mock("../../hooks/useSettings", async (importOriginal) => ({
40 ...(await importOriginal<typeof import("../../hooks/useSettings")>()),
41 PRIMARY_SETTINGS_UNAVAILABLE_MESSAGE: "Connect to an environment",
42 useClientSettings: (selector?: (settings: typeof DEFAULT_CLIENT_SETTINGS) => unknown) =>
43 selector ? selector(DEFAULT_CLIENT_SETTINGS) : DEFAULT_CLIENT_SETTINGS,
44 useClientSettingsHydrated: () => true,
45 usePrimarySettingsAvailable: () => true,
46 usePrimarySettings: () => DEFAULT_UNIFIED_SETTINGS,
47 useUpdatePrimarySettings: () => vi.fn(),
48}));
49vi.mock("./settingsLayout", async (importOriginal) => ({
50 ...(await importOriginal<typeof import("./settingsLayout")>()),
51 SettingsPageContainer: ({ children }: { children: ReactNode }) => children,
52}));
53vi.mock("./ProjectDefaultsSettings", () => ({ ProjectDefaultsSettings: () => null }));
54vi.mock("./SettingsScopeContext", () => ({
55 useSettingsScope: () => ({
56 scope: { kind: "all", environmentIds: [] },
57 search: {},
58 environment: null,
59 environments: [],
60 target: null,
61 connectedEnvironments: [],
62 targets: [],
63 }),
64 useOptionalSettingsScope: () => null,
65}));
66
67import { IntegrationsSettingsPanel } from "./IntegrationsSettings";
68import { deviceHubReadiness, platformSetupStatus } from "../device/DeviceSetup";
69
70let renderer: ReactTestRenderer | undefined;
71
72beforeEach(() => {
73 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
74 listBrowserImportSources.mockClear();
75});
76
77afterEach(async () => {
78 await act(() => renderer?.unmount());
79 vi.unstubAllGlobals();
80});
81
82async function openSettings() {
83 const router = createRouter({
84 routeTree: createRootRoute({ component: IntegrationsSettingsPanel }),
85 history: createMemoryHistory(),
86 });
87 await router.load();
88 await act(() => {
89 renderer = create(
90 <StrictMode>
91 <RouterProvider router={router} />
92 </StrictMode>,
93 );
94 });
95 expect(renderer!.root.findByType(IntegrationsSettingsPanel)).toBeDefined();
96}
97
98describe("Integrations browser discovery", () => {
99 it("does not scan browser files when entering or revisiting settings", async () => {
100 await openSettings();
101 expect(listBrowserImportSources).not.toHaveBeenCalled();
102
103 await act(() => renderer?.unmount());
104 await openSettings();
105 expect(listBrowserImportSources).not.toHaveBeenCalled();
106 });
107
108 it("places device settings directly after browser settings", async () => {
109 await openSettings();
110 const sections = renderer!.root
111 .findAll((node) => node.type === "section")
112 .map((node) => node.props.id)
113 .filter(Boolean);
114 expect(sections.indexOf("devices")).toBeGreaterThan(sections.indexOf("browser"));
115 });
116});
117
118const deviceState = (overrides: Partial<DeviceServiceState> = {}): DeviceServiceState => ({
119 hosts: [
120 {
121 id: "local",
122 kind: "local",
123 label: "This machine",
124 hubInstalled: false,
125 agentDeviceInstalled: false,
126 platforms: [
127 { platform: "ios", available: true },
128 { platform: "android", available: true },
129 ],
130 },
131 ],
132 hostStatus: "ready",
133 hostStatuses: {},
134 devices: [],
135 sessions: [],
136 onboardingCompleted: false,
137 agentAccessEnabled: false,
138 hubBasePath: "/api/device-hub",
139 revision: 0,
140 ...overrides,
141});
142
143describe("device setup guidance", () => {
144 it("directs users to install an iOS runtime and create an Android virtual device", () => {
145 expect(platformSetupStatus(deviceState(), "ios").message).toContain("Xcode Settings");
146 expect(platformSetupStatus(deviceState(), "android").message).toContain("Device Manager");
147 });
148
149 it("preserves a specific missing-tool explanation from the server", () => {
150 const state = deviceState({
151 hosts: [
152 {
153 ...deviceState().hosts[0]!,
154 platforms: [
155 { platform: "ios", available: true },
156 { platform: "android", available: false, reason: "Android Emulator is missing." },
157 ],
158 },
159 ],
160 });
161 expect(platformSetupStatus(state, "android").message).toBe("Android Emulator is missing.");
162 });
163
164 it("counts a remote device host when this machine has no simulator tooling", () => {
165 const state = deviceState({
166 hostStatus: "idle",
167 hostStatuses: { macmini: { status: "ready" } },
168 hosts: [
169 {
170 ...deviceState().hosts[0]!,
171 platforms: [
172 { platform: "ios", available: false, reason: "Xcode was not found." },
173 { platform: "android", available: false, reason: "Android SDK was not found." },
174 ],
175 },
176 {
177 id: "macmini",
178 kind: "ssh",
179 label: "Mac Mini",
180 hubInstalled: true,
181 agentDeviceInstalled: true,
182 platforms: [
183 { platform: "ios", available: true },
184 { platform: "android", available: false, reason: "Android SDK was not found." },
185 ],
186 },
187 ],
188 devices: [
189 {
190 hostId: "macmini",
191 id: "iphone",
192 platform: "ios",
193 name: "iPhone 16 Pro",
194 version: "iOS 27.0",
195 booted: true,
196 physical: false,
197 },
198 ],
199 });
200 expect(deviceHubReadiness(state).ready).toBe(true);
201 expect(platformSetupStatus(state, "ios")).toEqual({
202 ready: true,
203 message: "Xcode and iOS Simulator are available on Mac Mini.",
204 });
205 expect(platformSetupStatus(state, "android").ready).toBe(false);
206 });
207});