Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

8.6 KB
Raw Blame
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.

import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
import { createRequire } from "node:module";
import { tmpdir } from "node:os";
import * as path from "node:path";
import { execFileSync } from "node:child_process";
import { afterEach, describe, expect, it } from "vitest";
import { LINUX_WINDOW_ICON, windowIconPath } from "../src/icon";

const require = createRequire(import.meta.url);
const config = require("../electron-builder.config.cjs");
const { normalizeModes } = require("../packaging/linux-permissions.cjs");

const repo = path.join(import.meta.dirname, "../../..");
const readJson = (file: string) => JSON.parse(readFileSync(path.join(repo, file), "utf8"));

describe("packaging", () => {
  it("follows the Cua Spaces version (Release Please bumps both)", () => {
    const manifest = readJson(".release-please-manifest.json");
    expect(readJson("apps/cua-spaces-desktop/package.json").version).toBe(manifest["apps/cua-spaces-macos"]);
    const extras = readJson("release-please-config.json").packages["apps/cua-spaces-macos"]["extra-files"];
    expect(extras).toContainEqual({ type: "json", path: "/apps/cua-spaces-desktop/package.json", jsonpath: "$.version" });
  });

  it("installs under the product's own name, not the npm scope", () => {
    // electron-builder names the Windows install folder and the updater cache after `name`.
    expect(config.extraMetadata.name).toBe("cua-spaces");
    expect(config.extraMetadata.name).toMatch(/^[a-z0-9-]+$/);
    expect(config.productName).toBe("Cua Spaces");
  });

  it("ships the Linux window icon next to the tray icon", () => {
    const filter = config.linux.extraResources.flatMap((r: { to: string; filter: string[] }) => (r.to === "tray" ? r.filter : []));
    expect(filter).toContain(LINUX_WINDOW_ICON);
    expect(filter).toContain("32x32.png");
  });

  it("ships a light and a dark one-colour tray glyph for Linux panels", () => {
    const res = config.linux.extraResources.find((r: { to: string; from: string }) => r.to === "tray" && r.from === "packaging/linux-tray");
    expect(res.filter.sort()).toEqual(["tray-dark.png", "tray-light.png"]);
    for (const f of res.filter) expect(existsSync(path.join(import.meta.dirname, "..", res.from, f))).toBe(true);
  });

  it("keeps Chromium's en-US strings: without them menu accelerators lose their modifier on Linux", () => {
    // en-US.pak holds "Ctrl" and "Alt"; "en" alone matches no pak (CUA-1530).
    expect(config.electronLanguages).toContain("en-US");
  });
});

describe("the .deb's removal script (CUA-1521)", () => {
  const script = path.join(import.meta.dirname, "../packaging/linux/prerm.sh");

  it("runs before the package is removed", () => {
    // An option goes before the first `source=target` path: after it, fpm reads everything as a path.
    const args: string[] = config.deb.fpm;
    expect(args.slice(0, 2)).toEqual(["--before-remove", script]);
    expect(args.findIndex((a) => a.includes("="))).toBeGreaterThan(1);
  });

  it("is valid shell and turns off the host service, the daemon and the relay entry", () => {
    execFileSync("sh", ["-n", script]);
    const text = readFileSync(script, "utf8");
    expect(text).toContain("host remove --force --uninstalling");
    expect(text).toContain("daemon stop");
    expect(text).toContain("autostart/cua-spaces.desktop");
    expect(statSync(script).mode & 0o111).not.toBe(0);
  });

  it("leaves an upgrade alone", () => {
    // Only remove and purge turn anything off; an upgrade replaces the files and keeps the host running.
    for (const action of ["upgrade", "failed-upgrade", "deconfigure"]) {
      expect(execFileSync("sh", [script, action]).toString()).toBe("");
    }
  });
});

describe("the Linux install tree", () => {
  const dirs: string[] = [];
  afterEach(() => dirs.splice(0).forEach((d) => rmSync(d, { recursive: true, force: true })));
  const mode = (p: string) => statSync(p).mode & 0o7777;

  /** An unpacked app as a build machine with umask 002 leaves it. */
  function groupWritableApp(): string {
    const root = mkdtempSync(path.join(tmpdir(), "cua-linux-unpacked-"));
    dirs.push(root);
    const native = path.join(root, "resources", "native");
    mkdirSync(native, { recursive: true });
    const files = { "cua-spaces": 0o775, "resources.pak": 0o664, "resources/native/cua": 0o775, "resources/native/libcua_spaces_ffi.so": 0o664, "resources/native/cua_node_runtime.node": 0o664, "resources/native/cua-spacesd-linux-x86_64": 0o775 };
    for (const [f, m] of Object.entries(files)) {
      writeFileSync(path.join(root, f), "x");
      chmodSync(path.join(root, f), m);
    }
    for (const d of [root, path.join(root, "resources"), native]) chmodSync(d, 0o775);
    symlinkSync("cua-spaces", path.join(root, "link"));
    return root;
  }

  it("is writable by its owner only, executables kept executable", () => {
    const root = groupWritableApp();
    normalizeModes(root);
    // What the Keyvault asks of every folder above the bundled cua (cua-keyvault `root_protected`).
    for (const d of [root, path.join(root, "resources"), path.join(root, "resources/native")]) expect(mode(d)).toBe(0o755);
    expect(mode(path.join(root, "resources/native/cua"))).toBe(0o755);
    expect(mode(path.join(root, "cua-spaces"))).toBe(0o755);
    expect(mode(path.join(root, "resources/native/libcua_spaces_ffi.so"))).toBe(0o644);
    expect(mode(path.join(root, "resources.pak"))).toBe(0o644);
  });

  it("ships chrome-sandbox setuid (4755) for electron-builder's after-install to settle per host", () => {
    const root = groupWritableApp();
    writeFileSync(path.join(root, "chrome-sandbox"), "x");
    chmodSync(path.join(root, "chrome-sandbox"), 0o775);
    normalizeModes(root);
    expect(mode(path.join(root, "chrome-sandbox"))).toBe(0o4755);
    // Stable on a second pass, and nothing else gets the setuid bit.
    normalizeModes(root);
    expect(mode(path.join(root, "chrome-sandbox"))).toBe(0o4755);
    expect(mode(path.join(root, "cua-spaces"))).toBe(0o755);
  });

  it("keeps electron-builder's own after-install script (its userns-aware chrome-sandbox chmod)", () => {
    expect(config.linux.afterInstall ?? config.deb.afterInstall).toBeUndefined();
  });

  it("is fixed by afterPack before the deb and the AppImage are made", async () => {
    const root = groupWritableApp();
    await config.afterPack({ electronPlatformName: "linux", arch: 1, appOutDir: root, packager: { appInfo: { productFilename: "Cua Spaces" } } });
    expect(mode(path.join(root, "resources/native"))).toBe(0o755);
    expect(mode(path.join(root, "resources/native/cua"))).toBe(0o755);
    expect(mode(path.join(root, "resources/native/cua-spacesd-linux-x86_64"))).toBe(0o755);
  });

  it("refuses a Linux package without the static cua-spacesd next to cua", async () => {
    const root = groupWritableApp();
    rmSync(path.join(root, "resources/native/cua-spacesd-linux-x86_64"));
    const pack = { electronPlatformName: "linux", appOutDir: root, packager: { appInfo: { productFilename: "Cua Spaces" } } };
    await expect(config.afterPack({ ...pack, arch: 1 })).rejects.toThrow(/cua-spacesd-linux-x86_64/);
    // arm64 wants its own architecture's binary.
    writeFileSync(path.join(root, "resources/native/cua-spacesd-linux-x86_64"), "x");
    await expect(config.afterPack({ ...pack, arch: 3 })).rejects.toThrow(/cua-spacesd-linux-aarch64/);
  });

  it("describes the app in the desktop entry and the package, not the shell it is built with", () => {
    expect(config.linux.description).toBe("Run apps and agents in Cua Spaces");
    // electron-builder writes the description over an entry's own Comment.
    expect(config.linux.desktop.entry.Comment).toBeUndefined();
    expect(config.linux.description).not.toMatch(/electron/i);
  });
});

describe("windowIconPath", () => {
  const base = { packaged: true, resourcesPath: "/opt/Cua Spaces/resources", devIconsDir: "/src/icons", exists: () => true };

  it("gives Linux windows the packaged icon", () => {
    expect(windowIconPath({ ...base, platform: "linux" })).toBe(path.join("/opt/Cua Spaces/resources", "tray", LINUX_WINDOW_ICON));
    expect(windowIconPath({ ...base, platform: "linux", packaged: false })).toBe(path.join("/src/icons", LINUX_WINDOW_ICON));
  });

  it("leaves Windows and macOS to the exe and the bundle, and skips a missing file", () => {
    expect(windowIconPath({ ...base, platform: "win32" })).toBeUndefined();
    expect(windowIconPath({ ...base, platform: "darwin" })).toBeUndefined();
    expect(windowIconPath({ ...base, platform: "linux", exists: () => false })).toBeUndefined();
  });
});