testEditSeveralSavesEachAsItReadsAlone

tcmts.cli.test.e2e.editing.testEditSeveralSavesEachAsItReadsAlone · #fyrvcz4s · term

Definitions edited in one scratch file are each saved as they read alone: an import the file had to number (join_1, as another is join) is stored as join; one added to a file that means something else by a name is written with another (limit_1), not taken to mean the file's.

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

export const testEditSeveralSavesEachAsItReadsAlone = () => {
  const dir = tempDir("edit several, each alone");
  const ok = (...args: string[]) => okIn(dir, ...args);
  ok("init");
  fs.rmSync(path.join(dir, "scratch.ts"), { force: true });
  const write = (f: string, text: string) => fs.writeFileSync(path.join(dir, f), text);
  write(
    "lib.ts",
    `// tcm: namespace lib\nimport { join } from "node:path";\nexport const where = (f: string): string => join("a", f);\nexport const limit = 3;\n`,
  );
  write(
    "posix.ts",
    `// tcm: namespace posix\nimport { join } from "node:path/posix";\nexport const place = (f: string): string => join("b", f);\n`,
  );
  write(
    "other.ts",
    `// tcm: namespace other\nexport const limit = 5;\nexport const twice = (): number => limit * 2;\n`,
  );
  ok("add", "lib.ts", "posix.ts", "other.ts");
  ok("edit", "lib.where", "posix.place");
  const scratch = path.join(dir, "scratch.ts");
  const text = fs.readFileSync(scratch, "utf8");
  assert.match(text, /join as join_1/, text);
  fs.writeFileSync(
    scratch,
    text
      .replace('join("a", f)', 'join("a", "c", f)')
      .replace('join_1("b", f)', 'join_1("b", "c", f)'),
  );
  ok("update");
  const place = ok("view", "posix.place");
  assert.match(
    place,
    /import \{ join \} from "node:path\/posix";[\s\S]*join\("b", "c", f\)/,
    place,
  );
  assert.doesNotMatch(place, /join_1/);
  // added to a file declaring `limit`, other.twice's `limit` (other.limit) is another name there
  ok("edit", "lib.limit");
  fs.writeFileSync(scratch, fs.readFileSync(scratch, "utf8").replace("limit = 3", "limit = 4"));
  ok("edit", "other.twice");
  const both = fs.readFileSync(scratch, "utf8");
  assert.match(both, /limit as limit_1/, both);
  fs.writeFileSync(scratch, both.replace("limit_1 * 2", "limit_1 * 3"));
  ok("update");
  assert.equal(ok("run", "other.twice").trim(), "15");
  assert.equal(ok("run", "lib.limit").trim(), "4");
}

uses