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

DeviceToolUpdate.browser.test.tsx

147 lines4.6 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 { createRoot, type Root } from "react-dom/client";
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 root: Root | undefined;
90let container: HTMLDivElement | undefined;
91​
92beforeEach(() => {
93 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
94 list.mockReset();
95});
96​
97afterEach(async () => {
98 await act(() => root?.unmount());
99 container?.remove();
100 vi.unstubAllGlobals();
101});
102​
103it.each([
104 {
105 name: "a failed install",
106 error: new DeviceOperationError({
107 operation: "update device tool",
108 reason: "command_failed",
109 exitCode: 243,
110 cause: new Error("npm error EACCES https://private:credential@registry.example.test/hub"),
111 }),
112 message: "Device update device tool failed: The device command failed (exit code 243).",
113 },
114 {
115 name: "a lost connection",
116 error: new EnvironmentRpcUnavailableError({
117 environmentId,
118 message: "Query environment is not connected.",
119 }),
120 message: "Query environment is not connected.",
121 },
122])(
123 "reports $name as the server describes it, not as a network problem",
124 async ({ error, message }) => {
125 list.mockResolvedValue({ _tag: "Failure", cause: Cause.fail(error) });
126 container = document.body.appendChild(document.createElement("div"));
127 root = createRoot(container);
128 await act(() => {
129 root!.render(
130 <DeviceIntegrationControls
131 environmentId={environmentId}
132 enabled
133 agentAccessEnabled={false}
134 />,
135 );
136 });
137​
138 const update = [...container.querySelectorAll("button")].find(
139 (button) => button.textContent === "Update to v0.12.0",
140 );
141 expect(update).toBeDefined();
142 await act(async () => update!.click());
143​
144 expect(list).toHaveBeenCalledWith({ environmentId, input: { updateTool: "hub" } });
145 expect(container.querySelector('[role="alert"]')?.textContent).toBe(message);
146 },
147);