iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / core / lib / src / cli / provide.test.tsmain at aacbc74

// provide.test.ts — `iterate provide <file>` as a person runs it (the package's bin): a missing
// dependency named before any sign-in, a file with no default export, and what `rpcTargetOf`
// refuses to lend. And the loop itself (`runProvide`) over stand-in connections: the handover before a
// sign-in ends.
import { tmpdir } from "node:os";
import { execFile, type ChildProcess } from "node:child_process";
import { mkdirSync, realpathSync, writeFileSync, mkdtempDisposableSync } from "node:fs";
import { join } from "node:path";
import { createInterface } from "node:readline";
import { fileURLToPath } from "node:url";
import { expect, test, vi } from "vitest";
import { rpcTargetOf, runProvide } from "./provide.ts";
import { finalStateOf, ProvideRefusal, ProvideState } from "./provide-state.ts";

const bin = fileURLToPath(new URL("../../bin/iterate.js", import.meta.url));

test("a file whose dependency is not installed names the folder to install in, before any sign-in; a file with no default export says what to export", async () => {
  using temporary = mkdtempDisposableSync(join(tmpdir(), "iterate-test-"));
  // Node names the file by its real path (macOS's temp dir is a symlink into /private)
  const folder = { path: realpathSync(temporary.path) };
  writeFileSync(join(folder.path, "package.json"), JSON.stringify({ type: "module" }));
  writeFileSync(
    join(folder.path, "whatsapp.ts"),
    `import "baileys-not-installed"; export default () => ({});`,
  );
  writeFileSync(join(folder.path, "nothing.mjs"), `export const x = 1;`);
  // nothing listens here: the file's refusal must come first
  using cli = cliConfig("http://127.0.0.1:1");

  const missing = cli.provide([join(folder.path, "whatsapp.ts"), "--mind", "demo"]);
  expect(await missing.exited).not.toBe(0);
  expect(missing.stderr()).toContain(
    `Cannot find package 'baileys-not-installed' imported from ${join(folder.path, "whatsapp.ts")}. Install whatsapp.ts's dependencies first: npm install (or pnpm install) in ${folder.path}.`,
  );

  const empty = cli.provide([join(folder.path, "nothing.mjs"), "--mind", "demo"]);
  expect(await empty.exited).not.toBe(0);
  expect(empty.stderr()).toContain("nothing.mjs has no default export to provide");
  // two starts of the CLI: how long a process takes to start is the machine's, not this test's
}, 60_000);

test("rpcTargetOf: nothing to lend is refused, saying why", () => {
  expect(() => rpcTargetOf(undefined)).toThrow(
    "The default export answered undefined: it must answer an object of functions.",
  );
  expect(() => rpcTargetOf({ answer: 42 })).toThrow(
    "The default export answered an object with no functions: nothing to lend.",
  );
});

test("before a sign-in ends the name is lent over the next connection, and only then is the old one let go", async () => {
  using quiet = quietConsole();
  const happened: string[] = [];
  const first = connectionOf("first", happened);
  const second = connectionOf("second", happened);
  const ends = [Date.now() + 60, undefined];
  const run = runProvide({
    connection: first.connection,
    reconnect: async () => second.connection,
    file: { provide: () => ({ hello: () => "hello" }) },
    name: "standIn",
    endsAt: () => ends.shift(),
    renewBeforeMs: 30,
    reconnectDelaysMs: [],
  });
  await second.lent;
  // the old connection is let go after the new one lends, and its end is no disconnection
  await first.disposed;
  expect(happened).toEqual(["first lends", "second lends", "first let go"]);
  expect(quiet.lines.join("\n")).not.toMatch(/disconnected|Reconnect/);
  // the end of the run: the second connection closes, and no reconnect is allowed here
  second.close();
  await expect(run).rejects.toThrow("itx.standIn disconnected and could not reconnect");
});

test("when the next connection cannot be made, the present one is kept until it closes", async () => {
  using quiet = quietConsole();
  const happened: string[] = [];
  const first = connectionOf("first", happened);
  let asked = 0;
  const run = runProvide({
    connection: first.connection,
    reconnect: async () => {
      asked += 1;
      throw new Error("the mind is not there");
    },
    file: { provide: () => ({ hello: () => "hello" }) },
    name: "standIn",
    endsAt: () => Date.now() + 40,
    renewBeforeMs: 30,
    reconnectDelaysMs: [],
  });
  await vi.waitFor(() => expect(asked).toBe(1));
  expect(happened).toEqual(["first lends"]);
  first.close();
  await expect(run).rejects.toThrow("itx.standIn disconnected and could not reconnect");
  // one try to renew, and none after it: the reconnect schedule here is empty
  expect({ asked, happened }).toEqual({ asked: 1, happened: ["first lends", "first let go"] });
  expect(quiet.lines.join("\n")).toContain("Could not renew itx.standIn before its sign-in ends");
});

test("a lend from a sandbox's own session is spelled through the sandbox's context", async () => {
  using quiet = quietConsole();
  const box = connectionOf("box", [], "/agents/ada/box");
  const run = runProvide({
    connection: box.connection,
    reconnect: async () => box.connection,
    file: { provide: () => ({ hello: () => "hello" }) },
    name: "standIn",
    reconnectDelaysMs: [],
  });
  await box.lent;
  box.close();
  await expect(run).rejects.toThrow("could not reconnect");
  expect(quiet.lines.at(0)).toBe('itx.cd("/agents/ada/box").standIn');
});

test("--json: live, then before each wait the attempt, its delay and the last failure, then live again; when every attempt fails it stops", async () => {
  using _quiet = quietConsole();
  const reported: ProvideState[] = [];
  const first = connectionOf("first", []);
  const second = connectionOf("second", []);
  const reconnects = [second.connection];
  const run = runProvide({
    connection: first.connection,
    reconnect: async () => reconnects.shift() || Promise.reject(new Error("the mind is not there")),
    file: { provide: () => ({ hello: () => "hello" }) },
    name: "standIn",
    reconnectDelaysMs: [1, 2],
    report: (state) => reported.push(ProvideState.parse(state)),
  });
  await first.lent;
  first.close();
  await second.lent;
  second.close();
  const stopped = await run.then(() => null, finalStateOf);
  const closed = "1008: the sign-in for this session ended";
  expect([...reported, stopped]).toEqual([
    { state: "live", mind: "example.com", name: "itx.standIn", again: false },
    { state: "reconnecting", attempt: 1, of: 2, inMs: 1, lastFailure: closed },
    { state: "live", mind: "example.com", name: "itx.standIn", again: true },
    { state: "reconnecting", attempt: 1, of: 2, inMs: 1, lastFailure: closed },
    { state: "reconnecting", attempt: 2, of: 2, inMs: 2, lastFailure: "the mind is not there" },
    {
      state: "stopped",
      why: "itx.standIn disconnected and could not reconnect (the mind is not there). Run iterate provide again.",
    },
  ]);
});

test("a refusal from the mind about the key ends the run at once: another try would change nothing", async () => {
  using _quiet = quietConsole();
  const first = connectionOf("first", []);
  let asked = 0;
  const removed = new ProvideRefusal("This bridge's key is not allowed on example.com", {
    state: "notAllowed",
    why: "the key is not one this mind knows.",
    waiting: false,
  });
  const run = runProvide({
    connection: first.connection,
    reconnect: async () => {
      asked += 1;
      throw removed;
    },
    file: { provide: () => ({ hello: () => "hello" }) },
    name: "standIn",
    reconnectDelaysMs: [1, 1, 1],
  });
  await first.lent;
  first.close();
  await expect(run).rejects.toBe(removed);
  expect({ asked, state: finalStateOf(removed).state }).toEqual({ asked: 1, state: "notAllowed" });
});

test("iterate provide --json prints only states on stdout, the last one as it ends: here stopped, with the reason", async () => {
  using temporary = mkdtempDisposableSync(join(tmpdir(), "iterate-test-"));
  const file = join(temporary.path, "hello.mjs");
  writeFileSync(file, `export default () => ({ hello: () => "hello" });`);
  using cli = cliConfig("http://127.0.0.1:1");
  // a name that never resolves (RFC 2606), and the bridge's own key: no sign-in, no browser
  const key = join(temporary.path, "hello.key");
  const run = cli.provide([file, "--mind", "mind.invalid", "--key", key, "--json"]);
  expect(await run.exited, run.stderr()).not.toBe(0);
  const states = run
    .stdout()
    .trim()
    .split("\n")
    .map((line) => ProvideState.parse(JSON.parse(line)));
  // the address is checked before any connection: no "connecting"
  expect(states).toEqual([
    { state: "stopped", why: "iterate.mind.invalid could not be reached. Check the address." },
  ]);
}, 60_000);

/** A config naming `baseUrl` with a stored token, and `provide` run as a child process: `live`
 *  answers the line it prints once lent, `stop` sends Ctrl-C and answers the exit code. */
function cliConfig(baseUrl: string) {
  const config = mkdtempDisposableSync(join(tmpdir(), "iterate-test-"));
  mkdirSync(join(config.path, "iterate"));
  writeFileSync(
    join(config.path, "iterate/config.json"),
    JSON.stringify({
      default: "test",
      configs: { test: { osBaseUrl: baseUrl, session: { token: "test-token" } } },
    }),
  );
  const children: ChildProcess[] = [];
  return {
    provide(args: string[]) {
      const child = execFile(process.execPath, [bin, "provide", ...args], {
        env: {
          ...process.env,
          XDG_CONFIG_HOME: config.path,
          NO_COLOR: "1",
          ITERATE_ADMIN_BEARER: "",
          ITERATE_BEARER_TOKEN: "",
        },
      });
      children.push(child);
      let stderr = "";
      let stdout = "";
      child.stderr!.on("data", (chunk) => (stderr += chunk));
      child.stdout!.on("data", (chunk) => (stdout += chunk));
      const exited = new Promise<number | null>((resolve) => child.once("exit", resolve));
      const live = new Promise<string>((resolve, reject) => {
        createInterface({ input: child.stdout! }).once("line", resolve);
        void exited.then((code) =>
          reject(new Error(`iterate provide exited ${code} before it was live: ${stderr}`)),
        );
      });
      live.catch(() => {}); // a file refused before it was live is read through `exited`
      return {
        live,
        exited,
        stderr: () => stderr,
        stdout: () => stdout,
        stop: () => {
          child.kill("SIGINT");
          return exited;
        },
      };
    },
    [Symbol.dispose]: () => {
      for (const child of children) child.kill("SIGKILL");
      config[Symbol.dispose]();
    },
  };
}

/** A mind's connection as `runProvide` uses it, which says in `happened` when the name is lent over
 *  it and when it is let go. `close()` is the mind closing it. */
function connectionOf(name: string, happened: string[], path = "/") {
  const lent = Promise.withResolvers<void>();
  const disposed = Promise.withResolvers<void>();
  const closed = Promise.withResolvers<{ code: number; reason: string }>();
  let over = false;
  const connection = {
    address: "example.com",
    itx: {
      whoami: async () => ({ path }),
      provide: async () => {
        happened.push(`${name} lends`);
        lent.resolve();
        return { lendEnded: () => new Promise<string>(() => {}), [Symbol.dispose]: () => {} };
      },
    },
    closed: closed.promise,
    [Symbol.dispose]() {
      if (over) return;
      over = true;
      happened.push(`${name} let go`);
      disposed.resolve();
    },
  } as unknown as Parameters<typeof runProvide>[0]["connection"];
  return {
    connection,
    lent: lent.promise,
    disposed: disposed.promise,
    close: () => closed.resolve({ code: 1008, reason: "the sign-in for this session ended" }),
  };
}

/** What the run prints, kept from the test's output. */
function quietConsole() {
  const lines: string[] = [];
  const log = vi.spyOn(console, "log").mockImplementation((line) => void lines.push(String(line)));
  const error = vi
    .spyOn(console, "error")
    .mockImplementation((line) => void lines.push(String(line)));
  return {
    lines,
    [Symbol.dispose]() {
      log.mockRestore();
      error.mockRestore();
    },
  };
}