testLibrariesTcmShips

tcmts.cli.test.e2e.libraries.testLibrariesTcmShips · #i7bxxsao · term

the libraries tcm ships (react, server) are a source of their own, tcm: mounted offline, used like any library, and upgraded from the tcm running

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

export const testLibrariesTcmShips = () => {
  const me = codebase("me");
  me.save(`// tcm: namespace app\nexport const greeting = "hi";\n`, "add");
  assert.match(
    me.tcm("lib", "add", "tcm", "react"),
    /added app\.lib\.react: react@\d+\.\d+\.\d+ from tcm \(\d+ names; \d+ definitions received, verified by re-hashing\)/,
  );
  assert.match(me.tcm("lib"), /app\.lib\.react {2}react@\d+\.\d+\.\d+ {2}from tcm/);
  me.save(
    `// tcm: namespace app\nimport { match } from "#codebase/app/lib/react";\nexport const route = () => JSON.stringify(match("/n/:id", "/n/7"));\n`,
    "add",
  );
  assert.equal(me.tcm("run", "app.route").trim(), '{"id":"7"}');
  // placed by its code: routes and stylesheets render anywhere, the location's hooks and navigating in the browser
  assert.match(me.tcm("view", "app.lib.react.match"), /runs anywhere/);
  assert.match(me.tcm("view", "app.lib.react.Stylesheet"), /runs anywhere/);
  assert.match(me.tcm("view", "app.lib.react.navigate"), /runs in the browser/);
  assert.match(me.tcm("view", "app.lib.react.useLocation"), /runs in the browser/);
  // and #tcm/react, which it replaces, says how to use it
  fs.writeFileSync(
    me.file("old.ts"),
    `// tcm: namespace app\nimport { useData } from "#tcm/react";\nexport const x = useData;\n`,
  );
  const old = me.run("check", "old.ts");
  assert.notEqual(old.status, 0);
  assert.match(old.out, /#tcm\/react is the react library now: `tcm lib add tcm react`/);
  // (the release this tcm ships is the newest it knows)
  assert.match(me.tcm("lib", "upgrade", "react", "--check"), /react@\d+\.\d+\.\d+/);

  // the server API, the same way: its HttpError runs anywhere, its tables on the server (its host's)
  assert.match(
    me.tcm("lib", "add", "tcm", "server"),
    /added app\.lib\.server: server@\d+\.\d+\.\d+ from tcm/,
  );
  assert.match(me.tcm("view", "app.lib.server.HttpError"), /runs anywhere/);
  assert.match(me.tcm("view", "app.lib.server.table"), /runs on the server: .*#tcm\/host/);
  // and #tcm/server, which it replaces, says how to use it
  fs.writeFileSync(
    me.file("old-server.ts"),
    `// tcm: namespace app\nimport { table } from "#tcm/server";\nexport const t = table;\n`,
  );
  const oldServer = me.run("check", "old-server.ts");
  assert.notEqual(oldServer.status, 0);
  assert.match(oldServer.out, /#tcm\/server is the server library now: `tcm lib add tcm server`/);
}

uses