Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
48.4 KB
Raw Blame
/**
 * Unit tests for lib/redact-engine.ts + lib/redact-patterns.ts.
 *
 * One positive test per pattern, plus FP-filters, validators (Luhn/entropy/
 * RFC1918), email allowlist, no-promotion visibility semantics, tool-fence
 * degrade, normalization (zero-width / homoglyph / entity), oversize fail-closed,
 * and pure-function purity.
 */
import { describe, test, expect } from "bun:test";
import {
  scan,
  exitCodeFor,
  maskPreview,
  normalizeWithMap,
  redactFindingSpans,
  type RepoVisibility,
} from "../lib/redact-engine";
import {
  PATTERNS,
  luhnValid,
  shannonEntropy,
  isPublicIPv4,
  isPlaceholderSpan,
  URL_PASSWORD_PLACEHOLDER_WORDS,
} from "../lib/redact-patterns";

function ids(text: string, vis: RepoVisibility = "private"): string[] {
  return scan(text, { repoVisibility: vis }).findings.map((f) => f.id);
}

describe("HIGH credential patterns", () => {
  const cases: Array<[string, string]> = [
    ["aws.access_key", "key = AKIA1234567890ABCDEF"],
    ["aws.secret_key", "aws_secret_access_key = AbCdEfGhIjKlMnOpQrStUvWxYz0123456789AbCd"],
    ["github.pat", "token ghp_" + "1234567890abcdefghijklmnopqrstuvwxyz"],
    ["github.oauth", "gho_" + "1234567890abcdefghijklmnopqrstuvwxyz"],
    ["github.server", "ghs_1234567890abcdefghijklmnopqrstuvwxyz"],
    ["github.fine_grained", "github_pat_" + "A".repeat(82)],
    ["anthropic.key", "sk-ant-" + "api03-abcdefghij1234567890XYZ"],
    ["openai.key", "sk-proj-" + "a".repeat(40)],
    ["sendgrid.key", "SG." + "a".repeat(22) + "." + "b".repeat(43)],
    ["stripe.secret", "sk_live_" + "a".repeat(30)],
    ["slack.token", "xox" + "b-1234567890-abcdefghijklmnop"],
    ["slack.webhook", "https://hooks.slack.com/services/T00000000/B11111111/" + "a".repeat(24)],
    ["discord.webhook", "https://discord.com/api/webhooks/123456789012345678/" + "a".repeat(60)],
    ["pem.private_key", "-----BEGIN RSA PRIVATE KEY-----"],
    // #1946 coverage-gap additions
    ["gitlab.token", "remote: glpat-" + "Ab12Cd34Ef56Gh78Ij90"],
    ["gitlab.token", "trigger glptt-" + "a1b2c3d4e5f6a7b8c9d0e1f2"],
    ["gitlab.token", "deploy gldt-" + "Zy98Xw76Vu54Ts32Rq10"],
    ["groq.key", "gsk_" + "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789AbCdEfGhIjKlMn"],
    ["tavily.key", "tvly-" + "AbCdEfGhIjKlMnOpQrStUvWx"],
    ["tavily.key", "tvly-dev-" + "AbCdEfGhIjKlMnOpQrStUvWx"],
    ["notion.token", "ntn_" + "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789AbCdEfGh"],
    ["notion.token", "secret_" + "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789AbCdEfGh"],
    ["huggingface.token", "hf_" + "AbCdEfGhIjKlMnOpQrStUvWxYz012345"],
    ["npm.token", "npm_" + "a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R8"],
    ["digitalocean.token", "dop_v1_" + "0123456789abcdef".repeat(4)],
    [
      "gcp.service_account",
      '{"private_key_id": "abc123", "private_key": "-----BEGIN PRIVATE KEY-----\\nMIIE..."}',
    ],
    ["google.oauth_client_secret", 'client_secret: "GOCSPX-' + "Ab3xQ9zLmNp2RtVw7YkD1sHf" + '"'],
    ["telegram.bot_token", "TELEGRAM_TOKEN=8326208591:AA" + "HdqRy9Lm2ZpXvKb4NcQw8TuEr6YoP1sVg"],
    ["aws.access_key", "temporary key ASIA" + "ZQ3XN7P2LM4RT8WV"],
    ["github.user_to_server", "ghu_" + "a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R8"],
    ["github.refresh", "ghr_" + "a1B2c3D4e5F6g7H8i9J0".repeat(3) + "k1L2m3N4o5P6q7R8"],
    ["openrouter.key", "OPENROUTER sk-or-v1-" + "9f8e7d6c5b4a39281706f5e4d3c2b1a0".repeat(2)],
    ["stripe.restricted", "rk_live_" + "a1B2c3D4e5F6g7H8i9J0k1L2"],
    ["supabase.secret", "sb_secret_" + "tR4i6cyMIrYTE3s6OyHGHw_ppx2p6WK"],
    ["supabase.pat", "sbp_" + "9f8e7d6c5b4a39281706f5e4d3c2b1a09f8e7d6c"],
  ];
  for (const [id, text] of cases) {
    test(`flags ${id}`, () => {
      expect(ids(text)).toContain(id);
    });
  }

  // #1868 — modern OpenAI keys use base64url bodies (with - and _). The old
  // [A-Za-z0-9]{32,} regex stopped at the first separator and missed them all,
  // failing a HIGH credential OPEN through the redaction gate.
  test("openai.key flags modern sk-proj-/sk-svcacct-/sk-admin- shapes (#1868)", () => {
    const missed = [
      "sk-proj-Ab12_Cd34-Ef56Gh78Ij90Kl12Mn34Op56Qr78St90Uv",
      "sk-svcacct-abc_def-ghijklmnopqrstuvwxyz0123456789ABCDEF",
      "sk-admin-AAAA_BBBB-CCCC_DDDD-EEEE_FFFF-GGGG_HHHH1234",
    ];
    for (const key of missed) {
      expect(ids(`OPENAI_API_KEY=${key}`)).toContain("openai.key");
    }
    // legacy contiguous shape still flags
    expect(ids("sk-proj-" + "a".repeat(40))).toContain("openai.key");
  });

  test("openai.key does not over-match prose / malformed sk- strings (#1868 calibration)", () => {
    // HIGH tier BLOCKS, so false positives on prose are costly. None of these
    // should flag as openai.key.
    const benign = [
      "the sk-learning-rate-schedule-was-tuned-carefully", // hyphenated prose
      "sk--double-dash-typo-not-a-real-key",
      "use sk-proj for the project prefix in docs", // no body
      "sk-short", // too short, no prefix
    ];
    for (const text of benign) {
      expect(ids(text)).not.toContain("openai.key");
    }
  });

  test("twilio.auth_token needs an SID nearby", () => {
    const sid = "AC" + "a".repeat(32);
    const tok = "b".repeat(32);
    expect(ids(`account ${sid} token ${tok}`)).toContain("twilio.auth_token");
    // bare 32-hex with no SID nearby should NOT flag as twilio
    expect(ids(`random ${tok} here`)).not.toContain("twilio.auth_token");
  });

  test("db.url_with_password flags real password, skips placeholder/env-var", () => {
    expect(ids("postgres://user:s3cretP@[email protected]/app")).toContain("db.url_with_password");
    expect(ids("postgres://user:${DB_PASSWORD}@host/app")).not.toContain("db.url_with_password");
    // Literal PASSWORD placeholder (URL-format doc comments).
    expect(ids("postgresql://USER:PASSWORD@host/db")).not.toContain("db.url_with_password");
    // JS template interpolations are code, not credentials — the
    // uppercase-only placeholder form blocked a push over
    // `postgresql://${dbUser}:${dbPass}@...` in a bash->TS port.
    // eslint-disable-next-line no-template-curly-in-string
    expect(ids("postgresql://${dbUser}:${dbPass}@${dbHost}:5432/db")).not.toContain("db.url_with_password");
    // Assembled at runtime so this file's own diff never contains a
    // credential-shaped literal (the prepush guard scans exact pushed bytes).
    expect(ids("postgres://admin:" + "hun" + "[email protected]/app")).toContain("db.url_with_password");
    // Bare $UPPER_SNAKE is shell convention → suppressed; bare $lowercase is
    // NOT an interpolation form — a real password starting with `$` must
    // still block (both-braces-optional would have let it through).
    expect(ids("postgres://user:$DB_PASSWORD@host/app")).not.toContain("db.url_with_password");
    expect(ids("postgres://admin:$" + "hun" + "[email protected]/app")).toContain("db.url_with_password");
    // Mismatched brace is not an interpolation either (assembled at runtime
    // so this file's own pushed bytes carry no blockable URL shape).
    expect(ids("postgres://admin:${" + "[email protected]/app")).toContain("db.url_with_password");
    // A fully-braced interpolation is code whatever it contains — the DSN
    // builder's `${encodeURIComponent(dbPass)}` call site must not scan as a
    // pushed secret.
    expect(ids("postgresql://user:${encodeURIComponent(dbPass)}@host:5432/db")).not.toContain("db.url_with_password");
    // A LOWERCASE literal 'password'/'pass' at the URL-password position is a
    // real (terrible) credential, not a doc placeholder — only the ALL-CAPS
    // doc convention (USER:PASSWORD) is suppressed. Assembled at runtime so
    // this file's own bytes never carry a live credential shape.
    expect(ids("postgres://admin:" + "pass" + "[email protected]/app")).toContain("db.url_with_password");
    expect(ids("https://root:" + "pa" + "[email protected]/")).toContain("creds.basic_auth_url");
    // Structural placeholders still suppress at the URL position.
    expect(ids("postgres://user:<your-password>@host/db")).not.toContain("db.url_with_password");
    // An ALL-CAPS password that is NOT an exact placeholder token is a real
    // secret and must block — the pre-fix shape rule (/^[A-Z][A-Z0-9_]*$/) waved
    // every all-caps password through. Substring of a placeholder word (SECRET)
    // must not rescue it. Assembled at runtime so this file's own pushed bytes
    // carry no live DSN shape.
    expect(ids("postgres://admin:" + "PROD2026" + "[email protected]/app")).toContain("db.url_with_password");
    expect(ids("postgres://admin:" + "ADMIN" + "123@host/db")).toContain("db.url_with_password");
  });

  // Every curated placeholder word must suppress at the URL-password position.
  // The fix replaced a shape rule with a hand-curated EXACT set, so a typo or a
  // dropped entry (CHANGEME -> CHANGME) would silently start blocking a legit
  // doc placeholder with zero failure elsewhere. Loop the real exported set so
  // the test can't drift from the source list.
  test("db.url_with_password suppresses every curated placeholder word", () => {
    for (const word of URL_PASSWORD_PLACEHOLDER_WORDS) {
      expect(ids(`postgres://user:${word}@host/db`)).not.toContain("db.url_with_password");
    }
    // Guard the set stays a non-trivial curated list (catches an accidental clear).
    expect(URL_PASSWORD_PLACEHOLDER_WORDS.size).toBeGreaterThanOrEqual(8);
    // And a real secret that merely CONTAINS a placeholder word still blocks.
    expect(ids("postgres://user:" + "MY" + "SECRETPASS@host/db")).toContain("db.url_with_password");
  });

  test("all HIGH patterns block (exit 3)", () => {
    const r = scan("AKIA1234567890ABCDEF", { repoVisibility: "private" });
    expect(exitCodeFor(r)).toBe(3);
  });
});

describe("MEDIUM demoted credential-shaped patterns (TENSION-1)", () => {
  test("stripe.publishable is MEDIUM not HIGH", () => {
    const f = scan("pk_live_" + "a".repeat(30), { repoVisibility: "private" }).findings.find(
      (x) => x.id === "stripe.publishable",
    );
    expect(f?.tier).toBe("MEDIUM");
  });
  test("google.api_key is MEDIUM", () => {
    const f = scan("AIza" + "a".repeat(35), { repoVisibility: "private" }).findings.find(
      (x) => x.id === "google.api_key",
    );
    expect(f?.tier).toBe("MEDIUM");
  });
  test("an AQ. Gemini key from Google AI Studio is google.api_key", () => {
    const f = scan("AQ.Ab" + "c".repeat(40), { repoVisibility: "private" }).findings.find(
      (x) => x.id === "google.api_key",
    );
    expect(f?.tier).toBe("MEDIUM");
  });
  test("jwt is MEDIUM", () => {
    const jwt = "eyJhbGciOiJ.eyJzdWIiOiI." + "x".repeat(20);
    const f = scan(jwt, { repoVisibility: "private" }).findings.find((x) => x.id === "jwt");
    expect(f?.tier).toBe("MEDIUM");
  });
  test("env.kv fires on high-entropy, skips placeholder", () => {
    expect(ids("API_TOKEN=8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ")).toContain("env.kv");
    expect(ids("API_KEY=changeme")).not.toContain("env.kv");
    expect(ids("API_KEY=${MY_VAR}")).not.toContain("env.kv");
  });

  // #1946 gap 3: the uppercase-`=`-only shape made lowercase and YAML/JSON
  // colon assignments invisible — the exact config shapes people actually
  // push. Each closed detection fail-open gets a pinned case.
  test("env.kv fires on lowercase = assignment (#1946)", () => {
    expect(ids("api_key=8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ")).toContain("env.kv");
  });
  test("env.kv fires on YAML colon assignment (#1946)", () => {
    expect(ids("password: 8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ")).toContain("env.kv");
  });
  test("env.kv fires on quoted JSON key colon assignment (#1946)", () => {
    expect(ids('"apiKey": "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ"')).toContain("env.kv");
  });
  test("env.kv colon/lowercase forms stay entropy-gated and placeholder-safe", () => {
    expect(ids("password: changeme")).not.toContain("env.kv");
    expect(ids("apiKey: YOUR_API_KEY_HERE")).not.toContain("env.kv");
    expect(ids("api_key=${MY_VAR}")).not.toContain("env.kv");
  });
  // T1 calibration: the zero-or-more-prefix net matched ANY identifier ending
  // in a suffix, so ordinary code (`cacheKey: <entropic id>`) hit a MEDIUM
  // confirm prompt. Name shape must be credential-semantic to count.
  test("env.kv ignores non-credential names ending in a suffix (entropic values)", () => {
    const v = "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ";
    expect(ids(`cacheKey: ${v}`)).not.toContain("env.kv");
    expect(ids(`sortKey: ${v}`)).not.toContain("env.kv");
    expect(ids(`partitionKey: ${v}`)).not.toContain("env.kv");
    expect(ids(`hotkey: ${v}`)).not.toContain("env.kv");
    expect(ids(`monkey: ${v}`)).not.toContain("env.kv");
    expect(ids(`idempotencyKey: ${v}`)).not.toContain("env.kv");
  });
  test("env.kv still fires on every credential-shaped name form", () => {
    const v = "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ";
    expect(ids(`api_key=${v}`)).toContain("env.kv"); // (i) separator
    expect(ids(`API_KEY=${v}`)).toContain("env.kv"); // (i) + ALL-CAPS
    expect(ids(`x-access-key: ${v}`)).toContain("env.kv"); // (i) dash separator
    expect(ids(`key: ${v}`)).toContain("env.kv"); // (ii) bare suffix
    expect(ids(`APIKEY=${v}`)).toContain("env.kv"); // (iii) ALL-CAPS compound
    expect(ids(`apiKey: ${v}`)).toContain("env.kv"); // (iv) credential camel
    expect(ids(`authToken: ${v}`)).toContain("env.kv"); // (iv) credential camel
    expect(ids(`clientSecret: ${v}`)).toContain("env.kv"); // (iv) credential camel
  });
  // #3208: a catalog entry whose whole name is a data-model word plus `_key`
  // and whose value is a lowercase identifier names a field, not a credential.
  // Either side alone is not enough: an entropic value, a longer name or a
  // credential name still fires, in every config format.
  test("env.kv skips schema field identifiers only when name and value both say so (#3208)", () => {
    const v = "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ";
    expect(ids('{\n  "input_key": "expected_media_rate_bits_per_second_ingress",\n  "unit": "bps"\n}')).not.toContain("env.kv");
    for (const line of ["input_key: expected_media_rate_bits_per_second_ingress", '"output_key": "bits.per.second.egress"', "cache-key: user-profile-v2", "sort_key = created_at_desc"])
      expect(ids(line)).not.toContain("env.kv");
    for (const line of [
      `input_key: ${v}`, `INPUT_KEY=${v}`, `input_key: expectedMediaRateBitsPerSecond`, "input_key: abcdefghijkmnop",
      `cosmosdb_primary_key=${v}`, `cosmosdb_primary_key: ${v}`, `{\n  "cosmosdb_primary_key": "${v}"\n}`, `cosmosdb_primary_key = "${v}"`,
      `primary_key: ${v}`, `api_cache_key: ${v}`, "api_key: expected_media_rate_bits_per_second_ingress", "cosmosdb_primary_key: expected_media_rate_bits_per_second",
    ])
      expect(ids(line)).toContain("env.kv");
  });
  // #3048 — in a TS/JSX file an unquoted value after `:` is a type or an
  // expression, and a JSX brace of names is an expression. Decided by file
  // context: the same shapes in .env/YAML (or with no path) are literals.
  test("env.kv skips TS/JSX source syntax by file context, never by value shape (#3048)", () => {
    const kv = (text: string, sourcePath?: string) =>
      scan(text, { repoVisibility: "private", ...(sourcePath ? { sourcePath } : {}) }).findings.some((f) => f.id === "env.kv");
    const v = "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ";
    const signature = "export async function issueServerCoachItem(\n  db: Pool,\n  session: SessionState,\n  item: IssuedCoachItem,\n): Promise<void> {}";
    for (const [code, file] of [
      [signature, "src/coach.ts"],
      ["interface Props {\n  session: SessionState;\n}", "src/props.ts"],
      ["key={turn.requestId + turn.role + index}", "src/Turn.tsx"],
      ["apiKey={settings.apiKey}", "src/Settings.jsx"],
    ] as const) expect({ code, kv: kv(code, file) }).toEqual({ code, kv: false });
    for (const [literal, file] of [
      ["API_KEY=VelvetRiverOrbitSunset;", ".env"],
      ["password: {RiverStoneAmberCedar}", "config/app.yml"],
      [signature, undefined],
      ["  session: SessionState,", "deploy/values.yaml"],
      [`session: "${v}",`, "src/a.ts"], [`key={${v}}`, "src/A.tsx"], [`API_KEY={${v}}`, "src/A.tsx"],
      [`key={settings.apiKey + "${v}"}`, "src/A.tsx"], [`key="${v}"`, "src/A.tsx"], [`api_key: ${v}`, ".env"],
    ] as const) expect({ literal, file, kv: kv(literal, file) }).toEqual({ literal, file, kv: true });
    const jwt = ["eyJhbGciOiJIUzI1NiJ9", "eyJzdWIiOiIxMjM0NTY3ODkwIn0", "dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U"].join(".");
    expect(scan(`token: ${jwt},`, { repoVisibility: "private", sourcePath: "src/auth.ts" }).findings.map((f) => f.id)).toContain("jwt");
  });
  // #3630 (#3605, #3418, #3419) — the #3048 rule, extended: Dart and Python have
  // no unquoted string literal either, so a bare name chain after `:` is a name;
  // a JSX brace may join names with ?? || && as well as +. Each widening keeps a
  // negative control that still fires.
  test("env.kv skips Dart/Python source names and JSX logical joins, never a literal (#3630)", () => {
    const kv = (text: string, sourcePath?: string) =>
      scan(text, { repoVisibility: "private", ...(sourcePath ? { sourcePath } : {}) }).findings.some((f) => f.id === "env.kv");
    const v = "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ";
    const vIdent = "Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJx";
    for (const [code, file] of [
      ["def fetch_days(\n    *,\n    session: HttpSession,\n    start: int,\n) -> None:\n    pass", "app.py"],
      ["      password: pydantic.SecretStr", "app/models.py"],
      ["      password: pydantic.SecretStr", "app/models.pyi"],
      ['      "password": request.password,', "app/api/handlers.py"],
      ['      "api_key": settings.api_key,', "app/api/handlers.py"],
      ["      'password': request.password,", "backend/lib/src/handlers/onboarding_handlers.dart"],
      ["              password: request.password,", "backend/lib/src/handlers/pfs_handlers.dart"],
      ["const doc = '''usage''';\n  password: request.password,", "backend/lib/src/config.dart"],
      ["key={workout.id ?? index}", "src/SessionList.jsx"],
      ["key={item.uuid || item.slug}", "src/List.tsx"],
      ["key={row.ready && row.id}", "src/Grid.tsx"],
      // A trailing comment is not part of the expression.
      ["  password: req.body.password // don't log it (see docs", "src/a.ts"], ["    password: pydantic.SecretStr  # the user's password, or", "app/models.py"],
      ["    'password': request.password // don't log", "lib/a.dart"],
      // A quote inside a comment opens nothing.
      ["# it's a session\ndef f(\n    session: HttpSession,\n): pass", "app.py"], ["/* it's a session */\n  password: request.password,", "lib/a.dart"],
      // A literal in the next pair on the line belongs to that pair.
      ['    "session": request.session, "mode": "readwrite-primary",', "app.py"],
      // Quotes that close before the name leave it code.
      ["    login('it\\'s', password: request.password);", "lib/a.dart"], ['    "user": "a", "password": request.password,', "app.py"],
      // The triple-quote guard is Dart/Python only: a stray ''' in a TS string leaves #3048 as it was.
      ["const s = \"'''\";\n  session: SessionState,", "src/a.ts"],
      // #4209: a plain assignment of a dotted name reads an attribute; the keyword form below still fires.
      ["      password = request.password", "app/api/handlers.py"],
    ] as const) expect({ code, file, kv: kv(code, file) }).toEqual({ code, file, kv: false });
    for (const [literal, file] of [
      ["    session: HttpSession,", "deploy/values.yaml"],
      ["    session: HttpSession,", undefined],
      [`    session: "${v}",`, "app.py"],
      [`      "password": "${v}",`, "app/api/handlers.py"],
      [`  password = "${v}"`, "app/config.py"],
      ["      password=request.password,", "app/api/handlers.py"],
      [`CONFIG = """\npassword: ${vIdent}\n"""`, "tests/conftest.py"],
      [`      'password': '${v}',`, "backend/lib/src/handlers/onboarding_handlers.dart"],
      [`  password: "${v}",`, "backend/lib/src/config.dart"],
      [`  password = "${v}";`, "backend/lib/src/config.dart"],
      [`  password: ${v},`, "backend/lib/src/config.dart"],
      [`const fixture = '''\npassword: ${vIdent}\n''';`, "test/fixtures.dart"],
      [`key={settings.apiKey ?? "${v}"}`, "src/A.tsx"],
      [`key={${v} || index}`, "src/A.tsx"],
      ["key={workout.id ?? index}", ".env"],
      // An annotation with a literal default is still a literal.
      [`    api_token: AuthToken = "${v}"`, "app.py"], [`    api_token: AuthToken="${v}"`, "app.py"],
      [`  apiToken: AuthToken = "${v}";`, "src/a.ts"], [`  apiToken: AuthToken = '${v}',`, "lib/a.dart"],
      // An escaped delimiter does not close the block.
      [`CONFIG = """\n\\"""\npassword: ${vIdent}\n"""`, "tests/conftest.py"],
      // A delimiter in a comment or a one-line string opens nothing, so the block after it is still a block.
      [`# don't use """ here\nCONFIG = """\npassword: ${vIdent}\n"""`, "tests/conftest.py"],
      [`DELIM = '"""'\nCONFIG = """\npassword: ${vIdent}\n"""`, "tests/conftest.py"],
      [`// no ''' here\nconst fixture = '''\npassword: ${vIdent}\n''';`, "test/fixtures.dart"],
      [`/* ''' */\nconst fixture = '''\npassword: ${vIdent}\n''';`, "test/fixtures.dart"],
      [`/* outer /* ''' */ still a comment ''' */\nconst fixture = '''\npassword: ${vIdent}\n''';`, "test/fixtures.dart"],
      [`const dir = r'''C:\\dir\\''';\nconst fixture = '''\npassword: ${vIdent}\n''';`, "test/fixtures.dart"],
      // An expression that continues on the next line may carry a literal there.
      [`    api_token: AuthToken = (\n        "${v}"\n    )`, "app.py"],
      // A literal fallback after a name is still a literal.
      [`      "password": settings.db_pass or "${v}",`, "app.py"], [`      'password': request.password ?? '${v}',`, "lib/a.dart"],
      // A one-line string continued by a backslash spans lines.
      [`CFG = "db: \\\npassword: ${vIdent} \\\n"`, "app.py"],
      // A key and value inside one quoted string are text, not a dict entry.
      [`    'password: ${vIdent} more',`, "lib/a.dart"], [`    "password: ${vIdent} more",`, "app.py"],
    ] as const) expect({ literal, file, kv: kv(literal, file) }).toEqual({ literal, file, kv: true });
  });
  // Value: protects=an interpolated expression inside a Python f-string or Dart string is skipped whole, so a quote inside it never ends the outer string; fails_when=a """/''' delimiter in a nested string closes the block early and a password: line inside it reads as code;
  //   why_new=GST-738 (outside voice); seam=none; no_claim=Python before 3.12, where the nested quote is a syntax error
  test("env.kv: a quote inside an f-string or Dart interpolation does not end the string (GST-738)", () => {
    const kv = (text: string, sourcePath: string) => scan(text, { repoVisibility: "private", sourcePath, processEnv: {} }).findings.some((f) => f.id === "env.kv");
    const v = "Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJx";
    for (const [literal, file] of [
      [`CONFIG = f'''\n{"'''"}\npassword: ${v}\n'''\n`, "app.py"],
      [`CONFIG = rf"""\n{x.replace('"""', "")}\npassword: ${v}\n"""\n`, "app.py"],
      [`CONFIG = f'''\n{f"{'\'\'\''}"}\npassword: ${v}\n'''\n`, "app.py"],
      [`const s = '''\${"'''"}\npassword: ${v}\n''';\n`, "lib/a.dart"],
      [`const s = """\${m["k"] ?? '"""'}\npassword: ${v}\n""";\n`, "lib/a.dart"],
      // A comment inside an interpolation holds no delimiter, and a Dart block comment is text.
      [`CONFIG = f"""\n{1 # } """\n}\npassword: ${v}\n"""\n`, "app.py"],
      [`const s = '''\${x // '''\n}\npassword: ${v}\n''';\n`, "lib/a.dart"],
      [`void main() {}\n/*\npassword: ${v}\n*/\n`, "lib/a.dart"],
      // A format spec is text: its # or quote opens nothing.
      [`render({\n    "id": f"{n:#x}",\n}, """\npassword: ${v}\n""")\n`, "app.py"],
      [`render({\n    "id": f"{n:'>10}",\n}, """\npassword: ${v}\n""")\n`, "app.py"],
      [`render({\n    "id": f"{n!r:{w}}",\n}, """\npassword: ${v}\n""")\n`, "app.py"],
      // A # comment in a single-quoted f-string's multi-line field holds no delimiter either.
      [`msg = f"{\n  1 # }" """\n}"\nCONFIG = """\npassword: ${v}\n"""\n`, "app.py"],
      // In an f-string a backslash does not escape the brace after it.
      [`CONFIG = f"""\\{"""\npassword: ${v}\n"""}"""\n`, "app.py"],
      // Nesting past the depth cap runs to the end of the text instead of overflowing the stack.
      ["const s = '" + "${'".repeat(20000) + `\npassword: ${v}\n`, "lib/a.dart"],
      ["x = f'" + "{a:".repeat(100000) + `\npassword: ${v}\n`, "app.py"],
    ] as const) expect({ literal, kv: kv(literal, file) }).toEqual({ literal, kv: true });
    for (const [code, file] of [
      ["msg = f'{a}:{b}'\ndef f(\n    session: HttpSession,\n): pass\n", "app.py"],
      ["msg = f'''{{literal brace'''\ndef f(\n    session: HttpSession,\n): pass\n", "app.py"],
      ["msg = f'{x:>{width}}'\ndef f(\n    session: HttpSession,\n): pass\n", "app.py"],
      ["msg = f\"{when:%b %d '%y} {d[':']} {a[1:2]} {a!=b}\"\nrows = {\n    \"password\": session.password,\n}\n", "app.py"],
      // Code inside a multi-line interpolation is code, not string text.
      ["msg = f\"{\n {\n  'password': request.password,\n }\n}\"\n", "app.py"],
      ["final s = \"${login(\n  password: request.password,\n)}\";\n", "lib/a.dart"],
      ["if'a' in s: pass\ndef f(\n    session: HttpSession,\n): pass\n", "app.py"],
      ["final s = 'hi ${user.name} and $id';\n  login(password: request.password);\n", "lib/a.dart"],
      ["final s = r'${raw}';\n  login(password: request.password);\n", "lib/a.dart"],
    ] as const) expect({ code, kv: kv(code, file) }).toEqual({ code, kv: false });
  });
  test("env.kv's source-syntax check stays linear on a match-dense file (#3630)", () => {
    for (const file of ["app.py", "src/a.ts"]) {
      const text = "def f(\n" + "    session: HttpSession,\n".repeat(12_000) + "): pass\n";
      const t0 = performance.now();
      const found = scan(text, { repoVisibility: "private", sourcePath: file }).findings.filter((f) => f.id === "env.kv").length;
      expect({ file, found, fast: performance.now() - t0 < 3_000 }).toEqual({ file, found: 0, fast: true });
    }
  });
  // #2912 — a line that READS a secret from the environment holds no secret;
  // it must not fire (and so must not be masked or withhold a source file).
  test("env.kv skips exact environment reads (#2912)", () => {
    for (const line of [
      'api_key=os.environ["AGENTOPS_API_KEY"]',
      '        api_key=os.environ["AGENTOPS_API_KEY"]',
      "LIVEKIT_API_KEY = os.getenv('LIVEKIT_API_KEY')",
      'token = os.environ.get("GH_TOKEN")',
      "API_KEY=process.env.OPENAI_API_KEY",
      "SECRET_KEY=getenv(\"APP_SECRET_KEY\")",
    ]) {
      expect(ids(line)).not.toContain("env.kv");
      expect(redactFindingSpans(`x=1\n${line}\n`, {})).toBe(`x=1\n${line}\n`);
    }
  });
  test("env.kv still fires on real high-entropy keys beside or instead of an env read (#2912 negative controls)", () => {
    const v = "Zq8vR2mN5tYb7Lc3Wd9K";
    expect(ids(`SECRET_KEY = os.getenv('DJANGO_SECRET', '${v}')`)).toContain("env.kv");
    expect(ids(`API_KEY=process.env.OPENAI_API_KEY||"${v}"`)).toContain("env.kv");
    expect(ids(`API_KEY=os.environ["X"]+"${v}"`)).toContain("env.kv");
    expect(ids(`API_KEY=os.environ_${v}`)).toContain("env.kv");
    expect(ids(`API_KEY=getenv${v}`)).toContain("env.kv");
    expect(ids(`API_KEY="${v}"`)).toContain("env.kv");
  });
  test("env.kv stays MEDIUM (calibration: generic net, not a blocker)", () => {
    const f = scan("api_key=8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJ", { repoVisibility: "private" })
      .findings.find((x) => x.id === "env.kv");
    expect(f?.tier).toBe("MEDIUM");
  });

  // #1946 — Bearer is the most FP-prone shape in the wave: docs and examples
  // are full of "Authorization: Bearer <token>". MEDIUM + header proximity +
  // the env.kv entropy recipe keep it calibrated.
  test("auth.bearer fires on a high-entropy token in header context", () => {
    const text = "curl -H 'Authorization: Bearer 8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJq'";
    const f = scan(text, { repoVisibility: "private" }).findings.find(
      (x) => x.id === "auth.bearer",
    );
    expect(f).toBeDefined();
    expect(f?.tier).toBe("MEDIUM");
  });
  test("auth.bearer skips placeholders and env interpolations", () => {
    expect(ids("Authorization: Bearer YOUR_TOKEN_HERE_PLACEHOLDER")).not.toContain("auth.bearer");
    expect(ids("Authorization: Bearer ${ACCESS_TOKEN_FROM_ENV}")).not.toContain("auth.bearer");
  });
  test("auth.bearer requires header context (bare 'Bearer x' prose doesn't fire)", () => {
    expect(ids("the Bearer 8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJq walked in")).not.toContain(
      "auth.bearer",
    );
  });
});

describe("#1946 pattern negatives (placeholders never fire)", () => {
  test("short or placeholder shapes don't trip the new HIGH patterns", () => {
    expect(ids("glpat-xxxx")).not.toContain("gitlab.token");
    expect(ids("hf_token")).not.toContain("huggingface.token");
    expect(ids("gsk_key")).not.toContain("groq.key");
    expect(ids("tvly-key")).not.toContain("tavily.key");
    expect(ids("ntn_token")).not.toContain("notion.token");
    // `secret_` is an ordinary word; only the length makes it a credential.
    expect(ids("secret_value")).not.toContain("notion.token");
    expect(ids("npm_install")).not.toContain("npm.token");
    expect(ids("dop_v1_short")).not.toContain("digitalocean.token");
    // pem header WITHOUT the GCP JSON shape stays pem.private_key only.
    expect(ids("-----BEGIN PRIVATE KEY-----")).not.toContain("gcp.service_account");
  });
});

describe("OpenRouter, GitHub ghu_/ghr_, Stripe restricted, Supabase and AWS temporary key negatives", () => {
  test("near-miss shapes never fire", () => {
    expect(ids("sk-or-v1-" + "9f8e7d6c5b4a39281706f5e4d3c2b1a0")).not.toContain("openrouter.key");
    expect(ids("ghu_" + "a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R")).not.toContain("github.user_to_server");
    expect(ids("ghr_" + "a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R")).not.toContain("github.refresh");
    expect(ids("rk_test_" + "a1B2c3D4e5F6g7H8i9J0k1L2")).not.toContain("stripe.restricted");
    expect(ids("sb_publishable_" + "tR4i6cyMIrYTE3s6OyHGHw_ppx2p6WK")).not.toContain("supabase.secret");
    expect(ids("sb_secret_key")).not.toContain("supabase.secret");
    expect(ids("sbp_" + "9f8e7d6c5b4a39281706f5e4d3c2b1a09f8e7d6")).not.toContain("supabase.pat");
    expect(ids("sbp_" + "Zf8e7d6c5b4a39281706f5e4d3c2b1a09f8e7d6c")).not.toContain("supabase.pat");
    expect(ids("ASIA" + "ZQ3XN7P2LM4RT8W")).not.toContain("aws.access_key");
  });
});

describe("google.oauth_client_secret / telegram.bot_token negatives", () => {
  test("undersized and placeholder shapes never fire", () => {
    // Length floor keeps short repo fixtures quiet (e.g. the 19-char body in
    // openclaw's extensions/google/oauth.test.ts).
    expect(ids("GOCSPX-FakeSecretValue123")).not.toContain("google.oauth_client_secret");
    expect(ids("GOCSPX-short")).not.toContain("google.oauth_client_secret");
    // Placeholder suppression on an otherwise correctly-sized body.
    expect(ids("GOCSPX-example" + "a".repeat(17))).not.toContain("google.oauth_client_secret");
    expect(ids("1234567890:AAexample" + "a".repeat(26))).not.toContain("telegram.bot_token");
    // A plain number pair must not read as a bot token.
    expect(ids("1234567890:1234567890")).not.toContain("telegram.bot_token");
    // The AIza key stays MEDIUM (google.api_key); it is not promoted here.
    expect(ids("AIza" + "a".repeat(35))).not.toContain("google.oauth_client_secret");
  });
});

describe("PII patterns", () => {
  test("email flags + is autoRedactable", () => {
    const f = scan("ping [email protected] please", { repoVisibility: "private" }).findings.find(
      (x) => x.id === "pii.email",
    );
    expect(f).toBeTruthy();
    expect(f?.autoRedactable).toBe(true);
  });
  test("email allowlist: example.com, noreply, self, repo-public", () => {
    expect(ids("see [email protected]")).not.toContain("pii.email");
    expect(ids("from [email protected]")).not.toContain("pii.email");
    expect(
      scan("[email protected]", { repoVisibility: "private", selfEmail: "[email protected]" }).findings,
    ).toHaveLength(0);
    expect(
      scan("[email protected]", { repoVisibility: "private", repoPublicEmails: ["[email protected]"] }).findings,
    ).toHaveLength(0);
  });
  // A git SSH remote's `git@host` is a transport user@host, not a person's
  // address. Suppressed by URL SHAPE rather than by allowlisting the `git`
  // local part: a bare `git@` entry would also silently hide a real address
  // at a domain that merely starts with "git".
  test("ssh git remotes are not flagged as emails", () => {
    expect(ids("set :repo_url, '[email protected]:acme/widgets.git'")).not.toContain(
      "pii.email",
    );
    expect(ids("git clone [email protected]:acme/widgets.git")).not.toContain("pii.email");
    expect(ids("[email protected]:acme/widgets.git")).not.toContain("pii.email");
    expect(ids("[email protected]:v3/acme/widgets/widgets")).not.toContain("pii.email");
    expect(ids("ssh -T [email protected]")).not.toContain("pii.email");
    // General case: any host in <user>@<host>:<path>.git position.
    expect(ids("[email protected]:infra/tools.git")).not.toContain("pii.email");
    expect(ids("ssh://[email protected]/infra/tools.git")).not.toContain("pii.email");
  });
  test("a real address is still flagged, including at a git host", () => {
    expect(ids("ping [email protected] about the issue")).toContain("pii.email");
    // A domain that merely STARTS WITH "git" is not a git host — this is the
    // case a bare `git@` local-part allowlist would have wrongly suppressed.
    expect(ids("contact [email protected] for access")).toContain("pii.email");
  });
  test("phone E.164 flags, skips compact timestamps", () => {
    expect(ids("call +14155550123 now")).toContain("pii.phone.e164");
    expect(ids("backup stamp 20260727202423 ran late")).not.toContain("pii.phone.e164");
  });
  test("ssn flags valid, skips 000 octet", () => {
    expect(ids("ssn 123-45-6789")).toContain("pii.ssn");
    expect(ids("000-12-3456")).not.toContain("pii.ssn");
  });
  test("credit card needs Luhn", () => {
    expect(ids("card 4111111111111111")).toContain("pii.cc");
    expect(ids("num 4111111111111112")).not.toContain("pii.cc");
  });
  test("public IP flagged, RFC1918 skipped", () => {
    expect(ids("connect 8.8.8.8")).toContain("pii.ip_public");
    expect(ids("local 192.168.1.5")).not.toContain("pii.ip_public");
    expect(ids("local 10.0.0.1")).not.toContain("pii.ip_public");
  });

  // Digit-only UUIDs are the standard test-fixture shape, and their digit runs
  // collide with both the card pattern (a 13-19 digit slice passes Luhn often
  // enough to matter) and the phone pattern (hyphen groups read as national
  // formatting). Observed live: 14 of 21 MEDIUM findings on one ordinary branch
  // were exactly this, all from test files — the volume that makes people stop
  // reading MEDIUM output at all.
  test("digit-only UUID fixtures are not cards or phones", () => {
    expect(ids("owner_user_id: '00000000-0000-0000-0000-000000000000'")).not.toContain("pii.cc");
    expect(ids("const OWNER = '11111111-1111-1111-1111-111111111111'")).not.toContain(
      "pii.phone.e164",
    );
    expect(ids("const TEAM = '22222222-2222-2222-2222-222222222222'")).not.toContain(
      "pii.phone.e164",
    );
    // Hex UUIDs never matched these digit patterns; pinned so the suppression
    // is not silently widened to something that swallows real numbers.
    expect(ids("id 'a1b2c3d4-1111-2222-3333-444455556666'")).not.toContain("pii.cc");
  });

  test("UUID suppression requires TOTAL containment", () => {
    // Real card sitting next to a UUID still reports — suppression is the
    // exception and may only fire when the whole match is UUID interior.
    expect(ids("00000000-0000-0000-0000-000000000000 4111111111111111")).toContain("pii.cc");
    // And the plain cases are untouched.
    expect(ids("card 4111-1111-1111-1111")).toContain("pii.cc");
    expect(ids("reach me on +1 415 555 2671")).toContain("pii.phone.e164");
  });
});

describe("internal + legal patterns", () => {
  test("internal hostname", () => {
    expect(ids("db1.corp internal host")).toContain("internal.hostname");
  });
  test("localhost url with path", () => {
    expect(ids("hit http://localhost:8080/admin/secrets")).toContain("internal.url_private");
  });
  test("NDA marker", () => {
    expect(ids("This is CONFIDENTIAL material")).toContain("legal.nda_marker");
  });
  test("named criticism needs a capitalized full name nearby", () => {
    expect(ids("John Smith is incompetent at this")).toContain("legal.named_criticism");
    expect(ids("the build is incompet019ently configured".replace("019", ""))).not.toContain(
      "legal.named_criticism",
    );
  });
});

describe("LOW patterns surface only", () => {
  test("user path is LOW", () => {
    const f = scan("/Users/bob/secret/config", { repoVisibility: "private" }).findings.find(
      (x) => x.id === "internal.user_path",
    );
    expect(f?.tier).toBe("LOW");
  });
  test("TODO marker is LOW", () => {
    const f = scan("TODO(alice) fix later", { repoVisibility: "private" }).findings.find(
      (x) => x.id === "hygiene.todo",
    );
    expect(f?.tier).toBe("LOW");
  });
  test("gstack-shortcut(dec-*) debt marker survives redaction untouched", () => {
    // The shortcut ledger convention (AskUserQuestion Format / /retro Step 11.5)
    // ships in commits and PR bodies BY DESIGN — the ledger is the point. It
    // must not be caught by hygiene.todo's TODO(owner) shape or anything else.
    const marker =
      "// gstack-shortcut(dec-a1b2c3): global lock, upgrade when throughput matters";
    const result = scan(marker, { repoVisibility: "public" });
    expect(result.findings).toHaveLength(0);
  });
});

describe("placeholder suppression (per-span)", () => {
  test("AWS docs EXAMPLE key not flagged", () => {
    expect(ids("AKIAIOSFODNN7EXAMPLE")).not.toContain("aws.access_key");
  });
  test("your_ prefix not flagged", () => {
    expect(isPlaceholderSpan("your_api_key")).toBe(true);
  });
  test("a real secret on a line that ALSO contains EXAMPLE still flags", () => {
    // line-based suppression would wrongly skip this; per-span must catch it.
    expect(ids("# EXAMPLE usage\nkey AKIA1234567890ABCDEF")).toContain("aws.access_key");
  });
});

describe("no visibility-based tier promotion (TENSION-2-followup)", () => {
  test("email stays MEDIUM on both private and public", () => {
    const priv = scan("[email protected]", { repoVisibility: "private" }).findings[0];
    const pub = scan("[email protected]", { repoVisibility: "public" }).findings[0];
    expect(priv.tier).toBe("MEDIUM");
    expect(pub.tier).toBe("MEDIUM");
    expect(pub.severity).toBe("MEDIUM"); // NOT promoted to HIGH
    expect(pub.repoVisibility).toBe("public"); // recorded for sterner wording
  });
  test("demoted credential patterns stay MEDIUM on public", () => {
    const pub = scan("pk_live_" + "a".repeat(30), { repoVisibility: "public" }).findings[0];
    expect(pub.severity).toBe("MEDIUM");
  });
  test("unknown visibility treated as public for wording, still no promotion", () => {
    const r = scan("[email protected]", { repoVisibility: "unknown" });
    expect(r.findings[0].severity).toBe("MEDIUM");
  });
});

describe("tool-attributed fence WARN-degrade (TENSION-3)", () => {
  test("placeholder-shaped credential in tool fence → WARN", () => {
    const text = "```codex-review\nfound your_aws_key AKIAIOSFODNN7EXAMPLE in code\n```";
    const r = scan(text, { repoVisibility: "private" });
    // the EXAMPLE key is suppressed as placeholder; verify a non-credential note doesn't block
    expect(r.counts.HIGH).toBe(0);
  });
  test("live-format credential in tool fence STILL blocks", () => {
    const text = "```codex-review\nleaked AKIA1234567890ABCDEF here\n```";
    const r = scan(text, { repoVisibility: "private" });
    expect(r.counts.HIGH).toBe(1); // not degraded — live format
  });
  test("AKIA outside any fence blocks", () => {
    expect(exitCodeFor(scan("AKIA1234567890ABCDEF", {}))).toBe(3);
  });
});

describe("normalization", () => {
  test("zero-width chars inside a key are stripped before matching", () => {
    const zwsp = "​";
    const broken = "AKIA1234567890" + zwsp + "ABCDEF";
    expect(ids(broken)).toContain("aws.access_key");
  });
  test("HTML entity decode", () => {
    const { normalized } = normalizeWithMap("a &amp; b");
    expect(normalized).toBe("a & b");
  });
  test("offset map points back into original", () => {
    const input = "xy​z";
    const { normalized, map } = normalizeWithMap(input);
    expect(normalized).toBe("xyz");
    // 'z' is at normalized index 2, original index 3
    expect(map[2]).toBe(3);
  });
});

describe("oversize fails CLOSED", () => {
  test("input over the byte cap returns a single blocking HIGH finding", () => {
    const big = "a".repeat(2000);
    const r = scan(big, { maxBytes: 1000 });
    expect(r.oversize).toBe(true);
    expect(r.counts.HIGH).toBe(1);
    expect(r.findings[0].id).toBe("engine.input_too_large");
    expect(exitCodeFor(r)).toBe(3);
  });

  // #1824: a malformed --max-bytes used to reach the engine as NaN. `byteLen >
  // NaN` is always false, silently disabling the fail-closed guard. The engine
  // guardrail must fall back to the default cap for any non-finite / <= 0 value.
  test("NaN maxBytes falls back to the default cap (does NOT disable the guard)", () => {
    const big = "a".repeat(2 * 1024 * 1024); // > 1 MiB default cap
    const r = scan(big, { maxBytes: NaN });
    expect(r.oversize).toBe(true);
    expect(r.findings[0].id).toBe("engine.input_too_large");
    expect(exitCodeFor(r)).toBe(3);
  });

  test("negative / zero maxBytes falls back to the default cap", () => {
    // negative would make `byteLen > -5` always true (block everything);
    // the guardrail normalizes it to the default instead.
    const small = "ok";
    expect(scan(small, { maxBytes: -5 }).oversize).toBeFalsy();
    expect(scan(small, { maxBytes: 0 }).oversize).toBeFalsy();
    const big = "a".repeat(2 * 1024 * 1024);
    expect(scan(big, { maxBytes: -5 }).oversize).toBe(true);
  });
});

describe("validators", () => {
  test("luhn", () => {
    expect(luhnValid("4111111111111111")).toBe(true);
    expect(luhnValid("4111111111111112")).toBe(false);
  });
  test("entropy", () => {
    expect(shannonEntropy("aaaaaaaa")).toBeLessThan(1);
    expect(shannonEntropy("8Fk2pQ9vXz4wL7mN")).toBeGreaterThan(3);
  });
  test("isPublicIPv4", () => {
    expect(isPublicIPv4("8.8.8.8")).toBe(true);
    expect(isPublicIPv4("10.1.2.3")).toBe(false);
    expect(isPublicIPv4("172.16.5.5")).toBe(false);
    expect(isPublicIPv4("999.1.1.1")).toBe(false);
  });
});

describe("masking + purity", () => {
  test("preview never leaks more than 4 leading chars", () => {
    expect(maskPreview("AKIA1234567890ABCDEF")).toBe("AKIA********…");
    expect(maskPreview("abc")).toBe("abc");
  });
  test("scan is pure — same input twice yields identical findings", () => {
    const a = scan("AKIA1234567890ABCDEF [email protected]", { repoVisibility: "public" });
    const b = scan("AKIA1234567890ABCDEF [email protected]", { repoVisibility: "public" });
    expect(a).toEqual(b);
  });
});

describe("redactFindingSpans — machine-egress masking (#1947)", () => {
  test("clean input passes through unchanged", () => {
    const text = "push failed: remote rejected the branch";
    expect(redactFindingSpans(text, { repoVisibility: "private" })).toBe(text);
  });

  test("a single finding's span becomes <REDACTED-{id}>, context survives", () => {
    const token = "ghp_" + "1234567890abcdefghijklmnopqrstuvwxyz";
    const out = redactFindingSpans(`auth ${token} rejected`, { repoVisibility: "private" });
    expect(out).toBe("auth <REDACTED-github.pat> rejected");
  });

  test("a 76-character GitHub refresh token is masked whole", () => {
    const token = "ghr_" + "a1B2c3D4e5F6g7H8i9J0".repeat(3) + "k1L2m3N4o5P6q7R8";
    const out = redactFindingSpans(`auth ${token} rejected`, { repoVisibility: "private" });
    expect(out).toBe("auth <REDACTED-github.refresh> rejected");
  });

  test("multiple findings are all replaced (right-to-left splice keeps offsets valid)", () => {
    const pat = "ghp_" + "1234567890abcdefghijklmnopqrstuvwxyz";
    const aws = "AKIA1234567890ABCDEF";
    const out = redactFindingSpans(`first ${aws} then ${pat} end`, {
      repoVisibility: "private",
    });
    expect(out).toBe("first <REDACTED-aws.access_key> then <REDACTED-github.pat> end");
  });

  test("masks an anchored env.kv value rather than withholding the whole payload", () => {
    const secret = "8Fk2pQ9vXz4wL7mN3rT6yB1cD5eG0hJq";
    const out = redactFindingSpans(`API_KEY=${secret}`, { repoVisibility: "private" });
    expect(out).toBe("API_KEY=<REDACTED-env.kv>");
  });

  test("line/col at boundaries: line start, after blank lines, first char, last unterminated line", () => {
    const token = "ghp_" + "1234567890abcdefghijklmnopqrstuvwxyz";
    const at = (text: string) => {
      const f = scan(text, { repoVisibility: "private" }).findings.find((x) => x.id === "github.pat");
      expect(f).toBeDefined();
      return [f!.line, f!.col];
    };
    expect(at(`a\nb\n${token} x`)).toEqual([3, 1]);
    expect(at(`a\n\n\n  ${token}`)).toEqual([4, 3]);
    expect(at(token)).toEqual([1, 1]);
    expect(at(`one\r\ntwo ${token}`)).toEqual([2, 5]);
    expect(redactFindingSpans(`a\nb\n${token} x`, { repoVisibility: "private" })).toBe("a\nb\n<REDACTED-github.pat> x");
  });

  test("multiline input redacts a finding past the first line (original span map)", () => {
    const token = "ghp_" + "1234567890abcdefghijklmnopqrstuvwxyz";
    const out = redactFindingSpans(`line one\nline two has ${token}\nline three`, {
      repoVisibility: "private",
    });
    expect(out).toBe("line one\nline two has <REDACTED-github.pat>\nline three");
  });

  // Pre-landing review CRITICAL: pem.private_key and gcp.service_account
  // capture only the HEADER, not the key material — a span splice would
  // redact the marker and forward the key body. Marker-only patterns must
  // drop the whole payload.
  test("PEM private key → null (header-only span must not forward the key body)", () => {
    const msg =
      "deploy failed: -----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC\n-----END PRIVATE KEY-----";
    expect(redactFindingSpans(msg, { repoVisibility: "private" })).toBeNull();
  });

  test("GCP service-account JSON → null (key body follows the captured marker)", () => {
    const msg =
      'config dump: {"private_key_id": "abc123", "private_key": "-----BEGIN PRIVATE KEY-----\\nMIIEvQIBADANBg..."}';
    expect(redactFindingSpans(msg, { repoVisibility: "private" })).toBeNull();
  });

  // Pre-landing review: overlapping spans (a Bearer token that is also a
  // JWT) must coalesce — independent splices apply stale offsets and can
  // leave trailing secret bytes or mangled markers.
  test("overlapping spans (Bearer JWT fires auth.bearer + jwt) never leak and produce clean markers", () => {
    const jwt = "eyJ" + "a".repeat(20) + ".eyJ" + "b".repeat(20) + "." + "c".repeat(20);
    const out = redactFindingSpans(`Authorization: Bearer ${jwt}`, { repoVisibility: "private" });
    expect(out).not.toBeNull();
    expect(out!).not.toContain("eyJ");
    expect(out!).not.toContain("aaaa");
    expect(out!).not.toContain("cccc");
    // One coalesced, well-formed marker — no truncated fragments.
    expect(out!).toMatch(/^Authorization: Bearer <REDACTED-[a-z._+]+>$/);
  });
});

describe("taxonomy integrity", () => {
  test("every pattern has a unique id", () => {
    const set = new Set(PATTERNS.map((p) => p.id));
    expect(set.size).toBe(PATTERNS.length);
  });
  test("autoRedactable patterns have a redactToken", () => {
    for (const p of PATTERNS) {
      if (p.autoRedactable) expect(p.redactToken).toBeTruthy();
    }
  });
});

describe("#3048: gstack-redact --from-file passes the file's path as scan context", () => {
  test("the same line is source syntax in a .ts file and a literal in YAML", () => {
    const fs = require("node:fs") as typeof import("node:fs");
    const os = require("node:os") as typeof import("node:os");
    const path = require("node:path") as typeof import("node:path");
    const { spawnSync } = require("node:child_process") as typeof import("node:child_process");
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), "redact-from-file-"));
    try {
      const kv = (name: string) => {
        fs.writeFileSync(path.join(dir, name), "  session: SessionState,\n");
        const r = spawnSync("bun", [path.join(import.meta.dir, "..", "bin", "gstack-redact"), "--from-file", path.join(dir, name), "--json"], { encoding: "utf8", timeout: 30_000 });
        return (JSON.parse(r.stdout).findings as Array<{ id: string }>).some((f) => f.id === "env.kv");
      };
      expect(kv("coach.ts")).toBe(false);
      expect(kv("values.yaml")).toBe(true);
    } finally { fs.rmSync(dir, { recursive: true, force: true }); }
  });
});