Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

8.7 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

const mod = await import("../../scripts/check/check-ai-attribution.mjs");
const { findAiAttribution, scanRange, main, inputsFromGithubEvent, loadAllowlist } = mod;

test("findAiAttribution: rejects AI/bot Co-Authored-By trailers (both spellings, vendor e-mails)", () => {
  const cases = [
    "Co-Authored-By: Claude Opus 5 <[email protected]>",
    "Co-authored-by: Claude Fable 5.1 <[email protected]>",
    "Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>",
    "Co-authored-by: Codex <[email protected]>",
    "Co-authored-by: GPT-5 <[email protected]>",
    "Co-authored-by: Copilot <[email protected]>",
    // dependabot[bot] is exempted (maintainer decision, 2026-09-29) — see the dedicated test below;
    // every other [bot] account still fails.
    "Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>",
    "Co-authored-by: Someone <[email protected]>",
  ];
  for (const c of cases) assert.deepEqual(findAiAttribution(`subject\n\n${c}\n`), [c], c);
});

test("findAiAttribution: rejects AI-generation footers and session trailers", () => {
  const cases = [
    "🤖 Generated with [Claude Code](https://claude.com/claude-code)",
    "Generated with Claude Code",
    "generated by ChatGPT",
    "Made with Cursor",
    "Claude-Session: https://claude.ai/code/session_01CJf2dxEpiwZqyZujWk57T2",
  ];
  for (const c of cases) assert.deepEqual(findAiAttribution(`subject\n\nbody\n${c}\n`), [c], c);
});

test("findAiAttribution: keeps human co-authors, product names in prose, and ordinary text", () => {
  const clean = [
    "Co-authored-by: diegosouzapw <[email protected]>",
    "Co-authored-by: Devin Smith <[email protected]>",
    "Co-authored-by: Cursor Lee <[email protected]>",
    "Co-authored-by: Amirreza Kimiyaei <[email protected]>",
    "fix(claude): keep a Claude personal workspace and a Team org as separate connections",
    "feat(codex): safely discover compatible models (#12933)",
    "Adds the Copilot chat catalog from the account's live listing.",
    "Refs #14436 — the gate that rejects `Co-Authored-By: Claude …` in commit metadata",
    "Signed-off-by: Jane Doe <[email protected]>",
  ];
  assert.deepEqual(findAiAttribution(clean.join("\n")), []);
});

function git(cwd: string, ...args: string[]): string {
  return execFileSync("git", args, {
    cwd,
    encoding: "utf8",
    env: {
      ...process.env,
      GIT_AUTHOR_NAME: "t",
      GIT_AUTHOR_EMAIL: "[email protected]",
      GIT_COMMITTER_NAME: "t",
      GIT_COMMITTER_EMAIL: "[email protected]",
      GIT_CONFIG_GLOBAL: "/dev/null",
      GIT_CONFIG_NOSYSTEM: "1",
    },
  }).trim();
}

test("scanRange + main: flags only the commits in the range that carry AI attribution", () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ai-attr-"));
  const cwd = process.cwd();
  try {
    git(dir, "init", "-q", "-b", "main");
    git(dir, "commit", "-q", "--allow-empty", "-m", "root");
    const base = git(dir, "rev-parse", "HEAD");
    git(
      dir,
      "commit",
      "-q",
      "--allow-empty",
      "-m",
      "feat: clean\n\nCo-authored-by: Jane Doe <[email protected]>"
    );
    git(
      dir,
      "commit",
      "-q",
      "--allow-empty",
      "-m",
      "fix: tainted\n\nCo-Authored-By: Claude Opus 5 <[email protected]>\nClaude-Session: https://claude.ai/code/x"
    );
    process.chdir(dir);
    const bad = scanRange(`${base}..HEAD`);
    assert.equal(bad.length, 1);
    assert.equal(bad[0].subject, "fix: tainted");
    assert.equal(bad[0].hits.length, 2);
    const errors: string[] = [];
    const orig = console.error;
    console.error = (...a: unknown[]) => errors.push(a.join(" "));
    try {
      assert.equal(main(["--range", `${base}..HEAD`]), 1);
      assert.equal(main(["--range", `${base}..${base}`]), 0);
      assert.equal(main(["--pr-title", "feat: made with Claude Code"]), 1);
      assert.equal(main(["--pr-title", "feat(claude): normal title"]), 0);
      // in Actions GITHUB_EVENT_PATH is set (the real PR event) — isolate the no-input case
      const savedEvent = process.env.GITHUB_EVENT_PATH;
      delete process.env.GITHUB_EVENT_PATH;
      try {
        assert.equal(main([]), 2, "no input → usage");
      } finally {
        if (savedEvent !== undefined) process.env.GITHUB_EVENT_PATH = savedEvent;
      }
    } finally {
      console.error = orig;
    }
    assert.ok(errors.some((e) => e.includes("Hard Rule #16")));
  } finally {
    process.chdir(cwd);
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test("inputsFromGithubEvent: pull_request payload → range + title + body; other events → null", () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ai-attr-ev-"));
  try {
    const ev = path.join(dir, "event.json");
    fs.writeFileSync(
      ev,
      JSON.stringify({
        pull_request: {
          base: { sha: "a".repeat(40) },
          head: { sha: "b".repeat(40) },
          title: "t",
          body: "b",
        },
      })
    );
    assert.deepEqual(inputsFromGithubEvent(ev), {
      range: `${"a".repeat(40)}..${"b".repeat(40)}`,
      prTitle: "t",
      prBody: "b",
    });
    fs.writeFileSync(ev, JSON.stringify({ workflow_dispatch: {} }));
    assert.equal(inputsFromGithubEvent(ev), null);
    assert.equal(inputsFromGithubEvent(path.join(dir, "missing.json")), undefined);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test("allowlist: skips only the listed historical full SHAs in --range; new tainted commits still fail", () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ai-attr-allow-"));
  const cwd = process.cwd();
  try {
    git(dir, "init", "-q", "-b", "main");
    git(dir, "commit", "-q", "--allow-empty", "-m", "root");
    const base = git(dir, "rev-parse", "HEAD");
    git(
      dir,
      "commit",
      "-q",
      "--allow-empty",
      "-m",
      "deps: bump x (#1)\n\nSigned-off-by: dependabot[bot] <[email protected]>"
    );
    const historical = git(dir, "rev-parse", "HEAD");
    const list = path.join(dir, "allow.json");
    fs.writeFileSync(
      list,
      JSON.stringify({ commits: { [historical]: { pr: 1, kind: "dependabot", reason: "r" } } })
    );
    process.chdir(dir);
    const allow = loadAllowlist(list);
    const skipped: string[] = [];
    assert.deepEqual(scanRange(`${base}..HEAD`, allow, skipped), []);
    assert.deepEqual(skipped, [historical]);
    const log = console.log;
    const err = console.error;
    console.log = () => {};
    console.error = () => {};
    try {
      assert.equal(main(["--range", `${base}..HEAD`, "--allowlist", list]), 0);
      git(
        dir,
        "commit",
        "-q",
        "--allow-empty",
        "-m",
        "fix: new\n\nCo-Authored-By: Claude Opus 5 <[email protected]>"
      );
      assert.equal(main(["--range", `${base}..HEAD`, "--allowlist", list]), 1);
      // The allowlist never covers PR title/body.
      assert.equal(main(["--pr-title", "Generated with Claude Code", "--allowlist", list]), 1);
    } finally {
      console.log = log;
      console.error = err;
    }
    // Abbreviated SHAs and entries without a reason are rejected.
    fs.writeFileSync(
      list,
      JSON.stringify({ commits: { [historical.slice(0, 10)]: { reason: "r" } } })
    );
    assert.throws(() => loadAllowlist(list), /not a full SHA/);
    fs.writeFileSync(list, JSON.stringify({ commits: { [historical]: {} } }));
    assert.throws(() => loadAllowlist(list), /no reason/);
    assert.equal(loadAllowlist(path.join(dir, "missing.json")).size, 0);
  } finally {
    process.chdir(cwd);
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test("allowlist file in the repo: every entry is a full SHA with a reason", () => {
  const allow = loadAllowlist();
  assert.ok(allow.size > 0);
});

test("findAiAttribution: exempts only GitHub's own dependabot[bot] co-author trailer", () => {
  const dependabot =
    "Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>";
  assert.deepEqual(findAiAttribution(`chore(deps): bump x\n\n${dependabot}\n`), []);
  // Any other bot account, a look-alike name, or the right name with another address still fails.
  for (const c of [
    "Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>",
    "Co-authored-by: dependabot[bot] <[email protected]>",
    "Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>",
    "Co-authored-by: Claude <[email protected]>",
  ]) {
    assert.deepEqual(findAiAttribution(`subject\n\n${c}\n`), [c], c);
  }
});