jacquardSnapshot

← snapshot

3529 bytes
/**
 * What a judge is allowed to do.
 *
 * The model proposes; determinism disposes. These pin the refusals — the
 * things that must hold no matter what comes back from the model.
 */
import test from "node:test";
import assert from "node:assert/strict";

import { applyJudgement, judgePayload } from "../diff-group.ts";

const group = (id, paths, before, after, count = paths.length) => ({
  id,
  paths,
  lines: paths.length,
  substitutions: [{ before, after, count }],
});

const A = group("sigA", ["a.rs", "b.rs", "c.rs"], "retry", "retry_with_jitter");
const B = group("sigB", ["d.rs", "e.rs"], "retry", "retry_jittered");
const C = group("sigC", ["f.rs", "g.rs"], "open", "open_readonly");
const base = { groups: [A, B, C], singles: ["odd.rs"] };

test("no judgement leaves the grouping untouched", () => {
  const out = applyJudgement(base, null);
  assert.equal(out.groups.length, 3);
  assert.deepEqual(out.singles, ["odd.rs"]);
});

test("a sound cluster merges and keeps its parts separable", () => {
  const out = applyJudgement(base, {
    clusters: [{ ids: ["sigA", "sigB"], label: "jitter the retry", why: "same rename" }],
  });
  const m = out.groups.find((g) => g.judged);
  assert.ok(m, "a judged group exists");
  assert.equal(m.paths.length, 5, "all five files carried over");
  assert.equal(m.judged.from.length, 2, "both proven folds still listed");
  assert.equal(m.judged.label, "jitter the retry");
  // The untouched group survives.
  assert.ok(out.groups.some((g) => g.id === "sigC"));
});

test("ids the judge invented are refused", () => {
  const out = applyJudgement(base, {
    clusters: [{ ids: ["sigA", "nonexistent"], label: "x", why: "y" }],
  });
  assert.equal(out.groups.filter((g) => g.judged).length, 0, "no merge from one real id");
  assert.equal(out.groups.length, 3);
});

test("a cluster of one is not a merge", () => {
  const out = applyJudgement(base, {
    clusters: [{ ids: ["sigA"], label: "x", why: "y" }],
  });
  assert.equal(out.groups.filter((g) => g.judged).length, 0);
});

test("a group cannot be claimed twice", () => {
  const out = applyJudgement(base, {
    clusters: [
      { ids: ["sigA", "sigB"], label: "first", why: "" },
      { ids: ["sigB", "sigC"], label: "second", why: "" },
    ],
  });
  const judged = out.groups.filter((g) => g.judged);
  assert.equal(judged.length, 1, "the second cluster loses its only free id");
  assert.equal(judged[0].judged.label, "first");
});

test("singles are never touched by a judgement", () => {
  const out = applyJudgement(base, {
    clusters: [{ ids: ["sigA", "sigB", "odd.rs"], label: "x", why: "y" }],
  });
  assert.deepEqual(out.singles, ["odd.rs"], "a single stays a single");
  const m = out.groups.find((g) => g.judged);
  assert.ok(!m.paths.includes("odd.rs"), "and is never absorbed");
});

test("the merge does not depend on the judge's ordering", () => {
  const one = applyJudgement(base, {
    clusters: [{ ids: ["sigA", "sigB"], label: "l", why: "w" }],
  });
  const two = applyJudgement(base, {
    clusters: [{ ids: ["sigB", "sigA"], label: "l", why: "w" }],
  });
  const id = (o) => o.groups.find((g) => g.judged).id;
  assert.equal(id(one), id(two), "same identity either way round");
});

test("the judge is shown substitutions and counts, never paths", () => {
  const sent = JSON.stringify(judgePayload([A, B]));
  assert.ok(sent.includes("retry_with_jitter"));
  assert.ok(!sent.includes("a.rs"), "no file paths leave");
  assert.ok(!sent.includes("paths"), "not even the key");
});