Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b444300af0 |
No files matched your search
@@ -1,5 +1,4 @@
|
||||
import { assert, describe, it } from "@effect/vitest";
|
||||
import { RemoteEnvironmentAuthInvalidJsonError } from "@t3tools/client-runtime/authorization";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
@@ -29,29 +28,12 @@ const config: DesktopBackendManager.DesktopBackendStartConfig = {
|
||||
captureOutput: true,
|
||||
};
|
||||
|
||||
const managerLayer = Layer.succeed(DesktopBackendManager.DesktopBackendManager, {
|
||||
start: Effect.void,
|
||||
stop: () => Effect.void,
|
||||
currentConfig: Effect.succeed(Option.some(config)),
|
||||
snapshot: Effect.succeed({
|
||||
desiredRunning: true,
|
||||
ready: true,
|
||||
activePid: Option.none(),
|
||||
restartAttempt: 0,
|
||||
restartScheduled: false,
|
||||
}),
|
||||
});
|
||||
|
||||
const testLayer = (httpClient: HttpClient.HttpClient) =>
|
||||
DesktopLocalEnvironmentAuth.layer.pipe(
|
||||
Layer.provide(Layer.mergeAll(managerLayer, Layer.succeed(HttpClient.HttpClient, httpClient))),
|
||||
);
|
||||
|
||||
describe("DesktopLocalEnvironmentAuth", () => {
|
||||
it.effect("exchanges the desktop bootstrap credential only once", () =>
|
||||
Effect.gen(function* () {
|
||||
const requestCount = yield* Ref.make(0);
|
||||
const layer = testLayer(
|
||||
const httpClientLayer = Layer.succeed(
|
||||
HttpClient.HttpClient,
|
||||
HttpClient.make((request) =>
|
||||
Ref.update(requestCount, (count) => count + 1).pipe(
|
||||
Effect.as(
|
||||
@@ -72,41 +54,30 @@ describe("DesktopLocalEnvironmentAuth", () => {
|
||||
),
|
||||
),
|
||||
);
|
||||
const managerLayer = Layer.succeed(DesktopBackendManager.DesktopBackendManager, {
|
||||
start: Effect.void,
|
||||
stop: () => Effect.void,
|
||||
currentConfig: Effect.succeed(Option.some(config)),
|
||||
snapshot: Effect.succeed({
|
||||
desiredRunning: true,
|
||||
ready: true,
|
||||
activePid: Option.none(),
|
||||
restartAttempt: 0,
|
||||
restartScheduled: false,
|
||||
}),
|
||||
});
|
||||
const testLayer = DesktopLocalEnvironmentAuth.layer.pipe(
|
||||
Layer.provide(Layer.mergeAll(managerLayer, httpClientLayer)),
|
||||
);
|
||||
|
||||
const [first, second] = yield* Effect.gen(function* () {
|
||||
const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth;
|
||||
return yield* Effect.all([auth.getBearerToken, auth.getBearerToken]);
|
||||
}).pipe(Effect.provide(layer));
|
||||
}).pipe(Effect.provide(testLayer));
|
||||
|
||||
assert.strictEqual(first, "desktop-bearer-token");
|
||||
assert.strictEqual(second, "desktop-bearer-token");
|
||||
assert.strictEqual(yield* Ref.get(requestCount), 1);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("preserves the backend origin and bootstrap failure cause", () =>
|
||||
Effect.gen(function* () {
|
||||
const layer = testLayer(
|
||||
HttpClient.make((request) =>
|
||||
Effect.succeed(HttpClientResponse.fromWeb(request, Response.json({ unexpected: true }))),
|
||||
),
|
||||
);
|
||||
|
||||
const error = yield* Effect.gen(function* () {
|
||||
const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth;
|
||||
return yield* auth.getBearerToken;
|
||||
}).pipe(Effect.provide(layer), Effect.flip);
|
||||
|
||||
assert.instanceOf(
|
||||
error,
|
||||
DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuthSessionBootstrapError,
|
||||
);
|
||||
assert.strictEqual(error.backendOrigin, "http://127.0.0.1:3773");
|
||||
assert.strictEqual(
|
||||
error.message,
|
||||
"Failed to create the local desktop bearer session for http://127.0.0.1:3773.",
|
||||
);
|
||||
assert.instanceOf(error.cause, RemoteEnvironmentAuthInvalidJsonError);
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -21,13 +21,10 @@ export class DesktopLocalEnvironmentAuthBackendNotConfiguredError extends Schema
|
||||
|
||||
export class DesktopLocalEnvironmentAuthSessionBootstrapError extends Schema.TaggedErrorClass<DesktopLocalEnvironmentAuthSessionBootstrapError>()(
|
||||
"DesktopLocalEnvironmentAuthSessionBootstrapError",
|
||||
{
|
||||
backendOrigin: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
{ cause: Schema.Defect() },
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to create the local desktop bearer session for ${this.backendOrigin}.`;
|
||||
return "Failed to create the local desktop bearer session.";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +60,6 @@ export const make = Effect.gen(function* () {
|
||||
return yield* new DesktopLocalEnvironmentAuthBackendNotConfiguredError();
|
||||
}
|
||||
const config = configOption.value;
|
||||
const backendOrigin = config.httpBaseUrl.origin;
|
||||
const session = yield* bootstrapRemoteBearerSession({
|
||||
httpBaseUrl: config.httpBaseUrl.href,
|
||||
credential: config.bootstrap.desktopBootstrapToken,
|
||||
@@ -76,7 +72,6 @@ export const make = Effect.gen(function* () {
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new DesktopLocalEnvironmentAuthSessionBootstrapError({
|
||||
backendOrigin,
|
||||
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