Files
hyperframes/packages/cli/src/auth/loopback.test.ts
T
Vance IngallsandClaude Opus 5.5 34ddced2d0 fix(cli): keep browser sign-in open through checkout (#6879)
* fix(cli): keep browser sign-in open through checkout

The loopback listener timed out after 5 minutes, but plan checkout runs
inside the OAuth flow. A slower purchase ended on ERR_CONNECTION_REFUSED.
Raise the default to 60 minutes, matching pacific's plan-checkout handoff.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* fix(cli): show the sign-in limit and how to recover

The waiting line now states the 60-minute limit and Ctrl+C. After a
timed-out browser login the CLI says to run auth login again.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* fix(cli): drop unverified purchase claim, harden timer test, document limit

Review fixes: the timeout hint no longer says purchases stay on the account
(the CLI cannot observe billing); the no-timer-left test now asserts before
any explicit close() and uses node:http so undici timers cannot pollute the
count; docs mention the 60-minute window and Ctrl+C.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* fix(cli): close the sign-in listener on every path and record Ctrl+C

Ctrl+C is now the advertised exit, but events only flush on exit, which a
default SIGINT skips, so an abandoned login vanished from telemetry. Record
it as aborted and exit 130 through requestCliExit. Close the listener on any
throw, so a failure before the wait can't hold the process for the full
window. Derive the waiting line and timeout message from the listener's own
limit, and name the constant for why it is long, which clears the comment
ratchet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 12:29:35 -08:00

110 lines
4.1 KiB
TypeScript

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("Signed in");
});
async function expectRejection(args: {
expectedState: string;
query: string;
pattern: RegExp;
}): Promise<void> {
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("rejects when state does not match", async () => {
await expectRejection({
expectedState: "expected",
query: "code=abc&state=wrong",
pattern: /state mismatch/i,
});
});
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("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;
});
});