|
| 1 | +import { EnvironmentRpcUnavailableError } from "@t3tools/client-runtime/rpc"; |
| 2 | +import { |
| 3 | + DeviceOperationError, |
| 4 | + EnvironmentId, |
| 5 | + LOCAL_DEVICE_HOST_ID, |
| 6 | + type DeviceServiceState, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import * as Cause from "effect/Cause"; |
| 9 | +import { act, type ReactNode } from "react"; |
| 10 | +import { create, type ReactTestRenderer } from "react-test-renderer"; |
| 11 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 12 | + |
| 13 | +const { list } = vi.hoisted(() => ({ list: vi.fn() })); |
| 14 | + |
| 15 | +vi.mock("~/state/use-atom-command", () => ({ |
| 16 | + useAtomCommand: (command: unknown) => (command === "list" ? list : vi.fn()), |
| 17 | +})); |
| 18 | +vi.mock("~/state/device", () => ({ |
| 19 | + deviceEnvironment: { list: "list", configure: "configure" }, |
| 20 | + useDeviceState: () => ({ state: deviceState, loaded: true }), |
| 21 | +})); |
| 22 | +vi.mock("~/state/session", async (importOriginal) => ({ |
| 23 | + ...(await importOriginal<typeof import("~/state/session")>()), |
| 24 | + useEnvironmentScope: () => true, |
| 25 | + readEnvironmentScope: () => true, |
| 26 | +})); |
| 27 | +vi.mock("./SettingsScopeContext", () => ({ |
| 28 | + useSettingsScope: () => ({ |
| 29 | + scope: { kind: "all", environmentIds: [] }, |
| 30 | + search: {}, |
| 31 | + environment: null, |
| 32 | + environments: [], |
| 33 | + target: null, |
| 34 | + connectedEnvironments: [], |
| 35 | + targets: [], |
| 36 | + }), |
| 37 | + useOptionalSettingsScope: () => null, |
| 38 | +})); |
| 39 | +vi.mock("./useScopedSettings", async (importOriginal) => ({ |
| 40 | + ...(await importOriginal<typeof import("./useScopedSettings")>()), |
| 41 | + useUpdateScopedSettings: () => vi.fn(), |
| 42 | +})); |
| 43 | +// Rows resolve scope and permissions; only their controls matter here. |
| 44 | +vi.mock("./settingsLayout", async (importOriginal) => ({ |
| 45 | + ...(await importOriginal<typeof import("./settingsLayout")>()), |
| 46 | + SettingsRow: ({ control, children }: { control?: ReactNode; children?: ReactNode }) => ( |
| 47 | + <> |
| 48 | + {control} |
| 49 | + {children} |
| 50 | + </> |
| 51 | + ), |
| 52 | +})); |
| 53 | +// The version popover holds the Update button; render the button in place of the popover. |
| 54 | +vi.mock("../device/DeviceToolVersions", () => ({ |
| 55 | + DeviceToolVersions: ({ action }: { action?: ReactNode }) => action ?? null, |
| 56 | +})); |
| 57 | +vi.mock("../device/DeviceHostUpdates", () => ({ DeviceHostUpdates: () => null })); |
| 58 | +vi.mock("./DeviceHostsSettings", () => ({ DeviceHostsSettings: () => null })); |
| 59 | + |
| 60 | +import { DeviceIntegrationControls } from "./IntegrationsSettings"; |
| 61 | + |
| 62 | +const environmentId = EnvironmentId.make("environment-1"); |
| 63 | +const deviceState = { |
| 64 | + supportsToolUpdate: true, |
| 65 | + hosts: [ |
| 66 | + { |
| 67 | + id: LOCAL_DEVICE_HOST_ID, |
| 68 | + kind: "local", |
| 69 | + label: "This computer", |
| 70 | + platforms: [], |
| 71 | + tools: { |
| 72 | + hub: { requiredVersion: "0.12.0", installedVersions: [], runningVersion: null }, |
| 73 | + agent: { requiredVersion: "1.0.0", installedVersions: ["1.0.0"], runningVersion: null }, |
| 74 | + }, |
| 75 | + hubInstalled: false, |
| 76 | + agentDeviceInstalled: true, |
| 77 | + }, |
| 78 | + ], |
| 79 | + hostStatus: "idle", |
| 80 | + hostStatuses: {}, |
| 81 | + devices: [], |
| 82 | + sessions: [], |
| 83 | + onboardingCompleted: true, |
| 84 | + agentAccessEnabled: false, |
| 85 | + hubBasePath: "/device-hub", |
| 86 | + revision: 1, |
| 87 | +} satisfies DeviceServiceState; |
| 88 | + |
| 89 | +let renderer: ReactTestRenderer | undefined; |
| 90 | + |
| 91 | +beforeEach(() => { |
| 92 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 93 | + list.mockReset(); |
| 94 | +}); |
| 95 | + |
| 96 | +afterEach(async () => { |
| 97 | + await act(() => renderer?.unmount()); |
| 98 | + vi.unstubAllGlobals(); |
| 99 | +}); |
| 100 | + |
| 101 | +it.each([ |
| 102 | + { |
| 103 | + name: "a failed install", |
| 104 | + error: new DeviceOperationError({ |
| 105 | + operation: "update device tool", |
| 106 | + reason: "command_failed", |
| 107 | + exitCode: 243, |
| 108 | + cause: new Error("npm error EACCES https://private:[email protected]/hub"), |
| 109 | + }), |
| 110 | + message: "Device update device tool failed: The device command failed (exit code 243).", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "a lost connection", |
| 114 | + error: new EnvironmentRpcUnavailableError({ |
| 115 | + environmentId, |
| 116 | + message: "Query environment is not connected.", |
| 117 | + }), |
| 118 | + message: "Query environment is not connected.", |
| 119 | + }, |
| 120 | +])( |
| 121 | + "reports $name as the server describes it, not as a network problem", |
| 122 | + async ({ error, message }) => { |
| 123 | + list.mockResolvedValue({ _tag: "Failure", cause: Cause.fail(error) }); |
| 124 | + await act(() => { |
| 125 | + renderer = create( |
| 126 | + <DeviceIntegrationControls |
| 127 | + environmentId={environmentId} |
| 128 | + enabled |
| 129 | + agentAccessEnabled={false} |
| 130 | + />, |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + const update = renderer!.root.find( |
| 135 | + (node) => node.type === "button" && node.props.children === "Update to v0.12.0", |
| 136 | + ); |
| 137 | + await act(async () => update.props.onClick()); |
| 138 | + |
| 139 | + expect(list).toHaveBeenCalledWith({ environmentId, input: { updateTool: "hub" } }); |
| 140 | + expect(renderer!.root.find((node) => node.props.role === "alert").props.children).toBe(message); |
| 141 | + }, |
| 142 | +); |
0 commit comments