onFakeNetwork

tcmts.cli.test.e2e.releases.onFakeNetwork · #hwxbxsbl · term · runs on the server

uses node:path

Running tcm on the fake network: identities looked up on it, sign-ins kept in a directory of the test's own, and a browser that follows the sign-in page's redirect back to tcm, as if the person had said yes. Each run is alongside this process (a spawnSync would stop the fake network answering): run says how it ended, ok insists it worked.

import type { Keep } from "#codebase/tcmts/cli/test/e2e/contributions";
import type { FakeAtproto } from "#codebase/tcmts/cli/test/e2e/examples";
import { plainEnv, strip, tcmBin, tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";

export function onFakeNetwork(keep: Keep, net: FakeAtproto) {
  const home = tempDir("atproto config");
  const browse = path.join(home, "browse.mjs");
  fs.writeFileSync(
    browse,
    `const r = await fetch(process.argv[2]);\nprocess.exit(r.ok ? 0 : 1);\n`,
  );
  const env = {
    ...plainEnv(),
    TCM_AUTHOR: "ada",
    TCM_ATPROTO_DIRECTORY: net.url,
    // (its servers are on this machine, over http: allowed only so)
    TCM_ATPROTO_LOCAL: "1",
    XDG_CONFIG_HOME: home,
    BROWSER: `"${process.execPath}" "${browse}"`,
  };
  const run = (dir: string, ...args: string[]) =>
    new Promise<{ status: number | null; out: string }>((resolve) => {
      const c = keep(spawn(process.execPath, [tcmBin(), ...args], { cwd: dir, env }));
      let out = "";
      c.stdout!.setEncoding("utf8").on("data", (d: string) => (out += d));
      c.stderr!.setEncoding("utf8").on("data", (d: string) => (out += d));
      c.on("close", (status) => resolve({ status, out: strip(out) }));
    });
  const ok = async (dir: string, ...args: string[]) => {
    const r = await run(dir, ...args);
    assert.equal(r.status, 0, `tcm ${args.join(" ")}:\n${r.out}`);
    return r.out;
  };
  return { home, run, ok };
}

uses

used by