import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; import { loadRegistryKits, registrySource } from "./node/registry"; import { loadKitFrom } from "./load"; import { defaultSelections, resolve, initArgs, validateSelections } from "./resolve"; import { selectionsFrom } from "./selections"; import { buildPlan } from "./plan"; import { selectedServices, workspaceChoices } from "./workspace"; const registryDir = fileURLToPath(new URL("../../../registry", import.meta.url)); const source = registrySource(registryDir); const kit = await loadKitFrom(await source.read("monorepo"), source, { admit: true }); const workspaces = (await loadRegistryKits(registryDir)).filter((preset) => preset.workspace); describe("workspace composition", () => { it.each(workspaces)("$name offers Next.js with Swagger inside the NestJS service and installs its skills once", (preset) => { const sel = selectionsFrom(preset, { options: { web: ["nextjs"], api: ["nestjs"], "api.nestjs.quality": ["nestjs-swagger"] }, agents: ["codex"], }, { requirements: "throw" }).selections; const plan = buildPlan({ kit: preset, kitDir: "", sel, parentDir: "/tmp", name: "demo", git: false }); const runs = plan.filter((s) => s.kind === "run"); const skills = runs.filter((s) => s.command.includes("skills@latest add nestjs/agent-skills")); expect(skills).toHaveLength(1); expect(skills[0]).toMatchObject({ cwd: "/tmp/demo", command: expect.stringContaining("--agent codex") }); expect(skills[0]!.command).toContain("nestjs-production"); expect(runs.filter((s) => s.command.includes("@nestjs/swagger")).map((s) => s.cwd)).toEqual(["/tmp/demo/apps/api"]); const webPath = preset.workspace!.services.web!.path; expect(plan.some((s) => s.kind === "write" && s.path === `/tmp/demo/${webPath}/api-client.js`)).toBe(true); }); it("reuses standalone libraries while isolating package managers and dependencies", () => { const sel = selectionsFrom(kit, { options: { "web.nextjs.components": ["shadcn"], "api.fastapi.database": ["sqlmodel"] } }, { requirements: "throw" }).selections; const services = selectedServices(kit, sel); expect(services.map((s) => [s.id, s.sel.pm])).toEqual([["web", "pnpm"], ["api", "pip"]]); const resolved = resolve(kit, sel); expect(resolved.dependencies).not.toContain("sqlmodel"); expect(resolved.services!.find((s) => s.id === "api")!.resolved.dependencies).toContain("sqlmodel"); const plan = buildPlan({ kit, kitDir: "", sel, parentDir: "/tmp", name: "demo", git: true }); expect(plan.filter((s) => s.kind === "git")).toHaveLength(1); expect(plan.some((s) => s.kind === "run" && s.cwd === "/tmp/demo/apps/api" && s.command.includes("sqlmodel"))).toBe(true); expect(plan.some((s) => s.kind === "run" && s.cwd === "/tmp/demo" && s.command.includes("sqlmodel"))).toBe(false); }); it("removes inactive options and serializes only the selected framework", () => { const initial = defaultSelections(kit); const sel = selectionsFrom(kit, { base: initial, options: { web: ["vue"], api: ["django"], docs: ["starlight"] } }).selections; validateSelections(kit, sel); expect(sel.options["web.nextjs.components"]).toEqual([]); expect(initArgs("monorepo", kit, sel).some((arg) => arg.startsWith("--web.nextjs."))).toBe(false); expect(selectedServices(kit, sel).map((s) => s.kit.name)).toEqual(["web-vue-vite", "api-django", "docs-starlight"]); }); it("does not accept a service selection belonging to a different framework", () => { expect(() => selectionsFrom(kit, { options: { web: ["vue"], "web.nextjs.components": ["shadcn"] } }, { requirements: "throw" })).toThrow(); }); it("rejects overlapping directories and nested workspaces before creating a plan", async () => { const raw = await source.read("monorepo"); const data = structuredClone(raw.raw) as any; data.workspace.services.api.path = data.workspace.services.web.path; await expect(loadKitFrom({ ...raw, raw: data }, source)).rejects.toThrow("overlap"); data.workspace.services.api.path = "apps/api"; data.workspace.services.api.variants.fastapi = "monorepo@1.0.0"; await expect(loadKitFrom({ ...raw, raw: data }, source)).rejects.toThrow("Nested"); }); }); it("patches framework integrations idempotently and refuses changed anchors", async () => { const { applyWrite } = await import("./plan"); const patch = applyWrite({ path: "src/server.ts", mode: "replace", anchor: "port: 3000", content: "port: Number(process.env.PORT ?? 8000)" }); const output = patch("serve({ port: 3000 });"); expect(output).toBe("serve({ port: Number(process.env.PORT ?? 8000) });"); expect(patch(output)).toBe(output); expect(() => patch("serve({ port: 1234 });")).toThrow("Couldn't find"); expect(() => patch(undefined)).toThrow(); }); it("recommendations keep libraries inside the selected service", async () => { const { recommendHeuristic, resolveRecommendation } = await import("./recommend"); const recommendation = recommendHeuristic("monorepo nextjs django mantine", [kit]); const resolved = resolveRecommendation(kit, recommendation, "heuristic"); expect(resolved.selections.options.api).toEqual(["django"]); expect(resolved.selections.options.docs).toEqual(["none"]); expect(resolved.selections.options["web.nextjs.components"]).toEqual(["mantine"]); const conflicting = resolveRecommendation(kit, { kit: kit.name, reasons: [], options: { api: ["django"], "api.fastapi.database": ["sqlmodel"] } }, "ai"); expect(conflicting.selections.options.api).toEqual(["django"]); }); it("keeps native presets intact and removes them when their composition changes", async () => { const { preservesWorkspaceBase } = await import("./workspace"); for (const name of ["monorepo-go", "monorepo-packages", "monorepo-t3-turbo"]) { const preset = await loadKitFrom(await source.read(name), source, { admit: true }); const sel = defaultSelections(preset); expect(preservesWorkspaceBase(preset, sel)).toBe(true); const plan = buildPlan({ kit: preset, kitDir: "", sel, parentDir: "/tmp", name: "native", git: false }); expect(plan.some((step) => step.kind === "run" && step.command.includes("Update the CLI"))).toBe(false); const changed = selectionsFrom(preset, { base: sel, options: { api: ["django"] } }).selections; if (name !== "monorepo-packages") expect(preservesWorkspaceBase(preset, changed)).toBe(false); if (name === "monorepo-t3-turbo") { expect(sel.options["web.nextjs.components"]).toEqual(["none"]); expect(plan.some((step) => step.kind === "run" && step.title.startsWith("web:") && step.command.includes("shadcn"))).toBe(false); expect(() => selectionsFrom(preset, { options: { web: ["none"], api: ["none"], docs: ["none"], mobile: ["none"] } })).toThrow("Select at least one service"); expect(resolve(preset, sel).services?.filter((s) => s.embedded).length).toBeGreaterThan(0); } } }); it("generates real root CI for package-only workspaces and respects deployment mode", async () => { const native = await loadKitFrom(await source.read("monorepo-packages"), source); const plan = buildPlan({ kit: native, kitDir: "", sel: defaultSelections(native), parentDir: "/tmp", name: "packages", git: false }); const workflow = plan.find((step) => step.kind === "write" && step.path.endsWith(".github/workflows/workspace.yml")); expect(workflow?.kind).toBe("write"); if (workflow?.kind !== "write") return; const data = JSON.parse(workflow.merge(undefined)!); expect(data.jobs.workspace.steps.some((step: { run?: string }) => step.run?.includes("pnpm build && pnpm test"))).toBe(true); const sel = defaultSelections(kit); expect(() => selectionsFrom(kit, { base: sel, options: { "web.nextjs.deploy": ["vercel"] } }, { requirements: "throw" })).toThrow(); const independent = selectionsFrom(kit, { base: sel, options: { deployment: ["independent"], "web.nextjs.deploy": ["vercel"] } }, { requirements: "throw" }).selections; expect(buildPlan({ kit, kitDir: "", sel: independent, parentDir: "/tmp", name: "hosted", git: false }).some((step) => step.kind === "write" && step.path.endsWith("apps/web/vercel.json"))).toBe(true); }); it("translates old Turbo configurations without changing their service choices", async () => { const turbo = await loadKitFrom(await source.read("monorepo-turbo"), source); const sel = selectionsFrom(turbo, { options: { web: ["vite"], apps: ["api", "docs"] } }, { requirements: "throw" }).selections; expect(selectedServices(turbo, sel).map((service) => [service.id, service.variant])).toEqual([["web", "react"], ["api", "hono"], ["docs", "starlight"]]); const empty = selectionsFrom(turbo, { options: { web: ["next"], apps: [] } }).selections; expect(selectedServices(turbo, empty).map((service) => service.id)).toEqual(["web"]); }); it("uses standalone hosting for a custom T3 composition and removes it when the native base returns", async () => { const preset = await loadKitFrom(await source.read("monorepo-t3-turbo"), source); const native = selectionsFrom(preset, { options: { deployment: ["independent"] } }).selections; const option = preset.options.find((o) => o.id === "web.nextjs.deploy")!; const choices = (sel: typeof native) => workspaceChoices(preset, sel, option).map((c) => c.id); const paths = (sel: typeof native) => buildPlan({ kit: preset, kitDir: "", sel, parentDir: "/tmp", name: "demo", git: false }) .flatMap((step) => step.kind === "write" ? [step.path] : []); expect(choices(native)).toEqual(["none", "vercel"]); expect(() => selectionsFrom(preset, { base: native, options: { [option.id]: ["docker"] } }, { requirements: "throw" })).toThrow(); const request = { api: ["django"], [option.id]: ["docker"] }; const custom = selectionsFrom(preset, { base: native, options: request }, { requirements: "throw" }).selections; expect(choices(custom)).toEqual(["none", "docker", "railway", "render", "vercel"]); expect(paths(custom)).toContain("/tmp/demo/apps/nextjs/Dockerfile"); const restored = selectionsFrom(preset, { base: custom, options: { api: ["none"] } }).selections; expect(choices(restored)).toEqual(choices(native)); expect(restored.options[option.id]).toEqual(["none"]); expect(paths(restored)).not.toContain("/tmp/demo/apps/nextjs/Dockerfile"); expect(() => selectionsFrom(preset, { base: restored, options: { deployment: ["compose"] } }, { requirements: "throw" })).toThrow(); const compose = selectionsFrom(preset, { base: custom, options: { deployment: ["compose"] } }, { requirements: "throw" }).selections; expect(paths(compose)).toEqual(expect.arrayContaining(["/tmp/demo/compose.yaml", "/tmp/demo/apps/nextjs/Dockerfile", "/tmp/demo/apps/api/Dockerfile"])); expect(selectionsFrom(preset, { base: compose, options: { api: ["none"] } }).selections.options.deployment).toEqual(["none"]); });