import assert from "node:assert/strict" import test from "node:test" import { organisationTools } from "../src/mcp/tools/organisation" import { activateProjectPhase } from "../src/mcp/projectPhases" const phases = [ { key: "start", label: "Erstkontakt", active: true, optional: false }, { key: "planning", label: "Planung", optional: false }, { key: "review", label: "Prüfung", optional: true }, { key: "execution", label: "Umsetzung", optional: false }, { key: "done", label: "Abgeschlossen", optional: false }, ] test("registers a write-protected project phase update tool", () => { const tool = organisationTools.find((candidate) => candidate.name === "organisation.projects.phase.update") assert.deepEqual(tool?.requiredPermissions, ["organisation.projects.write"]) assert.deepEqual((tool?.inputSchema as any).required, ["id"]) }) test("activates the next project phase by key and records actor and timestamp", () => { const result = activateProjectPhase(phases, { phaseKey: "planning" }, "user-1", "2026-08-31T12:00:00.000Z") assert.equal(result.activePhase, "Planung") assert.equal(result.previousPhase, "Erstkontakt") assert.equal(result.phases[0].active, false) assert.deepEqual(result.phases[1], { key: "planning", label: "Planung", optional: false, active: true, activated_at: "2026-08-31T12:00:00.000Z", activated_by: "user-1", }) }) test("allows skipping optional phases and selecting a unique phase label", () => { const planningActive = phases.map((phase) => ({ ...phase, active: phase.key === "planning" })) const result = activateProjectPhase(planningActive, { phaseLabel: "Umsetzung" }, "user-1", "2026-08-31T12:00:00.000Z") assert.equal(result.activePhase, "Umsetzung") }) test("rejects skipping required phases", () => { assert.throws( () => activateProjectPhase(phases, { phaseKey: "execution" }, "user-1", "2026-08-31T12:00:00.000Z"), /Planung.*zuerst aktiviert/, ) }) test("allows completing a project directly like the UI", () => { const result = activateProjectPhase(phases, { phaseKey: "done" }, "user-1", "2026-08-31T12:00:00.000Z") assert.equal(result.activePhase, "Abgeschlossen") }) test("rejects unknown, ambiguous and already activated phases", () => { assert.throws( () => activateProjectPhase(phases, { phaseKey: "missing" }, "user-1", "2026-08-31T12:00:00.000Z"), /nicht gefunden/, ) assert.throws( () => activateProjectPhase([ ...phases, { key: "planning-2", label: "Planung", optional: true }, ], { phaseLabel: "Planung" }, "user-1", "2026-08-31T12:00:00.000Z"), /nicht eindeutig/, ) assert.throws( () => activateProjectPhase(phases, { phaseKey: "start" }, "user-1", "2026-08-31T12:00:00.000Z"), /bereits aktiv/, ) })