Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<typeof useHostConnectionChecks>) => 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<typeof useHostConnectionChecks>;
await using _screen = await render(<Harness onReady={(next) => (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.",
});
});
});
4 changes: 2 additions & 2 deletions apps/web/src/components/settings/useHostConnectionChecks.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -30,7 +30,7 @@ export function useHostConnectionChecks(targets: ReadonlyArray<DeviceHostCheckTa
throw new Error("This connection cannot test device hosts.");
}
const result = await test({ environmentId, input });
if (result._tag === "Failure") throw new Error(Cause.pretty(result.cause));
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
return result.value;
},
(environmentId, result) => {
Expand Down
Loading