import { get } from "node:http"; import { afterEach, describe, expect, it, vi } from "vitest"; import { startLoopback, type LoopbackHandle } from "./loopback.js"; describe("auth/loopback", () => { let active: LoopbackHandle | null = null; afterEach(async () => { vi.useRealTimers(); if (active) { await active.close().catch(() => {}); active = null; } }); it("captures `code` when state matches", async () => { const handle = await startLoopback({ state: "expected_state", timeoutMs: 5_000 }); active = handle; const redirect = new URL(handle.redirectUri); const callback = new URL(`${handle.redirectUri}?code=abc123&state=expected_state`); const fetchPromise = fetch(callback.toString()); const result = await handle.result; const res = await fetchPromise; expect(result.code).toBe("abc123"); expect(result.redirectUri).toContain(redirect.host); expect(res.status).toBe(200); const body = await res.text(); expect(body).toContain("You're connected"); }); async function expectRejection(args: { expectedState: string; query: string; pattern: RegExp; }): Promise { const handle = await startLoopback({ state: args.expectedState, timeoutMs: 5_000 }); active = handle; await fetch(`${handle.redirectUri}?${args.query}`).catch(() => {}); await expect(handle.result).rejects.toThrow(args.pattern); } it.each([ ["a wrong state", "code=abc&state=wrong"], ["no state", "code=abc"], ["an error and no state", "error=access_denied&error_description=denied"], ["an error and a wrong state", "error=server_error&state=wrong"], ])("answers a callback with %s with a 400 and keeps waiting", async (_label, query) => { const handle = await startLoopback({ state: "expected", timeoutMs: 5_000 }); active = handle; const settled = vi.fn(); handle.result.then(settled, settled); const res = await fetch(`${handle.redirectUri}?${query}`); expect(res.status).toBe(400); expect(await res.text()).toContain("Your terminal is still waiting"); await new Promise((resolve) => setTimeout(resolve, 20)); expect(settled).not.toHaveBeenCalled(); await fetch(`${handle.redirectUri}?code=the_code&state=expected`); await expect(handle.result).resolves.toMatchObject({ code: "the_code" }); }); it("rejects when the IdP returns an error", async () => { await expectRejection({ expectedState: "s", query: "error=access_denied&error_description=user+denied&state=s", pattern: /access_denied/, }); }); it("shows control characters in the IdP error as visible escapes", async () => { const handle = await startLoopback({ state: "s", timeoutMs: 5_000 }); active = handle; const failed = handle.result.catch((error: Error) => error.message); await fetch( `${handle.redirectUri}?error=server%1B%5D0%3Bx%07&error_description=a%0D%0Ab%1B%5B2J&state=s`, ); expect(await failed).toBe( "OAuth authorize returned error: server\\x1b]0;x\\x07 — a\\x0d\\x0ab\\x1b[2J", ); }); async function pageFor(query: string): Promise { const handle = await startLoopback({ state: "s", timeoutMs: 5_000 }); active = handle; handle.result.catch(() => {}); const res = await fetch(`${handle.redirectUri}?${query}`); expect(res.status).toBe(400); return res.text(); } it("says Not authorized only when the person pressed Cancel", async () => { const body = await pageFor("error=access_denied&state=s"); expect(body).toContain("Not authorized"); expect(body).not.toContain("Couldn't connect"); }); it("says Couldn't connect, with the code, for a failure that is not a Cancel", async () => { const body = await pageFor("error=server_error&error_description=try+later&state=s"); expect(body).toContain("Couldn't connect"); expect(body).toContain("server_error"); expect(body).toContain("try later"); expect(body).not.toContain("Not authorized"); }); it("rejects when code is missing from the callback", async () => { await expectRejection({ expectedState: "s", query: "state=s", pattern: /code/ }); }); it("times out when no callback arrives", async () => { const handle = await startLoopback({ state: "s", timeoutMs: 200 }); active = handle; await expect(handle.result).rejects.toThrow(/timed out/i); }); it("404s non-callback paths and does not resolve the flow", async () => { const handle = await startLoopback({ state: "s", timeoutMs: 1_000 }); active = handle; const res = await fetch(`${handle.redirectUri.replace("/oauth/callback", "/other")}`); expect(res.status).toBe(404); // Flow is still waiting — kill it via timeout. await expect(handle.result).rejects.toThrow(/timed out/i); }); // Literal on purpose: pins the default. Sign-in can include Stripe checkout. const CHECKOUT_WINDOW_MS = 60 * 60 * 1000; it("accepts a callback after a slow checkout and leaves no timer behind", async () => { vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); const handle = await startLoopback({ state: "s" }); active = handle; await vi.advanceTimersByTimeAsync(CHECKOUT_WINDOW_MS - 1_000); // node:http, not fetch: undici registers its own timers, which would pollute the count. get(`${handle.redirectUri}?code=the_code&state=s`).on("error", () => {}); await expect(handle.result).resolves.toMatchObject({ code: "the_code" }); // Checked before any explicit close(): the flow must clean up after itself on success, // or the CLI stays alive for the whole window after "Signed in". expect(vi.getTimerCount()).toBe(0); await handle.close(); }); it("gives up at the checkout window, not before", async () => { vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); const handle = await startLoopback({ state: "s" }); active = handle; const settled = vi.fn(); handle.result.catch(settled); await vi.advanceTimersByTimeAsync(CHECKOUT_WINDOW_MS - 1); expect(settled).not.toHaveBeenCalled(); expect(handle.timeoutLabel).toBe("60 minutes"); const rejected = expect(handle.result).rejects.toThrow(/timed out after 60 minutes/i); await vi.advanceTimersByTimeAsync(1); await rejected; }); });