Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b98359c8b |
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,
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user