Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b444300af0 |
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,9 +1,15 @@
|
||||
import type { AdvertisedEndpoint, DesktopServerExposureState } from "@t3tools/contracts";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
|
||||
import { AtomRegistry } from "effect/unstable/reactivity";
|
||||
import { describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
import { createDesktopNetworkAccessStateAtom } from "./desktopNetworkAccess";
|
||||
import {
|
||||
createDesktopNetworkAccessStateAtom,
|
||||
DesktopNetworkAccessLoadError,
|
||||
DesktopNetworkAccessUnavailableError,
|
||||
} from "./desktopNetworkAccess";
|
||||
|
||||
const serverExposureState: DesktopServerExposureState = {
|
||||
advertisedHost: "192.168.1.10",
|
||||
@@ -14,6 +20,7 @@ const serverExposureState: DesktopServerExposureState = {
|
||||
};
|
||||
|
||||
const advertisedEndpoints: ReadonlyArray<AdvertisedEndpoint> = [];
|
||||
const isDesktopNetworkAccessLoadError = Schema.is(DesktopNetworkAccessLoadError);
|
||||
|
||||
describe("desktopNetworkAccessState", () => {
|
||||
it("retains the loaded snapshot when the settings screen remounts", async () => {
|
||||
@@ -47,4 +54,57 @@ describe("desktopNetworkAccessState", () => {
|
||||
remount();
|
||||
registry.dispose();
|
||||
});
|
||||
|
||||
it("reports an unavailable desktop bridge without inventing a cause", async () => {
|
||||
const atom = createDesktopNetworkAccessStateAtom(() => undefined);
|
||||
const registry = AtomRegistry.make();
|
||||
registry.mount(atom);
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
|
||||
});
|
||||
const result = registry.get(atom);
|
||||
if (!AsyncResult.isFailure(result)) {
|
||||
throw new Error("Expected desktop network access to fail");
|
||||
}
|
||||
const error = Cause.squash(result.cause);
|
||||
|
||||
expect(error).toBeInstanceOf(DesktopNetworkAccessUnavailableError);
|
||||
expect(error).toMatchObject({
|
||||
_tag: "DesktopNetworkAccessUnavailableError",
|
||||
message: "Desktop network access is unavailable.",
|
||||
});
|
||||
|
||||
registry.dispose();
|
||||
});
|
||||
|
||||
it("retains the failing bridge operation and cause", async () => {
|
||||
const cause = new Error("native bridge rejected");
|
||||
const atom = createDesktopNetworkAccessStateAtom(() => ({
|
||||
getAdvertisedEndpoints: () => Promise.reject(cause),
|
||||
getServerExposureState: () => Promise.resolve(serverExposureState),
|
||||
}));
|
||||
const registry = AtomRegistry.make();
|
||||
registry.mount(atom);
|
||||
|
||||
await vi.waitFor(() => {
|
||||
expect(AsyncResult.isFailure(registry.get(atom))).toBe(true);
|
||||
});
|
||||
const result = registry.get(atom);
|
||||
if (!AsyncResult.isFailure(result)) {
|
||||
throw new Error("Expected desktop network access to fail");
|
||||
}
|
||||
const error = Cause.squash(result.cause);
|
||||
if (!isDesktopNetworkAccessLoadError(error)) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
expect(error).toMatchObject({
|
||||
operation: "get-advertised-endpoints",
|
||||
message: "Desktop network access operation 'get advertised endpoints' failed.",
|
||||
});
|
||||
expect(error.cause).toBe(cause);
|
||||
|
||||
registry.dispose();
|
||||
});
|
||||
});
|
||||
@@ -21,13 +21,32 @@ export interface DesktopNetworkAccessSnapshot {
|
||||
readonly serverExposureState: DesktopServerExposureState;
|
||||
}
|
||||
|
||||
class DesktopNetworkAccessError extends Schema.TaggedErrorClass<DesktopNetworkAccessError>()(
|
||||
"DesktopNetworkAccessError",
|
||||
export class DesktopNetworkAccessUnavailableError extends Schema.TaggedErrorClass<DesktopNetworkAccessUnavailableError>()(
|
||||
"DesktopNetworkAccessUnavailableError",
|
||||
{},
|
||||
) {
|
||||
override get message(): string {
|
||||
return "Desktop network access is unavailable.";
|
||||
}
|
||||
}
|
||||
|
||||
export class DesktopNetworkAccessLoadError extends Schema.TaggedErrorClass<DesktopNetworkAccessLoadError>()(
|
||||
"DesktopNetworkAccessLoadError",
|
||||
{
|
||||
message: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect()),
|
||||
operation: Schema.Literals(["get-advertised-endpoints", "get-server-exposure-state"]),
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {}
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Desktop network access operation '${this.operation.replaceAll("-", " ")}' failed.`;
|
||||
}
|
||||
}
|
||||
|
||||
export const DesktopNetworkAccessError = Schema.Union([
|
||||
DesktopNetworkAccessUnavailableError,
|
||||
DesktopNetworkAccessLoadError,
|
||||
]);
|
||||
export type DesktopNetworkAccessError = typeof DesktopNetworkAccessError.Type;
|
||||
|
||||
function getDesktopNetworkAccessBridge(): DesktopNetworkAccessBridge | undefined {
|
||||
return typeof window === "undefined" ? undefined : window.desktopBridge;
|
||||
@@ -39,25 +58,30 @@ export function createDesktopNetworkAccessStateAtom(
|
||||
const loadDesktopNetworkAccess = Effect.fn("loadDesktopNetworkAccess")(function* () {
|
||||
const bridge = getBridge();
|
||||
if (!bridge) {
|
||||
return yield* new DesktopNetworkAccessError({
|
||||
message: "Desktop network access is unavailable.",
|
||||
});
|
||||
return yield* new DesktopNetworkAccessUnavailableError();
|
||||
}
|
||||
return yield* Effect.tryPromise({
|
||||
try: async (): Promise<DesktopNetworkAccessSnapshot> => {
|
||||
const [serverExposureState, advertisedEndpoints] = await Promise.all([
|
||||
bridge.getServerExposureState(),
|
||||
bridge.getAdvertisedEndpoints(),
|
||||
]);
|
||||
return { advertisedEndpoints, serverExposureState };
|
||||
},
|
||||
catch: (cause) =>
|
||||
new DesktopNetworkAccessError({
|
||||
message:
|
||||
cause instanceof Error ? cause.message : "Failed to load desktop network access.",
|
||||
cause,
|
||||
const { serverExposureState, advertisedEndpoints } = yield* Effect.all(
|
||||
{
|
||||
serverExposureState: Effect.tryPromise({
|
||||
try: () => bridge.getServerExposureState(),
|
||||
catch: (cause) =>
|
||||
new DesktopNetworkAccessLoadError({
|
||||
operation: "get-server-exposure-state",
|
||||
cause,
|
||||
}),
|
||||
}),
|
||||
});
|
||||
advertisedEndpoints: Effect.tryPromise({
|
||||
try: () => bridge.getAdvertisedEndpoints(),
|
||||
catch: (cause) =>
|
||||
new DesktopNetworkAccessLoadError({
|
||||
operation: "get-advertised-endpoints",
|
||||
cause,
|
||||
}),
|
||||
}),
|
||||
},
|
||||
{ concurrency: "unbounded" },
|
||||
);
|
||||
return { advertisedEndpoints, serverExposureState };
|
||||
});
|
||||
|
||||
return Atom.make(loadDesktopNetworkAccess()).pipe(
|
||||
|
||||
Reference in new issue
Block a user