agentlens / tests /checker.test.ts
hongcoding's picture
feat: AgentLens v0.1.0 — cross-agent config migration + adversarial code QA
7bedc79 verified
Raw History Blame Contribute Delete
4.64 kB
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");
});
});