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("../preview/previewBridge", () => ({
21 previewBridge: { listBrowserImportSources },
22}));
23vi.mock("../../env", () => ({ isElectron: true }));
24vi.mock("../../state/environments", () => ({
25 useEnvironments: () => ({ environments: [], isReady: true }),
26 usePrimaryEnvironment: () => null,
27}));
28vi.mock("../../hooks/useSettings", async (importOriginal) => ({
29 ...(await importOriginal<typeof import("../../hooks/useSettings")>()),
30 PRIMARY_SETTINGS_UNAVAILABLE_MESSAGE: "Connect to an environment",
31 useClientSettings: (selector?: (settings: typeof DEFAULT_CLIENT_SETTINGS) => unknown) =>
32 selector ? selector(DEFAULT_CLIENT_SETTINGS) : DEFAULT_CLIENT_SETTINGS,
33 useClientSettingsHydrated: () => true,
34 usePrimarySettingsAvailable: () => true,
35 usePrimarySettings: () => DEFAULT_UNIFIED_SETTINGS,
36 useUpdatePrimarySettings: () => vi.fn(),
37}));
38vi.mock("./settingsLayout", async (importOriginal) => ({
39 ...(await importOriginal<typeof import("./settingsLayout")>()),
40 SettingsPageContainer: ({ children }: { children: ReactNode }) => children,
41}));
42// The scoped agent-access rows need the settings layout's scope provider;
43// this test covers the device-local browser sections only.
44vi.mock("./ProjectDefaultsSettings", () => ({ ProjectDefaultsSettings: () => null }));
45vi.mock("./SettingsScopeContext", () => ({
46 useSettingsScope: () => ({
47 scope: { kind: "all", environmentIds: [] },
48 search: {},
49 environment: null,
50 environments: [],
51 target: null,
52 connectedEnvironments: [],
53 targets: [],
54 }),
55 useOptionalSettingsScope: () => null,
56}));
57
58import { IntegrationsSettingsPanel } from "./IntegrationsSettings";
59import { platformSetupStatus } from "../device/DeviceSetup";
60
61let renderer: ReactTestRenderer | undefined;
62
63beforeEach(() => {
64 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
65 listBrowserImportSources.mockClear();
66});
67
68afterEach(async () => {
69 await act(() => renderer?.unmount());
70 vi.unstubAllGlobals();
71});
72
73async function openSettings() {
74 const router = createRouter({
75 routeTree: createRootRoute({ component: IntegrationsSettingsPanel }),
76 history: createMemoryHistory(),
77 });
78 await router.load();
79 await act(() => {
80 renderer = create(
81 <StrictMode>
82 <RouterProvider router={router} />
83 </StrictMode>,
84 );
85 });
86 expect(renderer!.root.findByType(IntegrationsSettingsPanel)).toBeDefined();
87}
88
89describe("Integrations browser discovery", () => {
90 it("does not scan browser files when entering or revisiting settings", async () => {
91 await openSettings();
92 expect(listBrowserImportSources).not.toHaveBeenCalled();
93
94 await act(() => renderer?.unmount());
95 await openSettings();
96 expect(listBrowserImportSources).not.toHaveBeenCalled();
97 });
98
99 it("places device settings directly after browser settings", async () => {
100 await openSettings();
101 const sections = renderer!.root
102 .findAll((node) => node.type === "section")
103 .map((node) => node.props.id)
104 .filter(Boolean);
105 expect(sections.indexOf("devices")).toBeGreaterThan(sections.indexOf("browser"));
106 });
107});
108
109const deviceState = (overrides: Partial<DeviceServiceState> = {}): DeviceServiceState => ({
110 hosts: [
111 {
112 id: "local",
113 kind: "local",
114 label: "This machine",
115 hubInstalled: false,
116 agentDeviceInstalled: false,
117 platforms: [
118 { platform: "ios", available: true },
119 { platform: "android", available: true },
120 ],
121 },
122 ],
123 hostStatus: "ready",
124 hostStatuses: {},
125 devices: [],
126 sessions: [],
127 onboardingCompleted: false,
128 agentAccessEnabled: false,
129 hubBasePath: "/api/device-hub",
130 revision: 0,
131 ...overrides,
132});
133
134describe("device setup guidance", () => {
135 it("directs users to install an iOS runtime and create an Android virtual device", () => {
136 expect(platformSetupStatus(deviceState(), "ios").message).toContain("Xcode Settings");
137 expect(platformSetupStatus(deviceState(), "android").message).toContain("Device Manager");
138 });
139
140 it("preserves a specific missing-tool explanation from the server", () => {
141 const state = deviceState({
142 hosts: [
143 {
144 ...deviceState().hosts[0]!,
145 platforms: [
146 { platform: "ios", available: true },
147 { platform: "android", available: false, reason: "Android Emulator is missing." },
148 ],
149 },
150 ],
151 });
152 expect(platformSetupStatus(state, "android").message).toBe("Android Emulator is missing.");
153 });
154});