Skip to content

Commit df614ee

Browse files
committed
test(server): control the preview title deadline with fake timers
1 parent 989a080 commit df614ee

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ it.live("preview_open reports its navigation timeout instead of the request budg
440440
).pipe(Effect.provide(layer)),
441441
);
442442

443-
it.live.each([
443+
it.effect.each([
444444
{ renderer: "idle", title: "Heavy page" },
445445
{ renderer: "busy", title: null },
446446
])(
@@ -450,10 +450,14 @@ it.live.each([
450450
Effect.gen(function* () {
451451
const { broker, tabId } = yield* ready;
452452
const page = contexts[0]!.page;
453-
// A busy main thread never answers title(); the status must not wait past the broker's grace.
454-
page.title.mockImplementation(() =>
455-
renderer === "busy" ? new Promise<string>(() => {}) : Promise.resolve("Heavy page"),
456-
);
453+
const titleRequested = Promise.withResolvers<void>();
454+
// A busy main thread never answers title(); drive its native deadline explicitly.
455+
page.title.mockImplementation(() => {
456+
titleRequested.resolve();
457+
return renderer === "busy"
458+
? new Promise<string>(() => {})
459+
: Promise.resolve("Heavy page");
460+
});
457461
const heavy = "http://localhost:5173/heavy";
458462
const timedOut = (method: string, timeout: number | undefined) =>
459463
Object.assign(new Error(`${method}: Timeout ${timeout}ms exceeded.`), {
@@ -475,16 +479,24 @@ it.live.each([
475479
}
476480
});
477481

478-
const status = yield* broker.invoke<PreviewAutomationStatus>({
479-
scope,
480-
tabId,
481-
operation: "navigate",
482-
input: { url: heavy, timeoutMs: 4_000 },
483-
});
482+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
483+
const result = yield* broker
484+
.invoke<PreviewAutomationStatus>({
485+
scope,
486+
tabId,
487+
operation: "navigate",
488+
input: { url: heavy, timeoutMs: 4_000 },
489+
})
490+
.pipe(Effect.forkScoped);
491+
yield* Effect.promise(() => titleRequested.promise);
492+
if (renderer === "busy") {
493+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(250));
494+
}
495+
const status = yield* Fiber.join(result);
484496

485497
expect(status).toMatchObject({ url: heavy, title, loading: true });
486498
}),
487-
).pipe(Effect.provide(layer)),
499+
).pipe(Effect.ensuring(Effect.sync(() => vi.useRealTimers())), Effect.provide(layer)),
488500
);
489501

490502
it.live("navigate fails when the page never commits within its timeout", () =>

0 commit comments

Comments
 (0)