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

5.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/release/reconcile-changelog.mjs");
const { readFragments, fragmentOrigins } = mod;

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();
}

function commitFile(cwd: string, rel: string, text: string, subject: string) {
  fs.mkdirSync(path.dirname(path.join(cwd, rel)), { recursive: true });
  fs.writeFileSync(path.join(cwd, rel), text);
  git(cwd, "add", "-A");
  git(cwd, "commit", "-q", "-m", subject);
  return git(cwd, "rev-parse", "--short=9", "HEAD");
}

function makeRepo(): string {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "reconcile-fragments-"));
  git(dir, "init", "-q", "-b", "main");
  git(dir, "commit", "-q", "--allow-empty", "-m", "root");
  return dir;
}

test("readFragments: origin = the commit that ADDED the fragment, text + prefix parsed, one git walk", () => {
  const dir = makeRepo();
  try {
    const h1 = commitFile(
      dir,
      "changelog.d/features/xquik.md",
      "- **feat(search):** Add Xquik X search\n  with typed results\n",
      "feat(search): add Xquik (#11370)"
    );
    const h2 = commitFile(
      dir,
      "changelog.d/fixes/11685-antigravity.md",
      "- Default Antigravity connections to auto-sync\n",
      "fix(antigravity): auto-sync (#11887)"
    );
    // a later commit that only EDITS the fragment must not become its origin
    commitFile(
      dir,
      "changelog.d/fixes/11685-antigravity.md",
      "- Default Antigravity connections to auto-sync (edited)\n",
      "chore: touch fragment (#12000)"
    );
    // README/.gitkeep are never fragments
    commitFile(dir, "changelog.d/maintenance/.gitkeep", "", "chore: keep");

    const frags = readFragments(dir);
    assert.deepEqual(
      frags.map((f) => [f.path, f.section, f.originHash, f.originPr, f.prefixPr]),
      [
        ["changelog.d/features/xquik.md", "features", h1, 11370, null],
        ["changelog.d/fixes/11685-antigravity.md", "fixes", h2, 11887, 11685],
      ]
    );
    assert.equal(frags[0].text, "- **feat(search):** Add Xquik X search\n  with typed results");
    assert.equal(frags[1].text, "- Default Antigravity connections to auto-sync (edited)");
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test("fragmentOrigins: a fragment deleted and re-added keeps its ORIGINAL adding commit", () => {
  const dir = makeRepo();
  try {
    const first = commitFile(
      dir,
      "changelog.d/fixes/phantom.md",
      "- fix: original\n",
      "fix: original (#100)"
    );
    fs.rmSync(path.join(dir, "changelog.d/fixes/phantom.md"));
    git(dir, "add", "-A");
    git(dir, "commit", "-q", "-m", "chore(release): aggregate fragments");
    commitFile(dir, "changelog.d/fixes/phantom.md", "- fix: original\n", "merge: re-add (#200)");

    const origins = fragmentOrigins(dir);
    assert.ok(
      origins.get("changelog.d/fixes/phantom.md")?.hash.startsWith(first),
      "oldest add wins"
    );
    assert.match(origins.get("changelog.d/fixes/phantom.md")?.subject ?? "", /\(#100\)$/);
    assert.equal(readFragments(dir)[0].originPr, 100);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test("readFragments: empty tree / missing changelog.d is not an error", () => {
  const dir = makeRepo();
  try {
    assert.deepEqual(readFragments(dir), []);
    assert.equal(fragmentOrigins(dir).size, 0);
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

test("readFragments: inventory comes from the WORKING TREE, origins from git (v3.8.52 lesson)", () => {
  // Second reconciliation pass with the first pass's fragment deletions still uncommitted: HEAD
  // carries every fragment, the disk does not. Reading HEAD re-folded ~990 bullets in v3.8.52.
  const dir = makeRepo();
  try {
    const hKept = commitFile(
      dir,
      "changelog.d/fixes/kept.md",
      "- **fix(a):** still on disk\n",
      "fix(a): kept (#100)"
    );
    commitFile(
      dir,
      "changelog.d/fixes/consumed.md",
      "- **fix(b):** folded by the previous pass\n",
      "fix(b): consumed (#200)"
    );
    // pass 1 consumed this fragment (unlinked from disk, deletion NOT committed yet)
    fs.rmSync(path.join(dir, "changelog.d/fixes/consumed.md"));
    // a fragment that exists on disk but was never committed is still part of the inventory
    fs.mkdirSync(path.join(dir, "changelog.d/features"), { recursive: true });
    fs.writeFileSync(
      path.join(dir, "changelog.d/features/untracked.md"),
      "- **feat(c):** written, not committed\n"
    );
    // README/.gitkeep on disk are never fragments
    fs.writeFileSync(path.join(dir, "changelog.d/features/README.md"), "# how to\n");

    const frags = readFragments(dir);
    assert.deepEqual(
      frags.map((f) => [f.path, f.section, f.originHash, f.originPr]),
      [
        ["changelog.d/features/untracked.md", "features", null, null],
        ["changelog.d/fixes/kept.md", "fixes", hKept, 100],
      ],
      "only fragments present on disk are folded; a fragment deleted in the working tree is gone"
    );
    assert.equal(frags[0].text, "- **feat(c):** written, not committed");
    assert.equal(frags[1].text, "- **fix(a):** still on disk");
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
});