testReceivedConstantsArentRun

tcmts.cli.test.e2e.sync.testReceivedConstantsArentRun · #lcvxrdkp · term

Nothing received is run while it's checked, not even a pure constant whose value tcm would keep where it runs it (TCM_DEBUG says when it evaluates some): a constant that never finished computing couldn't hold a server up.

import { plainEnv, tcmBin, tempDir, withChildren } from "#codebase/tcmts/cli/test/e2e/harness";
import { codebase } from "#codebase/tcmts/cli/test/e2e/sync";
import assert from "node:assert/strict";
import { spawn, spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

export const testReceivedConstantsArentRun = () =>
  withChildren(async (keep) => {
    const root = tempDir("sync kept");
    const server = codebase(root, "server");
    fs.rmSync(path.join(server.dir, "scratch.ts"));
    const child = keep(
      spawn(process.execPath, [tcmBin(), "serve", "--port", "0", "--token", "tok"], {
        cwd: server.dir,
        env: { ...plainEnv(), TCM_DEBUG: "1" },
      }),
    );
    let out = "";
    let err = "";
    child.stdout!.setEncoding("utf8").on("data", (d: string) => (out += d));
    child.stderr!.setEncoding("utf8").on("data", (d: string) => (err += d));
    const url = await new Promise<string>((resolve, reject) => {
      const check = () => {
        const m = /url\s+(https?:\/\/[\d.]+:\d+)/.exec(out);
        if (m) resolve(m[1]!);
        else if (child.exitCode !== null) reject(new Error(out + err));
        else setTimeout(check, 20);
      };
      check();
    });

    const a = codebase(root, "a");
    a.scratch(
      `// tcm: namespace lib\nexport const total = [1, 2, 3].reduce((x, y) => x + y, 0);\n`,
    );
    // (saved, it's evaluated and kept: so it's a constant a tcm runs)
    const added = spawnSync(process.execPath, [tcmBin(), "add"], {
      cwd: a.dir,
      encoding: "utf8",
      env: { ...plainEnv(), TCM_DEBUG: "1" },
    });
    assert.equal(added.status, 0, added.stdout + added.stderr);
    assert.match(added.stderr, /keeping 1 value\(s\)/);
    assert.equal(a.tcm("run", "lib.total").trim(), "6");

    a.tcm("remote", "add", "origin", url, "--token", "tok");
    assert.match(a.tcm("push"), /sent 1 definition\(s\)/);
    await new Promise((r) => setTimeout(r, 200));
    assert.match(out, /received 1 definition\(s\)/);
    assert.doesNotMatch(err, /keeping \d+ value/, "received, not run");
  })

uses