#!/usr/bin/env node
import {
  TEST_CATEGORIES,
  TEST_SKILLS,
  createMockMatrix,
  getTestSkill
} from "../chunk-7FMEMXJ4.js";
import {
  beforeEach,
  describe,
  globalExpect,
  it
} from "../chunk-XY3XDVMI.js";
import {
  useWizardStore
} from "../chunk-WF6RM73R.js";
import "../chunk-TZXYBG3R.js";
import "../chunk-OCEFD7V6.js";
import {
  typedKeys
} from "../chunk-T4EXUIBY.js";
import "../chunk-LWXRUR6B.js";
import {
  DEFAULT_PRESELECTED_SKILLS
} from "../chunk-6OWHQ7HM.js";
import "../chunk-EC3UJRKZ.js";
import {
  init_esm_shims
} from "../chunk-DHET7RCE.js";

// src/cli/stores/wizard-store.test.ts
init_esm_shims();
function sa(id, preloaded = false) {
  return { id, preloaded };
}
describe("WizardStore", () => {
  beforeEach(() => {
    useWizardStore.getState().reset();
  });
  describe("initial state", () => {
    it("should start at stack step", () => {
      const { step } = useWizardStore.getState();
      globalExpect(step).toBe("stack");
    });
    it("should have no approach selected", () => {
      const { approach } = useWizardStore.getState();
      globalExpect(approach).toBeNull();
    });
    it("should have no selected stack", () => {
      const { selectedStackId } = useWizardStore.getState();
      globalExpect(selectedStackId).toBeNull();
    });
    it("should have empty skillConfigs", () => {
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs).toEqual([]);
    });
    it("should have null focusedSkillId", () => {
      const { focusedSkillId } = useWizardStore.getState();
      globalExpect(focusedSkillId).toBeNull();
    });
    it("should have empty navigation history", () => {
      const { history } = useWizardStore.getState();
      globalExpect(history).toEqual([]);
    });
    it("should have empty selected domains", () => {
      const { selectedDomains } = useWizardStore.getState();
      globalExpect(selectedDomains).toEqual([]);
    });
    it("should have empty domain selections", () => {
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections).toEqual({});
    });
  });
  describe("step navigation", () => {
    it("should update step with setStep", () => {
      const store = useWizardStore.getState();
      store.setStep("stack");
      const { step } = useWizardStore.getState();
      globalExpect(step).toBe("stack");
    });
    it("should track navigation history when setting step", () => {
      const store = useWizardStore.getState();
      store.setStep("build");
      store.setStep("confirm");
      const { history } = useWizardStore.getState();
      globalExpect(history).toEqual(["stack", "build"]);
    });
    it("should go back through history", () => {
      const store = useWizardStore.getState();
      store.setStep("build");
      store.setStep("confirm");
      store.goBack();
      const { step, history } = useWizardStore.getState();
      globalExpect(step).toBe("build");
      globalExpect(history).toEqual(["stack"]);
    });
    it("when goBack is called with empty history, should return to stack step", () => {
      const store = useWizardStore.getState();
      store.setStep("build");
      store.goBack();
      store.goBack();
      const { step } = useWizardStore.getState();
      globalExpect(step).toBe("stack");
    });
  });
  describe("approach selection", () => {
    it("should set approach to stack", () => {
      const store = useWizardStore.getState();
      store.setApproach("stack");
      const { approach } = useWizardStore.getState();
      globalExpect(approach).toBe("stack");
    });
    it("should set approach to scratch", () => {
      const store = useWizardStore.getState();
      store.setApproach("scratch");
      const { approach } = useWizardStore.getState();
      globalExpect(approach).toBe("scratch");
    });
  });
  describe("stack selection", () => {
    it("should select stack by id", () => {
      const store = useWizardStore.getState();
      store.selectStack("nextjs-fullstack");
      const { selectedStackId } = useWizardStore.getState();
      globalExpect(selectedStackId).toBe("nextjs-fullstack");
    });
    it("when selectStack is called with null, should clear previously selected stack", () => {
      const store = useWizardStore.getState();
      store.selectStack("nextjs-fullstack");
      store.selectStack(null);
      const { selectedStackId } = useWizardStore.getState();
      globalExpect(selectedStackId).toBeNull();
    });
  });
  describe("domain selection", () => {
    it("should toggle domain on", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      const { selectedDomains } = useWizardStore.getState();
      globalExpect(selectedDomains).toContain("web");
    });
    it("should toggle domain off", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("web");
      const { selectedDomains } = useWizardStore.getState();
      globalExpect(selectedDomains).not.toContain("web");
    });
    it("should allow multiple domain selection", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.toggleDomain("cli");
      const { selectedDomains } = useWizardStore.getState();
      globalExpect(selectedDomains).toEqual(["web", "api", "cli"]);
    });
    it("should remove skills from deselected domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-styling", "web-styling-scss-modules", true);
      store.toggleDomain("web");
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web).toBeUndefined();
      globalExpect(store.getAllSelectedTechnologies()).toEqual([]);
      globalExpect(store.getTechnologyCount()).toBe(0);
    });
    it("should not affect skills in other domains when deselecting a domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      store.toggleDomain("web");
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web).toBeUndefined();
      globalExpect(domainSelections.api["api-api"]).toEqual(["api-framework-hono"]);
      globalExpect(store.getAllSelectedTechnologies()).toEqual(["api-framework-hono"]);
    });
    it("should not auto-select skills when toggling domain on", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web).toBeUndefined();
      globalExpect(store.getAllSelectedTechnologies()).toEqual([]);
    });
    it("should reflect correct technology count after domain deselection", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-styling", "web-styling-scss-modules", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      globalExpect(store.getTechnologyCount()).toBe(3);
      store.toggleDomain("web");
      globalExpect(store.getTechnologyCount()).toBe(1);
      globalExpect(store.getAllSelectedTechnologies()).toEqual(["api-framework-hono"]);
    });
    it("should restore stack skills when re-toggling a domain ON after populateFromStack", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          web: {
            "web-framework": [sa("web-framework-react", true)],
            "web-client-state": [sa("web-state-zustand")]
          },
          api: { "api-api": [sa("api-framework-hono", true)] }
        }
      };
      const categories = {
        "web-framework": { domain: "web" },
        "web-client-state": { domain: "web" },
        "api-api": { domain: "api" }
      };
      store.populateFromStack(stack, categories);
      store.toggleDomain("web");
      globalExpect(useWizardStore.getState().domainSelections.web).toBeUndefined();
      store.toggleDomain("web");
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
      globalExpect(domainSelections.web["web-client-state"]).toEqual(["web-state-zustand"]);
    });
    it("should restore stack skills when re-toggling a domain ON after populateFromSkillIds", () => {
      const store = useWizardStore.getState();
      const skills = {
        "web-framework-react": TEST_SKILLS.react,
        "api-framework-hono": TEST_SKILLS.hono
      };
      const categories = {
        "web-framework": TEST_CATEGORIES.framework,
        "api-api": TEST_CATEGORIES.api
      };
      store.populateFromSkillIds(["web-framework-react", "api-framework-hono"], skills, categories);
      store.toggleDomain("web");
      globalExpect(useWizardStore.getState().domainSelections.web).toBeUndefined();
      store.toggleDomain("web");
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
    });
    it("should not restore skills when no stack was populated", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleDomain("web");
      store.toggleDomain("web");
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web).toBeUndefined();
      globalExpect(store.getAllSelectedTechnologies()).toEqual([]);
    });
    it("should not affect other domains when restoring stack skills for one domain", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          web: { "web-framework": [sa("web-framework-react", true)] },
          api: { "api-api": [sa("api-framework-hono", true)] }
        }
      };
      const categories = {
        "web-framework": { domain: "web" },
        "api-api": { domain: "api" }
      };
      store.populateFromStack(stack, categories);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      store.toggleTechnology("api", "api-api", "api-framework-express", true);
      store.toggleDomain("web");
      store.toggleDomain("web");
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
      globalExpect(domainSelections.api["api-api"]).toEqual(["api-framework-express"]);
    });
  });
  describe("technology selection", () => {
    it("should toggle technology in exclusive mode", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
    });
    it("should replace technology in exclusive mode", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-framework", "web-framework-vue", true);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-framework"]).toEqual(["web-framework-vue"]);
    });
    it("should toggle off technology in exclusive mode", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-framework"]).toEqual([]);
    });
    it("should allow multiple selections in non-exclusive mode", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-testing", "web-testing-vitest", false);
      store.toggleTechnology("web", "web-testing", "web-testing-playwright-e2e", false);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-testing"]).toEqual([
        "web-testing-vitest",
        "web-testing-playwright-e2e"
      ]);
    });
    it("should toggle off technology in non-exclusive mode", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-testing", "web-testing-vitest", false);
      store.toggleTechnology("web", "web-testing", "web-testing-playwright-e2e", false);
      store.toggleTechnology("web", "web-testing", "web-testing-vitest", false);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-testing"]).toEqual(["web-testing-playwright-e2e"]);
    });
  });
  describe("domain navigation", () => {
    it("should move to next domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      const result = store.nextDomain();
      const { currentDomainIndex } = useWizardStore.getState();
      globalExpect(result).toBe(true);
      globalExpect(currentDomainIndex).toBe(1);
    });
    it("when already at the last domain, should return false from nextDomain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      const result = store.nextDomain();
      globalExpect(result).toBe(false);
    });
    it("should move to previous domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.nextDomain();
      const result = store.prevDomain();
      const { currentDomainIndex } = useWizardStore.getState();
      globalExpect(result).toBe(true);
      globalExpect(currentDomainIndex).toBe(0);
    });
    it("when already at the first domain, should return false from prevDomain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      const result = store.prevDomain();
      globalExpect(result).toBe(false);
    });
    it("should get current domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.nextDomain();
      const domain = store.getCurrentDomain();
      globalExpect(domain).toBe("api");
    });
    it("should return null when no domains selected", () => {
      const store = useWizardStore.getState();
      const domain = store.getCurrentDomain();
      globalExpect(domain).toBeNull();
    });
  });
  describe("mode toggles", () => {
    it("should toggle show labels", () => {
      const store = useWizardStore.getState();
      store.toggleShowLabels();
      const { showLabels } = useWizardStore.getState();
      globalExpect(showLabels).toBe(true);
    });
    it("should toggle help on", () => {
      const store = useWizardStore.getState();
      store.toggleHelp();
      const { showHelp } = useWizardStore.getState();
      globalExpect(showHelp).toBe(true);
    });
    it("should toggle help off (show then hide)", () => {
      const store = useWizardStore.getState();
      store.toggleHelp();
      store.toggleHelp();
      const { showHelp } = useWizardStore.getState();
      globalExpect(showHelp).toBe(false);
    });
    it("should toggle settings on", () => {
      const store = useWizardStore.getState();
      store.toggleSettings();
      const { showSettings } = useWizardStore.getState();
      globalExpect(showSettings).toBe(true);
    });
    it("should toggle settings off (show then hide)", () => {
      const store = useWizardStore.getState();
      store.toggleSettings();
      store.toggleSettings();
      const { showSettings } = useWizardStore.getState();
      globalExpect(showSettings).toBe(false);
    });
    it("should start with showHelp false", () => {
      const { showHelp } = useWizardStore.getState();
      globalExpect(showHelp).toBe(false);
    });
    it("should start with showSettings false", () => {
      const { showSettings } = useWizardStore.getState();
      globalExpect(showSettings).toBe(false);
    });
    it("should reset showHelp to false after reset", () => {
      const store = useWizardStore.getState();
      store.toggleHelp();
      store.reset();
      const { showHelp } = useWizardStore.getState();
      globalExpect(showHelp).toBe(false);
    });
    it("should reset showSettings to false after reset", () => {
      const store = useWizardStore.getState();
      store.toggleSettings();
      store.reset();
      const { showSettings } = useWizardStore.getState();
      globalExpect(showSettings).toBe(false);
    });
  });
  describe("skillConfigs and per-skill scope", () => {
    it("should sync skillConfigs when toggling a technology on", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs).toHaveLength(1);
      globalExpect(skillConfigs[0]).toEqual({
        id: "web-framework-react",
        scope: "project",
        source: "agents-inc"
      });
    });
    it("should remove from skillConfigs when toggling a technology off", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs).toHaveLength(0);
    });
    it("should replace skillConfigs entry in exclusive mode", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-framework", "web-framework-vue", true);
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs).toHaveLength(1);
      globalExpect(skillConfigs[0].id).toBe("web-framework-vue");
    });
    it("should accumulate skillConfigs in non-exclusive mode", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-testing", "web-testing-vitest", false);
      store.toggleTechnology("web", "web-testing", "web-testing-playwright-e2e", false);
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs).toHaveLength(2);
      globalExpect(skillConfigs.map((sc) => sc.id)).toEqual([
        "web-testing-vitest",
        "web-testing-playwright-e2e"
      ]);
    });
    it("should toggle skill scope between project and global", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleSkillScope("web-framework-react");
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs[0].scope).toBe("global");
    });
    it("should toggle skill scope back to project", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleSkillScope("web-framework-react");
      store.toggleSkillScope("web-framework-react");
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs[0].scope).toBe("project");
    });
    it("should update source via setSkillSource", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.setSkillSource("web-framework-react", "local");
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs[0].source).toBe("local");
    });
    it("should set and clear focusedSkillId", () => {
      const store = useWizardStore.getState();
      store.setFocusedSkillId("web-framework-react");
      globalExpect(useWizardStore.getState().focusedSkillId).toBe("web-framework-react");
      store.setFocusedSkillId(null);
      globalExpect(useWizardStore.getState().focusedSkillId).toBeNull();
    });
    it("should update source via setSourceSelection on skillConfigs", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.setSourceSelection("web-framework-react", "local");
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs[0].source).toBe("local");
    });
    it("should populate skillConfigs from populateFromStack", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          web: {
            "web-framework": [sa("web-framework-react", true)],
            "web-client-state": [sa("web-state-zustand")]
          }
        }
      };
      const categories = {
        "web-framework": { domain: "web" },
        "web-client-state": { domain: "web" }
      };
      store.populateFromStack(stack, categories);
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs).toHaveLength(2);
      globalExpect(skillConfigs.map((sc) => sc.id)).toEqual(["web-framework-react", "web-state-zustand"]);
      globalExpect(skillConfigs.every((sc) => sc.scope === "project")).toBe(true);
      globalExpect(skillConfigs.every((sc) => sc.source === "agents-inc")).toBe(true);
    });
    it("should populate skillConfigs from populateFromSkillIds", () => {
      const store = useWizardStore.getState();
      const skills = {
        "web-framework-react": TEST_SKILLS.react,
        "api-framework-hono": TEST_SKILLS.hono
      };
      const categories = {
        "web-framework": TEST_CATEGORIES.framework,
        "api-api": TEST_CATEGORIES.api
      };
      store.populateFromSkillIds(["web-framework-react", "api-framework-hono"], skills, categories);
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs).toHaveLength(2);
      globalExpect(skillConfigs.map((sc) => sc.id)).toEqual([
        "web-framework-react",
        "api-framework-hono"
      ]);
    });
    it("should remove skillConfigs when domain is deselected", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      globalExpect(useWizardStore.getState().skillConfigs).toHaveLength(1);
      store.toggleDomain("web");
      globalExpect(useWizardStore.getState().skillConfigs).toHaveLength(0);
    });
    it("should restore skillConfigs when domain is re-toggled after populateFromStack", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          web: { "web-framework": [sa("web-framework-react", true)] }
        }
      };
      const categories = {
        "web-framework": { domain: "web" }
      };
      store.populateFromStack(stack, categories);
      globalExpect(useWizardStore.getState().skillConfigs).toHaveLength(1);
      store.toggleDomain("web");
      globalExpect(useWizardStore.getState().skillConfigs).toHaveLength(0);
      store.toggleDomain("web");
      globalExpect(useWizardStore.getState().skillConfigs).toHaveLength(1);
      globalExpect(useWizardStore.getState().skillConfigs[0].id).toBe("web-framework-react");
    });
    it("should reset skillConfigs and focusedSkillId on reset", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.setFocusedSkillId("web-framework-react");
      store.reset();
      const state = useWizardStore.getState();
      globalExpect(state.skillConfigs).toEqual([]);
      globalExpect(state.focusedSkillId).toBeNull();
    });
    it("should set all sources to local via setAllSourcesLocal", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      store.setAllSourcesLocal();
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs.every((sc) => sc.source === "local")).toBe(true);
    });
    it("should set all sources to plugin via setAllSourcesPlugin", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.setSourceSelection("web-framework-react", "local");
      const matrix = createMockMatrix({
        "web-framework-react": {
          ...getTestSkill("react"),
          availableSources: [{ name: "Acme Corp", type: "private", installed: false }]
        }
      });
      store.setAllSourcesPlugin(matrix);
      const { skillConfigs } = useWizardStore.getState();
      globalExpect(skillConfigs[0].source).toBe("Acme Corp");
    });
  });
  describe("computed getters", () => {
    it("should get all selected technologies", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-styling", "web-styling-scss-modules", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      const technologies = store.getAllSelectedTechnologies();
      globalExpect(technologies).toContain("web-framework-react");
      globalExpect(technologies).toContain("web-styling-scss-modules");
      globalExpect(technologies).toContain("api-framework-hono");
    });
    it("should get default methodology skills", () => {
      const store = useWizardStore.getState();
      const skills = store.getDefaultMethodologySkills();
      for (const skill of DEFAULT_PRESELECTED_SKILLS) {
        globalExpect(skills).toContain(skill);
      }
    });
    it("should get selected technologies per domain", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-styling", "web-styling-scss-modules", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      const perDomain = store.getSelectedTechnologiesPerDomain();
      globalExpect(perDomain.web).toEqual(["web-framework-react", "web-styling-scss-modules"]);
      globalExpect(perDomain.api).toEqual(["api-framework-hono"]);
      globalExpect(perDomain.cli).toBeUndefined();
    });
    it("should return empty object for getSelectedTechnologiesPerDomain with no selections", () => {
      const store = useWizardStore.getState();
      const perDomain = store.getSelectedTechnologiesPerDomain();
      globalExpect(perDomain).toEqual({});
    });
    it("should omit domains with empty category arrays from getSelectedTechnologiesPerDomain", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const perDomain = store.getSelectedTechnologiesPerDomain();
      globalExpect(perDomain.web).toBeUndefined();
    });
    it("should get technology count", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-styling", "web-styling-scss-modules", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      globalExpect(store.getTechnologyCount()).toBe(3);
    });
    it("should return 0 for getTechnologyCount with no selections", () => {
      const store = useWizardStore.getState();
      globalExpect(store.getTechnologyCount()).toBe(0);
    });
  });
  describe("navigation guards", () => {
    it("canGoToNextDomain should return true when not at last domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      globalExpect(store.canGoToNextDomain()).toBe(true);
    });
    it("canGoToNextDomain should return false when at last domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      globalExpect(store.canGoToNextDomain()).toBe(false);
    });
    it("canGoToNextDomain should return false with no domains", () => {
      const store = useWizardStore.getState();
      globalExpect(store.canGoToNextDomain()).toBe(false);
    });
    it("canGoToPreviousDomain should return false when at first domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      globalExpect(store.canGoToPreviousDomain()).toBe(false);
    });
    it("canGoToPreviousDomain should return true when past first domain", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.nextDomain();
      globalExpect(store.canGoToPreviousDomain()).toBe(true);
    });
    it("canGoToPreviousDomain should return false with no domains", () => {
      const store = useWizardStore.getState();
      globalExpect(store.canGoToPreviousDomain()).toBe(false);
    });
  });
  describe("reset", () => {
    it("should reset to initial state", () => {
      const store = useWizardStore.getState();
      store.setStep("stack");
      store.setApproach("scratch");
      store.selectStack("nextjs-fullstack");
      store.toggleDomain("web");
      store.reset();
      const state = useWizardStore.getState();
      globalExpect(state.step).toBe("stack");
      globalExpect(state.approach).toBeNull();
      globalExpect(state.selectedStackId).toBeNull();
      globalExpect(state.selectedDomains).toEqual([]);
      globalExpect(state.history).toEqual([]);
    });
  });
  describe("populateFromStack", () => {
    it("should set selectedDomains to all domains regardless of stack contents", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          web: { "web-framework": [sa("web-framework-react", true)] }
        }
      };
      const categories = {
        "web-framework": { domain: "web" }
      };
      store.populateFromStack(stack, categories);
      const { selectedDomains, domainSelections } = useWizardStore.getState();
      globalExpect(selectedDomains).toEqual(["web", "api", "mobile", "cli", "shared"]);
      globalExpect(domainSelections.web).toBeDefined();
      globalExpect(domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
      globalExpect(domainSelections.api).toBeUndefined();
    });
    it("should populate domainSelections from stack agents", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          web: {
            "web-framework": [sa("web-framework-react", true)],
            "web-client-state": [sa("web-state-zustand")]
          },
          api: { "api-api": [sa("api-framework-hono", true)] }
        }
      };
      const categories = {
        "web-framework": { domain: "web" },
        "web-client-state": { domain: "web" },
        "api-api": { domain: "api" }
      };
      store.populateFromStack(stack, categories);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
      globalExpect(domainSelections.web["web-client-state"]).toEqual(["web-state-zustand"]);
      globalExpect(domainSelections.api["api-api"]).toEqual(["api-framework-hono"]);
    });
    it("should skip entries without a domain", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          misc: { "shared-methodology": [sa("meta-methodology-vitest")] }
        }
      };
      const categories = {
        "shared-methodology": {}
      };
      store.populateFromStack(stack, categories);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(typedKeys(domainSelections)).toHaveLength(0);
    });
    it("should populate multiple skills from array-valued categories", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          "pattern-scout": {
            "shared-methodology": [
              sa("meta-methodology-investigation-requirements", true),
              sa("meta-methodology-anti-over-engineering", true),
              sa("meta-methodology-success-criteria", true)
            ]
          }
        }
      };
      const categories = {
        "shared-methodology": { domain: "shared" }
      };
      store.populateFromStack(stack, categories);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.shared["shared-methodology"]).toEqual([
        "meta-methodology-investigation-requirements",
        "meta-methodology-anti-over-engineering",
        "meta-methodology-success-criteria"
      ]);
    });
    it("should handle single-element and multi-element arrays across agents", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          web: {
            "web-framework": [sa("web-framework-react", true)],
            "shared-methodology": [
              sa("meta-methodology-investigation-requirements", true),
              sa("meta-methodology-anti-over-engineering", true)
            ]
          },
          api: { "api-api": [sa("api-framework-hono", true)] }
        }
      };
      const categories = {
        "web-framework": { domain: "web" },
        "shared-methodology": { domain: "shared" },
        "api-api": { domain: "api" }
      };
      store.populateFromStack(stack, categories);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
      globalExpect(domainSelections.shared["shared-methodology"]).toEqual([
        "meta-methodology-investigation-requirements",
        "meta-methodology-anti-over-engineering"
      ]);
      globalExpect(domainSelections.api["api-api"]).toEqual(["api-framework-hono"]);
    });
    it("should deduplicate skills from arrays across multiple agents", () => {
      const store = useWizardStore.getState();
      const stack = {
        agents: {
          agent1: {
            "shared-methodology": [
              sa("meta-methodology-investigation-requirements", true),
              sa("meta-methodology-anti-over-engineering", true)
            ]
          },
          agent2: {
            "shared-methodology": [
              sa("meta-methodology-anti-over-engineering", true),
              sa("meta-methodology-success-criteria", true)
            ]
          }
        }
      };
      const categories = {
        "shared-methodology": { domain: "shared" }
      };
      store.populateFromStack(stack, categories);
      const { domainSelections } = useWizardStore.getState();
      globalExpect(domainSelections.shared["shared-methodology"]).toEqual([
        "meta-methodology-investigation-requirements",
        "meta-methodology-anti-over-engineering",
        "meta-methodology-success-criteria"
      ]);
    });
  });
  describe("complex flows", () => {
    it("should handle complete stack selection flow", () => {
      const store = useWizardStore.getState();
      store.setApproach("stack");
      store.selectStack("nextjs-fullstack");
      store.setStackAction("customize");
      store.setStep("build");
      store.setStep("confirm");
      const state = useWizardStore.getState();
      globalExpect(state.step).toBe("confirm");
      globalExpect(state.approach).toBe("stack");
      globalExpect(state.selectedStackId).toBe("nextjs-fullstack");
      globalExpect(state.stackAction).toBe("customize");
      globalExpect(state.history).toEqual(["stack", "build"]);
    });
    it("should handle complete scratch flow", () => {
      const store = useWizardStore.getState();
      store.setApproach("scratch");
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.setStep("build");
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-styling", "web-styling-scss-modules", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      store.setStep("confirm");
      const state = useWizardStore.getState();
      globalExpect(state.step).toBe("confirm");
      globalExpect(state.approach).toBe("scratch");
      globalExpect(state.selectedDomains).toEqual(["web", "api"]);
      globalExpect(state.domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
      globalExpect(state.domainSelections.web["web-styling"]).toEqual(["web-styling-scss-modules"]);
      globalExpect(state.domainSelections.api["api-api"]).toEqual(["api-framework-hono"]);
    });
    it("should preserve selections when going back", () => {
      const store = useWizardStore.getState();
      store.setApproach("scratch");
      store.toggleDomain("web");
      store.setStep("build");
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.goBack();
      const state = useWizardStore.getState();
      globalExpect(state.selectedDomains).toContain("web");
      globalExpect(state.domainSelections.web["web-framework"]).toEqual(["web-framework-react"]);
    });
  });
  describe("buildSourceRows sort order", () => {
    function makeSource(overrides) {
      return {
        type: "private",
        installed: false,
        ...overrides
      };
    }
    it("should sort local sources before scoped marketplace sources", () => {
      const store = useWizardStore.getState();
      const skill = {
        ...getTestSkill("react"),
        availableSources: [
          makeSource({ name: "Acme Corp", type: "private", primary: true }),
          makeSource({ name: "local", type: "local", installed: true, installMode: "local" })
        ]
      };
      const matrix = createMockMatrix({ "web-framework-react": skill });
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const rows = store.buildSourceRows(matrix);
      globalExpect(rows).toHaveLength(1);
      globalExpect(rows[0].options[0].id).toBe("local");
      globalExpect(rows[0].options[1].id).toBe("Acme Corp");
    });
    it("should sort scoped marketplace before default public marketplace", () => {
      const store = useWizardStore.getState();
      const skill = {
        ...getTestSkill("react"),
        availableSources: [
          makeSource({ name: "agents-inc", type: "public" }),
          makeSource({ name: "Acme Corp", type: "private", primary: true })
        ]
      };
      const matrix = createMockMatrix({ "web-framework-react": skill });
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const rows = store.buildSourceRows(matrix);
      globalExpect(rows).toHaveLength(1);
      globalExpect(rows[0].options[0].id).toBe("local");
      globalExpect(rows[0].options[1].id).toBe("Acme Corp");
      globalExpect(rows[0].options[2].id).toBe("agents-inc");
    });
    it("should sort default public marketplace before third-party sources", () => {
      const store = useWizardStore.getState();
      const skill = {
        ...getTestSkill("react"),
        availableSources: [
          makeSource({ name: "Extra Corp", type: "private" }),
          makeSource({ name: "agents-inc", type: "public" })
        ]
      };
      const matrix = createMockMatrix({ "web-framework-react": skill });
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const rows = store.buildSourceRows(matrix);
      globalExpect(rows).toHaveLength(1);
      globalExpect(rows[0].options[0].id).toBe("local");
      globalExpect(rows[0].options[1].id).toBe("agents-inc");
      globalExpect(rows[0].options[2].id).toBe("Extra Corp");
    });
    it("should sort all four tiers in correct order", () => {
      const store = useWizardStore.getState();
      const skill = {
        ...getTestSkill("react"),
        availableSources: [
          makeSource({ name: "Extra Corp", type: "private" }),
          makeSource({ name: "agents-inc", type: "public" }),
          makeSource({ name: "Acme Corp", type: "private", primary: true }),
          makeSource({ name: "local", type: "local", installed: true, installMode: "local" })
        ]
      };
      const matrix = createMockMatrix({ "web-framework-react": skill });
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const rows = store.buildSourceRows(matrix);
      globalExpect(rows).toHaveLength(1);
      const sourceNames = rows[0].options.map((opt) => opt.id);
      globalExpect(sourceNames).toEqual(["local", "Acme Corp", "agents-inc", "Extra Corp"]);
    });
  });
  describe("agent selection", () => {
    it("should start with empty selectedAgents", () => {
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toEqual([]);
    });
    it("should toggle agent on", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toContain("web-developer");
    });
    it("should toggle agent off", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      store.toggleAgent("web-developer");
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).not.toContain("web-developer");
    });
    it("should allow multiple agents to be selected", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      store.toggleAgent("api-developer");
      store.toggleAgent("web-reviewer");
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toEqual(["web-developer", "api-developer", "web-reviewer"]);
    });
    it("should reset selectedAgents on reset", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      store.reset();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toEqual([]);
    });
  });
  describe("agentConfigs and scope management", () => {
    it("should have empty agentConfigs initially", () => {
      const { agentConfigs } = useWizardStore.getState();
      globalExpect(agentConfigs).toEqual([]);
    });
    it("should sync agentConfigs when toggleAgent is called", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      const { agentConfigs } = useWizardStore.getState();
      globalExpect(agentConfigs).toEqual([{ name: "web-developer", scope: "project" }]);
    });
    it("should remove from agentConfigs when agent is toggled off", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      store.toggleAgent("web-developer");
      const { agentConfigs } = useWizardStore.getState();
      globalExpect(agentConfigs).toEqual([]);
    });
    it("should toggle agent scope between project and global", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      store.toggleAgentScope("web-developer");
      const { agentConfigs } = useWizardStore.getState();
      globalExpect(agentConfigs).toEqual([{ name: "web-developer", scope: "global" }]);
      store.toggleAgentScope("web-developer");
      globalExpect(useWizardStore.getState().agentConfigs).toEqual([
        { name: "web-developer", scope: "project" }
      ]);
    });
    it("should set and clear focusedAgentId", () => {
      const store = useWizardStore.getState();
      store.setFocusedAgentId("web-developer");
      globalExpect(useWizardStore.getState().focusedAgentId).toBe("web-developer");
      store.setFocusedAgentId(null);
      globalExpect(useWizardStore.getState().focusedAgentId).toBeNull();
    });
    it("should not toggle locked agents", () => {
      useWizardStore.setState({ lockedAgentNames: ["web-developer"] });
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      const { selectedAgents, agentConfigs } = useWizardStore.getState();
      globalExpect(selectedAgents).toEqual([]);
      globalExpect(agentConfigs).toEqual([]);
    });
    it("should not toggle scope of locked agents", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("web-developer");
      useWizardStore.setState({ lockedAgentNames: ["web-developer"] });
      store.toggleAgentScope("web-developer");
      const { agentConfigs } = useWizardStore.getState();
      globalExpect(agentConfigs).toEqual([{ name: "web-developer", scope: "project" }]);
    });
  });
  describe("preselectAgentsFromDomains", () => {
    it("should preselect web-related agents when web domain is selected", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toContain("web-developer");
      globalExpect(selectedAgents).toContain("web-reviewer");
      globalExpect(selectedAgents).toContain("web-researcher");
      globalExpect(selectedAgents).toContain("web-tester");
      globalExpect(selectedAgents).toContain("web-pm");
      globalExpect(selectedAgents).toContain("web-architecture");
    });
    it("should preselect api-related agents when api domain is selected", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("api");
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toContain("api-developer");
      globalExpect(selectedAgents).toContain("api-reviewer");
      globalExpect(selectedAgents).toContain("api-researcher");
      globalExpect(selectedAgents).not.toContain("web-developer");
    });
    it("should preselect cli agents when cli domain is selected", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("cli");
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toContain("cli-developer");
      globalExpect(selectedAgents).toContain("cli-tester");
      globalExpect(selectedAgents).toContain("cli-reviewer");
    });
    it("should never include optional agents regardless of domains", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.toggleDomain("cli");
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).not.toContain("agent-summoner");
      globalExpect(selectedAgents).not.toContain("skill-summoner");
      globalExpect(selectedAgents).not.toContain("documentor");
      globalExpect(selectedAgents).not.toContain("pattern-scout");
      globalExpect(selectedAgents).not.toContain("web-pattern-critique");
    });
    it("should return empty agents when no domains are selected", () => {
      const store = useWizardStore.getState();
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toEqual([]);
    });
    it("should produce union of agents for multiple domains", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toContain("web-developer");
      globalExpect(selectedAgents).toContain("api-developer");
      globalExpect(selectedAgents).toContain("web-reviewer");
      globalExpect(selectedAgents).toContain("api-reviewer");
    });
    it("should not preselect api agents when only web domain is selected", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).toContain("web-developer");
      globalExpect(selectedAgents).not.toContain("api-developer");
      globalExpect(selectedAgents).not.toContain("api-reviewer");
    });
    it("should return sorted agents", () => {
      const store = useWizardStore.getState();
      store.toggleDomain("web");
      store.toggleDomain("api");
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      const sorted = [...selectedAgents].sort();
      globalExpect(selectedAgents).toEqual(sorted);
    });
    it("should replace previous agent selection", () => {
      const store = useWizardStore.getState();
      store.toggleAgent("documentor");
      store.toggleDomain("web");
      store.preselectAgentsFromDomains();
      const { selectedAgents } = useWizardStore.getState();
      globalExpect(selectedAgents).not.toContain("documentor");
    });
  });
  describe("step progress with agents step", () => {
    it("should include agents in completed steps when on confirm", () => {
      const store = useWizardStore.getState();
      store.setApproach("scratch");
      store.setStep("build");
      store.setStep("sources");
      store.setStep("agents");
      store.setStep("confirm");
      const { completedSteps } = store.getStepProgress();
      globalExpect(completedSteps).toContain("agents");
      globalExpect(completedSteps).toContain("sources");
      globalExpect(completedSteps).toContain("build");
    });
    it("should include sources in completed steps when on agents step", () => {
      const store = useWizardStore.getState();
      store.setApproach("scratch");
      store.setStep("build");
      store.setStep("sources");
      store.setStep("agents");
      const { completedSteps } = store.getStepProgress();
      globalExpect(completedSteps).toContain("build");
      globalExpect(completedSteps).toContain("sources");
      globalExpect(completedSteps).not.toContain("agents");
    });
    it("should skip agents step when using stack defaults", () => {
      const store = useWizardStore.getState();
      store.setApproach("stack");
      store.selectStack("nextjs-fullstack");
      store.setStackAction("defaults");
      store.setStep("confirm");
      const { skippedSteps } = store.getStepProgress();
      globalExpect(skippedSteps).toContain("agents");
      globalExpect(skippedSteps).toContain("build");
      globalExpect(skippedSteps).toContain("sources");
    });
  });
  describe("deriveInstallMode", () => {
    it("should return 'plugin' when all skills have default marketplace source", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("web", "web-styling", "web-styling-scss-modules", true);
      const result = store.deriveInstallMode();
      globalExpect(result).toBe("plugin");
    });
    it("should return 'local' when all skills are set to local", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      store.setSourceSelection("web-framework-react", "local");
      store.setSourceSelection("api-framework-hono", "local");
      const result = store.deriveInstallMode();
      globalExpect(result).toBe("local");
    });
    it("should return 'mixed' when some skills are local and some are not", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.toggleTechnology("api", "api-api", "api-framework-hono", true);
      store.setSourceSelection("web-framework-react", "local");
      store.setSourceSelection("api-framework-hono", "agents-inc");
      const result = store.deriveInstallMode();
      globalExpect(result).toBe("mixed");
    });
    it("should return 'local' when no skills are configured", () => {
      const store = useWizardStore.getState();
      const result = store.deriveInstallMode();
      globalExpect(result).toBe("local");
    });
    it("should handle single skill as local", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      store.setSourceSelection("web-framework-react", "local");
      const result = store.deriveInstallMode();
      globalExpect(result).toBe("local");
    });
    it("should handle single skill as plugin", () => {
      const store = useWizardStore.getState();
      store.toggleTechnology("web", "web-framework", "web-framework-react", true);
      const result = store.deriveInstallMode();
      globalExpect(result).toBe("plugin");
    });
  });
});
//# sourceMappingURL=wizard-store.test.js.map