testPullPushDivergence

tcmts.cli.test.e2e.sync.testPullPushDivergence · #v7islgoh · term

pull, push and divergence between codebase directories

import { tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import { codebase } from "#codebase/tcmts/cli/test/e2e/sync";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testPullPushDivergence = () => {
  const root = tempDir("sync");
  const a = codebase(root, "a");
  a.tcm("add");
  a.scratch(`// tcm: namespace lib\nimport { square } from "#codebase";\nexport const sumSquares = (xs: number[]) => xs.map(square).reduce((x, y) => x + y, 0);\n`);
  a.tcm("add");

  const b = codebase(root, "b");
  fs.rmSync(path.join(b.dir, "scratch.ts"));
  b.tcm("remote", "add", "origin", a.dir);
  assert.match(b.tcm("pull"), /received 2 definition\(s\), verified by re-hashing[\s\S]*fast-forwarded main/);
  assert.equal(b.tcm("ls"), a.tcm("ls"), "same names, same hashes");

  b.scratch(`export const square = (x: number): number => x * x * x;\n`);
  b.tcm("update");
  assert.match(b.tcm("push"), /pushed origin\/main: 0 added, 2 changed/);
  assert.match(a.tcm("diff"), /edited\s+~ square[\s\S]*updated because a dependency changed[\s\S]*lib\.sumSquares/);

  a.scratch(`export const onlyInA = 1;\n`);
  a.tcm("add");
  b.scratch(`export const onlyInB = 2;\n`);
  b.tcm("add");
  assert.throws(() => b.tcm("push"), /has changes you don't have/);
  // pulling merges the remote's changes with ours; pushing then publishes the merge
  assert.match(b.tcm("pull"), /merged origin\/main into main[\s\S]*\+ onlyInA[\s\S]*merged cleanly/);
  assert.match(b.tcm("ls"), /onlyInA[\s\S]*onlyInB/);
  assert.match(b.tcm("push"), /pushed origin\/main/);
  assert.match(a.tcm("ls"), /onlyInA[\s\S]*onlyInB/);
}

uses