test: drive login poll loop with a fake clock

Give login an optional clock ({ now, sleep }) with the real timers as
the default, so the device-code poll loop can be tested without
wall-clock time. Neither t.mock.timers nor jiti aliasing reaches
node:timers/promises, so a seam is the only way in; auth.oauth.login
still passes only the interaction, so pi's interface is unchanged.

Six tests cover pending to slow_down to success, the interval fallback
and floor, the deadline cap, a non-retryable error and a non-OK device
code response. The token mock gives up after 100 polls: an
auto-advancing fake starves the event loop, so a sleep-less regression
would otherwise hang the suite with no test timeout able to fire.
This commit is contained in:
AlexKucera committed 2026-11-25 13:50:00 +01:00
1 parent cb5aecf815
commit 544af1eeea
4 files changed
+69 -8

No files matched your search

+8 -5
View File
@@ -107,8 +107,11 @@ export function toCredential(token: TokenResponse, previous?: OAuthCredential):
};
}
type Clock = { now: () => number; sleep: (ms: number, signal?: AbortSignal) => Promise<void> };
// Production adapter: the node:timers/promises sleep, which rejects on abort. Tests pass an auto-advancing fake.
const realClock: Clock = { now: Date.now, sleep: (ms, signal) => sleep(ms, undefined, { signal }) };
async function login(interaction: ProviderAuthInteraction): Promise<OAuthCredential> {
export async function login(interaction: ProviderAuthInteraction, clock: Clock = realClock): Promise<OAuthCredential> {
const { signal } = interaction;
const response = await fetch(`${PORTAL}/api/oauth/device/code`, {
method: "POST",
@@ -130,14 +133,14 @@ async function login(interaction: ProviderAuthInteraction): Promise<OAuthCredent
// pi's dialog shows the code and opens the browser on Enter. Do NOT prompt here: a prompt clears that
// confirm action (login-dialog.js showPrompt → clearDeviceCodeAction), so Enter would open nothing.
const deadline = Date.now() + device.expires_in * 1000;
const deadline = clock.now() + device.expires_in * 1000;
// Floor at 1 s and reject non-finite values like pi-ai's own poller: a missing, zero, non-numeric or
// overflowing interval must not become a tight loop (Node clamps an overflowing setTimeout to 1 ms).
const requested = Number(device.interval);
let interval = Number.isFinite(requested) && requested > 0 ? Math.max(requested, 1) : 5;
// Never sleep past the deadline.
const nap = () => sleep(Math.min(interval * 1000, deadline - Date.now()), undefined, { signal });
while (Date.now() < deadline) {
const nap = () => clock.sleep(Math.min(interval * 1000, deadline - clock.now()), signal);
while (clock.now() < deadline) {
try {
return toCredential(
await tokenRequest({ grant_type: "urn:ietf:params:oauth:grant-type:device_code", device_code: device.device_code }, signal),
@@ -161,7 +164,7 @@ const auth: ProviderAuth = {
name: "Nous Portal (subscription)",
isSubscription: true,
loginLabel: "Sign in to Nous Portal (subscription)",
login,
login: (interaction) => login(interaction),
refresh: (credential, signal) =>
tokenRequest({ grant_type: "refresh_token", refresh_token: credential.refresh }, signal).then((t) => toCredential(t, credential)),
// The access token is the inference credential; no agent key is minted.