Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b98359c8b |
No files matched your search
@@ -75,4 +75,30 @@ describe("migrateLegacyConnectionCatalog", () => {
|
||||
expect(catalog.targets).toEqual([]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("preserves the JSON parse failure", () =>
|
||||
Effect.gen(function* () {
|
||||
const error = yield* Effect.flip(migrateLegacyConnectionCatalog("{"));
|
||||
|
||||
expect(error).toMatchObject({
|
||||
_tag: "LegacyConnectionDocumentParseError",
|
||||
cause: expect.any(SyntaxError),
|
||||
});
|
||||
expect(error.message).toBe("Could not parse the legacy mobile connection catalog.");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("preserves the schema decode failure", () =>
|
||||
Effect.gen(function* () {
|
||||
const error = yield* Effect.flip(
|
||||
migrateLegacyConnectionCatalog(JSON.stringify({ connections: "invalid" })),
|
||||
);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
_tag: "LegacyConnectionDocumentDecodeError",
|
||||
cause: { _tag: "SchemaError" },
|
||||
});
|
||||
expect(error.message).toBe("Could not decode the legacy mobile connection catalog.");
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -33,12 +33,23 @@ const LegacyConnectionDocument = Schema.Struct({
|
||||
});
|
||||
const decodeLegacyConnectionDocument = Schema.decodeUnknownEffect(LegacyConnectionDocument);
|
||||
|
||||
export class LegacyConnectionMigrationError extends Schema.TaggedErrorClass<LegacyConnectionMigrationError>()(
|
||||
"LegacyConnectionMigrationError",
|
||||
{
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
export class LegacyConnectionDocumentParseError extends Schema.TaggedErrorClass<LegacyConnectionDocumentParseError>()(
|
||||
"LegacyConnectionDocumentParseError",
|
||||
{ cause: Schema.Defect() },
|
||||
) {
|
||||
override get message(): string {
|
||||
return "Could not parse the legacy mobile connection catalog.";
|
||||
}
|
||||
}
|
||||
|
||||
export class LegacyConnectionDocumentDecodeError extends Schema.TaggedErrorClass<LegacyConnectionDocumentDecodeError>()(
|
||||
"LegacyConnectionDocumentDecodeError",
|
||||
{ cause: Schema.Defect() },
|
||||
) {
|
||||
override get message(): string {
|
||||
return "Could not decode the legacy mobile connection catalog.";
|
||||
}
|
||||
}
|
||||
|
||||
function isRelayManaged(connection: typeof LegacySavedRemoteConnection.Type): boolean {
|
||||
return connection.relayManaged === true || connection.authenticationMethod === "dpop";
|
||||
@@ -92,18 +103,10 @@ export const migrateLegacyConnectionCatalog = Effect.fn(
|
||||
)(function* (raw: string) {
|
||||
const parsed = yield* Effect.try({
|
||||
try: () => JSON.parse(raw) as unknown,
|
||||
catch: (cause) =>
|
||||
new LegacyConnectionMigrationError({
|
||||
message: `Could not parse the legacy mobile connection catalog: ${String(cause)}`,
|
||||
}),
|
||||
catch: (cause) => new LegacyConnectionDocumentParseError({ cause }),
|
||||
});
|
||||
const legacy = yield* decodeLegacyConnectionDocument(parsed).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new LegacyConnectionMigrationError({
|
||||
message: `Could not decode the legacy mobile connection catalog: ${String(cause)}`,
|
||||
}),
|
||||
),
|
||||
Effect.mapError((cause) => new LegacyConnectionDocumentDecodeError({ cause })),
|
||||
);
|
||||
|
||||
return (legacy.connections ?? []).reduce(migrateConnection, EMPTY_CONNECTION_CATALOG_DOCUMENT);
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
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,
|
||||
DesktopNetworkAccessLoadError,
|
||||
DesktopNetworkAccessUnavailableError,
|
||||
} from "./desktopNetworkAccess";
|
||||
import { createDesktopNetworkAccessStateAtom } from "./desktopNetworkAccess";
|
||||
|
||||
const serverExposureState: DesktopServerExposureState = {
|
||||
advertisedHost: "192.168.1.10",
|
||||
@@ -20,7 +14,6 @@ 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 () => {
|
||||
@@ -54,57 +47,4 @@ 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,32 +21,13 @@ export interface DesktopNetworkAccessSnapshot {
|
||||
readonly serverExposureState: DesktopServerExposureState;
|
||||
}
|
||||
|
||||
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",
|
||||
class DesktopNetworkAccessError extends Schema.TaggedErrorClass<DesktopNetworkAccessError>()(
|
||||
"DesktopNetworkAccessError",
|
||||
{
|
||||
operation: Schema.Literals(["get-advertised-endpoints", "get-server-exposure-state"]),
|
||||
cause: Schema.Defect(),
|
||||
message: Schema.String,
|
||||
cause: Schema.optional(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;
|
||||
@@ -58,30 +39,25 @@ export function createDesktopNetworkAccessStateAtom(
|
||||
const loadDesktopNetworkAccess = Effect.fn("loadDesktopNetworkAccess")(function* () {
|
||||
const bridge = getBridge();
|
||||
if (!bridge) {
|
||||
return yield* new DesktopNetworkAccessUnavailableError();
|
||||
return yield* new DesktopNetworkAccessError({
|
||||
message: "Desktop network access is unavailable.",
|
||||
});
|
||||
}
|
||||
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,
|
||||
}),
|
||||
}),
|
||||
return yield* Effect.tryPromise({
|
||||
try: async (): Promise<DesktopNetworkAccessSnapshot> => {
|
||||
const [serverExposureState, advertisedEndpoints] = await Promise.all([
|
||||
bridge.getServerExposureState(),
|
||||
bridge.getAdvertisedEndpoints(),
|
||||
]);
|
||||
return { advertisedEndpoints, serverExposureState };
|
||||
},
|
||||
{ concurrency: "unbounded" },
|
||||
);
|
||||
return { advertisedEndpoints, serverExposureState };
|
||||
catch: (cause) =>
|
||||
new DesktopNetworkAccessError({
|
||||
message:
|
||||
cause instanceof Error ? cause.message : "Failed to load desktop network access.",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
return Atom.make(loadDesktopNetworkAccess()).pipe(
|
||||
|
||||
Reference in new issue
Block a user