Compare commits

...
Author SHA1 Message Date
Julius Marmingeandcodex 313b61cdb1 refactor(web): structure desktop SSH discovery failures
Co-authored-by: codex <[email protected]>
2026-06-20 05:14:01 -07:00
2 changed files with 61 additions and 14 deletions

No files matched your search

@@ -1,4 +1,5 @@
import type { DesktopDiscoveredSshHost } from "@t3tools/contracts";
import * as Option from "effect/Option";
import { AtomRegistry } from "effect/unstable/reactivity";
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
import { describe, expect, it, vi } from "vite-plus/test";
@@ -38,4 +39,44 @@ describe("desktopSshHostsState", () => {
remount();
registry.dispose();
});
it("distinguishes an unavailable desktop bridge", async () => {
const atom = createDesktopSshHostsStateAtom(() => undefined);
const registry = AtomRegistry.make();
const unmount = registry.mount(atom);
await vi.waitFor(() => {
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
});
const error = Option.getOrThrow(AsyncResult.error(registry.get(atom)));
expect(error._tag).toBe("DesktopSshDiscoveryBridgeUnavailableError");
expect(error.message).toBe("Desktop SSH host discovery is unavailable.");
expect("cause" in error).toBe(false);
unmount();
registry.dispose();
});
it("preserves the exact SSH discovery failure cause", async () => {
const cause = new Error("Could not parse Include directive");
const discoverSshHosts = vi.fn(() => Promise.reject(cause));
const atom = createDesktopSshHostsStateAtom(() => ({ discoverSshHosts }));
const registry = AtomRegistry.make();
const unmount = registry.mount(atom);
await vi.waitFor(() => {
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
});
const error = Option.getOrThrow(AsyncResult.error(registry.get(atom)));
expect(error._tag).toBe("DesktopSshDiscoveryError");
expect(error.message).toBe("Failed to discover SSH hosts.");
expect(error.message).not.toContain(cause.message);
expect("cause" in error ? error.cause : undefined).toBe(cause);
expect(discoverSshHosts).toHaveBeenCalledTimes(1);
unmount();
registry.dispose();
});
});
+20 -14
View File
@@ -1,17 +1,29 @@
import type { DesktopBridge, DesktopDiscoveredSshHost } from "@t3tools/contracts";
import type { DesktopBridge } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { Atom } from "effect/unstable/reactivity";
type DesktopSshDiscoveryBridge = Pick<DesktopBridge, "discoverSshHosts">;
class DesktopSshDiscoveryError extends Schema.TaggedErrorClass<DesktopSshDiscoveryError>()(
export class DesktopSshDiscoveryBridgeUnavailableError extends Schema.TaggedErrorClass<DesktopSshDiscoveryBridgeUnavailableError>()(
"DesktopSshDiscoveryBridgeUnavailableError",
{},
) {
override get message(): string {
return "Desktop SSH host discovery is unavailable.";
}
}
export class DesktopSshDiscoveryError extends Schema.TaggedErrorClass<DesktopSshDiscoveryError>()(
"DesktopSshDiscoveryError",
{
message: Schema.String,
cause: Schema.optional(Schema.Defect()),
cause: Schema.Defect(),
},
) {}
) {
override get message(): string {
return "Failed to discover SSH hosts.";
}
}
function getDesktopSshDiscoveryBridge(): DesktopSshDiscoveryBridge | undefined {
return typeof window === "undefined" ? undefined : window.desktopBridge;
@@ -23,17 +35,11 @@ export function createDesktopSshHostsStateAtom(
const discoverDesktopSshHosts = Effect.fn("discoverDesktopSshHosts")(function* () {
const bridge = getBridge();
if (!bridge) {
return yield* new DesktopSshDiscoveryError({
message: "Desktop SSH host discovery is unavailable.",
});
return yield* new DesktopSshDiscoveryBridgeUnavailableError();
}
return yield* Effect.tryPromise({
try: (): Promise<ReadonlyArray<DesktopDiscoveredSshHost>> => bridge.discoverSshHosts(),
catch: (cause) =>
new DesktopSshDiscoveryError({
message: cause instanceof Error ? cause.message : "Failed to discover SSH hosts.",
cause,
}),
try: () => bridge.discoverSshHosts(),
catch: (cause) => new DesktopSshDiscoveryError({ cause }),
});
});