import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { Readable } from "node:stream";
vi.mock("../../utils/logger.js", () => ({
    logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
}));
import { manualCodeEntry, promptForCode } from "../manual-flow.js";
describe("manualCodeEntry", () => {
    let originalStdin;
    let mockStdin;
    beforeEach(() => {
        originalStdin = process.stdin;
        mockStdin = new Readable({ read() { } });
        Object.defineProperty(process, "stdin", { value: mockStdin, writable: true });
    });
    afterEach(() => {
        Object.defineProperty(process, "stdin", { value: originalStdin, writable: true });
    });
    it("should display auth URL and return pasted code", async () => {
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        const codePromise = manualCodeEntry("https://auth.example.com/authorize?provider=github");
        // Simulate user typing a code
        mockStdin.push("my-auth-code-123\n");
        const code = await codePromise;
        expect(code).toBe("my-auth-code-123");
        // Verify the auth URL was displayed
        const allOutput = consoleSpy.mock.calls.map((c) => c.join(" ")).join("\n");
        expect(allOutput).toContain("https://auth.example.com/authorize?provider=github");
        expect(allOutput).toContain("browser");
        consoleSpy.mockRestore();
    });
    it("should trim whitespace from pasted code", async () => {
        vi.spyOn(console, "log").mockImplementation(() => { });
        const codePromise = manualCodeEntry("https://auth.example.com/authorize");
        mockStdin.push("  my-code-with-spaces  \n");
        const code = await codePromise;
        expect(code).toBe("my-code-with-spaces");
        vi.restoreAllMocks();
    });
    it("should throw if empty code is provided", async () => {
        vi.spyOn(console, "log").mockImplementation(() => { });
        const codePromise = manualCodeEntry("https://auth.example.com/authorize");
        mockStdin.push("\n");
        await expect(codePromise).rejects.toThrow("No authorization code provided");
        vi.restoreAllMocks();
    });
    it("should throw if only whitespace is provided", async () => {
        vi.spyOn(console, "log").mockImplementation(() => { });
        const codePromise = manualCodeEntry("https://auth.example.com/authorize");
        mockStdin.push("   \n");
        await expect(codePromise).rejects.toThrow("No authorization code provided");
        vi.restoreAllMocks();
    });
});
describe("promptForCode", () => {
    let originalStdin;
    let mockStdin;
    beforeEach(() => {
        originalStdin = process.stdin;
        mockStdin = new Readable({ read() { } });
        Object.defineProperty(process, "stdin", { value: mockStdin, writable: true });
    });
    afterEach(() => {
        Object.defineProperty(process, "stdin", { value: originalStdin, writable: true });
    });
    it("should read a line from stdin", async () => {
        const codePromise = promptForCode();
        mockStdin.push("the-code\n");
        const result = await codePromise;
        expect(result).toBe("the-code");
    });
});
//# sourceMappingURL=manual-flow.test.js.map