testPackagesPinnedByRange

tcmts.cli.test.e2e.packages.testPackagesPinnedByRange · #o2eih4o7 · term

Packages: provided without installing, pinned by range, one version per range, upgrades and guards.

import { setUp } from "#codebase/tcmts/cli/test/e2e/packages";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testPackagesPinnedByRange = () => {
  const { dir, tcm, write, sql, hashOf } = setUp();
  write("time.ts", `// tcm: namespace time\nimport ms from "ms";\nexport const short = (n: number): string => ms(n);\n\nshort(90_000);\n`);
  assert.match(tcm("check", "time.ts"), /⧩ '2m'/, "imported with nothing installed");
  assert.ok(!fs.existsSync(path.join(dir, "node_modules")), "the project has no node_modules");
  tcm("add", "time.ts");
  fs.rmSync(path.join(dir, "time.ts"));
  assert.equal(tcm("deps", "time.short").trim(), "npm:ms@2#default", "pinned by compatible range");
  const locked = sql("SELECT version FROM packages WHERE name = 'ms' AND range = '2'")[0]!.version!;
  assert.match(tcm("packages"), new RegExp(`ms\\s+2\\s+${locked.replaceAll(".", "\\.")}\\s+1 definition`));

  // within a range: the lock moves, nothing is re-hashed
  const before = hashOf("time.short");
  sql("UPDATE packages SET version = '2.1.1' WHERE name = 'ms' AND range = '2'");
  const within = tcm("upgrade", "ms");
  assert.match(within, new RegExp(`ms 2\\s+2\\.1\\.1 → ${locked.replaceAll(".", "\\.")}`));
  assert.match(within, /1 definition\(s\) re-checked; no hashes changed/);
  assert.equal(hashOf("time.short"), before);
  assert.match(tcm("upgrade", "ms"), /ms is up to date/);

  // to another range: re-pinned and re-hashed, and it runs on that version
  const across = tcm("upgrade", "ms@1", "time");
  assert.match(across, /re-pinning 1 definition\(s\) in time to ms 1/);
  assert.equal(tcm("deps", "time.short").trim(), "npm:ms@1#default");
  assert.notEqual(hashOf("time.short"), before);
  assert.match(tcm("verify"), /rendering and re-hashing is the identity/);
  write("scratch.ts", `import { short } from "#codebase/time";\n\nshort(90_000);\n`);
  assert.match(tcm("check"), /⧩ '2m'/);
}

uses