Spaces:
Configuration error
Configuration error
File size: 13,376 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | /**
* shellVerifier.test.ts — S415b test suite
*
* Copre:
* - detectEntryPoint: rileva entry point da VFS
* · package.json "main" field
* · package.json "scripts.start" field
* · candidati standard (src/main.ts, index.ts, app.py, ecc.)
* · TypeScript vs JavaScript vs Python
* · tsconfig.json presente → usa tsc --noEmit
* - buildShellVerifyPrompt: genera prompt solo quando opportuno
* - parseShellCheckFromSteps: estrae risultato da execute_shell steps
* - formatShellCheckRepairHint: formatta hint per il loop
*/
import { describe, it, expect } from "vitest";
import {
detectEntryPoint,
buildShellVerifyPrompt,
parseShellCheckFromSteps,
formatShellCheckRepairHint,
} from "../shellVerifier";
// ─── Helpers ─────────────────────────────────────────────────────────────────
const mkFile = (path: string, content = "") => ({ path, content });
const pkgJson = (main?: string, scripts?: Record<string, string>, deps?: Record<string, string>) =>
mkFile("package.json", JSON.stringify({
name: "test-app",
...(main ? { main } : {}),
...(scripts ? { scripts } : {}),
...(deps ? { dependencies: deps } : {}),
}));
// ─── detectEntryPoint ─────────────────────────────────────────────────────────
describe("detectEntryPoint", () => {
it("ritorna null per VFS vuoto", () => {
expect(detectEntryPoint([])).toBeNull();
});
it("rileva entry da package.json 'main'", () => {
const files = [pkgJson("src/server.ts"), mkFile("src/server.ts", "")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("src/server.ts");
expect(ep?.projectType).toBe("typescript");
});
it("rileva entry da package.json 'scripts.start'", () => {
const files = [
pkgJson(undefined, { start: "node dist/index.js" }),
mkFile("dist/index.js", ""),
];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("dist/index.js");
expect(ep?.projectType).toBe("javascript");
});
it("usa candidato standard src/main.tsx per TypeScript", () => {
const files = [mkFile("src/main.tsx", ""), mkFile("src/App.tsx", "")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("src/main.tsx");
expect(ep?.projectType).toBe("typescript");
});
it("usa candidato standard src/index.ts", () => {
const files = [mkFile("src/index.ts", "const x = 1;")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("src/index.ts");
expect(ep?.projectType).toBe("typescript");
});
it("usa candidato standard src/main.js", () => {
const files = [mkFile("src/main.js", "console.log('ok')"), pkgJson()];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("src/main.js");
expect(ep?.projectType).toBe("javascript");
});
it("usa candidato standard index.js quando non c'è src/", () => {
const files = [mkFile("index.js", "module.exports = {}"), pkgJson()];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("index.js");
expect(ep?.projectType).toBe("javascript");
});
it("rileva progetto Python con app.py", () => {
const files = [mkFile("app.py", "from flask import Flask")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("app.py");
expect(ep?.projectType).toBe("python");
expect(ep?.checkCmd).toContain("py_compile");
});
it("rileva progetto Python con main.py", () => {
const files = [mkFile("main.py", "if __name__ == '__main__': main()")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("main.py");
expect(ep?.projectType).toBe("python");
});
it("usa tsc --noEmit quando tsconfig.json è presente", () => {
const files = [mkFile("src/main.ts", ""), mkFile("tsconfig.json", "{}")];
const ep = detectEntryPoint(files);
expect(ep?.checkCmd).toContain("tsc --noEmit");
expect(ep?.hasConfig).toBe(true);
});
it("usa tsc con opzioni inline senza tsconfig", () => {
const files = [mkFile("src/main.ts", "")];
const ep = detectEntryPoint(files);
expect(ep?.checkCmd).toContain("tsc --noEmit");
expect(ep?.hasConfig).toBe(false);
});
it("usa node --check per JavaScript", () => {
const files = [mkFile("server.js", "")];
const ep = detectEntryPoint(files);
expect(ep?.checkCmd).toContain("node --check");
expect(ep?.checkCmd).toContain("server.js");
});
it("path package.json main con ./ prefix normalizzato", () => {
const files = [pkgJson("./src/app.ts"), mkFile("src/app.ts", "")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("src/app.ts");
});
it("ignora main non trovato nel VFS → fallback a candidati", () => {
const files = [pkgJson("dist/missing.js"), mkFile("src/index.ts", "")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("src/index.ts");
});
it("server.ts riconosciuto come TypeScript entry", () => {
const files = [mkFile("server.ts", "import express from 'express';")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("server.ts");
expect(ep?.projectType).toBe("typescript");
});
it("app.ts riconosciuto come TypeScript entry", () => {
const files = [mkFile("app.ts", "")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("app.ts");
expect(ep?.projectType).toBe("typescript");
});
it("fallback a primo .py trovato se non è app.py/main.py", () => {
const files = [mkFile("service/worker.py", "")];
const ep = detectEntryPoint(files);
expect(ep?.path).toBe("service/worker.py");
expect(ep?.projectType).toBe("python");
});
it("ritorna null se solo file non-codice nel VFS", () => {
const files = [mkFile("README.md", "# App"), mkFile("styles.css", "body{}")];
const ep = detectEntryPoint(files);
expect(ep).toBeNull();
});
});
// ─── buildShellVerifyPrompt ───────────────────────────────────────────────────
describe("buildShellVerifyPrompt", () => {
it("ritorna null all'iter 0 (codice non ancora generato)", () => {
const files = [mkFile("src/main.ts", "")];
const prompt = buildShellVerifyPrompt(files, [], 0);
expect(prompt).toBeNull();
});
it("ritorna null se entry point non trovato", () => {
const files = [mkFile("README.md", "# docs only")];
const prompt = buildShellVerifyPrompt(files, [], 1);
expect(prompt).toBeNull();
});
it("genera prompt con iter >= 1 e entry point trovato", () => {
const files = [mkFile("src/main.ts", "export const x = 1;")];
const prompt = buildShellVerifyPrompt(files, [], 1);
expect(prompt).not.toBeNull();
expect(prompt).toContain("SHELL_VERIFY");
expect(prompt).toContain("execute_shell");
expect(prompt).toContain("SHELL_CHECK_OK");
});
it("non genera prompt se shell check già richiesto (execute_shell con --check)", () => {
const files = [mkFile("server.js", "")];
const steps = [{
tool: "execute_shell",
args: { command: "node --check server.js" },
result: "SHELL_CHECK_OK",
}];
const prompt = buildShellVerifyPrompt(files, steps, 2);
expect(prompt).toBeNull(); // già richiesto
});
it("prompt contiene il comando corretto (node --check per JS)", () => {
const files = [mkFile("index.js", "const x = 1;")];
const prompt = buildShellVerifyPrompt(files, [], 1);
expect(prompt).toContain("node --check");
});
it("prompt contiene il comando corretto (tsc --noEmit per TS)", () => {
const files = [mkFile("src/main.ts", ""), mkFile("tsconfig.json", "{}")];
const prompt = buildShellVerifyPrompt(files, [], 1);
expect(prompt).toContain("tsc --noEmit");
});
it("prompt contiene python py_compile per Python", () => {
const files = [mkFile("app.py", "print('hello')")];
const prompt = buildShellVerifyPrompt(files, [], 1);
expect(prompt).toContain("py_compile");
});
});
// ─── parseShellCheckFromSteps ─────────────────────────────────────────────────
describe("parseShellCheckFromSteps", () => {
it("checked=false se nessuno step execute_shell di check", () => {
const result = parseShellCheckFromSteps([]);
expect(result.checked).toBe(false);
});
it("checked=false per execute_shell non-check (es: npm install)", () => {
const steps = [{ tool: "execute_shell", args: { command: "npm install" }, result: "ok" }];
const result = parseShellCheckFromSteps(steps);
expect(result.checked).toBe(false);
});
it("ok=true se output vuoto dopo --check", () => {
const steps = [{
tool: "execute_shell",
args: { command: "node --check server.js" },
result: "",
}];
const result = parseShellCheckFromSteps(steps);
expect(result.checked).toBe(true);
expect(result.ok).toBe(true);
expect(result.errors).toHaveLength(0);
});
it("ok=true se output contiene SHELL_CHECK_OK", () => {
const steps = [{
tool: "execute_shell",
args: { command: "tsc --noEmit" },
result: "SHELL_CHECK_OK\nEverything looks good.",
}];
const result = parseShellCheckFromSteps(steps);
expect(result.ok).toBe(true);
});
it("estrae errori tsc da output", () => {
const steps = [{
tool: "execute_shell",
args: { command: "tsc --noEmit" },
result: "src/main.ts(5,3): error TS2345: Argument of type 'string' is not assignable to 'number'.\nsrc/index.ts(1,1): error TS2304: Cannot find name 'x'.",
}];
const result = parseShellCheckFromSteps(steps);
expect(result.checked).toBe(true);
expect(result.ok).toBe(false);
expect(result.errors.length).toBe(2);
expect(result.errors[0]).toContain("error TS2345");
});
it("estrae SyntaxError da node --check", () => {
const steps = [{
tool: "execute_shell",
args: { command: "node --check index.js" },
result: "/app/index.js:3\nSyntaxError: Unexpected token '}'",
}];
const result = parseShellCheckFromSteps(steps);
expect(result.ok).toBe(false);
expect(result.errors.some(e => /SyntaxError/.test(e))).toBe(true);
});
it("estrae ModuleNotFoundError da Python", () => {
const steps = [{
tool: "execute_shell",
args: { command: "python -m py_compile app.py" },
result: "ModuleNotFoundError: No module named 'flask'",
}];
const result = parseShellCheckFromSteps(steps);
expect(result.ok).toBe(false);
expect(result.errors[0]).toContain("ModuleNotFoundError");
});
it("usa l'ultimo step execute_shell di check se ce ne sono più di uno", () => {
const steps = [
{ tool: "execute_shell", args: { command: "tsc --noEmit" }, result: "error TS1234: qualcosa" },
{ tool: "write_file", args: { path: "src/x.ts" }, result: "ok" },
{ tool: "execute_shell", args: { command: "tsc --noEmit" }, result: "" }, // secondo check → ok
];
const result = parseShellCheckFromSteps(steps);
expect(result.ok).toBe(true); // usa l'ultimo
});
it("max 8 errori estratti", () => {
const manyErrors = Array.from({ length: 15 }, (_, i) =>
`src/file${i}.ts(1,1): error TS${1000 + i}: msg`
).join("\n");
const steps = [{
tool: "execute_shell",
args: { command: "tsc --noEmit" },
result: manyErrors,
}];
const result = parseShellCheckFromSteps(steps);
expect(result.errors.length).toBeLessThanOrEqual(8);
});
});
// ─── formatShellCheckRepairHint ───────────────────────────────────────────────
describe("formatShellCheckRepairHint", () => {
it("ritorna stringa vuota per errors vuoto", () => {
expect(formatShellCheckRepairHint([], "src/main.ts", "tsc --noEmit")).toBe("");
});
it("contiene [SHELL_CHECK_FAIL] come header", () => {
const hint = formatShellCheckRepairHint(["error TS2345: ..."], "src/main.ts", "tsc --noEmit");
expect(hint).toContain("[SHELL_CHECK_FAIL");
});
it("include tutti gli errori con prefisso ❌", () => {
const errors = ["error TS2345: wrong type", "error TS2304: cannot find"];
const hint = formatShellCheckRepairHint(errors, "src/main.ts", "tsc --noEmit");
expect(hint).toContain("❌ error TS2345");
expect(hint).toContain("❌ error TS2304");
});
it("include il nome dell'entry point", () => {
const hint = formatShellCheckRepairHint(["SyntaxError"], "src/server.ts", "node --check src/server.ts");
expect(hint).toContain("src/server.ts");
});
it("include il comando nel header", () => {
const hint = formatShellCheckRepairHint(["err"], "main.py", "python -m py_compile main.py");
expect(hint).toContain("py_compile");
});
it("contiene call-to-action NON puoi dichiarare DONE", () => {
const hint = formatShellCheckRepairHint(["error"], "x.ts", "tsc --noEmit");
expect(hint).toContain("NON");
expect(hint.toLowerCase()).toContain("done");
});
});
|