#!/usr/bin/env node
import {
  createMockMatrix,
  getTestSkill
} from "../chunk-3JRWNWBF.js";
import {
  beforeEach,
  describe,
  globalExpect,
  it
} from "../chunk-XY3XDVMI.js";
import {
  useWizardStore
} from "../chunk-FYNMNY4P.js";
import "../chunk-KZNPPUQG.js";
import "../chunk-ZECXM7LP.js";
import {
  typedKeys
} from "../chunk-T4EXUIBY.js";
import "../chunk-473YHDYQ.js";
import {
  DEFAULT_PRESELECTED_SKILLS
} from "../chunk-FVN5PFFY.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 expert mode off", () => {
      const { expertMode } = useWizardStore.getState();
      globalExpect(expertMode).toBe(false);
    });
    it("should default to local install mode", () => {
      const { installMode } = useWizardStore.getState();
      globalExpect(installMode).toBe("local");
    });
    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"]);
    });
  });
  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 expert mode on", () => {
      const store = useWizardStore.getState();
      store.toggleExpertMode();
      const { expertMode } = useWizardStore.getState();
      globalExpect(expertMode).toBe(true);
    });
    it("should toggle expert mode off", () => {
      const store = useWizardStore.getState();
      store.toggleExpertMode();
      store.toggleExpertMode();
      const { expertMode } = useWizardStore.getState();
      globalExpect(expertMode).toBe(false);
    });
    it("should toggle install mode to plugin", () => {
      const store = useWizardStore.getState();
      store.toggleInstallMode();
      const { installMode } = useWizardStore.getState();
      globalExpect(installMode).toBe("plugin");
    });
    it("should toggle install mode back to local", () => {
      const store = useWizardStore.getState();
      store.toggleInstallMode();
      store.toggleInstallMode();
      const { installMode } = useWizardStore.getState();
      globalExpect(installMode).toBe("local");
    });
    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("install mode auto-defaulting", () => {
    it("should default installMode to plugin when set via setState (marketplace available)", () => {
      useWizardStore.setState({ installMode: "plugin" });
      const { installMode } = useWizardStore.getState();
      globalExpect(installMode).toBe("plugin");
    });
    it("should keep installMode as local when set via setState (no marketplace)", () => {
      useWizardStore.setState({ installMode: "local" });
      const { installMode } = useWizardStore.getState();
      globalExpect(installMode).toBe("local");
    });
    it("should allow toggling installMode after auto-defaulting to plugin", () => {
      useWizardStore.setState({ installMode: "plugin" });
      useWizardStore.getState().toggleInstallMode();
      const { installMode } = useWizardStore.getState();
      globalExpect(installMode).toBe("local");
    });
    it("should reset installMode to local after reset even if previously set to plugin", () => {
      useWizardStore.setState({ installMode: "plugin" });
      useWizardStore.getState().reset();
      const { installMode } = useWizardStore.getState();
      globalExpect(installMode).toBe("local");
    });
  });
  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 subcategory 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.toggleExpertMode();
      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.expertMode).toBe(false);
      globalExpect(state.installMode).toBe("local");
      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", "cli", "mobile", "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 subcategories", () => {
      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("Acme Corp");
      globalExpect(rows[0].options[1].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("Agents Inc");
      globalExpect(rows[0].options[1].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("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");
      globalExpect(selectedAgents).toContain("cli-migrator");
    });
    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");
    });
  });
});
//# sourceMappingURL=wizard-store.test.js.map