Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

7.9 KB
Raw Blame
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";

import {
  HAND_OFF_PACKAGE_TYPES,
  linuxPackageType,
  packageInstallCommand,
  shellQuote,
  stagedInstallFile,
} from "./package-install-command.mjs";

test("the Ubuntu command resolves dependencies", () => {
  const command = packageInstallCommand("deb", "/home/u/.cache/openmausbot-updater/pending/x.deb");

  assert.equal(
    command,
    "sudo apt-get install -y '/home/u/.cache/openmausbot-updater/pending/x.deb'",
  );
  // electron-updater ran `dpkg -i`, which installs nothing when a release adds a dependency; only apt resolves virtual Provides.
  assert.doesNotMatch(command, /dpkg/);
});

test("every hand-off package type has a command", () => {
  for (const packageType of HAND_OFF_PACKAGE_TYPES) {
    assert.match(packageInstallCommand(packageType, "/tmp/pkg"), /^sudo \S+/);
  }
  assert.throws(() => packageInstallCommand("snap", "/tmp/pkg"), /No install command/);
});

test("inherited Object names are not install commands", () => {
  for (const name of ["toString", "constructor", "__proto__"]) {
    assert.throws(() => packageInstallCommand(name, "/tmp/pkg"), /No install command/);
  }
});

test("a staged path that is gone is not used", () => {
  const workspace = mkdtempSync(join(tmpdir(), "omb-staged-"));
  try {
    const present = join(workspace, "OpenMausBot.deb");
    writeFileSync(present, "x");
    const missing = join(workspace, "gone.deb");

    assert.equal(stagedInstallFile([present]), present);
    assert.equal(stagedInstallFile([missing, present]), present);
    assert.equal(stagedInstallFile([missing]), undefined);
    assert.equal(stagedInstallFile([]), undefined);
    assert.equal(stagedInstallFile(undefined), undefined);
    assert.throws(
      () => packageInstallCommand("deb", stagedInstallFile([missing])),
      /no longer available/,
    );
  } finally {
    rmSync(workspace, { recursive: true, force: true });
  }
});

test("a missing download is reported instead of building a broken command", () => {
  assert.throws(() => packageInstallCommand("deb", undefined), /no longer available/);
  assert.throws(() => packageInstallCommand("deb", ""), /no longer available/);
  assert.throws(() => packageInstallCommand("deb", undefined), (error) =>
    error.message.endsWith("Try the update again.") && !/download it/i.test(error.message));
});

// A POSIX shell is the referee for what the quoting parses into; Windows CI has no /bin/sh and Windows users never get this command.
const posixShell = existsSync("/bin/sh") ? false : "needs /bin/sh";

// The path lives under $HOME, so it can carry whatever a directory name can.
test("the quoted path survives a shell round-trip", { skip: posixShell }, () => {
  const workspace = mkdtempSync(join(tmpdir(), "omb-quote-"));
  try {
    for (const name of ["plain.deb", "with space.deb", "o'brien.deb", "a;b&c.deb", "$(echo bad).deb"]) {
      const file = join(workspace, name);
      writeFileSync(file, "x");
      // `printf %s` echoes exactly one argument: what the shell parsed out of our quoting.
      const parsed = execFileSync("/bin/sh", ["-c", `printf %s ${shellQuote(file)}`], {
        encoding: "utf8",
      });
      assert.equal(parsed, file, `quoting mangled ${name}`);
    }
  } finally {
    rmSync(workspace, { recursive: true, force: true });
  }
});

test("the whole command parses into the arguments apt-get would receive", { skip: posixShell }, () => {
  const file = "/home/o'brien/.cache/openmausbot-updater/pending/OpenMausBot-0.1.44-amd64.deb";
  const command = packageInstallCommand("deb", file);

  // The privileged verb is replaced with a printer, so the shell shows exactly the flags and the one path it would pass.
  const printed = execFileSync(
    "/bin/sh",
    ["-c", `set -- ${command.replace("sudo apt-get", "")}; for a in "$@"; do printf '%s\\n' "$a"; done`],
    { encoding: "utf8" },
  );

  assert.deepEqual(printed.split("\n").filter(Boolean), ["install", "-y", file]);
});

test("the rpm command picks a dependency-resolving package manager and passes only the file", { skip: posixShell }, () => {
  const file = "/home/o'brien/.cache/openmausbot-updater/pending/OpenMausBot-0.1.44-x86_64.rpm";
  const command = packageInstallCommand("rpm", file);

  // Fake package managers on PATH announce themselves and print their arguments; a bare rpm would resolve
  // nothing, so the fallback branch must never run here.
  const bins = [];
  const fakeBin = (present) => {
    const bin = mkdtempSync(join(tmpdir(), "omb-rpm-pm-"));
    bins.push(bin);
    for (const name of present) {
      writeFileSync(
        join(bin, name),
        `#!/bin/sh\necho "PM-\${0##*/}"\nfor a in "$@"; do printf '%s\\n' "$a"; done\n`,
        { mode: 0o755 },
      );
    }
    // sudo becomes a pass-through printer of what it would run as root.
    writeFileSync(join(bin, "sudo"), "#!/bin/sh\nprintf '%s\\n' sudo\nfor a in \"$@\"; do printf '%s\\n' \"$a\"; done\nexec \"$@\"\n", { mode: 0o755 });
    return bin;
  };

  const run = (bin) =>
    execFileSync("/bin/sh", ["-c", command], {
      encoding: "utf8",
      env: { ...process.env, PATH: `${bin}:${process.env.PATH}` },
    }).split("\n").filter(Boolean);

  try {
    // Fedora: dnf first, resolving dependencies, with the file as its only operand.
    let out = run(fakeBin(["dnf", "rpm"]));
    assert.deepEqual(out.slice(0, 3), ["sudo", "sh", "-c"]);
    assert.deepEqual(out.slice(-5), ["PM-dnf", "install", "--nogpgcheck", "-y", file]);

    // openSUSE: no dnf, no yum, so zypper is picked, again with dependency resolution.
    out = run(fakeBin(["zypper", "rpm"]));
    assert.deepEqual(out.slice(-7), ["PM-zypper", "--non-interactive", "--no-refresh", "install", "--allow-unsigned-rpm", "-f", file]);
  } finally {
    for (const bin of bins) rmSync(bin, { recursive: true, force: true });
  }
});

test("the package marker decides the install path", () => {
  const marker = (declared) => (file) => (file.endsWith("package-type") ? declared : null);

  assert.equal(
    linuxPackageType({ platform: "linux", resourcesPath: "/opt/app/resources", readMarker: marker("deb\n") }),
    "deb",
  );
  // An AppImage ships no marker, so the env probe is what identifies it.
  assert.equal(
    linuxPackageType({
      platform: "linux",
      resourcesPath: "/tmp/squashfs-root/resources",
      appImage: "/home/u/OpenMausBot.AppImage",
      readMarker: marker(null),
    }),
    "AppImage",
  );
  // The marker wins, matching electron-updater's own precedence.
  assert.equal(
    linuxPackageType({
      platform: "linux",
      resourcesPath: "/opt/app/resources",
      appImage: "/home/u/OpenMausBot.AppImage",
      readMarker: marker("deb"),
    }),
    "deb",
  );
});

test("a build with nothing to identify it keeps the restart path", () => {
  const none = () => null;

  assert.equal(
    linuxPackageType({ platform: "linux", resourcesPath: "/opt/app/resources", appImage: undefined, readMarker: none }),
    null,
  );
  assert.equal(linuxPackageType({ platform: "linux", resourcesPath: undefined, readMarker: none }), null);
  assert.equal(
    linuxPackageType({ platform: "linux", resourcesPath: undefined, appImage: "/a.AppImage", readMarker: none }),
    "AppImage",
  );
  assert.equal(linuxPackageType({ platform: "darwin", readMarker: none }), null);
  assert.equal(linuxPackageType({ platform: "win32", readMarker: none }), null);
  assert.equal(
    linuxPackageType({
      platform: "linux",
      resourcesPath: "/opt/app/resources",
      appImage: undefined,
      readMarker: () => "  \n",
    }),
    null,
  );
  assert.equal(
    linuxPackageType({
      platform: "linux",
      resourcesPath: "/opt/app/resources",
      appImage: undefined,
      readMarker: () => {
        throw new Error("EACCES");
      },
    }),
    null,
  );
});