testMergeTexts

tcmts.cli.merge.testMergeTexts · #cruojxxj · term

Text versions merge three-way: each side's change is kept, both sides' changes to one definition keep ours.

import type { NameState } from "#codebase/tcmts/cli/diff";
import { mergeTexts } from "#codebase/tcmts/cli/merge";
import assert from "node:assert/strict";

export const testMergeTexts = () => {
  const st = (o: Record<string, string[]>): NameState =>
    new Map(Object.entries(o).map(([k, v]) => [k, new Set(v)]));
  const both = st({ f: ["F"], g: ["G"], h: ["H"], k: ["K"] });
  const r = mergeTexts(
    new Map([
      ["H", "h0"],
      ["K", "k0"],
    ]),
    new Map([
      ["F", "f1"],
      ["H", "h1"],
      ["K", "k1"],
    ]),
    new Map([
      ["G", "g2"],
      ["H", "h0"],
      ["K", "k2"],
    ]),
    { ours: both, theirs: both, merged: both },
  );
  assert.deepEqual(Object.fromEntries(r.texts), { F: "f1", G: "g2", H: "h1", K: "k1" });
  assert.deepEqual(r.conflicts, ["K"]);
  assert.deepEqual(r.lost, []);
  // a comment changed on one side, the code on the other: the merge keeps the code, and says the comment was lost
  const lost = mergeTexts(new Map(), new Map(), new Map([["F", "f2"]]), {
    ours: st({ f: ["F2"] }),
    theirs: st({ f: ["F"] }),
    merged: st({ f: ["F2"] }),
  });
  assert.deepEqual(lost.lost, ["F"]);
}

uses