|
| 1 | +import { test, expect, describe } from "bun:test"; |
| 2 | +import { mkdtemp, rm } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { startMetaLogin } from "./login.js"; |
| 6 | +import { loadMetaProfile } from "../../config/oauth-stores.js"; |
| 7 | + |
| 8 | +const AUTH_URL = "https://auth.meta.com/oidc/device/authorization/"; |
| 9 | +const TOKEN_URL = "https://auth.meta.com/oidc/device/token/"; |
| 10 | +const MINT_URL = "https://api.meta.ai/muse-code/key"; |
| 11 | + |
| 12 | +function jsonResponse(body: unknown, status = 200): Response { |
| 13 | + return new Response(JSON.stringify(body), { |
| 14 | + status, |
| 15 | + headers: { "content-type": "application/json" }, |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function noopSleep(): Promise<void> { |
| 20 | + return Promise.resolve(); |
| 21 | +} |
| 22 | + |
| 23 | +async function withHome<T>(fn: (home: string) => Promise<T>): Promise<T> { |
| 24 | + const home = await mkdtemp(join(tmpdir(), "meta-login-")); |
| 25 | + try { |
| 26 | + return await fn(home); |
| 27 | + } finally { |
| 28 | + await rm(home, { recursive: true, force: true }); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +describe("startMetaLogin (device flow)", () => { |
| 33 | + test("authorize → poll pending → complete → mint → saved profile", async () => { |
| 34 | + await withHome(async (home) => { |
| 35 | + const requests: string[] = []; |
| 36 | + const fetchImpl = (async (input: RequestInfo | URL) => { |
| 37 | + const url = String(input); |
| 38 | + requests.push(url); |
| 39 | + if (url === AUTH_URL) { |
| 40 | + return jsonResponse({ |
| 41 | + device_code: "dc-1", |
| 42 | + user_code: "ABCD-1234", |
| 43 | + verification_uri_complete: |
| 44 | + "https://auth.meta.com/device?code=ABCD-1234", |
| 45 | + interval: 1, |
| 46 | + expires_in: 1800, |
| 47 | + }); |
| 48 | + } |
| 49 | + if (url === TOKEN_URL) { |
| 50 | + return jsonResponse({ |
| 51 | + access_token: "identity-token", |
| 52 | + token_type: "bearer", |
| 53 | + }); |
| 54 | + } |
| 55 | + if (url === MINT_URL) { |
| 56 | + return jsonResponse({ api_key: "LLM|minted" }); |
| 57 | + } |
| 58 | + throw new Error(`unexpected fetch ${url}`); |
| 59 | + }) as unknown as typeof fetch; |
| 60 | + |
| 61 | + let notified: { verificationUri: string; userCode: string } | undefined; |
| 62 | + const controller = new AbortController(); |
| 63 | + const handle = startMetaLogin({ |
| 64 | + profile: "work", |
| 65 | + signal: controller.signal, |
| 66 | + home, |
| 67 | + fetchImpl, |
| 68 | + sleep: noopSleep, |
| 69 | + notify: (event) => { |
| 70 | + notified = { |
| 71 | + verificationUri: event.verificationUri, |
| 72 | + userCode: event.userCode, |
| 73 | + }; |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + const staged = await handle.completed; |
| 78 | + expect(notified).toEqual({ |
| 79 | + verificationUri: "https://auth.meta.com/device?code=ABCD-1234", |
| 80 | + userCode: "ABCD-1234", |
| 81 | + }); |
| 82 | + expect(staged.profile.name).toBe("work"); |
| 83 | + expect(staged.profile.tokens.access).toBe("LLM|minted"); |
| 84 | + expect(staged.profile.tokens.refresh).toBe("identity-token"); |
| 85 | + |
| 86 | + await staged.commit(); |
| 87 | + const saved = await loadMetaProfile("work", home); |
| 88 | + expect(saved?.tokens.access).toBe("LLM|minted"); |
| 89 | + expect(saved?.tokens.refresh).toBe("identity-token"); |
| 90 | + expect(requests).toEqual([AUTH_URL, TOKEN_URL, MINT_URL]); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + test("abort rejects with the cancelled-login message", async () => { |
| 95 | + const fetchImpl = (async (input: RequestInfo | URL, init?: RequestInit) => { |
| 96 | + const url = String(input); |
| 97 | + if (url === AUTH_URL) { |
| 98 | + return jsonResponse({ |
| 99 | + device_code: "dc-1", |
| 100 | + user_code: "ABCD-1234", |
| 101 | + verification_uri_complete: "https://auth.meta.com/device", |
| 102 | + interval: 1, |
| 103 | + expires_in: 1800, |
| 104 | + }); |
| 105 | + } |
| 106 | + // Token poll parks until the caller aborts, then rejects like real fetch. |
| 107 | + return new Promise<Response>((_resolve, reject) => { |
| 108 | + init?.signal?.addEventListener( |
| 109 | + "abort", |
| 110 | + () => reject(new DOMException("aborted", "AbortError")), |
| 111 | + { once: true }, |
| 112 | + ); |
| 113 | + }); |
| 114 | + }) as unknown as typeof fetch; |
| 115 | + |
| 116 | + const controller = new AbortController(); |
| 117 | + const handle = startMetaLogin({ |
| 118 | + profile: "work", |
| 119 | + signal: controller.signal, |
| 120 | + fetchImpl, |
| 121 | + sleep: noopSleep, |
| 122 | + }); |
| 123 | + |
| 124 | + const pending = handle.completed.catch((err: unknown) => err); |
| 125 | + setTimeout(() => controller.abort(), 0); |
| 126 | + const err = await pending; |
| 127 | + expect(err).toBeInstanceOf(Error); |
| 128 | + expect((err as Error).message).toBe("Login cancelled"); |
| 129 | + }); |
| 130 | +}); |
0 commit comments