File size: 4,635 Bytes
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect } from "vitest";
import { analyzeCode, generateReport, formatReport } from "../src/checker/engine.js";

describe("analyzeCode — Security", () => {
  it("detects command injection (SEC001)", () => {
    const code = `const result = exec(userInput);`;
    const findings = analyzeCode(code, "test.js");
    const ids = findings.map((f) => f.message);
    expect(ids.some((m) => m.includes("SEC001"))).toBe(true);
  });

  it("detects hardcoded secrets (SEC002)", () => {
    const code = `const apiKey = "sk-1234567890abcdef";`;
    const findings = analyzeCode(code, "test.js");
    expect(findings.some((f) => f.message.includes("SEC002"))).toBe(true);
    expect(findings.find((f) => f.message.includes("SEC002"))?.severity).toBe("critical");
  });

  it("detects SQL injection (SEC003)", () => {
    const code = `db.query("SELECT * FROM users WHERE name = " + userName);`;
    const findings = analyzeCode(code, "test.js");
    expect(findings.some((f) => f.message.includes("SEC003"))).toBe(true);
  });

  it("detects XSS via innerHTML (SEC004)", () => {
    const code = `element.innerHTML = userInput;`;
    const findings = analyzeCode(code, "test.js");
    expect(findings.some((f) => f.message.includes("SEC004"))).toBe(true);
  });

  it("detects disabled TLS verification (SEC005)", () => {
    const code = `rejectUnauthorized: false`;
    const findings = analyzeCode(code, "test.js");
    expect(findings.some((f) => f.message.includes("SEC005"))).toBe(true);
  });
});

describe("analyzeCode — Correctness", () => {
  it("detects loose null comparison (COR001)", () => {
    const code = `if (value == null) { }`;
    const findings = analyzeCode(code, "test.js");
    expect(findings.some((f) => f.message.includes("COR001"))).toBe(true);
  });

  it("detects empty catch block (COR002)", () => {
    const code = `try { foo(); } catch (e) { }`;
    const findings = analyzeCode(code, "test.js");
    expect(findings.some((f) => f.message.includes("COR002"))).toBe(true);
  });

  it("detects TODO markers (COR006)", () => {
    const code = `// TODO: implement this`;
    const findings = analyzeCode(code, "test.js");
    expect(findings.some((f) => f.message.includes("COR006"))).toBe(true);
    expect(findings.find((f) => f.message.includes("COR006"))?.severity).toBe("info");
  });
});

describe("analyzeCode — Style & Performance", () => {
  it("detects console.log (STY001)", () => {
    const code = `console.log("debug");`;
    const findings = analyzeCode(code, "test.js", "javascript");
    expect(findings.some((f) => f.message.includes("STY001"))).toBe(true);
  });

  it("detects unbounded queries (PERF001)", () => {
    const code = `SELECT * FROM orders`;
    const findings = analyzeCode(code, "test.sql");
    expect(findings.some((f) => f.message.includes("PERF001"))).toBe(true);
  });

  it("skips language-specific rules when language mismatch", () => {
    const code = `console.log("debug");`;
    const findings = analyzeCode(code, "test.py", "python");
    expect(findings.some((f) => f.message.includes("STY001"))).toBe(false);
  });
});

describe("generateReport", () => {
  it("produces sorted report with summary counts", () => {
    const code = `
      const apiKey = "sk-secret123";
      if (x == null) {}
      console.log("debug");
    `;
    const report = generateReport([{ path: "test.js", content: code, language: "javascript" }]);
    expect(report.summary.critical).toBeGreaterThanOrEqual(1);
    expect(report.summary.info).toBeGreaterThanOrEqual(1);
    // Findings should be sorted: critical first
    const criticalIdx = report.findings.findIndex((f) => f.severity === "critical");
    const infoIdx = report.findings.findIndex((f) => f.severity === "info");
    expect(criticalIdx).toBeLessThan(infoIdx);
  });

  it("handles clean code", () => {
    const report = generateReport([{ path: "clean.ts", content: `const x: number = 42;`, language: "typescript" }]);
    expect(report.findings.length).toBe(0);
    expect(report.summary.critical).toBe(0);
  });
});

describe("formatReport", () => {
  it("formats empty report as clean", () => {
    const report = generateReport([{ path: "a.ts", content: `const x = 1;` }]);
    const output = formatReport(report);
    expect(output).toContain("No issues found");
  });

  it("formats report with findings", () => {
    const report = generateReport([
      { path: "test.js", content: `const apiKey = "sk-secret";`, language: "javascript" },
    ]);
    const output = formatReport(report);
    expect(output).toContain("SEC002");
    expect(output).toContain("critical");
  });
});