Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

8.1 KB
Raw Blame
/**
 * resolveServerConfig: how the server reads its environment.
 *
 * - An empty PORT (the common `PORT=` line in `.env`) reads as unset, never as port 0; an
 *   explicit value takes effect and an explicit "0" is kept (bind a random free port); a
 *   non-integer or out-of-range value throws. This matches the CLI's resolvePort.
 * - ADELIE_SEED_ADMIN_PASSWORD: unset, empty or blank leaves the seed unpinned (null, so the
 *   seed generates its own); a value is kept trimmed; desktop mode changes neither.
 * - ADELIE_CLI_ENTRY (or the pre-rename PENGUIN_CLI_ENTRY): a value is kept trimmed; a blank one
 *   falls through to the checkout lookup like an unset one.
 * - The deployment names live in two spellings while their writers are outside this repository
 *   (core's state/boundary-env.ts): ADELIE_* wins, PENGUIN_* still reads.
 * - PENGUIN_GO_ORIGIN accepts a loopback HTTP origin for integration work and refuses anything
 *   that is not a bare origin, or plaintext HTTP to another host.
 * - MODELSCOPE_BRIDGE_URL may carry a path prefix but refuses plaintext HTTP, credentials, a
 *   query and a fragment.
 */
import { describe, expect, it } from "vitest";
import path from "node:path";
import { resolveRoot } from "@adelie/core";
import { resolveServerConfig } from "../src/config.js";

const base = { PENGUIN_HOME: "/tmp/penguin-config-test" };

describe("resolveServerConfig: data root", () => {
  it("still honors the pre-rename PENGUIN_HOME", () => {
    expect(resolveServerConfig({ ...base }).root).toBe(base.PENGUIN_HOME);
  });

  it("honors ADELIE_HOME, and prefers it over the pre-rename name", () => {
    expect(resolveServerConfig({ ADELIE_HOME: "/tmp/adelie-config-test" }).root).toBe(
      "/tmp/adelie-config-test",
    );
    expect(resolveServerConfig({ ADELIE_HOME: "/tmp/adelie-config-test", ...base }).root).toBe(
      "/tmp/adelie-config-test",
    );
  });

  it("falls back to the root shared with the SDK / CLI when neither name is set", () => {
    expect(resolveServerConfig({}).root).toBe(resolveRoot());
  });

  it("puts the index database inside whichever root was chosen", () => {
    expect(resolveServerConfig({ ADELIE_HOME: "/tmp/adelie-config-test" }).dbPath).toBe(
      path.join("/tmp/adelie-config-test", "web.db"),
    );
  });
});

describe("resolveServerConfig: PORT parsing", () => {
  it("reads an empty PORT as unset, not as port 0", () => {
    const unset = resolveServerConfig({ ...base }).port;
    expect(unset).not.toBe(0);
    expect(resolveServerConfig({ ...base, PORT: "" }).port).toBe(unset);
  });

  it('takes an explicit value, and keeps "0" (binds a random available port)', () => {
    expect(resolveServerConfig({ ...base, PORT: "8930" }).port).toBe(8930);
    expect(resolveServerConfig({ ...base, PORT: "0" }).port).toBe(0);
  });

  it("throws on a non-integer or out-of-range value", () => {
    for (const bad of ["abc", "3.14", "-1", "65536"]) {
      expect(() => resolveServerConfig({ ...base, PORT: bad }), bad).toThrow(/Invalid port/);
    }
  });
});

describe("resolveServerConfig: seed password", () => {
  it("leaves the seed unpinned when unset, empty or blank, and keeps a value trimmed", () => {
    expect(resolveServerConfig({ ...base }).seedAdminPassword).toBeNull();
    for (const blank of ["", "  "]) {
      expect(
        resolveServerConfig({ ...base, ADELIE_SEED_ADMIN_PASSWORD: blank }).seedAdminPassword,
      ).toBeNull();
    }
    expect(
      resolveServerConfig({ ...base, ADELIE_SEED_ADMIN_PASSWORD: " penguin-9999 " })
        .seedAdminPassword,
    ).toBe("penguin-9999");
  });

  it("desktop mode leaves the seed unpinned, and an explicit value still wins there", () => {
    // Nothing pins it: the password the seed generates on its own is already unguessable, so
    // supplying one here would just be a second way to say the same thing.
    const desktop = { ...base, PENGUIN_DESKTOP_TOKEN: "tok" };
    expect(resolveServerConfig(desktop).seedAdminPassword).toBeNull();
    expect(
      resolveServerConfig({ ...desktop, ADELIE_SEED_ADMIN_PASSWORD: "penguin-2026" })
        .seedAdminPassword,
    ).toBe("penguin-2026");
  });
});

describe("resolveServerConfig: ADELIE_CLI_ENTRY parsing", () => {
  it("keeps a value trimmed — it is what the <root>/bin/penguin shim execs", () => {
    expect(
      resolveServerConfig({ ...base, ADELIE_CLI_ENTRY: " /opt/adelie/dist/penguin.js " }).cliEntry,
    ).toBe("/opt/adelie/dist/penguin.js");
    // The pre-rename name is still what `penguin server|web` exports and the desktop shell
    // passes, so a deployment that only sets it keeps working.
    expect(
      resolveServerConfig({ ...base, PENGUIN_CLI_ENTRY: " /opt/penguin/dist/penguin.js " })
        .cliEntry,
    ).toBe("/opt/penguin/dist/penguin.js");
  });

  it("falls through to the checkout lookup on a blank value, like unset", () => {
    // What the lookup finds depends on whether this checkout has built its CLI, so the
    // claim here is only that a blank value is not treated as an entry (see cli-shim.test.ts
    // for checkoutCliEntry itself).
    const blank = resolveServerConfig({ ...base, ADELIE_CLI_ENTRY: "   " }).cliEntry;
    expect(blank).toBe(resolveServerConfig({ ...base }).cliEntry);
    expect(blank === null || blank?.endsWith(`${path.sep}penguin.js`)).toBe(true);
  });
});

describe("resolveServerConfig: the deployment names read in both spellings", () => {
  it("prefers the ADELIE_* spelling of every one of them", () => {
    const config = resolveServerConfig({
      ...base,
      ADELIE_WEB_DIST: "/srv/adelie/web",
      PENGUIN_WEB_DIST: "/opt/penguin/web",
      ADELIE_WEB_DB: "/srv/adelie/web.db",
      PENGUIN_WEB_DB: "/opt/penguin/web.db",
      ADELIE_PORT_FILE: "/run/adelie.port",
      PENGUIN_PORT_FILE: "/run/penguin.port",
      ADELIE_DESKTOP_TOKEN: " adelie-token ",
      PENGUIN_DESKTOP_TOKEN: "penguin-token",
    });
    expect(config.webDist).toBe("/srv/adelie/web");
    expect(config.dbPath).toBe("/srv/adelie/web.db");
    expect(config.portFile).toBe("/run/adelie.port");
    expect(config.desktopToken).toBe("adelie-token");
  });

  it("keeps reading the pre-rename spelling an existing deployment or the shell sets", () => {
    const config = resolveServerConfig({
      ...base,
      PENGUIN_WEB_DIST: "/opt/penguin/web",
      PENGUIN_WEB_DB: "/opt/penguin/web.db",
      PENGUIN_PORT_FILE: "/run/penguin.port",
      PENGUIN_DESKTOP_TOKEN: "penguin-token",
    });
    expect(config.webDist).toBe("/opt/penguin/web");
    expect(config.dbPath).toBe("/opt/penguin/web.db");
    expect(config.portFile).toBe("/run/penguin.port");
    expect(config.desktopToken).toBe("penguin-token");
  });
});

describe("resolveServerConfig: PENGUIN_GO_ORIGIN parsing", () => {
  it("accepts a loopback HTTP origin for integration work, trimmed", () => {
    expect(
      resolveServerConfig({ ...base, PENGUIN_GO_ORIGIN: " http://127.0.0.1:3000 " })
        .penguinGoOrigin,
    ).toBe("http://127.0.0.1:3000");
  });

  it("refuses anything but a bare origin, and plaintext HTTP to another host", () => {
    for (const bad of [
      "http://token.penguin.ooo",
      "https://token.penguin.ooo/path",
      "https://user:[email protected]",
      "https://token.penguin.ooo?next=x",
    ]) {
      expect(() => resolveServerConfig({ ...base, PENGUIN_GO_ORIGIN: bad }), bad).toThrow(
        /Invalid PENGUIN_GO_ORIGIN/,
      );
    }
  });
});

describe("resolveServerConfig: MODELSCOPE_BRIDGE_URL parsing", () => {
  it("allows a path prefix, trimmed of its surrounding space and trailing slash", () => {
    expect(
      resolveServerConfig({
        ...base,
        MODELSCOPE_BRIDGE_URL: " https://go.penguin.ooo/modelscope/ ",
      }).modelscopeBridgeUrl,
    ).toBe("https://go.penguin.ooo/modelscope");
  });

  it("refuses plaintext HTTP, credentials, a query and a fragment", () => {
    for (const bad of [
      "http://go.penguin.ooo/modelscope",
      "https://user:[email protected]/modelscope",
      "https://go.penguin.ooo/modelscope?next=x",
      "https://go.penguin.ooo/modelscope#token",
    ]) {
      expect(() => resolveServerConfig({ ...base, MODELSCOPE_BRIDGE_URL: bad }), bad).toThrow(
        /Invalid MODELSCOPE_BRIDGE_URL/,
      );
    }
  });
});