Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
313b61cdb1 |
No files matched your search
@@ -1,12 +1,9 @@
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Option from "effect/Option";
|
||||
import { AtomRegistry } from "effect/unstable/reactivity";
|
||||
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
||||
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
import {
|
||||
createSourceHighlightAtomFamily,
|
||||
isSourceHighlightError,
|
||||
type SourceHighlightTokens,
|
||||
} from "./sourceHighlightingState";
|
||||
|
||||
@@ -104,9 +101,8 @@ describe("sourceHighlightingState", () => {
|
||||
});
|
||||
|
||||
it("exposes highlighter errors as a failed async result", async () => {
|
||||
const cause = new Error("highlight failed");
|
||||
const highlight = vi.fn(async () => {
|
||||
throw cause;
|
||||
throw new Error("highlight failed");
|
||||
});
|
||||
const sourceHighlightAtom = createSourceHighlightAtomFamily({ highlight });
|
||||
const registry = AtomRegistry.make();
|
||||
@@ -117,17 +113,8 @@ describe("sourceHighlightingState", () => {
|
||||
});
|
||||
const unmount = registry.mount(atom);
|
||||
|
||||
await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
|
||||
const result = registry.get(atom);
|
||||
const error = AsyncResult.isFailure(result)
|
||||
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
|
||||
: undefined;
|
||||
|
||||
expect(isSourceHighlightError(error)).toBe(true);
|
||||
expect(error).toMatchObject({
|
||||
path: "src/example.ts",
|
||||
theme: "light",
|
||||
cause,
|
||||
await vi.waitFor(() => {
|
||||
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
|
||||
});
|
||||
|
||||
unmount();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import * as Data from "effect/Data";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { Atom } from "effect/unstable/reactivity";
|
||||
|
||||
import {
|
||||
@@ -23,20 +22,9 @@ type SourceHighlighter = (input: SourceHighlightInput) => Promise<SourceHighligh
|
||||
|
||||
class SourceHighlightCacheKey extends Data.Class<SourceHighlightInput> {}
|
||||
|
||||
export class SourceHighlightError extends Schema.TaggedErrorClass<SourceHighlightError>()(
|
||||
"SourceHighlightError",
|
||||
{
|
||||
path: Schema.String,
|
||||
theme: Schema.Literals(["light", "dark"]),
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to highlight ${this.path} with the ${this.theme} theme.`;
|
||||
}
|
||||
}
|
||||
|
||||
export const isSourceHighlightError = Schema.is(SourceHighlightError);
|
||||
class SourceHighlightError extends Data.TaggedError("SourceHighlightError")<{
|
||||
readonly cause: unknown;
|
||||
}> {}
|
||||
|
||||
export function createSourceHighlightAtomFamily(options?: {
|
||||
readonly highlight?: SourceHighlighter;
|
||||
@@ -48,8 +36,7 @@ export function createSourceHighlightAtomFamily(options?: {
|
||||
Atom.make(
|
||||
Effect.tryPromise({
|
||||
try: () => highlight(request),
|
||||
catch: (cause) =>
|
||||
new SourceHighlightError({ path: request.path, theme: request.theme, cause }),
|
||||
catch: (cause) => new SourceHighlightError({ cause }),
|
||||
}),
|
||||
).pipe(
|
||||
Atom.setIdleTTL(idleTtlMs),
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
import * as Cause from "effect/Cause";
|
||||
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";
|
||||
|
||||
import {
|
||||
createWorkspaceFileImageAtomFamily,
|
||||
isWorkspaceImagePrefetchError,
|
||||
} from "./workspace-file-image-cache";
|
||||
import { createWorkspaceFileImageAtomFamily } from "./workspace-file-image-cache";
|
||||
|
||||
describe("workspaceFileImageAtom", () => {
|
||||
it("reuses a prefetched image across route remounts", async () => {
|
||||
@@ -53,47 +48,14 @@ describe("workspaceFileImageAtom", () => {
|
||||
registry.dispose();
|
||||
});
|
||||
|
||||
it("exposes a false native prefetch result", async () => {
|
||||
it("exposes prefetch failures", async () => {
|
||||
const imageAtom = createWorkspaceFileImageAtomFamily({ prefetch: async () => false });
|
||||
const registry = AtomRegistry.make();
|
||||
const atom = imageAtom("https://example.test/missing.png");
|
||||
const unmount = registry.mount(atom);
|
||||
|
||||
await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
|
||||
const result = registry.get(atom);
|
||||
const error = AsyncResult.isFailure(result)
|
||||
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
|
||||
: undefined;
|
||||
|
||||
expect(isWorkspaceImagePrefetchError(error)).toBe(true);
|
||||
expect(error).toMatchObject({ uri: "https://example.test/missing.png" });
|
||||
expect(error).not.toHaveProperty("cause");
|
||||
|
||||
unmount();
|
||||
registry.dispose();
|
||||
});
|
||||
|
||||
it("preserves thrown prefetch causes", async () => {
|
||||
const cause = new Error("native image prefetch failed");
|
||||
const imageAtom = createWorkspaceFileImageAtomFamily({
|
||||
prefetch: async () => {
|
||||
throw cause;
|
||||
},
|
||||
});
|
||||
const registry = AtomRegistry.make();
|
||||
const atom = imageAtom("https://example.test/missing.png");
|
||||
const unmount = registry.mount(atom);
|
||||
|
||||
await vi.waitFor(() => expect(AsyncResult.isFailure(registry.get(atom))).toBe(true));
|
||||
const result = registry.get(atom);
|
||||
const error = AsyncResult.isFailure(result)
|
||||
? Option.getOrUndefined(Cause.findErrorOption(result.cause))
|
||||
: undefined;
|
||||
|
||||
expect(isWorkspaceImagePrefetchError(error)).toBe(true);
|
||||
expect(error).toMatchObject({
|
||||
uri: "https://example.test/missing.png",
|
||||
cause,
|
||||
await vi.waitFor(() => {
|
||||
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
|
||||
});
|
||||
|
||||
unmount();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import * as Data from "effect/Data";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { Atom } from "effect/unstable/reactivity";
|
||||
|
||||
const WORKSPACE_IMAGE_IDLE_TTL_MS = 30 * 60_000;
|
||||
@@ -9,19 +8,10 @@ type ImagePrefetch = (uri: string) => Promise<boolean>;
|
||||
|
||||
class WorkspaceImageCacheKey extends Data.Class<{ readonly uri: string }> {}
|
||||
|
||||
export class WorkspaceImagePrefetchError extends Schema.TaggedErrorClass<WorkspaceImagePrefetchError>()(
|
||||
"WorkspaceImagePrefetchError",
|
||||
{
|
||||
uri: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect()),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to prefetch workspace image ${this.uri}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export const isWorkspaceImagePrefetchError = Schema.is(WorkspaceImagePrefetchError);
|
||||
export class WorkspaceImagePrefetchError extends Data.TaggedError("WorkspaceImagePrefetchError")<{
|
||||
readonly cause?: unknown;
|
||||
readonly uri: string;
|
||||
}> {}
|
||||
|
||||
async function prefetchWithNativeImage(uri: string): Promise<boolean> {
|
||||
const { Image } = await import("react-native");
|
||||
@@ -45,7 +35,7 @@ export function createWorkspaceFileImageAtomFamily(options?: {
|
||||
return key.uri;
|
||||
},
|
||||
catch: (cause) =>
|
||||
isWorkspaceImagePrefetchError(cause)
|
||||
cause instanceof WorkspaceImagePrefetchError
|
||||
? cause
|
||||
: new WorkspaceImagePrefetchError({ uri: key.uri, cause }),
|
||||
}),
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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 }),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user