32 lines · 1.2 KB
import { describe, expect, test } from "bun:test";
import { scanText } from "./detectors";

const KEY = `sk-proj-${"a".repeat(40)}T3BlbkFJ${"b".repeat(40)}`;

describe("secret detectors (INT-A9)", () => {
  test("still finds OpenAI keys", () => {
    const findings = scanText(`const key = "${KEY}";\n`);
    expect(findings.map((finding) => finding.detector)).toContain("openai_api_key");
    expect(findings[0].secret).toBe(KEY);
  });

  test("decoy runs of sk- prefixes scan in linear time", () => {
    const decoy = "sk-".repeat(350_000) + "T3BlbkFJ";
    const started = performance.now();
    scanText(decoy);
    expect(performance.now() - started).toBeLessThan(2000);
  });

  test("skips the detector entirely when its anchor is absent", () => {
    const started = performance.now();
    expect(scanText("sk-".repeat(350_000))).toEqual([]);
    expect(performance.now() - started).toBeLessThan(500);
  });

  test("reports correct line numbers for many findings", () => {
    const lines = Array.from({ length: 30 }, (_, index) => `line ${index} ghp_${"x".repeat(36)}`);
    const findings = scanText(lines.join("\n"));
    expect(findings.map((finding) => finding.line)).toEqual(lines.map((_, index) => index + 1));
  });
});