test: close ≥90% per-file coverage gaps for mid-session auth
The mid-session auth work passed `validate`/CI but failed the local-only
`npm run test:coverage` per-file gate (≥90% lines/statements/functions/
branches) on 12 files. Add targeted unit/integration tests so every changed
file clears the gate; annotate provably-dead defensive branches with justified
`/* v8 ignore */` comments rather than relaxing the gate.
Files brought to ≥90 on all four dimensions:
- core/auth: mcpAuth, oauthUx, utils, challenge, node/runner-interactive-oauth
- core/mcp: oauthManager, inspectorClient, node/authChallengeFetch,
remote/remoteClientTransport, remote/node/{remote-session,server}
- clients/web/src/utils: oauthResume, browserTabVisibility
`npm run test:coverage` now passes (3575 tests, 0 threshold failures);
`npm run validate` green across all four clients.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DZyeWManYTvcXxjcrhxSQc
This commit is contained in:
parent
a066873e37
commit
f4b6095719
1 changed files with 3447 additions and 37 deletions
|
|
@ -41,4 +41,83 @@ describe("browserTabVisibility", () => {
|
|||
handler!();
|
||||
expect(callback).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("returned unsubscribe removes the visibilitychange listener", () => {
|
||||
let handler: (() => void) | undefined;
|
||||
vi.mocked(document.addEventListener).mockImplementation((_event, fn) => {
|
||||
handler = fn as () => void;
|
||||
});
|
||||
|
||||
const callback = vi.fn();
|
||||
const unsubscribe = onBrowserTabVisible(callback);
|
||||
expect(handler).toBeDefined();
|
||||
|
||||
unsubscribe();
|
||||
expect(document.removeEventListener).toHaveBeenCalledWith(
|
||||
"visibilitychange",
|
||||
handler,
|
||||
);
|
||||
|
||||
// After unsubscribing the stored handler must no longer fire the callback
|
||||
// through the (now removed) listener path.
|
||||
vi.mocked(document.addEventListener).mockClear();
|
||||
expect(document.addEventListener).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("integrates with a real document: dispatch, hidden/visible, and unsubscribe", () => {
|
||||
vi.unstubAllGlobals();
|
||||
const original = Object.getOwnPropertyDescriptor(
|
||||
Document.prototype,
|
||||
"visibilityState",
|
||||
);
|
||||
const setVisibility = (value: string): void => {
|
||||
Object.defineProperty(document, "visibilityState", {
|
||||
configurable: true,
|
||||
get: () => value,
|
||||
});
|
||||
};
|
||||
|
||||
try {
|
||||
setVisibility("visible");
|
||||
expect(isBrowserTabVisible()).toBe(true);
|
||||
|
||||
const callback = vi.fn();
|
||||
const unsubscribe = onBrowserTabVisible(callback);
|
||||
|
||||
// Hidden branch: listener fires but callback is not invoked.
|
||||
setVisibility("hidden");
|
||||
document.dispatchEvent(new Event("visibilitychange"));
|
||||
expect(callback).not.toHaveBeenCalled();
|
||||
expect(isBrowserTabVisible()).toBe(false);
|
||||
|
||||
// Visible branch: callback invoked.
|
||||
setVisibility("visible");
|
||||
document.dispatchEvent(new Event("visibilitychange"));
|
||||
expect(callback).toHaveBeenCalledOnce();
|
||||
|
||||
// Unsubscribe: further events must not re-invoke the callback.
|
||||
unsubscribe();
|
||||
document.dispatchEvent(new Event("visibilitychange"));
|
||||
expect(callback).toHaveBeenCalledOnce();
|
||||
} finally {
|
||||
if (original) {
|
||||
Object.defineProperty(document, "visibilityState", original);
|
||||
} else {
|
||||
delete (document as { visibilityState?: string }).visibilityState;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("no-ops safely when document is undefined (SSR guard)", () => {
|
||||
vi.stubGlobal("document", undefined);
|
||||
|
||||
expect(isBrowserTabVisible()).toBe(false);
|
||||
|
||||
const callback = vi.fn();
|
||||
const unsubscribe = onBrowserTabVisible(callback);
|
||||
expect(typeof unsubscribe).toBe("function");
|
||||
// The returned no-op must be callable without throwing.
|
||||
expect(() => unsubscribe()).not.toThrow();
|
||||
expect(callback).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue