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:
1 parent
cb5aecf815
commit
544af1eeea
4 files changed
+69
-8
No files matched your search
+57
-1
@@ -17,7 +17,7 @@ const jiti = createJiti(import.meta.url, {
|
||||
},
|
||||
});
|
||||
const extension = await jiti.import("../index.ts");
|
||||
const { toModel, toCredential, tokenRequest, fetchModels, tierOf } = extension;
|
||||
const { toModel, toCredential, login, tokenRequest, fetchModels, tierOf } = extension;
|
||||
// The stub shares its private agent dir through globalThis (see stubs/pi-ai.mjs); the catalog flag lives there.
|
||||
const { agentDir } = await import("./stubs/pi-ai.mjs");
|
||||
const flagPath = join(agentDir, "nous-portal-catalog");
|
||||
@@ -56,6 +56,62 @@ test("toModel: the catalog tier is a model property, never a header, so nothing
|
||||
assert.equal(toModel({ id: "openai/gpt-5.4-nano" }).catalogTier, undefined, "untiered models carry no tier");
|
||||
});
|
||||
|
||||
// login() is driven with an auto-advancing clock: each sleep adds its ms to `now` and resolves at once, so the
|
||||
// deadline falls out of the sleeps themselves. The token endpoint is mocked; the last reply repeats, and the mock
|
||||
// gives up after 100 polls so a sleep-less regression FAILS instead of hanging (the fake clock starves the event
|
||||
// loop, so no test timeout can fire).
|
||||
const DEVICE = { device_code: "d", user_code: "USER-1", verification_uri: "https://portal.nousresearch.com/device", expires_in: 60, interval: 5 };
|
||||
const oauthError = (error) => ({ ok: false, status: 400, text: async () => JSON.stringify({ error }) });
|
||||
const token = (access_token) => ({ ok: true, text: async () => JSON.stringify({ access_token, expires_in: 3600 }) });
|
||||
const driveLogin = (t, replies, device = DEVICE) => {
|
||||
const sleeps = [], events = [];
|
||||
let now = 1_000_000, call = 0;
|
||||
t.mock.method(globalThis, "fetch", async (url) => {
|
||||
if (String(url).endsWith("/device/code")) return { ok: true, json: async () => device };
|
||||
if (call >= 100) throw new Error("runaway poll loop: 100 token polls without reaching the deadline");
|
||||
return replies[Math.min(call++, replies.length - 1)];
|
||||
});
|
||||
const clock = { now: () => now, sleep: async (ms) => { sleeps.push(ms); now += ms; } };
|
||||
return { sleeps, events, run: () => login({ notify: (event) => events.push(event), signal: new AbortController().signal }, clock) };
|
||||
};
|
||||
|
||||
test("login: shows the device code once, waits the server interval on pending and 5 s more on slow_down, then returns the credential", async (t) => {
|
||||
const { run, sleeps, events } = driveLogin(t, [oauthError("authorization_pending"), oauthError("slow_down"), token("tok")]);
|
||||
const credential = await run();
|
||||
assert.deepEqual(sleeps, [5000, 10000]);
|
||||
assert.deepEqual(events, [{ type: "device_code", userCode: "USER-1", verificationUri: "https://portal.nousresearch.com/device", intervalSeconds: 5, expiresInSeconds: 60 }]);
|
||||
assert.equal(credential.access, "tok");
|
||||
});
|
||||
|
||||
test("login: a missing interval falls back to 5 s", async (t) => {
|
||||
const { run, sleeps } = driveLogin(t, [oauthError("authorization_pending"), token("a")], { ...DEVICE, interval: undefined });
|
||||
await run();
|
||||
assert.deepEqual(sleeps, [5000]);
|
||||
});
|
||||
|
||||
test("login: a fractional interval is floored at 1 s", async (t) => {
|
||||
const { run, sleeps } = driveLogin(t, [oauthError("authorization_pending"), token("b")], { ...DEVICE, interval: 0.5 });
|
||||
await run();
|
||||
assert.deepEqual(sleeps, [1000]);
|
||||
});
|
||||
|
||||
test("login: never sleeps past the deadline; a poll that stays pending times out", async (t) => {
|
||||
const { run, sleeps } = driveLogin(t, [oauthError("authorization_pending")], { ...DEVICE, expires_in: 12 });
|
||||
await assert.rejects(run(), /Nous login timed out/);
|
||||
assert.deepEqual(sleeps, [5000, 5000, 2000]);
|
||||
});
|
||||
|
||||
test("login: a non-retryable OAuth error propagates without sleeping", async (t) => {
|
||||
const { run, sleeps } = driveLogin(t, [oauthError("expired_token")]);
|
||||
await assert.rejects(run(), /expired_token/);
|
||||
assert.deepEqual(sleeps, []);
|
||||
});
|
||||
|
||||
test("login: a non-OK device-code response throws with its status (real clock; no sleep is reached)", async (t) => {
|
||||
t.mock.method(globalThis, "fetch", async () => ({ ok: false, status: 403 }));
|
||||
await assert.rejects(login({ notify() {}, signal: new AbortController().signal }), /device code request failed: 403/);
|
||||
});
|
||||
|
||||
test("toCredential: keeps the previous refresh token when the server rotates only the access token", () => {
|
||||
const prev = { type: "oauth", access: "old", refresh: "rt_keep", expires: 0 };
|
||||
const rotated = toCredential({ access_token: "new", expires_in: 100 });
|
||||
|
||||
Reference in new issue
Block a user