File size: 13,326 Bytes
cc11e77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
 * executionPlanner.test.ts — S415 test suite
 *
 * Copre:
 *  - buildExecutionPlan(goal, spec, arch) → ExecutionPlan
 *  - formatExecutionPlan(plan) → string
 *  - formatExecutionPlanCompact(plan) → string
 *  - Ordinamento layer: auth → database → backend → frontend → integration
 *  - Assegnazione reqIds per layer
 *  - isMinimal per goal semplici
 *  - criticalPath esclude integration steps
 */

import { describe, it, expect } from "vitest";
import { buildExecutionPlan, formatExecutionPlan, formatExecutionPlanCompact } from "../executionPlanner";
import { decomposeGoal }    from "../requirementDecomposer";
import { planArchitecture } from "../architecturePlanner";
import type { GoalSpec }          from "../requirementDecomposer";
import type { ArchitecturePlan }  from "../architecturePlanner";

// ─── helper per costruire spec/arch minimali senza LLM ────────────────────────

function makeSpec(reqs: Array<{ feature: string; priority?: string }>): GoalSpec {
  return {
    goal:         "test goal",
    requirements: reqs.map((r, i) => ({
      id:                 `REQ-${String(i + 1).padStart(3, "0")}`,
      feature:            r.feature,
      priority:           (r.priority ?? "high") as "critical" | "high" | "normal",
      acceptanceCriteria: [`${r.feature} funzionante`],
    })),
    complexity: reqs.length >= 4 ? "high" : reqs.length >= 2 ? "medium" : "low",
  };
}

function makeArch(layers: Array<import("../architecturePlanner").LayerType>): ArchitecturePlan {
  return {
    goal:       "test goal",
    nodes:      layers.map((layer, i) => ({
      id:          `node-${i}`,
      layer,
      name:        `${layer.charAt(0).toUpperCase()}${layer.slice(1)} Service`,
      tech:        "generic",
      description: "",
    })),
    edges:      [],
    entryPoint: layers[0] ?? "frontend",
  };
}

// ─── 1. buildExecutionPlan — ordinamento layer ─────────────────────────────────

describe("buildExecutionPlan — ordinamento layer", () => {
  it("auth → database → backend → frontend (ordine garantito)", () => {
    const spec = makeSpec([
      { feature: "Auth/Login", priority: "critical" },
      { feature: "Database schema", priority: "high" },
      { feature: "API REST", priority: "high" },
      { feature: "Dashboard UI", priority: "normal" },
    ]);
    const arch = makeArch(["auth", "database", "backend", "frontend"]);
    const plan = buildExecutionPlan("test", spec, arch);

    const layers = plan.steps.map(s => s.layer);
    const authIdx     = layers.indexOf("auth");
    const dbIdx       = layers.indexOf("database");
    const backendIdx  = layers.indexOf("backend");
    const frontendIdx = layers.indexOf("frontend");

    expect(authIdx).toBeLessThan(dbIdx);
    expect(dbIdx).toBeLessThan(backendIdx);
    expect(backendIdx).toBeLessThan(frontendIdx);
  });

  it("integration steps vengono DOPO backend", () => {
    const spec = makeSpec([
      { feature: "Backend API" },
      { feature: "Redis cache" },
    ]);
    const arch = makeArch(["backend", "cache"]);
    const plan = buildExecutionPlan("test", spec, arch);

    const backendIdx     = plan.steps.findIndex(s => s.layer === "backend");
    const integrationIdx = plan.steps.findIndex(s => s.layer === "integration");
    if (backendIdx >= 0 && integrationIdx >= 0) {
      expect(backendIdx).toBeLessThan(integrationIdx);
    }
  });

  it("senza auth nell'arch, ordine inizia da database o backend", () => {
    const spec = makeSpec([{ feature: "Database schema" }, { feature: "API" }]);
    const arch = makeArch(["database", "backend"]);
    const plan = buildExecutionPlan("test", spec, arch);
    expect(plan.steps[0].layer).toBe("database");
  });
});

// ─── 2. buildExecutionPlan — struttura ExecutionPlan ──────────────────────────

describe("buildExecutionPlan — struttura output", () => {
  it("plan ha goal, steps, criticalPath, totalSteps, isMinimal, estimatedComplexity", () => {
    const spec = makeSpec([{ feature: "Login" }, { feature: "CRUD" }]);
    const arch = makeArch(["auth", "database"]);
    const plan = buildExecutionPlan("app con login e CRUD", spec, arch);

    expect(plan).toHaveProperty("goal");
    expect(plan).toHaveProperty("steps");
    expect(plan).toHaveProperty("criticalPath");
    expect(plan).toHaveProperty("totalSteps");
    expect(plan).toHaveProperty("isMinimal");
    expect(plan).toHaveProperty("estimatedComplexity");
    expect(plan.totalSteps).toBe(plan.steps.length);
  });

  it("ogni step ha order, name, layer, purpose, filesToCreate, verifyHint, deps, isCritical", () => {
    const spec = makeSpec([{ feature: "Auth" }, { feature: "Database" }]);
    const arch = makeArch(["auth", "database"]);
    const plan = buildExecutionPlan("test", spec, arch);

    for (const step of plan.steps) {
      expect(step).toHaveProperty("order");
      expect(step).toHaveProperty("name");
      expect(step).toHaveProperty("layer");
      expect(step).toHaveProperty("purpose");
      expect(step).toHaveProperty("filesToCreate");
      expect(step).toHaveProperty("verifyHint");
      expect(step).toHaveProperty("deps");
      expect(step).toHaveProperty("isCritical");
      expect(Array.isArray(step.filesToCreate)).toBe(true);
      expect(Array.isArray(step.deps)).toBe(true);
    }
  });

  it("steps ordinati per order (0, 1, 2...)", () => {
    const spec = makeSpec([
      { feature: "Auth" }, { feature: "DB" }, { feature: "API" }, { feature: "UI" },
    ]);
    const arch = makeArch(["auth", "database", "backend", "frontend"]);
    const plan = buildExecutionPlan("test", spec, arch);

    const orders = plan.steps.map(s => s.order);
    for (let i = 0; i < orders.length; i++) {
      expect(orders[i]).toBe(i);
    }
  });

  it("integration step ha isCritical = false", () => {
    const spec = makeSpec([{ feature: "Redis caching" }]);
    const arch = makeArch(["cache"]);
    const plan = buildExecutionPlan("test", spec, arch);
    const integStep = plan.steps.find(s => s.layer === "integration");
    if (integStep) {
      expect(integStep.isCritical).toBe(false);
    }
  });

  it("auth step ha isCritical = true", () => {
    const spec = makeSpec([{ feature: "Login" }]);
    const arch = makeArch(["auth"]);
    const plan = buildExecutionPlan("test", spec, arch);
    const authStep = plan.steps.find(s => s.layer === "auth");
    if (authStep) {
      expect(authStep.isCritical).toBe(true);
    }
  });
});

// ─── 3. isMinimal ─────────────────────────────────────────────────────────────

describe("buildExecutionPlan — isMinimal", () => {
  it("piano con 1 step → isMinimal = true", () => {
    const spec = makeSpec([{ feature: "UI semplice" }]);
    const arch = makeArch(["frontend"]);
    const plan = buildExecutionPlan("test", spec, arch);
    expect(plan.isMinimal).toBe(plan.totalSteps < 2);
  });

  it("piano con ≥2 step → isMinimal = false", () => {
    const spec = makeSpec([{ feature: "Auth" }, { feature: "Database" }]);
    const arch = makeArch(["auth", "database"]);
    const plan = buildExecutionPlan("test", spec, arch);
    if (plan.totalSteps >= 2) expect(plan.isMinimal).toBe(false);
  });
});

// ─── 4. estimatedComplexity ────────────────────────────────────────────────────

describe("buildExecutionPlan — estimatedComplexity", () => {
  it("≥4 step → high", () => {
    const spec = makeSpec([
      { feature: "Auth" }, { feature: "DB" }, { feature: "API" }, { feature: "UI" },
    ]);
    const arch = makeArch(["auth", "database", "backend", "frontend"]);
    const plan = buildExecutionPlan("test", spec, arch);
    if (plan.totalSteps >= 4) expect(plan.estimatedComplexity).toBe("high");
  });

  it("2-3 step → medium", () => {
    const spec = makeSpec([{ feature: "DB" }, { feature: "API" }]);
    const arch = makeArch(["database", "backend"]);
    const plan = buildExecutionPlan("test", spec, arch);
    if (plan.totalSteps >= 2 && plan.totalSteps < 4) {
      expect(["medium", "high"]).toContain(plan.estimatedComplexity);
    }
  });
});

// ─── 5. criticalPath ──────────────────────────────────────────────────────────

describe("buildExecutionPlan — criticalPath", () => {
  it("criticalPath contiene solo step non-integration", () => {
    const spec = makeSpec([
      { feature: "Auth" }, { feature: "Backend" }, { feature: "Redis" },
    ]);
    const arch = makeArch(["auth", "backend", "cache"]);
    const plan = buildExecutionPlan("test", spec, arch);
    for (const idx of plan.criticalPath) {
      expect(plan.steps[idx]?.isCritical).toBe(true);
    }
  });
});

// ─── 6. formatExecutionPlan ───────────────────────────────────────────────────

describe("formatExecutionPlan", () => {
  it("piano isMinimal → stringa vuota", () => {
    const plan = buildExecutionPlan("test", makeSpec([{ feature: "UI" }]), makeArch(["frontend"]));
    if (plan.isMinimal) {
      expect(formatExecutionPlan(plan)).toBe("");
    }
  });

  it("piano con ≥2 step → contiene [STEP N]", () => {
    const spec = makeSpec([{ feature: "Auth" }, { feature: "Database" }]);
    const arch = makeArch(["auth", "database"]);
    const plan = buildExecutionPlan("test", spec, arch);
    if (!plan.isMinimal) {
      const text = formatExecutionPlan(plan);
      expect(text).toMatch(/\[STEP \d\]/);
      expect(text).toContain("Checkpoint");
      expect(text).toContain("REGOLA");
    }
  });

  it("output contiene il critical path", () => {
    const spec = makeSpec([{ feature: "Auth" }, { feature: "Database" }]);
    const arch = makeArch(["auth", "database"]);
    const plan = buildExecutionPlan("test", spec, arch);
    if (!plan.isMinimal) {
      expect(formatExecutionPlan(plan)).toContain("Critical path");
    }
  });

  it("ogni step contiene verifyHint nel testo", () => {
    const spec = makeSpec([{ feature: "Auth" }, { feature: "API" }]);
    const arch = makeArch(["auth", "backend"]);
    const plan = buildExecutionPlan("test", spec, arch);
    if (!plan.isMinimal) {
      const text = formatExecutionPlan(plan);
      expect(text).toMatch(/Verifica:/);
    }
  });
});

// ─── 7. formatExecutionPlanCompact ────────────────────────────────────────────

describe("formatExecutionPlanCompact", () => {
  it("piano isMinimal → stringa vuota", () => {
    const plan = buildExecutionPlan("test", makeSpec([{ feature: "UI" }]), makeArch(["frontend"]));
    if (plan.isMinimal) {
      expect(formatExecutionPlanCompact(plan)).toBe("");
    }
  });

  it("piano con ≥2 step → contiene 🗺️ e step names", () => {
    const spec = makeSpec([{ feature: "Auth" }, { feature: "Database" }]);
    const arch = makeArch(["auth", "database"]);
    const plan = buildExecutionPlan("test", spec, arch);
    if (!plan.isMinimal) {
      const compact = formatExecutionPlanCompact(plan);
      expect(compact).toContain("🗺️");
      expect(compact).toContain("→");
    }
  });

  it("compact è più corto di full", () => {
    const spec = makeSpec([{ feature: "Auth" }, { feature: "DB" }, { feature: "API" }]);
    const arch = makeArch(["auth", "database", "backend"]);
    const plan = buildExecutionPlan("test", spec, arch);
    if (!plan.isMinimal) {
      expect(formatExecutionPlanCompact(plan).length).toBeLessThan(formatExecutionPlan(plan).length);
    }
  });
});

// ─── 8. Integrazione con decomposeGoal + planArchitecture ─────────────────────

describe("buildExecutionPlan — integrazione decomposeGoal + planArchitecture", () => {
  it("goal 'app con login, database e dashboard' → piano ordinato auth→db→frontend", () => {
    const goal = "Crea app con login JWT, schema PostgreSQL e dashboard React";
    const spec  = decomposeGoal(goal);
    const arch  = planArchitecture(goal, spec.requirements);
    const plan  = buildExecutionPlan(goal, spec, arch);

    expect(plan.steps.length).toBeGreaterThan(0);
    expect(plan.goal).toBe(goal);
    // Auth deve venire prima di database se entrambi presenti
    const layers = plan.steps.map(s => s.layer);
    const authIdx = layers.indexOf("auth");
    const dbIdx   = layers.indexOf("database");
    if (authIdx >= 0 && dbIdx >= 0) {
      expect(authIdx).toBeLessThan(dbIdx);
    }
  });

  it("goal semplice → piano con almeno 1 step", () => {
    const goal = "Crea una semplice app React";
    const spec  = decomposeGoal(goal);
    const arch  = planArchitecture(goal, spec.requirements);
    const plan  = buildExecutionPlan(goal, spec, arch);
    expect(plan.steps.length).toBeGreaterThanOrEqual(1);
  });
});