Skip to content

Commit 6989a2e

Browse files
committed
test(nous-oauth): cover device-flow error paths and refresh-token fallback
- access_denied / expired_token surface as terminal NousTokenError - slow_down backs off (interval bump) then resumes polling to success - authorization_pending until deadline raises a timed-out error - refresh omitting a new refresh_token keeps the previous one (header sent)
1 parent 3ea4288 commit 6989a2e

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

‎tests/nous-oauth.test.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,129 @@ describe("Nous token-response wiring", () => {
124124
});
125125
});
126126

127+
describe("Nous device-flow error handling", () => {
128+
const realFetch = globalThis.fetch;
129+
130+
beforeEach(() => {
131+
previousPortalBase = process.env.NOUS_PORTAL_BASE_URL;
132+
process.env.NOUS_PORTAL_BASE_URL = TEST_PORTAL;
133+
});
134+
135+
afterEach(() => {
136+
globalThis.fetch = realFetch;
137+
if (previousPortalBase === undefined) delete process.env.NOUS_PORTAL_BASE_URL;
138+
else process.env.NOUS_PORTAL_BASE_URL = previousPortalBase;
139+
});
140+
141+
function deviceFlowFetch(respond: (grant: string | null) => Response): typeof fetch {
142+
return (async (input: RequestInfo | URL, init?: RequestInit) => {
143+
const url = String(input);
144+
if (url.endsWith("/api/oauth/device/code")) {
145+
return new Response(JSON.stringify({
146+
device_code: "dev-123",
147+
user_code: "ABCD-EFGH",
148+
verification_uri_complete: "https://portal.nousresearch.com/activate?code=ABCD-EFGH",
149+
expires_in: 600,
150+
interval: 1,
151+
}), { status: 200 });
152+
}
153+
return respond(new URLSearchParams(init?.body as string).get("grant_type"));
154+
}) as typeof fetch;
155+
}
156+
157+
test("access_denied surfaces as a terminal NousTokenError", async () => {
158+
globalThis.fetch = deviceFlowFetch(() =>
159+
new Response(JSON.stringify({ error: "access_denied", error_description: "User denied the request" }), { status: 400 }),
160+
);
161+
const ctrl: OAuthController = { onAuth() {} };
162+
await expect(loginNous(ctrl)).rejects.toThrow("Nous Portal device authorization denied");
163+
});
164+
165+
test("expired_token surfaces as a terminal NousTokenError", async () => {
166+
globalThis.fetch = deviceFlowFetch(() =>
167+
new Response(JSON.stringify({ error: "expired_token", error_description: "Code expired" }), { status: 400 }),
168+
);
169+
const ctrl: OAuthController = { onAuth() {} };
170+
await expect(loginNous(ctrl)).rejects.toThrow("Nous Portal device authorization expired");
171+
});
172+
173+
test("slow_down backs off and resumes polling until success", async () => {
174+
let pollCount = 0;
175+
const access = jwtWithClaims({ sub: "device-user", exp: Math.floor(Date.now() / 1000) + 3600 });
176+
globalThis.fetch = deviceFlowFetch(() => {
177+
pollCount += 1;
178+
if (pollCount === 1) {
179+
return new Response(JSON.stringify({ error: "slow_down", interval: 1 }), { status: 400 });
180+
}
181+
return new Response(JSON.stringify({
182+
access_token: access,
183+
refresh_token: "device-refresh",
184+
expires_in: 3600,
185+
}), { status: 200 });
186+
});
187+
const ctrl: OAuthController = { onAuth() {} };
188+
const cred = await loginNous(ctrl);
189+
expect(pollCount).toBe(2);
190+
expect(cred.access).toBe(access);
191+
expect(cred.refresh).toBe("device-refresh");
192+
expect(cred.accountId).toBe("device-user");
193+
}, 15_000);
194+
195+
test("device flow times out when the server never authorizes before the deadline", async () => {
196+
// The deadline comes from the device-code response: keep it tiny so the
197+
// polling loop exits quickly instead of running for the full server TTL.
198+
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
199+
const url = String(input);
200+
if (url.endsWith("/api/oauth/device/code")) {
201+
return new Response(JSON.stringify({
202+
device_code: "dev-123",
203+
user_code: "ABCD-EFGH",
204+
verification_uri_complete: "https://portal.nousresearch.com/activate?code=ABCD-EFGH",
205+
expires_in: 1,
206+
interval: 1,
207+
}), { status: 200 });
208+
}
209+
return new Response(JSON.stringify({ error: "authorization_pending" }), { status: 400 });
210+
}) as typeof fetch;
211+
const ctrl: OAuthController = { onAuth() {} };
212+
await expect(loginNous(ctrl)).rejects.toThrow("Nous Portal device flow timed out");
213+
}, 15_000);
214+
});
215+
216+
describe("Nous refresh fallback", () => {
217+
const realFetch = globalThis.fetch;
218+
219+
beforeEach(() => {
220+
previousPortalBase = process.env.NOUS_PORTAL_BASE_URL;
221+
process.env.NOUS_PORTAL_BASE_URL = TEST_PORTAL;
222+
});
223+
224+
afterEach(() => {
225+
globalThis.fetch = realFetch;
226+
if (previousPortalBase === undefined) delete process.env.NOUS_PORTAL_BASE_URL;
227+
else process.env.NOUS_PORTAL_BASE_URL = previousPortalBase;
228+
});
229+
230+
test("keeps the previous refresh token when the response omits a new one", async () => {
231+
const access = jwtWithClaims({ sub: "wired-user", exp: Math.floor(Date.now() / 1000) + 3600 });
232+
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
233+
const observedHeader = (init?.headers as Record<string, string> | undefined)?.["x-nous-refresh-token"];
234+
expect(observedHeader).toBe("old-refresh");
235+
return new Response(JSON.stringify({
236+
access_token: access,
237+
expires_in: 3600,
238+
// no refresh_token field on purpose
239+
}), { status: 200 });
240+
}) as typeof fetch;
241+
242+
const cred = await refreshNousToken("old-refresh");
243+
244+
expect(cred.access).toBe(access);
245+
expect(cred.refresh).toBe("old-refresh");
246+
expect(cred.accountId).toBe("wired-user");
247+
});
248+
});
249+
127250
describe("Nous multiauth via saveCredential", () => {
128251
beforeEach(() => {
129252
previousOpencodexHome = process.env.OPENCODEX_HOME;

0 commit comments

Comments
 (0)