File size: 4,189 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
/**
 * requestEnhancer.test.ts — Unit test S39 RequestEnhancer
 *
 * Copertura (10 test):
 *  1. enhanceRequest: ritorna EnhancedRequest valido
 *  2. code_fix breve → enhanced ha detailSuffix
 *  3. debug → suggerimenti include "stack trace"
 *  4. refactor → suggerimento rischio alta priorità
 *  5. full_app → modelTier premium nel classification
 *  6. richiesta lunga (>200 char) → nessun suffix aggiunto
 *  7. getHighPrioritySuggestions: solo suggerimenti high
 *  8. hasActionableSuggestions: true per debug
 *  9. hasActionableSuggestions: false per domanda semplice
 * 10. DI _setTestEnhancer: custom enhancer usato
 *
 * SESSIONE: S39 — TaskClassifier + ConfidenceEngine + RequestEnhancer
 */

import { describe, it, expect, afterEach } from "vitest";
import {
  enhanceRequest,
  getHighPrioritySuggestions,
  hasActionableSuggestions,
  _setTestEnhancer,
  _resetEnhancer,
} from "@/lib/requestEnhancer";

afterEach(() => {
  _resetEnhancer();
});

describe("enhanceRequest", () => {
  it("1. ritorna EnhancedRequest con tutti i campi obbligatori", () => {
    const r = enhanceRequest("Fix questo bug");
    expect(r.original).toBe("Fix questo bug");
    expect(typeof r.enhanced).toBe("string");
    expect(r.enhanced.length).toBeGreaterThan(0);
    expect(Array.isArray(r.suggestions)).toBe(true);
    expect(typeof r.hasChanges).toBe("boolean");
    expect(r.confidence).toBeGreaterThan(0);
    expect(r.classification).toBeDefined();
  });

  it("2. code_fix breve → enhanced contiene hint su file/errore", () => {
    const r = enhanceRequest("fix questa funzione");
    expect(r.hasChanges).toBe(true);
    expect(r.enhanced.length).toBeGreaterThan("fix questa funzione".length);
  });

  it("3. debug → suggestions include riferimento a stack trace", () => {
    const r = enhanceRequest("non funziona, crasha");
    const texts = r.suggestions.map(s => s.message.toLowerCase());
    expect(texts.some(t => t.includes("stack") || t.includes("errore") || t.includes("error"))).toBe(true);
  });

  it("4. refactor → suggerimento rischio ad alta priorità", () => {
    const r = enhanceRequest("Refactor il modulo di autenticazione");
    const highRisk = r.suggestions.filter(s => s.priority === "high" && s.kind === "risk");
    expect(highRisk.length).toBeGreaterThan(0);
  });

  it("5. full_app → classification.modelTier = premium", () => {
    const r = enhanceRequest("Crea un'app completa da zero con React e TypeScript");
    expect(r.classification.modelTier).toBe("premium");
    expect(r.classification.needsPlan).toBe(true);
  });

  it("6. richiesta lunga >200 char → nessun suffix aggiunto (già abbastanza dettagliata)", () => {
    const long = "Fix questo bug. ".repeat(15); // >200 chars
    const r = enhanceRequest(long);
    expect(r.enhanced.trim()).toBe(long.trim());
  });
});

describe("getHighPrioritySuggestions", () => {
  it("7. debug → almeno 1 suggerimento high priority", () => {
    const sug = getHighPrioritySuggestions("Il componente non funziona, crasha all'avvio");
    expect(sug.length).toBeGreaterThan(0);
    expect(sug.every(s => s.priority === "high")).toBe(true);
  });

  it("7b. tutti i suggerimenti ritornati sono high priority", () => {
    const sug = getHighPrioritySuggestions("refactor questo modulo enorme");
    for (const s of sug) {
      expect(s.priority).toBe("high");
    }
  });
});

describe("hasActionableSuggestions", () => {
  it("8. debug → hasActionableSuggestions=true", () => {
    expect(hasActionableSuggestions("Il codice non funziona crasha ovunque")).toBe(true);
  });

  it("9. domanda banale → hasActionableSuggestions=false", () => {
    expect(hasActionableSuggestions("Ciao, come stai?")).toBe(false);
  });
});

describe("DI _setTestEnhancer", () => {
  it("10. custom enhancer viene usato al posto del default", () => {
    _setTestEnhancer((original, cls) => ({
      original,
      enhanced: "CUSTOM: " + original,
      classification: cls,
      suggestions: [],
      hasChanges: true,
      confidence: 1.0,
    }));
    const r = enhanceRequest("test");
    expect(r.enhanced).toBe("CUSTOM: test");
    expect(r.confidence).toBe(1.0);
  });
});