diff --git a/apps/web/src/components/settings/useHostConnectionChecks.browser.test.tsx b/apps/web/src/components/settings/useHostConnectionChecks.browser.test.tsx new file mode 100644 index 0000000000000..93c2776034a4d --- /dev/null +++ b/apps/web/src/components/settings/useHostConnectionChecks.browser.test.tsx @@ -0,0 +1,64 @@ +import { DeviceHostUnavailableError, EnvironmentId } from "@t3tools/contracts"; +import * as Cause from "effect/Cause"; +import { AsyncResult } from "effect/reactivity"; +import { describe, expect, it, vi } from "vite-plus/test"; + +import { act, render } from "../../../test/render"; + +const state = vi.hoisted(() => ({ test: vi.fn() })); +function setup() { + state.test.mockReset(); +} + +vi.mock(import("../../state/use-atom-command"), async (original) => ({ + ...(await original()), + useAtomCommand: (() => state.test) as never, +})); +vi.mock(import("../../state/session"), async (original) => ({ + ...(await original()), + readEnvironmentScope: (() => true) as never, +})); + +import { useHostConnectionChecks } from "./useHostConnectionChecks"; + +const environmentId = EnvironmentId.make("environment-1"); +const targets = [{ environmentId, label: "This Mac", connected: true }]; + +function Harness({ + onReady, +}: { + onReady: (run: ReturnType) => void; +}) { + onReady(useHostConnectionChecks(targets)); + return null; +} + +describe("useHostConnectionChecks", () => { + it("reports a failed check by its message, without a stack trace", async () => { + setup(); + state.test.mockResolvedValue( + AsyncResult.failure( + Cause.fail( + new DeviceHostUnavailableError({ + hostId: "mac", + hostLabel: "Mac mini", + reason: "SSH connection refused.", + }), + ), + ), + ); + let checks!: ReturnType; + await using _screen = await render( (checks = next)} />, { + router: false, // the hook reads no route state + }); + + const results = await act(() => + checks.testConnection({ kind: "ssh", id: "mac", label: "Mac mini", target: "user@mac" }), + ); + + expect(results?.[environmentId]).toEqual({ + status: "failed", + error: "Mac mini is unavailable: SSH connection refused.", + }); + }); +}); diff --git a/apps/web/src/components/settings/useHostConnectionChecks.ts b/apps/web/src/components/settings/useHostConnectionChecks.ts index 4b2733cdfcf42..e96b992a149b8 100644 --- a/apps/web/src/components/settings/useHostConnectionChecks.ts +++ b/apps/web/src/components/settings/useHostConnectionChecks.ts @@ -1,7 +1,7 @@ import { AuthSettingsWriteScope } from "@t3tools/contracts"; import { readEnvironmentScope } from "../../state/session"; import { useRef, useState } from "react"; -import * as Cause from "effect/Cause"; +import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import type { SshDeviceHostConfig } from "@t3tools/contracts"; import { deviceEnvironment } from "../../state/device"; import { useAtomCommand } from "../../state/use-atom-command"; @@ -30,7 +30,7 @@ export function useHostConnectionChecks(targets: ReadonlyArray {