testMigrationIsTheSameEverywhere

tcmts.cli.test.e2e.migrate.testMigrationIsTheSameEverywhere · #2jdcadul · term

Two copies of a codebase migrate to the same hashes, so copies that synced go on syncing.

import { okIn } from "#codebase/tcmts/cli/test/e2e/harness";
import { v1Codebase } from "#codebase/tcmts/cli/test/e2e/migrate";
import assert from "node:assert/strict";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";

export const testMigrationIsTheSameEverywhere = () => {
  const summary = (dir: string) => {
    const db = new DatabaseSync(path.join(dir, ".tcm", "codebase.sqlite"), { readOnly: true });
    try {
      return [
        ...(
          db.prepare("SELECT hash FROM definitions ORDER BY hash").all() as { hash: string }[]
        ).map((r) => r.hash),
        ...(db.prepare("SELECT hash FROM causals ORDER BY hash").all() as { hash: string }[]).map(
          (r) => r.hash,
        ),
        ...(
          db.prepare("SELECT name || '=' || head AS b FROM branches ORDER BY name").all() as {
            b: string;
          }[]
        ).map((r) => r.b),
      ];
    } finally {
      db.close();
    }
  };
  const a = v1Codebase("migrate a");
  const b = v1Codebase("migrate b");
  okIn(a, "migrate");
  okIn(b, "migrate");
  assert.deepEqual(summary(a), summary(b));
}

uses