import { describe, expect, it, vi } from "vitest";
import { routeCliCommandInput } from "./command-routing.js";
function handlers(calls) {
    return {
        pixel: () => calls.push("pixel"),
        mcp: () => calls.push("mcp"),
        login: () => calls.push("login"),
        logout: () => calls.push("logout"),
        sessions: () => calls.push("sessions"),
        telegram: () => calls.push("telegram"),
        serve: () => calls.push("serve"),
        doctor: () => calls.push("doctor"),
        "agent-home-login": () => calls.push("agent-home-login"),
        "agent-home": () => calls.push("agent-home"),
    };
}
describe("routeCliCommandInput", () => {
    it("prints top-level help before dispatching subcommands", () => {
        const argv = ["node", "ezcoder", "login", "--help"];
        const printHelp = vi.fn();
        const exit = vi.fn((code) => {
            throw new Error(`exit:${code}`);
        });
        expect(() => routeCliCommandInput({ argv, printHelp, exit, handlers: handlers([]) })).toThrow("exit:0");
        expect(printHelp).toHaveBeenCalledOnce();
    });
    it("dispatches subcommands and strips command tokens only for modes that parse their own flags", () => {
        const calls = [];
        const sessionsArgv = ["node", "ezcoder", "sessions", "--foo"];
        const loginArgv = ["node", "ezcoder", "login", "--foo"];
        expect(routeCliCommandInput({
            argv: sessionsArgv,
            printHelp: vi.fn(),
            exit: vi.fn(),
            handlers: handlers(calls),
        })).toEqual({ kind: "handled", subcommand: "sessions" });
        expect(sessionsArgv).toEqual(["node", "ezcoder", "--foo"]);
        expect(routeCliCommandInput({
            argv: loginArgv,
            printHelp: vi.fn(),
            exit: vi.fn(),
            handlers: handlers(calls),
        })).toEqual({ kind: "handled", subcommand: "login" });
        expect(loginArgv).toEqual(["node", "ezcoder", "login", "--foo"]);
        expect(calls).toEqual(["sessions", "login"]);
    });
    it("converts continue into a later TUI resume route without handling it as a subcommand", () => {
        const argv = ["node", "ezcoder", "continue", "--model", "x"];
        const calls = [];
        expect(routeCliCommandInput({
            argv,
            printHelp: vi.fn(),
            exit: vi.fn(),
            handlers: handlers(calls),
        })).toEqual({ kind: "continue" });
        expect(argv).toEqual(["node", "ezcoder", "--model", "x"]);
        expect(calls).toEqual([]);
    });
});
//# sourceMappingURL=command-routing.test.js.map