#!/usr/bin/env node
import {
  CONFIRM_INPUT_DELAY_MS,
  ENTER,
  KEY_N,
  KEY_Y,
  RENDER_DELAY_MS,
  delay
} from "../../chunk-PUT7X3GA.js";
import {
  render
} from "../../chunk-66UDJBF6.js";
import {
  afterEach,
  describe,
  globalExpect,
  it,
  vi
} from "../../chunk-XY3XDVMI.js";
import {
  Confirm
} from "../../chunk-N6S7ZRIL.js";
import {
  init_esm_shims
} from "../../chunk-DHET7RCE.js";

// src/cli/components/common/confirm.test.tsx
init_esm_shims();
import { jsx } from "react/jsx-runtime";
describe("Confirm component", () => {
  let cleanup;
  afterEach(() => {
    cleanup?.();
    cleanup = void 0;
  });
  describe("rendering", () => {
    it("should render the message", () => {
      const { lastFrame, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Do you want to proceed?", onConfirm: () => {
        }, onCancel: () => {
        } })
      );
      cleanup = unmount;
      const output = lastFrame();
      globalExpect(output).toContain("Do you want to proceed?");
    });
    it("should show y/n prompt", () => {
      const { lastFrame, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Continue?", onConfirm: () => {
        }, onCancel: () => {
        } })
      );
      cleanup = unmount;
      const output = lastFrame();
      globalExpect(output?.toLowerCase()).toMatch(/[yn]/);
    });
    it("should show Y/n when default is confirm (true)", () => {
      const { lastFrame, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Confirm?", onConfirm: () => {
        }, onCancel: () => {
        }, defaultValue: true })
      );
      cleanup = unmount;
      const output = lastFrame();
      globalExpect(output).toContain("Y/n");
    });
    it("should show y/N when default is cancel (false)", () => {
      const { lastFrame, unmount } = render(
        /* @__PURE__ */ jsx(
          Confirm,
          {
            message: "Confirm?",
            onConfirm: () => {
            },
            onCancel: () => {
            },
            defaultValue: false
          }
        )
      );
      cleanup = unmount;
      const output = lastFrame();
      globalExpect(output).toContain("y/N");
    });
  });
  describe("keyboard interactions", () => {
    it("should call onConfirm when pressing y", async () => {
      const onConfirm = vi.fn();
      const onCancel = vi.fn();
      const { stdin, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Confirm?", onConfirm, onCancel })
      );
      cleanup = unmount;
      await delay(RENDER_DELAY_MS);
      await stdin.write(KEY_Y);
      await delay(CONFIRM_INPUT_DELAY_MS);
      globalExpect(onConfirm).toHaveBeenCalled();
      globalExpect(onCancel).not.toHaveBeenCalled();
    });
    it("should call onCancel when pressing n", async () => {
      const onConfirm = vi.fn();
      const onCancel = vi.fn();
      const { stdin, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Confirm?", onConfirm, onCancel })
      );
      cleanup = unmount;
      await delay(RENDER_DELAY_MS);
      await stdin.write(KEY_N);
      await delay(CONFIRM_INPUT_DELAY_MS);
      globalExpect(onCancel).toHaveBeenCalled();
      globalExpect(onConfirm).not.toHaveBeenCalled();
    });
    it("should call onConfirm when default is true and enter pressed", async () => {
      const onConfirm = vi.fn();
      const onCancel = vi.fn();
      const { stdin, unmount } = render(
        /* @__PURE__ */ jsx(
          Confirm,
          {
            message: "Confirm?",
            onConfirm,
            onCancel,
            defaultValue: true
          }
        )
      );
      cleanup = unmount;
      await delay(RENDER_DELAY_MS);
      await stdin.write(ENTER);
      await delay(CONFIRM_INPUT_DELAY_MS);
      globalExpect(onConfirm).toHaveBeenCalled();
      globalExpect(onCancel).not.toHaveBeenCalled();
    });
    it("should call onCancel when default is false and enter pressed", async () => {
      const onConfirm = vi.fn();
      const onCancel = vi.fn();
      const { stdin, unmount } = render(
        /* @__PURE__ */ jsx(
          Confirm,
          {
            message: "Confirm?",
            onConfirm,
            onCancel,
            defaultValue: false
          }
        )
      );
      cleanup = unmount;
      await delay(RENDER_DELAY_MS);
      await stdin.write(ENTER);
      await delay(CONFIRM_INPUT_DELAY_MS);
      globalExpect(onCancel).toHaveBeenCalled();
      globalExpect(onConfirm).not.toHaveBeenCalled();
    });
    it("should handle uppercase Y", async () => {
      const onConfirm = vi.fn();
      const { stdin, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Confirm?", onConfirm, onCancel: () => {
        } })
      );
      cleanup = unmount;
      await delay(RENDER_DELAY_MS);
      await stdin.write("Y");
      await delay(CONFIRM_INPUT_DELAY_MS);
      globalExpect(onConfirm).toHaveBeenCalled();
    });
    it("should handle uppercase N", async () => {
      const onCancel = vi.fn();
      const { stdin, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Confirm?", onConfirm: () => {
        }, onCancel })
      );
      cleanup = unmount;
      await delay(RENDER_DELAY_MS);
      await stdin.write("N");
      await delay(CONFIRM_INPUT_DELAY_MS);
      globalExpect(onCancel).toHaveBeenCalled();
    });
  });
  describe("default value", () => {
    it("should default to false when not specified", () => {
      const { lastFrame, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Confirm?", onConfirm: () => {
        }, onCancel: () => {
        } })
      );
      cleanup = unmount;
      const output = lastFrame();
      globalExpect(output).toContain("y/N");
    });
    it("should render with specified default value of true", () => {
      const { lastFrame, unmount } = render(
        /* @__PURE__ */ jsx(Confirm, { message: "Confirm?", onConfirm: () => {
        }, onCancel: () => {
        }, defaultValue: true })
      );
      cleanup = unmount;
      const output = lastFrame();
      globalExpect(output).toContain("Y/n");
    });
  });
});
//# sourceMappingURL=confirm.test.js.map