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

DeviceToolUpdate.test.tsx

142 lines4.5 KB

1import { EnvironmentRpcUnavailableError } from "@t3tools/client-runtime/rpc";
2import {
3 DeviceOperationError,
4 EnvironmentId,
5 LOCAL_DEVICE_HOST_ID,
6 type DeviceServiceState,
7} from "@t3tools/contracts";
8import * as Cause from "effect/Cause";
9import { act, type ReactNode } from "react";
10import { create, type ReactTestRenderer } from "react-test-renderer";
11import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
12​
13const { list } = vi.hoisted(() => ({ list: vi.fn() }));
14​
15vi.mock("~/state/use-atom-command", () => ({
16 useAtomCommand: (command: unknown) => (command === "list" ? list : vi.fn()),
17}));
18vi.mock("~/state/device", () => ({
19 deviceEnvironment: { list: "list", configure: "configure" },
20 useDeviceState: () => ({ state: deviceState, loaded: true }),
21}));
22vi.mock("~/state/session", async (importOriginal) => ({
23 ...(await importOriginal<typeof import("~/state/session")>()),
24 useEnvironmentScope: () => true,
25 readEnvironmentScope: () => true,
26}));
27vi.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}));
39vi.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.
44vi.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.
54vi.mock("../device/DeviceToolVersions", () => ({
55 DeviceToolVersions: ({ action }: { action?: ReactNode }) => action ?? null,
56}));
57vi.mock("../device/DeviceHostUpdates", () => ({ DeviceHostUpdates: () => null }));
58vi.mock("./DeviceHostsSettings", () => ({ DeviceHostsSettings: () => null }));
59​
60import { DeviceIntegrationControls } from "./IntegrationsSettings";
61​
62const environmentId = EnvironmentId.make("environment-1");
63const 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​
89let renderer: ReactTestRenderer | undefined;
90​
91beforeEach(() => {
92 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
93 list.mockReset();
94});
95​
96afterEach(async () => {
97 await act(() => renderer?.unmount());
98 vi.unstubAllGlobals();
99});
100​
101it.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:credential@registry.example.test/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);