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

useHostConnectionChecks.browser.test.tsx

64 lines1.9 KB

1import { DeviceHostUnavailableError, EnvironmentId } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import { AsyncResult } from "effect/reactivity";
4import { describe, expect, it, vi } from "vite-plus/test";
5​
6import { act, render } from "../../../test/render";
7​
8const state = vi.hoisted(() => ({ test: vi.fn() }));
9function setup() {
10 state.test.mockReset();
12​
13vi.mock(import("../../state/use-atom-command"), async (original) => ({
14 ...(await original()),
15 useAtomCommand: (() => state.test) as never,
16}));
17vi.mock(import("../../state/session"), async (original) => ({
18 ...(await original()),
19 readEnvironmentScope: (() => true) as never,
20}));
21​
22import { useHostConnectionChecks } from "./useHostConnectionChecks";
23​
24const environmentId = EnvironmentId.make("environment-1");
25const targets = [{ environmentId, label: "This Mac", connected: true }];
26​
27function Harness({
28 onReady,
29}: {
30 onReady: (run: ReturnType<typeof useHostConnectionChecks>) => void;
31}) {
32 onReady(useHostConnectionChecks(targets));
33 return null;
35​
36describe("useHostConnectionChecks", () => {
37 it("reports a failed check by its message, without a stack trace", async () => {
38 setup();
39 state.test.mockResolvedValue(
40 AsyncResult.failure(
41 Cause.fail(
42 new DeviceHostUnavailableError({
43 hostId: "mac",
44 hostLabel: "Mac mini",
45 reason: "SSH connection refused.",
46 }),
47 ),
48 ),
49 );
50 let checks!: ReturnType<typeof useHostConnectionChecks>;
51 await using _screen = await render(<Harness onReady={(next) => (checks = next)} />, {
52 router: false, // the hook reads no route state
53 });
54​
55 const results = await act(() =>
56 checks.testConnection({ kind: "ssh", id: "mac", label: "Mac mini", target: "user@mac" }),
57 );
58​
59 expect(results?.[environmentId]).toEqual({
60 status: "failed",
61 error: "Mac mini is unavailable: SSH connection refused.",
62 });
63 });
64});