Skip to content

Commit 945a89a

Browse files
fix(desktop): distinguish backend unreadiness from bootstrap failures
1 parent 3a053cd commit 945a89a

2 files changed

Lines changed: 60 additions & 36 deletions

File tree

‎apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts‎

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -174,40 +174,55 @@ describe("DesktopLocalEnvironmentAuth", () => {
174174
},
175175
]),
176176
} as unknown as DesktopBackendPool.DesktopBackendPool["Service"]);
177-
it.effect("waits for backend readiness before spending the bootstrap retry window", () =>
178-
Effect.gen(function* () {
179-
const ready = yield* Deferred.make<boolean>();
180-
const requests = yield* Ref.make(0);
181-
const pool = Layer.succeed(DesktopBackendPool.DesktopBackendPool, {
182-
list: Effect.succeed([
183-
{
184-
id: PRIMARY_LOCAL_ENVIRONMENT_ID,
185-
label: Effect.succeed("Local"),
186-
currentConfig: Effect.succeedSome(config),
187-
waitForReady: () => Deferred.await(ready),
188-
},
189-
]),
190-
} as unknown as DesktopBackendPool.DesktopBackendPool["Service"]);
191-
const client = Layer.succeed(
192-
HttpClient.HttpClient,
193-
HttpClient.make((request) =>
194-
Ref.update(requests, (count) => count + 1).pipe(Effect.as(tokenResponse(request))),
195-
),
196-
);
197-
const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth.pipe(
198-
Effect.provide(
199-
DesktopLocalEnvironmentAuth.layer.pipe(Layer.provide(Layer.merge(pool, client))),
200-
),
201-
);
202-
const token = yield* auth.getBearerToken.pipe(Effect.forkChild);
203-
yield* TestClock.adjust("40 seconds");
204-
assert.strictEqual(yield* Ref.get(requests), 0);
205-
assert.isUndefined(token.pollUnsafe());
177+
it.effect.each([true, false])(
178+
"waits for backend readiness (%s) before spending the bootstrap retry window",
179+
(isReady) =>
180+
Effect.gen(function* () {
181+
const ready = yield* Deferred.make<boolean>();
182+
const requests = yield* Ref.make(0);
183+
const pool = Layer.succeed(DesktopBackendPool.DesktopBackendPool, {
184+
list: Effect.succeed([
185+
{
186+
id: PRIMARY_LOCAL_ENVIRONMENT_ID,
187+
label: Effect.succeed("Local"),
188+
currentConfig: Effect.succeedSome(config),
189+
waitForReady: () => Deferred.await(ready),
190+
},
191+
]),
192+
} as unknown as DesktopBackendPool.DesktopBackendPool["Service"]);
193+
const client = Layer.succeed(
194+
HttpClient.HttpClient,
195+
HttpClient.make((request) =>
196+
Ref.update(requests, (count) => count + 1).pipe(Effect.as(tokenResponse(request))),
197+
),
198+
);
199+
const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth.pipe(
200+
Effect.provide(
201+
DesktopLocalEnvironmentAuth.layer.pipe(Layer.provide(Layer.merge(pool, client))),
202+
),
203+
);
204+
const token = yield* auth.getBearerToken.pipe(Effect.result, Effect.forkChild);
205+
yield* TestClock.adjust("40 seconds");
206+
assert.strictEqual(yield* Ref.get(requests), 0);
207+
assert.isUndefined(token.pollUnsafe());
206208

207-
yield* Deferred.succeed(ready, true);
208-
assert.strictEqual(yield* Fiber.join(token), "desktop-bearer-token");
209-
assert.strictEqual(yield* Ref.get(requests), 1);
210-
}).pipe(Effect.provide(TestClock.layer())),
209+
yield* Deferred.succeed(ready, isReady);
210+
const result = yield* Fiber.join(token);
211+
if (isReady) {
212+
assert.strictEqual(result._tag, "Success");
213+
if (result._tag !== "Success") return assert.fail("expected a bearer token");
214+
assert.strictEqual(result.success, "desktop-bearer-token");
215+
} else {
216+
assert.strictEqual(result._tag, "Failure");
217+
if (result._tag !== "Failure") return assert.fail("expected an unreadiness error");
218+
assert.strictEqual(
219+
result.failure._tag,
220+
"DesktopLocalEnvironmentAuthBackendNotReadyError",
221+
);
222+
assert.notProperty(result.failure, "cause");
223+
}
224+
assert.strictEqual(yield* Ref.get(requests), isReady ? 1 : 0);
225+
}).pipe(Effect.provide(TestClock.layer())),
211226
);
212227

213228
const makeExchange = (failures: number, failure: () => Response) =>
@@ -269,6 +284,7 @@ describe("DesktopLocalEnvironmentAuth", () => {
269284
const error = yield* Fiber.join(fiber);
270285

271286
assert.strictEqual(error._tag, "DesktopLocalEnvironmentAuthSessionBootstrapError");
287+
assert.property(error, "cause");
272288
assert.strictEqual(yield* Ref.get(requestCount), 1);
273289
}).pipe(Effect.provide(TestClock.layer())),
274290
);

‎apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ export class DesktopLocalEnvironmentAuthBackendNotConfiguredError extends Schema
4242
}
4343
}
4444

45+
export class DesktopLocalEnvironmentAuthBackendNotReadyError extends Schema.TaggedError<DesktopLocalEnvironmentAuthBackendNotReadyError>()(
46+
"DesktopLocalEnvironmentAuthBackendNotReadyError",
47+
{},
48+
) {
49+
override get message(): string {
50+
return "Local backend did not become ready.";
51+
}
52+
}
53+
4554
export class DesktopLocalEnvironmentAuthSessionBootstrapError extends Schema.TaggedError<DesktopLocalEnvironmentAuthSessionBootstrapError>()(
4655
"DesktopLocalEnvironmentAuthSessionBootstrapError",
4756
{ cause: Schema.Defect() },
@@ -53,6 +62,7 @@ export class DesktopLocalEnvironmentAuthSessionBootstrapError extends Schema.Tag
5362

5463
export const DesktopLocalEnvironmentAuthError = Schema.Union([
5564
DesktopLocalEnvironmentAuthBackendNotConfiguredError,
65+
DesktopLocalEnvironmentAuthBackendNotReadyError,
5666
DesktopLocalEnvironmentAuthSessionBootstrapError,
5767
]);
5868
export type DesktopLocalEnvironmentAuthError = typeof DesktopLocalEnvironmentAuthError.Type;
@@ -83,9 +93,7 @@ export const make = Effect.gen(function* () {
8393
const primary = instances.find((instance) => instance.id === PRIMARY_LOCAL_ENVIRONMENT_ID);
8494
// Cold startup must not spend the HTTP retry budget before the server can accept auth.
8595
if (primary !== undefined && !(yield* primary.waitForReady(BACKEND_START_TIMEOUT))) {
86-
return yield* new DesktopLocalEnvironmentAuthSessionBootstrapError({
87-
cause: new Error("Local backend did not become ready."),
88-
});
96+
return yield* new DesktopLocalEnvironmentAuthBackendNotReadyError();
8997
}
9098
const configOption = primary === undefined ? Option.none() : yield* primary.currentConfig;
9199
if (Option.isNone(configOption)) {

0 commit comments

Comments
 (0)