#!/usr/bin/env node
import {
  ARROW_DOWN,
  ARROW_UP,
  ENTER,
  ESCAPE,
  RENDER_DELAY_MS,
  SELECT_NAV_DELAY_MS,
  delay
} from "../../chunk-YZTWZVGX.js";
import {
  render
} from "../../chunk-66UDJBF6.js";
import {
  TEST_SKILLS,
  createMockCategory,
  createMockMatrix,
  createMockResolvedStack
} from "../../chunk-TBB3THSL.js";
import {
  afterEach,
  beforeEach,
  describe,
  globalExpect,
  it,
  vi
} from "../../chunk-XY3XDVMI.js";
import {
  StepStack
} from "../../chunk-FMQ3A7W4.js";
import "../../chunk-QR2TM4OY.js";
import "../../chunk-ZWAL2ZY7.js";
import "../../chunk-HLTJK3XB.js";
import "../../chunk-NRCKIHND.js";
import "../../chunk-M4P5YJ45.js";
import "../../chunk-B4QYXVPZ.js";
import {
  useWizardStore
} from "../../chunk-SPSGZWTZ.js";
import "../../chunk-INKJBMPJ.js";
import "../../chunk-GFDGYQ6M.js";
import "../../chunk-T4EXUIBY.js";
import "../../chunk-C4QI54PN.js";
import "../../chunk-FTD5Z6QD.js";
import {
  init_esm_shims
} from "../../chunk-DHET7RCE.js";

// src/cli/components/wizard/step-stack.test.tsx
init_esm_shims();
import { jsx } from "react/jsx-runtime";
var createMockStackWithSkills = () => {
  const skills = {
    ["web-framework-react"]: TEST_SKILLS.react,
    ["web-state-zustand"]: TEST_SKILLS.zustand,
    ["api-framework-hono"]: TEST_SKILLS.hono
  };
  const suggestedStacks = [
    createMockResolvedStack("react-fullstack", "React Fullstack", {
      description: "Full React stack with Zustand and Hono",
      allSkillIds: ["web-framework-react", "web-state-zustand", "api-framework-hono"]
    }),
    createMockResolvedStack("react-minimal", "React Minimal", {
      allSkillIds: ["web-framework-react"]
    })
  ];
  return createMockMatrix(skills, {
    suggestedStacks,
    categories: {
      "web-framework": createMockCategory("web-framework", "Web Framework", {
        domain: "web",
        exclusive: false
      }),
      "api-api": createMockCategory("api-api", "API Framework", {
        domain: "api",
        exclusive: false,
        order: 1
      }),
      "cli-framework": createMockCategory("cli-framework", "CLI Framework", {
        domain: "cli",
        exclusive: false,
        order: 2
      }),
      "mobile-framework": createMockCategory("mobile-framework", "Mobile Framework", {
        domain: "mobile",
        exclusive: false,
        order: 3
      })
    }
  });
};
describe("StepStack component", () => {
  let cleanup;
  let mockMatrix;
  beforeEach(() => {
    useWizardStore.getState().reset();
    mockMatrix = createMockStackWithSkills();
  });
  afterEach(() => {
    cleanup?.();
    cleanup = void 0;
  });
  describe("unified stack/scratch selection (initial view)", () => {
    describe("rendering", () => {
      it("should render stack options and scratch option", () => {
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("React Fullstack");
        globalExpect(output).toContain("React Minimal");
        globalExpect(output).toContain("Start from scratch");
      });
      it("should render focused stack description (only focused item shows description)", () => {
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("Full React stack");
      });
      it("should render ViewTitle for stack selection", () => {
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("Choose a stack");
      });
      it("should render divider between stacks and scratch option", () => {
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("\u2500");
      });
    });
    describe("stack selection", () => {
      it("should select first stack on Enter and show domain selection", async () => {
        const { stdin, lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { approach, selectedStackId } = useWizardStore.getState();
        globalExpect(approach).toBe("stack");
        globalExpect(selectedStackId).toBe("react-fullstack");
        const output = lastFrame();
        globalExpect(output).toContain("Select domains to configure");
      });
      it("should select second stack when navigated to", async () => {
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { selectedStackId } = useWizardStore.getState();
        globalExpect(selectedStackId).toBe("react-minimal");
      });
    });
    describe("scratch selection", () => {
      it("should show domain selection when scratch is selected", async () => {
        const { stdin, lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { approach, selectedStackId } = useWizardStore.getState();
        globalExpect(approach).toBe("scratch");
        globalExpect(selectedStackId).toBeNull();
        const output = lastFrame();
        globalExpect(output).toContain("Select domains to configure");
      });
      it("should pre-select domains except CLI when scratch is chosen", async () => {
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { selectedDomains } = useWizardStore.getState();
        globalExpect(selectedDomains).toContain("web");
        globalExpect(selectedDomains).toContain("api");
        globalExpect(selectedDomains).toContain("mobile");
        globalExpect(selectedDomains).not.toContain("cli");
      });
    });
    describe("cancel navigation", () => {
      it("should call onCancel when pressing Escape at initial view", async () => {
        const onCancel = vi.fn();
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix, onCancel }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ESCAPE);
        await delay(SELECT_NAV_DELAY_MS);
        globalExpect(onCancel).toHaveBeenCalledTimes(1);
      });
    });
    describe("arrow key navigation", () => {
      it("should navigate down to second stack card", async () => {
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { selectedStackId } = useWizardStore.getState();
        globalExpect(selectedStackId).toBe("react-minimal");
      });
      it("should navigate down then up back to first stack", async () => {
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ARROW_UP);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { selectedStackId } = useWizardStore.getState();
        globalExpect(selectedStackId).toBe("react-fullstack");
      });
      it("should clamp at top boundary", async () => {
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ARROW_UP);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ARROW_UP);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { selectedStackId } = useWizardStore.getState();
        globalExpect(selectedStackId).toBe("react-fullstack");
      });
      it("should clamp at bottom boundary (scratch is last)", async () => {
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ARROW_DOWN);
        await delay(SELECT_NAV_DELAY_MS);
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { approach } = useWizardStore.getState();
        globalExpect(approach).toBe("scratch");
      });
    });
    describe("empty state", () => {
      it("should still show scratch option with no stacks available", () => {
        const emptyMatrix = createMockMatrix({}, { suggestedStacks: [] });
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: emptyMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("Choose a stack");
        globalExpect(output).toContain("Start from scratch");
      });
    });
  });
  describe("domain selection mode (after stack/scratch choice)", () => {
    beforeEach(() => {
      useWizardStore.getState().setApproach("scratch");
    });
    describe("rendering", () => {
      it("should render domain options", () => {
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("Web");
        globalExpect(output).toContain("API");
        globalExpect(output).toContain("CLI");
      });
      it("should render header text for domain selection", () => {
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("Select domains to configure");
      });
      it("should show domain descriptions", () => {
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("Frontend web applications");
      });
    });
    describe("domain selection", () => {
      it("should toggle domain when selected", async () => {
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(" ");
        await delay(SELECT_NAV_DELAY_MS);
        const { selectedDomains } = useWizardStore.getState();
        globalExpect(selectedDomains).toContain("web");
      });
      it("should allow multiple domain selection", async () => {
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(" ");
        await delay(SELECT_NAV_DELAY_MS);
        globalExpect(useWizardStore.getState().selectedDomains).toContain("web");
        useWizardStore.getState().toggleDomain("api");
        const { selectedDomains } = useWizardStore.getState();
        globalExpect(selectedDomains).toContain("web");
        globalExpect(selectedDomains).toContain("api");
      });
      it("should show selected domains summary when domains selected", async () => {
        useWizardStore.getState().toggleDomain("web");
        const { lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        const output = lastFrame();
        globalExpect(output).toContain("Selected:");
        globalExpect(output).toContain("web");
      });
      it("should navigate to build step when continuing", async () => {
        useWizardStore.getState().toggleDomain("web");
        const { stdin, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        for (let i = 0; i < 4; i++) {
          stdin.write(ARROW_DOWN);
          await delay(SELECT_NAV_DELAY_MS);
        }
        stdin.write(ENTER);
        await delay(SELECT_NAV_DELAY_MS);
        const { step } = useWizardStore.getState();
        globalExpect(step).toBe("build");
      });
    });
    describe("back navigation", () => {
      it("should return to stack/scratch selection when pressing ESC", async () => {
        const { stdin, lastFrame, unmount } = render(/* @__PURE__ */ jsx(StepStack, { matrix: mockMatrix }));
        cleanup = unmount;
        await delay(RENDER_DELAY_MS);
        stdin.write(ESCAPE);
        await delay(SELECT_NAV_DELAY_MS);
        const { approach } = useWizardStore.getState();
        globalExpect(approach).toBeNull();
        const output = lastFrame();
        globalExpect(output).toContain("Choose a stack");
      });
    });
  });
});
//# sourceMappingURL=step-stack.test.js.map