Files
buzz/desktop/src/shared/lib/foregroundReady.test.mjs
Wes 59f613c404 fix(desktop): defer foreground resume work (#5696)
## Summary

- defer foreground resume work until the activation task has returned, a
frame has painted, and a trailing task gets a turn
- centralize app-focus subscribers and remove the broad TanStack
`refetchOnWindowFocus` fan-out
- coalesce relay recovery and preserve an explicit deferred refresh only
for workflow data without a polling/push freshness path
- defer the notification permission native check while keeping blur and
cheap correctness signals immediate

## Why

Buzz Desktop 0.5.10 can spend roughly 1.5 seconds in the WebKit
window-focus listener/microtask checkpoint before returning to the run
loop. Focus currently fans out into query refetches, React polling
updates, relay reconnect/replay, and native work in one activation turn.
This patch establishes an interaction-first foreground boundary rather
than letting those consumers compete with the activating input and first
paint.

## Validation

- focused foreground/workflow/relay tests: 18/18 passed before commit
- `pnpm --dir desktop typecheck`: passed before commit
- pre-commit desktop check and file-size gate: passed
- pre-push desktop check, typecheck, and full desktop unit suite:
4,743/4,743 passed at `704e7b4b6618fafce655bb2b07c7a9fe0fc8c643`
- Princess Donut independent adversarial review: PASS after two
lifecycle/freshness blockers were resolved

## Manual test

1. Install the PR build and use Buzz long enough to populate channels,
home, workflows, agents, and other polling surfaces.
2. Switch to another app for 30-60 seconds.
3. Return by clicking Buzz and immediately click a channel or scroll.
4. Confirm the first interaction and paint are prompt, then confirm
channels/home/workflows refresh and a degraded relay reconnects after
the activation boundary.
5. Repeat while rapidly switching away again to verify no resume work
starts after focus has been lost.

---------

Signed-off-by: Wes <wesbillman@users.noreply.github.com>
Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
2026-08-12 13:46:49 -07:00

95 lines
2.6 KiB
JavaScript

import assert from "node:assert/strict";
import { afterEach, describe, it } from "node:test";
import { scheduleAfterForegroundReady } from "./foregroundReady.ts";
const originalWindow = globalThis.window;
afterEach(() => {
if (originalWindow === undefined) delete globalThis.window;
else globalThis.window = originalWindow;
});
function installSchedulerStub({ animationFrame = true } = {}) {
const tasks = new Map();
const frames = new Map();
let nextId = 0;
globalThis.window = {
setTimeout(callback) {
const id = ++nextId;
tasks.set(id, callback);
return id;
},
clearTimeout(id) {
tasks.delete(id);
},
...(animationFrame
? {
requestAnimationFrame(callback) {
const id = ++nextId;
frames.set(id, callback);
return id;
},
cancelAnimationFrame(id) {
frames.delete(id);
},
}
: {}),
};
const runFirst = (queue, argument) => {
const entry = queue.entries().next().value;
assert.ok(entry, "expected queued work");
const [id, callback] = entry;
queue.delete(id);
callback(argument);
};
return {
frames,
tasks,
runTask: () => runFirst(tasks),
runFrame: () => runFirst(frames, 0),
};
}
describe("scheduleAfterForegroundReady", () => {
it("runs after task, animation frame, and trailing task", () => {
const stub = installSchedulerStub();
let calls = 0;
scheduleAfterForegroundReady(() => calls++);
assert.equal(calls, 0);
stub.runTask();
assert.equal(calls, 0);
stub.runFrame();
assert.equal(calls, 0);
stub.runTask();
assert.equal(calls, 1);
});
it("falls back to two tasks when animation frames are unavailable", () => {
const stub = installSchedulerStub({ animationFrame: false });
let calls = 0;
scheduleAfterForegroundReady(() => calls++);
stub.runTask();
assert.equal(calls, 0);
stub.runTask();
assert.equal(calls, 1);
});
it("cancels work at every scheduling stage", () => {
for (const cancelAfter of ["schedule", "task", "frame"]) {
const stub = installSchedulerStub();
let calls = 0;
const cancel = scheduleAfterForegroundReady(() => calls++);
if (cancelAfter !== "schedule") stub.runTask();
if (cancelAfter === "frame") stub.runFrame();
cancel();
assert.equal(stub.tasks.size, 0, `${cancelAfter}: tasks cleared`);
assert.equal(stub.frames.size, 0, `${cancelAfter}: frames cleared`);
assert.equal(calls, 0, `${cancelAfter}: callback suppressed`);
}
});
});