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