Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

47.1 KB
Raw Blame
import { describe, expect, spyOn, test } from "bun:test";
import { readFileSync } from "node:fs";
import { configuredAuthProviders, loadConfig } from "../src/config";

// Intelligence is part of the MINIMUM contract, so it belongs in the base environment every other
// case builds on. Leaving it out of the base would make most of this file assert the behaviour of a
// deployment that is not allowed to exist.
const baseEnvironment = {
  DATABASE_URL: "postgres://openbot:openbot@localhost:5432/openbot",
  KEY_ENCRYPTION_KEY: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
  GOOGLE_OAUTH_CLIENT_ID: "google-client-id",
  GOOGLE_OAUTH_CLIENT_SECRET: "google-client-secret",
  BETTER_AUTH_SECRET: "a-long-enough-local-development-auth-secret",
  BETTER_AUTH_URL: "http://localhost:3001",
  INITIAL_ADMIN_EMAILS: "[email protected]",
  INTELLIGENCE_API_URL: "http://localhost:7100",
  INTELLIGENCE_GATEWAY_WS_URL: "ws://localhost:7103",
  INTELLIGENCE_API_KEY: "tenant-api-key",
  COPILOTKIT_LICENSE_TOKEN: "license-token",
  MANAGED_AGENT_AG_UI_URL: " http://localhost:4200/ag-ui ",
  MANAGED_AGENT_TOKEN: "managed-agent-token",
};

/**
 * The same deployment with nothing signing anybody in.
 *
 * `baseEnvironment` ships Google and a session secret because most tests want authentication on.
 * The provider tests need the opposite starting point, or "Microsoft is configured" cannot be told
 * apart from "Microsoft and the Google that was already there".
 */
/**
 * A deployment that is actually deployed.
 *
 * `baseEnvironment` carries the example encryption key, which is refused under
 * `NODE_ENV=production` — so a production case built on it fails on the key before it reaches
 * whatever it meant to test. A real key here keeps each production test about its own subject.
 */
const productionEnvironment = {
  ...baseEnvironment,
  NODE_ENV: "production",
  KEY_ENCRYPTION_KEY: "b3BlbmJvdC1wcm9kdWN0aW9uLXRlc3Qta2V5LTMyMzI=",
};

const {
  GOOGLE_OAUTH_CLIENT_ID: _googleId,
  GOOGLE_OAUTH_CLIENT_SECRET: _googleSecret,
  BETTER_AUTH_SECRET: _authSecret,
  BETTER_AUTH_URL: _authUrl,
  INITIAL_ADMIN_EMAILS: _adminEmails,
  ...withoutSignIn
} = baseEnvironment;

describe("deployment configuration", () => {
  test.each([undefined, "", "stale-legacy-key"])(
    "uses the CLI runtime key with legacy value %s",
    (legacy) => {
      const config = loadConfig({
        ...baseEnvironment,
        INTELLIGENCE_API_KEY: legacy,
        CPK_INTELLIGENCE_API_KEY: "  cpk-current-project  ",
      });
      expect(config.runtime.intelligence.apiKey).toBe("cpk-current-project");
    },
  );

  test("an empty CLI key preserves legacy-only deployments", () => {
    expect(
      loadConfig({ ...baseEnvironment, CPK_INTELLIGENCE_API_KEY: "  " }).runtime
        .intelligence.apiKey,
    ).toBe("tenant-api-key");
  });

  test("organization authority disables the standalone actor without requiring local provider secrets", () => {
    const config = loadConfig({
      ...withoutSignIn,
      OPENBOT_SINGLE_USER: "true",
      OPENBOT_ORGANIZATION_AUTH_URL: "https://openbot.company.example",
    });
    expect(config.singleUser).toBe(false);
    expect(config.auth).toBeUndefined();
    expect(config.organizationAuthUrl).toBe("https://openbot.company.example");
    expect(config.runtime.intelligence.apiKey).toBe("tenant-api-key");
  });
  test("resolves the Intelligence runtime, which is the only runtime", () => {
    const config = loadConfig(baseEnvironment);

    expect(config.runtime).toEqual({
      mode: "intelligence",
      durableHistory: true,
      intelligence: {
        apiUrl: "http://localhost:7100",
        gatewayWsUrl: "ws://localhost:7103",
        apiKey: "tenant-api-key",
        licenseToken: "license-token",
      },
    });
    expect(config.managedAgent).toEqual({
      endpoint: new URL("http://localhost:4200/ag-ui"),
      token: "managed-agent-token",
    });
    expect(config.tenantPackageDirectory).toBe("../examples/fintech");
  });

  test("allows deployment without an authentication provider, when asked to", () => {
    const config = loadConfig({
      DATABASE_URL: baseEnvironment.DATABASE_URL,
      KEY_ENCRYPTION_KEY: baseEnvironment.KEY_ENCRYPTION_KEY,
      INTELLIGENCE_API_URL: baseEnvironment.INTELLIGENCE_API_URL,
      INTELLIGENCE_GATEWAY_WS_URL: baseEnvironment.INTELLIGENCE_GATEWAY_WS_URL,
      INTELLIGENCE_API_KEY: baseEnvironment.INTELLIGENCE_API_KEY,
      MANAGED_AGENT_AG_UI_URL: baseEnvironment.MANAGED_AGENT_AG_UI_URL,
      MANAGED_AGENT_TOKEN: baseEnvironment.MANAGED_AGENT_TOKEN,
      // Explicit, because no provider means every visitor is the administrator and a deployment has
      // to say it meant that. See single-user.test.ts.
      OPENBOT_SINGLE_USER: "true",
    });

    expect(config.auth).toBeUndefined();
  });

  // The product does not have a mode without Intelligence, so each of these is a refusal to boot
  // rather than a degraded capability. Named individually because a deployment that sets three of
  // four is the likeliest real mistake, and the message has to say which one is missing.
  test.each([
    "INTELLIGENCE_API_URL",
    "INTELLIGENCE_GATEWAY_WS_URL",
    "INTELLIGENCE_API_KEY",
  ])("refuses to start when %s is missing", (name) => {
    const environment: Record<string, string | undefined> = {
      ...baseEnvironment,
    };
    delete environment[name];

    expect(() => loadConfig(environment)).toThrow(
      `CopilotKit Intelligence is required and is not configured. Missing: ${name === "INTELLIGENCE_API_KEY" ? "CPK_INTELLIGENCE_API_KEY (or INTELLIGENCE_API_KEY)" : name}`,
    );
  });

  test("starts without COPILOTKIT_LICENSE_TOKEN, because managed Intelligence no longer issues one", () => {
    const environment: Record<string, string | undefined> = {
      ...baseEnvironment,
    };
    delete environment.COPILOTKIT_LICENSE_TOKEN;

    const config = loadConfig(environment);

    if (config.runtime.mode !== "intelligence") {
      throw new Error("expected the Intelligence runtime");
    }
    expect(config.runtime.intelligence.licenseToken).toBeUndefined();
    expect(config.runtime.intelligence.apiKey).toBe(
      baseEnvironment.INTELLIGENCE_API_KEY,
    );
  });

  // The browser opens the gateway itself, at the address `/info` gives it. A server in a container
  // reaches local Intelligence somewhere the browser on the host cannot, so it may name both.
  test("keeps the browser's gateway address apart from the server's, when one is given", () => {
    const config = loadConfig({
      ...baseEnvironment,
      INTELLIGENCE_GATEWAY_WS_URL: "ws://host.docker.internal:14401",
      INTELLIGENCE_BROWSER_GATEWAY_WS_URL: "ws://localhost:14401",
    });
    if (config.runtime.mode !== "intelligence") {
      throw new Error("expected the Intelligence runtime");
    }
    expect(config.runtime.intelligence.gatewayWsUrl).toBe(
      "ws://host.docker.internal:14401",
    );
    expect(config.runtime.intelligence.browserGatewayWsUrl).toBe(
      "ws://localhost:14401",
    );

    const without = loadConfig(baseEnvironment);
    if (without.runtime.mode !== "intelligence") {
      throw new Error("expected the Intelligence runtime");
    }
    expect(without.runtime.intelligence.browserGatewayWsUrl).toBeUndefined();

    expect(() =>
      loadConfig({
        ...baseEnvironment,
        INTELLIGENCE_BROWSER_GATEWAY_WS_URL: "not a url",
      }),
    ).toThrow("INTELLIGENCE_BROWSER_GATEWAY_WS_URL must be a valid URL");
  });

  test("still forwards a licence token when a deployment sets one", () => {
    const config = loadConfig({
      ...baseEnvironment,
      COPILOTKIT_LICENSE_TOKEN: "self-hosted-licence",
    });

    if (config.runtime.mode !== "intelligence") {
      throw new Error("expected the Intelligence runtime");
    }
    expect(config.runtime.intelligence.licenseToken).toBe(
      "self-hosted-licence",
    );
  });

  test("refuses to start when Intelligence is absent entirely, rather than degrading", () => {
    expect(() =>
      loadConfig({
        DATABASE_URL: baseEnvironment.DATABASE_URL,
        KEY_ENCRYPTION_KEY: baseEnvironment.KEY_ENCRYPTION_KEY,
        MANAGED_AGENT_AG_UI_URL: baseEnvironment.MANAGED_AGENT_AG_UI_URL,
        MANAGED_AGENT_TOKEN: baseEnvironment.MANAGED_AGENT_TOKEN,
      }),
    ).toThrow("CopilotKit Intelligence is required and is not configured");
  });

  test("rejects incomplete OAuth client configuration", () => {
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        GOOGLE_OAUTH_CLIENT_ID: "google-client-id",
        GOOGLE_OAUTH_CLIENT_SECRET: "",
      }),
    ).toThrow(
      "GOOGLE_OAUTH_CLIENT_ID and GOOGLE_OAUTH_CLIENT_SECRET must be set together",
    );
  });

  test("starts without a managed Bot when neither half is set", () => {
    const environment: Record<string, string | undefined> = {
      ...baseEnvironment,
    };
    delete environment.MANAGED_AGENT_AG_UI_URL;
    delete environment.MANAGED_AGENT_TOKEN;

    expect(loadConfig(environment).managedAgent).toBeUndefined();
  });

  test("refuses a URL with no token", () => {
    const environment: Record<string, string | undefined> = {
      ...baseEnvironment,
    };
    delete environment.MANAGED_AGENT_TOKEN;

    expect(() => loadConfig(environment)).toThrow(
      "MANAGED_AGENT_TOKEN must be set when MANAGED_AGENT_AG_UI_URL is set",
    );
  });

  test("ignores a leftover token when no URL is set", () => {
    const environment: Record<string, string | undefined> = {
      ...baseEnvironment,
    };
    delete environment.MANAGED_AGENT_AG_UI_URL;

    expect(loadConfig(environment).managedAgent).toBeUndefined();
  });

  test("refuses a non-HTTP MANAGED_AGENT_AG_UI_URL", () => {
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        MANAGED_AGENT_AG_UI_URL: "ftp://localhost:4200/ag-ui",
      }),
    ).toThrow("MANAGED_AGENT_AG_UI_URL");
  });

  test("requires a base64-encoded 32-byte key-encryption key", () => {
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        KEY_ENCRYPTION_KEY: "local-development-key",
      }),
    ).toThrow("KEY_ENCRYPTION_KEY must be a base64-encoded 32-byte key");
  });

  /*
   * The key in `.env.example`, refused on a deployed server.
   *
   * It is a valid key — right length, right encoding — so nothing else about it fails a check. A
   * deployment that never changed it encrypts its credential vault with a value printed in a public
   * repository and looks exactly like one that did, which is why this refusal is the only thing
   * standing between "copied the example file" and that outcome.
   */
  test("refuses the example encryption key on a production deployment", () => {
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        NODE_ENV: "production",
      }),
    ).toThrow("KEY_ENCRYPTION_KEY is still the example key");
  });

  /*
   * The same trim the private-hosts gate below already gets, on the gate that matters more.
   *
   * Both sides of the comparison come out of one env file, and a trailing space there is invisible:
   * Docker's `env_file` preserves it verbatim and so does every hosting dashboard with a text box.
   * Compared raw, `NODE_ENV="production "` downgraded this refusal to a warning nobody reads at boot
   * and started the deployment on the public key.
   */
  test("refuses the example key when NODE_ENV carries whitespace", () => {
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        NODE_ENV: "production ",
      }),
    ).toThrow("KEY_ENCRYPTION_KEY is still the example key");
  });

  // The local workflow is the reason the example key is usable at all, so off production it still
  // does exactly what it did: warns, and starts.
  test.each(["development", undefined])(
    "warns about the example key and still starts under NODE_ENV=%p",
    (nodeEnv) => {
      const consoleWarn = spyOn(console, "warn").mockImplementation(() => {});

      try {
        expect(() =>
          loadConfig({
            ...baseEnvironment,
            ...(nodeEnv ? { NODE_ENV: nodeEnv } : {}),
          }),
        ).not.toThrow();

        const warning = consoleWarn.mock.calls
          .map(([first]) => String(first))
          .find((line) => line.includes("KEY_ENCRYPTION_KEY"));

        expect(warning).toBeDefined();
        expect(warning).toContain("which is public");
      } finally {
        consoleWarn.mockRestore();
      }
    },
  );

  test("enables Google authentication when its complete deployment contract is present", () => {
    const config = loadConfig({
      ...baseEnvironment,
      GOOGLE_OAUTH_CLIENT_ID: "google-client-id",
      GOOGLE_OAUTH_CLIENT_SECRET: "google-client-secret",
      BETTER_AUTH_SECRET: "a-long-enough-local-development-auth-secret",
      BETTER_AUTH_URL: "http://localhost:3001",
      INITIAL_ADMIN_EMAILS: "[email protected], [email protected]",
    });

    expect(config.auth).toEqual({
      baseUrl: "http://localhost:3001",
      secret: "a-long-enough-local-development-auth-secret",
      google: {
        clientId: "google-client-id",
        clientSecret: "google-client-secret",
      },
      trustedOrigins: [
        "http://127.0.0.1:3010",
        "http://[::1]:3010",
        "http://localhost:3010",
      ],
      initialAdminEmails: ["[email protected]", "[email protected]"],
      // Nothing set, so no opinion and everybody is admitted: the shape a deployment that has not
      // heard of SIGNIN_ALLOWED_EMAIL_DOMAINS has, which is every deployment running today.
      allowedEmailDomains: [],
    });
  });

  test("names domains the way somebody writes them", () => {
    const config = loadConfig({
      ...baseEnvironment,
      SIGNIN_ALLOWED_EMAIL_DOMAINS: " @Example.COM. , ,  foo.TEST ",
    });

    expect(config.auth?.allowedEmailDomains).toEqual([
      "example.com",
      "foo.test",
    ]);
  });

  // A non-empty list that names nothing refuses every sign-in, and `commaSeparated` drops blanks
  // before this normalisation rather than after it, so these three survive it.
  test.each(["@", ".", "@."])(
    "refuses to start when the domain list is just %p",
    (value) => {
      expect(() =>
        loadConfig({
          ...baseEnvironment,
          SIGNIN_ALLOWED_EMAIL_DOMAINS: value,
        }),
      ).toThrow("names no domain");
    },
  );

  // The list is checked against an address the signing-in tenant writes for itself, so under
  // `common` it refuses the honest and admits the rest. Refused because the operator DID say what
  // they wanted; the warning case below is the one that has said nothing.
  test("refuses a domain list combined with the multi-tenant Entra default", () => {
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        MICROSOFT_OAUTH_CLIENT_ID: "microsoft-client-id",
        MICROSOFT_OAUTH_CLIENT_SECRET: "a-long-enough-microsoft-client-secret",
        SIGNIN_ALLOWED_EMAIL_DOMAINS: "example.com",
      }),
    ).toThrow("names no directory");
  });

  /*
   * THE AUDIENCES A LITERAL "common" CHECK WALKS PAST. Microsoft describes `organizations` as
   * admitting any work or school account in any directory, so a domain list is exactly as
   * unenforceable there as under `common`, and `consumers` is every personal account. A check
   * written against one spelling is a check that refuses the careless and admits the specific.
   */
  test.each(["organizations", "consumers", "Common", "  COMMON  "])(
    "refuses a domain list against the non-directory audience %p",
    (tenantId) => {
      expect(() =>
        loadConfig({
          ...baseEnvironment,
          MICROSOFT_OAUTH_CLIENT_ID: "microsoft-client-id",
          MICROSOFT_OAUTH_CLIENT_SECRET:
            "a-long-enough-microsoft-client-secret",
          MICROSOFT_OAUTH_TENANT_ID: tenantId,
          SIGNIN_ALLOWED_EMAIL_DOMAINS: "example.com",
        }),
      ).toThrow("names no directory");
    },
  );

  test("accepts the same list against a named directory", () => {
    const config = loadConfig({
      ...baseEnvironment,
      MICROSOFT_OAUTH_CLIENT_ID: "microsoft-client-id",
      MICROSOFT_OAUTH_CLIENT_SECRET: "a-long-enough-microsoft-client-secret",
      MICROSOFT_OAUTH_TENANT_ID: "9188040d-6c67-4c5b-b112-36a304b66dad",
      SIGNIN_ALLOWED_EMAIL_DOMAINS: "example.com",
    });

    expect(config.auth?.allowedEmailDomains).toEqual(["example.com"]);
  });

  /**
   * Sign-in with more than one identity provider.
   *
   * A company mid-migration has some people on Entra and some still on Okta, so more than one at a
   * time is the normal shape rather than a corner. These assert the shape the sign-in screen reads
   * and every arrangement that cannot work refusing at start-up, which is the only moment a
   * misconfiguration is cheap to find.
   */
  const SESSION = {
    BETTER_AUTH_SECRET: "a-long-enough-local-development-auth-secret",
    BETTER_AUTH_URL: "http://localhost:3001",
    INITIAL_ADMIN_EMAILS: "[email protected]",
  };

  /** What a deployment with no provider has to say before it is allowed to come up. */
  const OPEN = { OPENBOT_SINGLE_USER: "true" };

  test("enables Microsoft, and admits any account until told a directory", () => {
    const config = loadConfig({
      ...withoutSignIn,
      ...SESSION,
      MICROSOFT_OAUTH_CLIENT_ID: "entra-client-id",
      MICROSOFT_OAUTH_CLIENT_SECRET: "entra-client-secret",
    });

    // `common` is Microsoft's own default and admits personal accounts as well as work ones. A
    // deployment that means "our staff" has to say so with a directory GUID.
    expect(config.auth?.microsoft).toEqual({
      clientId: "entra-client-id",
      clientSecret: "entra-client-secret",
      tenantId: "common",
    });
    expect(configuredAuthProviders(config.auth)).toEqual(["microsoft"]);
  });

  test("narrows Microsoft to one directory when given a tenant", () => {
    const config = loadConfig({
      ...withoutSignIn,
      ...SESSION,
      MICROSOFT_OAUTH_CLIENT_ID: "entra-client-id",
      MICROSOFT_OAUTH_CLIENT_SECRET: "entra-client-secret",
      MICROSOFT_OAUTH_TENANT_ID: "8f2c1e40-0000-0000-0000-000000000000",
    });

    expect(config.auth?.microsoft?.tenantId).toBe(
      "8f2c1e40-0000-0000-0000-000000000000",
    );
  });

  test("enables Okta against its issuer", () => {
    const config = loadConfig({
      ...withoutSignIn,
      ...SESSION,
      OKTA_OAUTH_CLIENT_ID: "okta-client-id",
      OKTA_OAUTH_CLIENT_SECRET: "okta-client-secret",
      OKTA_OAUTH_ISSUER: "https://example.okta.com/oauth2/default",
    });

    expect(config.auth?.okta).toEqual({
      clientId: "okta-client-id",
      clientSecret: "okta-client-secret",
      issuer: "https://example.okta.com/oauth2/default",
    });
  });

  test("refuses Okta without an issuer, which names no particular Okta", () => {
    expect(() =>
      loadConfig({
        ...withoutSignIn,
        ...SESSION,
        OKTA_OAUTH_CLIENT_ID: "okta-client-id",
        OKTA_OAUTH_CLIENT_SECRET: "okta-client-secret",
      }),
    ).toThrow("OKTA_OAUTH_ISSUER");
  });

  test("refuses an Okta issuer with no credentials behind it", () => {
    expect(() =>
      loadConfig({
        ...withoutSignIn,
        ...SESSION,
        OKTA_OAUTH_ISSUER: "https://example.okta.com/oauth2/default",
      }),
    ).toThrow("OKTA_OAUTH_CLIENT_ID");
  });

  test("carries all three at once, in a fixed order", () => {
    const config = loadConfig({
      ...withoutSignIn,
      ...SESSION,
      GOOGLE_OAUTH_CLIENT_ID: "google-client-id",
      GOOGLE_OAUTH_CLIENT_SECRET: "google-client-secret",
      MICROSOFT_OAUTH_CLIENT_ID: "entra-client-id",
      MICROSOFT_OAUTH_CLIENT_SECRET: "entra-client-secret",
      OKTA_OAUTH_CLIENT_ID: "okta-client-id",
      OKTA_OAUTH_CLIENT_SECRET: "okta-client-secret",
      OKTA_OAUTH_ISSUER: "https://example.okta.com/oauth2/default",
    });

    // The order the buttons appear in, fixed here so it cannot change with how a .env was written.
    expect(configuredAuthProviders(config.auth)).toEqual([
      "google",
      "microsoft",
      "okta",
    ]);
  });

  /**
   * Somebody has to be an administrator.
   *
   * The role is written from this list and no route anywhere changes one, so a deployment that
   * configures sign-in without it admits everybody as a plain user and can never promote anyone.
   * Start-up is the only cheap moment to notice.
   */
  test("refuses sign-in with nobody named as an administrator", () => {
    const { INITIAL_ADMIN_EMAILS: _none, ...withoutAdmins } = baseEnvironment;

    expect(() => loadConfig(withoutAdmins)).toThrow("INITIAL_ADMIN_EMAILS");
  });

  test("asks for no administrator when nothing signs anybody in", () => {
    // One administrator either way, and no list to write. Requiring one here as well would mean a
    // deployment had to name an administrator for a mode that has exactly one.
    expect(() => loadConfig({ ...withoutSignIn, ...OPEN })).not.toThrow();
  });

  test("refuses to start with no provider and nothing saying that was meant", () => {
    // The whole of the sign-in story in one line. This used to come up open, and `NODE_ENV` was the
    // only thing standing between a bare-VM deployment and serving every visitor as an
    // administrator, which is unset by default on exactly that deployment.
    expect(() => loadConfig(withoutSignIn)).toThrow(
      "No identity provider is configured",
    );
  });

  // One administrator and no sign-in is a thing you run where only you can reach it. NOT gated on
  // NODE_ENV: the image and the chart both set it to production for every deployment, the local
  // trial included, so it says nothing about who can reach this.
  test.each([
    ["a public URL", { OPENBOT_PUBLIC_URL: "https://openbot.example.com" }],
    ["an app URL", { OPENBOT_APP_URL: "https://openbot.example.com" }],
    ["a trusted origin", { TRUSTED_ORIGINS: "https://openbot.example.com" }],
    [
      "one published origin among loopback ones",
      { TRUSTED_ORIGINS: "http://localhost:3010,https://openbot.example.com" },
    ],
    ["an address that is not a URL at all", { OPENBOT_PUBLIC_URL: "openbot" }],
    [
      "a public URL missing its scheme",
      { OPENBOT_PUBLIC_URL: "example.com:3010" },
    ],
    ["an app URL missing its scheme", { OPENBOT_APP_URL: "example.com:3010" }],
    [
      "a trusted origin missing its scheme",
      { TRUSTED_ORIGINS: "http://localhost:3010,example.com:3010" },
    ],
  ])("refuses no sign-in combined with %s", (_label, published) => {
    expect(() =>
      loadConfig({ ...withoutSignIn, ...OPEN, ...published }),
    ).toThrow("OPENBOT_SINGLE_USER");
  });

  /*
   * THE DEPLOYMENTS THE FLAG EXISTS FOR, WHICH ARE NOT LOOPBACK. A home server, a Tailnet, a VPN
   * address, an mDNS name: none of these is a stranger's to reach, and refusing them would refuse
   * this feature's own audience. They are allowed and warned about, not refused.
   */
  test.each([
    ["a home LAN address", { OPENBOT_PUBLIC_URL: "http://192.168.1.10:3001" }],
    ["a 10/8 address", { OPENBOT_PUBLIC_URL: "http://10.0.0.5:3001" }],
    ["a 172.16/12 address", { OPENBOT_PUBLIC_URL: "http://172.20.1.4:3001" }],
    ["a Tailscale address", { OPENBOT_PUBLIC_URL: "http://100.101.102.103" }],
    ["a unique-local IPv6 address", { OPENBOT_PUBLIC_URL: "http://[fd00::1]" }],
    ["an mDNS name", { TRUSTED_ORIGINS: "http://openbot.local:3010" }],
    ["a single-label LAN name", { TRUSTED_ORIGINS: "http://nas:3010" }],
  ])("still runs with no sign-in on %s", (_label, reachable) => {
    expect(() =>
      loadConfig({ ...withoutSignIn, ...OPEN, ...reachable }),
    ).not.toThrow();
  });

  // 172.32 is outside 172.16/12, and 100.128 is outside 100.64/10. The near miss is the case a
  // hand-written range check gets wrong, so both are pinned as refused.
  test.each([
    ["just outside 172.16/12", { OPENBOT_PUBLIC_URL: "http://172.32.0.1" }],
    ["just outside 100.64/10", { OPENBOT_PUBLIC_URL: "http://100.128.0.1" }],
  ])("refuses no sign-in on %s", (_label, published) => {
    expect(() =>
      loadConfig({ ...withoutSignIn, ...OPEN, ...published }),
    ).toThrow("OPENBOT_SINGLE_USER");
  });

  // A name that merely starts with 127. is a public name, not loopback. Only a whole 127.x.x.x
  // address is.
  test.each([
    [
      "a public name starting 127.",
      { OPENBOT_PUBLIC_URL: "https://127.example.com" },
    ],
    [
      "a wildcard DNS name that starts 127.",
      { TRUSTED_ORIGINS: "https://127.0.0.1.nip.io" },
    ],
  ])("refuses no sign-in on %s", (_label, published) => {
    expect(() =>
      loadConfig({ ...withoutSignIn, ...OPEN, ...published }),
    ).toThrow("OPENBOT_SINGLE_USER");
  });

  // The local workflow the flag exists for, and the two addresses the quick start hands out.
  test.each([
    {},
    { TRUSTED_ORIGINS: "http://localhost:3010" },
    { TRUSTED_ORIGINS: "http://127.0.0.1:3010,http://[::1]:3010" },
    { OPENBOT_PUBLIC_URL: "http://127.0.0.1:3001" },
    // The chart and the image both set this for every install; it must decide nothing. A real key
    // comes with it because the gate beside this one refuses the example key under production, and
    // this case is about sign-in rather than about that.
    {
      NODE_ENV: "production",
      KEY_ENCRYPTION_KEY: productionEnvironment.KEY_ENCRYPTION_KEY,
      TRUSTED_ORIGINS: "http://localhost:3010",
    },
  ])("still runs with no sign-in on loopback: %j", (loopback) => {
    expect(() =>
      loadConfig({ ...withoutSignIn, ...OPEN, ...loopback }),
    ).not.toThrow();
  });

  test("is off, and lists nothing, when no provider is configured", () => {
    const config = loadConfig({ ...withoutSignIn, ...OPEN });

    expect(config.auth).toBeUndefined();
    expect(configuredAuthProviders(config.auth)).toEqual([]);
  });

  test("refuses a session secret with no provider to use it", () => {
    expect(() => loadConfig({ ...withoutSignIn, ...SESSION })).toThrow(
      "no identity provider",
    );
  });

  test("rejects incomplete Google authentication deployment settings", () => {
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        GOOGLE_OAUTH_CLIENT_ID: "google-client-id",
        GOOGLE_OAUTH_CLIENT_SECRET: "google-client-secret",
        BETTER_AUTH_SECRET: "",
        BETTER_AUTH_URL: "http://localhost:3001",
      }),
    ).toThrow("Sign-in requires BETTER_AUTH_SECRET");
  });

  // A turn that is ended is a turn somebody loses, so an unset variable leaves every stream alone
  // rather than acquiring a timeout the deployment never asked for. `.env.example` ships a value.
  test("leaves the stall watchdog off when nothing is configured", () => {
    expect(loadConfig(baseEnvironment).agentStallTimeoutMs).toBe(0);
  });

  test("takes a timeout in milliseconds, and zero as switching it off", () => {
    expect(
      loadConfig({ ...baseEnvironment, AGENT_STALL_TIMEOUT_MS: "120000" })
        .agentStallTimeoutMs,
    ).toBe(120_000);
    expect(
      loadConfig({ ...baseEnvironment, AGENT_STALL_TIMEOUT_MS: "0" })
        .agentStallTimeoutMs,
    ).toBe(0);
  });

  // Refused rather than defaulted, for the same reason a malformed policy is: an operator who meant
  // to write a boundary and mistyped it would otherwise get a deployment enforcing something else.
  test.each(["two minutes", "-1", "1.5"])(
    "refuses to start on AGENT_STALL_TIMEOUT_MS=%p",
    (value) => {
      expect(() =>
        loadConfig({ ...baseEnvironment, AGENT_STALL_TIMEOUT_MS: value }),
      ).toThrow("AGENT_STALL_TIMEOUT_MS");
    },
  );

  /*
   * AND THE EMPTY STRING IS NOT ONE OF THEM, which is why it is not a row of the list above.
   *
   * It rode along in that `test.each` behind an `if` that returned early, so the generated case was
   * named "refuses to start on AGENT_STALL_TIMEOUT_MS=\"\"" over a body asserting that it STARTS.
   * A reader picking a failure out of a run would have been told the opposite of what was checked,
   * and either half could have been changed to agree with the other — a config that began refusing
   * an empty value would have gone on passing under a name that said it should.
   *
   * OFF RATHER THAN REFUSED IS THE BEHAVIOUR, and it is the same one `PORT` has for the same
   * reason: `optional` trims and coerces empty to undefined, so an unset variable declared in a
   * compose file or left as `AGENT_STALL_TIMEOUT_MS=` in a `.env` arrives here as absent, which it
   * is. Refusing it would fail a deployment for writing down the default.
   */
  test("reads an empty AGENT_STALL_TIMEOUT_MS as the absent one, and starts", () => {
    expect(
      loadConfig({ ...baseEnvironment, AGENT_STALL_TIMEOUT_MS: "" })
        .agentStallTimeoutMs,
    ).toBe(0);
    expect(
      loadConfig({ ...baseEnvironment, AGENT_STALL_TIMEOUT_MS: "   " })
        .agentStallTimeoutMs,
    ).toBe(0);
  });

  test("listens on 3001 when neither PORT nor SERVER_PORT is set", () => {
    expect(loadConfig(baseEnvironment).port).toBe(3001);
  });

  test("moves the server by either name", () => {
    expect(loadConfig({ ...baseEnvironment, PORT: "3005" }).port).toBe(3005);
    expect(loadConfig({ ...baseEnvironment, SERVER_PORT: "3005" }).port).toBe(
      3005,
    );
    expect(
      loadConfig({ ...baseEnvironment, PORT: " 3005 ", SERVER_PORT: "3005" })
        .port,
    ).toBe(3005);
  });

  /*
   * An unset variable declared in a compose file, or left as `PORT=` in a `.env`, arrives as an
   * empty string rather than as absent. `process.env.PORT ?? process.env.SERVER_PORT` saw the empty
   * string and never reached the second name, and `Number.parseInt("")` handed `Bun.serve` a NaN,
   * which it answers by binding an ephemeral port nobody asked for.
   */
  test("reads SERVER_PORT when PORT is declared but empty, and the other way round", () => {
    expect(
      loadConfig({ ...baseEnvironment, PORT: "", SERVER_PORT: "3005" }).port,
    ).toBe(3005);
    expect(
      loadConfig({ ...baseEnvironment, PORT: "3005", SERVER_PORT: "" }).port,
    ).toBe(3005);
    expect(
      loadConfig({ ...baseEnvironment, PORT: "", SERVER_PORT: "" }).port,
    ).toBe(3001);
  });

  test("refuses to start when PORT and SERVER_PORT disagree", () => {
    expect(() =>
      loadConfig({ ...baseEnvironment, PORT: "3001", SERVER_PORT: "3005" }),
    ).toThrow("PORT (3001) and SERVER_PORT (3005) disagree");
  });

  // `Number.parseInt("30o1")` is 30, and the server used to come up there. Refused instead, the way
  // a mistyped cap is: a port has to fail at start-up, where somebody is looking.
  test.each(["30o1", "three", "0", "65536", "1.5", "-1"])(
    "refuses to start on PORT=%p",
    (value) => {
      expect(() => loadConfig({ ...baseEnvironment, PORT: value })).toThrow(
        "PORT must be a whole number between 1 and 65535",
      );
      expect(() =>
        loadConfig({ ...baseEnvironment, SERVER_PORT: value }),
      ).toThrow("SERVER_PORT must be a whole number between 1 and 65535");
    },
  );

  test("configures Docker as the per-Bot computer provider", () => {
    const config = loadConfig({
      ...baseEnvironment,
      COMPUTER_SUPERVISOR_URL: "http://localhost:4000",
      SUPERVISOR_TOKEN: "supervisor-token",
      COMPUTER_TOKEN: "computer-token",
    });

    expect(config.computer?.provider).toBe("docker");
    expect(config.computer).toEqual({
      provider: "docker",
      baseUrl: "http://localhost:4000",
      supervisorToken: "supervisor-token",
      token: "computer-token",
      allowPrivateHosts: false,
    });
  });

  test("configures one shared computer", () => {
    const config = loadConfig({
      ...baseEnvironment,
      AGENT_COMPUTER_URL: "http://localhost:4100",
      COMPUTER_TOKEN: "computer-token",
    });

    expect(config.computer?.provider).toBe("shared");
    expect(config.computer).toEqual({
      provider: "shared",
      baseUrl: "http://localhost:4100",
      token: "computer-token",
      allowPrivateHosts: false,
    });
  });

  test("leaves computers off when no provider address is configured", () => {
    expect(loadConfig(baseEnvironment).computer).toBeUndefined();
  });

  // `.env.example` used to ship AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS=true, and copying that file is the
  // ordinary way a deployment gets its environment. So the way a hosted deployment ends up reaching
  // its own network is not forgetting to set something, it is inheriting something. Refused in
  // production for the same reason the example encryption key is: convenient locally, and an opening
  // anywhere else.
  test("refuses to start when a production deployment allows private hosts", () => {
    expect(() =>
      loadConfig({
        ...productionEnvironment,
        AGENT_COMPUTER_URL: "http://localhost:4100",
        AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS: "true",
      }),
    ).toThrow("AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS");
  });

  // Both sides of the comparison come out of the same env file, and the switch is read through
  // `optional`, which trims. Comparing NODE_ENV raw would mean a trailing space typed into that file
  // slipped past the refusal while the switch beside it still counted as set.
  test("refuses a production deployment whose NODE_ENV carries whitespace", () => {
    expect(() =>
      loadConfig({
        ...productionEnvironment,
        NODE_ENV: "production ",
        AGENT_COMPUTER_URL: "http://localhost:4100",
        AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS: "true",
      }),
    ).toThrow("AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS");
  });

  // The refusal has to name the way out, because the person reading it at boot is looking at a file
  // they copied and does not necessarily know which line is the problem.
  test("says to remove the line, and that it is local only", () => {
    const attempt = () =>
      loadConfig({
        ...productionEnvironment,
        AGENT_COMPUTER_URL: "http://localhost:4100",
        AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS: "true",
      });

    expect(attempt).toThrow("local development only");
    expect(attempt).toThrow("Remove it");
  });

  // The half of the matrix that was always right and has to stay right: absent means off, including
  // in the environment where the new refusal lives.
  test("starts in production when nothing asked for private hosts", () => {
    const config = loadConfig({
      ...productionEnvironment,
      AGENT_COMPUTER_URL: "http://localhost:4100",
      COMPUTER_TOKEN: "computer-token",
    });

    expect(config.computer?.allowPrivateHosts).toBe(false);
  });

  // The local workflow is the reason the flag exists, so outside production it still does exactly
  // what it did. Warned about, because a laptop is where a deployment is configured and the warning
  // is the only chance to say this line does not travel.
  test.each(["development", undefined])(
    "warns and still allows private hosts under NODE_ENV=%p",
    (nodeEnv) => {
      const consoleWarn = spyOn(console, "warn").mockImplementation(() => {});

      try {
        const config = loadConfig({
          ...baseEnvironment,
          ...(nodeEnv ? { NODE_ENV: nodeEnv } : {}),
          AGENT_COMPUTER_URL: "http://localhost:4100",
          AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS: "true",
        });

        expect(config.computer?.allowPrivateHosts).toBe(true);
        // Searched rather than indexed: `baseEnvironment` carries the example encryption key, which
        // warns on its own account first.
        const warning = consoleWarn.mock.calls
          .map(([first]) => String(first))
          .find((line) => line.includes("AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS"));

        expect(warning).toBeDefined();
        expect(warning).toContain("local development only");
        expect(warning).toContain("Remove it before deploying");
      } finally {
        consoleWarn.mockRestore();
      }
    },
  );

  // The refusal above only helps a deployment that reads it. The reason there was anything to refuse
  // is that the file everybody copies arrived with the switch on, so the file is worth asserting
  // about directly: a live line here is the regression, whatever the code does afterwards.
  test("the shipped example does not turn private hosts on", () => {
    const example = readFileSync(
      new URL("../../.env.example", import.meta.url),
      "utf8",
    );

    // Commented-out mentions are wanted — that is how the switch stays discoverable for a laptop.
    const live = example
      .split("\n")
      .filter((line) =>
        /^\s*AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS\s*=/.test(line),
      );

    expect(live).toEqual([]);
  });

  // Anything that is not the exact opt-in is not an opt-in, so it is not the thing being refused
  // either. A deployment that wrote something else has private hosts off and starts.
  test.each(["false", "1", "yes", ""])(
    "starts in production on AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS=%p",
    (value) => {
      const config = loadConfig({
        ...productionEnvironment,
        AGENT_COMPUTER_URL: "http://localhost:4100",
        AGENT_COMPUTER_ALLOW_PRIVATE_HOSTS: value,
      });

      expect(config.computer?.allowPrivateHosts).toBe(false);
    },
  );

  test.each([
    ["Docker", "COMPUTER_SUPERVISOR_URL"],
    ["shared", "AGENT_COMPUTER_URL"],
  ] as const)("refuses an invalid %s computer provider URL", (_, urlName) => {
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        [urlName]: "not a URL",
      }),
    ).toThrow(`${urlName} must be a valid URL`);
  });
});

describe("accessibility", () => {
  test("is on when nothing is set", () => {
    expect(loadConfig(baseEnvironment).accessibility).toBe(true);
  });

  test.each(["true", "1"])(
    "is off on OPENBOT_ACCESSIBILITY_DISABLED=%p",
    (value) => {
      expect(
        loadConfig({
          ...baseEnvironment,
          OPENBOT_ACCESSIBILITY_DISABLED: value,
        }).accessibility,
      ).toBe(false);
    },
  );

  // Anything else is not a way of saying off. A deployment that typed something
  // else has not opted out, and silently treating it as opt-out would be a
  // setting that appears to work and does not.
  test.each(["false", "no", "", "yes"])(
    "stays on for OPENBOT_ACCESSIBILITY_DISABLED=%p",
    (value) => {
      expect(
        loadConfig({
          ...baseEnvironment,
          OPENBOT_ACCESSIBILITY_DISABLED: value,
        }).accessibility,
      ).toBe(true);
    },
  );
});

/**
 * Whether a Bot may answer with an interface it wrote itself.
 *
 * Same shape as accessibility above, and tested to the same bar for the same reason: the off switch
 * has a second reader. It is projected on /api/capabilities so the browser stops offering the tool
 * too, so a value that silently failed to mean "off" would leave Bots generating interfaces nothing
 * renders rather than merely leaving a capability on.
 */
describe("generated interfaces", () => {
  test("are on when nothing is set", () => {
    expect(loadConfig(baseEnvironment).generativeUi).toBe(true);
  });

  test.each(["true", "1"])("stay on for OPENBOT_GENERATIVE_UI=%p", (value) => {
    expect(
      loadConfig({ ...baseEnvironment, OPENBOT_GENERATIVE_UI: value })
        .generativeUi,
    ).toBe(true);
  });

  test.each(["false", "0"])("are off for OPENBOT_GENERATIVE_UI=%p", (value) => {
    expect(
      loadConfig({ ...baseEnvironment, OPENBOT_GENERATIVE_UI: value })
        .generativeUi,
    ).toBe(false);
  });

  test.each(["no", "", "yes", "TRUE", "on"])(
    "stay on for OPENBOT_GENERATIVE_UI=%p",
    (value) => {
      expect(
        loadConfig({ ...baseEnvironment, OPENBOT_GENERATIVE_UI: value })
          .generativeUi,
      ).toBe(true);
    },
  );

  // The old spelling was a disable switch. It must not still work, or a deployment that set it
  // would read as having made a choice it has not made under the new name.
  test("ignore the disable switch this replaced", () => {
    expect(
      loadConfig({
        ...baseEnvironment,
        OPENBOT_GENERATIVE_UI_DISABLED: "false",
      }).generativeUi,
    ).toBe(true);
  });
});

describe("the self-host banner", () => {
  test("is on when nothing is set", () => {
    expect(loadConfig(baseEnvironment).selfHostBanner).toBe(true);
  });

  test.each(["false", "0"])(
    "is off for OPENBOT_SELF_HOST_BANNER=%p",
    (value) => {
      expect(
        loadConfig({ ...baseEnvironment, OPENBOT_SELF_HOST_BANNER: value })
          .selfHostBanner,
      ).toBe(false);
    },
  );

  test.each(["true", "1", "", "no"])(
    "stays on for OPENBOT_SELF_HOST_BANNER=%p",
    (value) => {
      expect(
        loadConfig({ ...baseEnvironment, OPENBOT_SELF_HOST_BANNER: value })
          .selfHostBanner,
      ).toBe(true);
    },
  );
});

describe("automatic Learning container assignment", () => {
  test("is on when nothing is set", () => {
    expect(loadConfig(baseEnvironment).learningAutoAssign).toBe(true);
  });

  test.each(["false", "0"])(
    "is off for OPENBOT_LEARNING_AUTO_ASSIGN=%p",
    (value) => {
      expect(
        loadConfig({ ...baseEnvironment, OPENBOT_LEARNING_AUTO_ASSIGN: value })
          .learningAutoAssign,
      ).toBe(false);
    },
  );

  test("is off when the environment names a container", () => {
    const config = loadConfig({
      ...baseEnvironment,
      CPK_INTELLIGENCE_LEARNING_CONTAINER_ID: "support",
    });
    expect(config.learning).toEqual({ containerId: "support" });
    expect(config.learningAutoAssign).toBe(false);
  });

  test("a service token and its project create the container; one without the other is refused", () => {
    expect(loadConfig(baseEnvironment).learningSetup).toBeUndefined();
    expect(
      loadConfig({
        ...baseEnvironment,
        INTELLIGENCE_SERVICE_TOKEN: "synthetic-token",
        INTELLIGENCE_PROJECT_ID: "7",
      }).learningSetup,
    ).toEqual({ serviceToken: "synthetic-token", projectId: "7" });
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        INTELLIGENCE_SERVICE_TOKEN: "synthetic-token",
      }),
    ).toThrow("go together");
    expect(() =>
      loadConfig({
        ...baseEnvironment,
        INTELLIGENCE_SERVICE_TOKEN: "synthetic-token",
        INTELLIGENCE_PROJECT_ID: "my-project",
      }),
    ).toThrow("numeric ID");
  });
});

describe("memory sources and background research", () => {
  test("are on by default for a fresh deployment", () => {
    expect(loadConfig(baseEnvironment).memoryDefaults).toEqual({
      sources: true,
      research: true,
    });
  });

  test.each(["false", "0"])("each switches off on its own with %p", (value) => {
    expect(
      loadConfig({ ...baseEnvironment, OPENBOT_MEMORY_SOURCE_DEFAULTS: value })
        .memoryDefaults,
    ).toEqual({ sources: false, research: true });
    expect(
      loadConfig({ ...baseEnvironment, OPENBOT_PROACTIVE_DEFAULTS: value })
        .memoryDefaults,
    ).toEqual({ sources: true, research: false });
  });
});

/**
 * Naming the private addresses an agent may live at.
 *
 * The refusal cases matter as much as the parse: a list written as URLs or with a wildcard is a
 * list somebody believed was working, and finding out at the first registration that silently never
 * matches is worse than being told at boot.
 */
describe("AGENT_ENDPOINT_ALLOWED_HOSTS", () => {
  // The suite's own base, so this describes only its subject rather than re-deriving a whole
  // deployment and failing on whichever requirement it forgot.
  const base = () => ({ ...baseEnvironment });

  test("unset means none, which is the posture that shipped", () => {
    expect(loadConfig(base()).agentEndpointAllowedHosts.size).toBe(0);
  });

  test("a comma-separated list is parsed, lower-cased and trimmed", () => {
    const hosts = loadConfig({
      ...base(),
      AGENT_ENDPOINT_ALLOWED_HOSTS: " Agents.Internal , 10.0.0.42:9000 ",
    }).agentEndpointAllowedHosts;
    expect([...hosts].sort()).toEqual(["10.0.0.42:9000", "agents.internal"]);
  });

  test("an IPv6 address is stored the way the endpoint check spells it", () => {
    // The check compares against `URL.hostname`: compressed, lower-case, in brackets. An entry kept
    // as the operator wrote it was a line that silently never matched.
    const hosts = loadConfig({
      ...base(),
      AGENT_ENDPOINT_ALLOWED_HOSTS:
        "[0:0:0:0:0:0:0:1]:8443, [FE80::1], [::1:8443]",
    }).agentEndpointAllowedHosts;
    expect([...hosts].sort()).toEqual([
      "[::1:8443]",
      "[::1]:8443",
      "[fe80::1]",
    ]);
  });

  test("a bracketed entry that is not an address is refused, naming the entry", () => {
    expect(() =>
      loadConfig({
        ...base(),
        AGENT_ENDPOINT_ALLOWED_HOSTS: "[not-an-address]",
      }),
    ).toThrow(/must be a host/);
    expect(() =>
      loadConfig({ ...base(), AGENT_ENDPOINT_ALLOWED_HOSTS: "[::1]junk" }),
    ).toThrow(/must be a host/);
  });

  test("a URL is refused, naming the entry", () => {
    expect(() =>
      loadConfig({
        ...base(),
        AGENT_ENDPOINT_ALLOWED_HOSTS: "http://agents.internal/ag-ui",
      }),
    ).toThrow(/must be a host/);
  });

  test("a wildcard is refused, naming the entry", () => {
    // A pattern that widens by accident is the usual way a host check fails, so there are no
    // patterns to get wrong.
    expect(() =>
      loadConfig({ ...base(), AGENT_ENDPOINT_ALLOWED_HOSTS: "*.internal" }),
    ).toThrow(/Patterns are not accepted/);
  });
});

/**
 * A cap is a safety number, so a value that is not one has to stop the deployment rather than be
 * quietly replaced by the default. Somebody who typed `two` would otherwise believe they had set a
 * cap, and find out at the first loop.
 */
describe("how far a Bot may hand work on", () => {
  test("defaults to one level and three per run", () => {
    const config = loadConfig({ ...baseEnvironment });
    expect(config.handoff).toEqual({ maxDepth: 1, maxPerRun: 3 });
  });

  test("a deployment can widen or switch it off", () => {
    expect(
      loadConfig({
        ...baseEnvironment,
        BOT_HANDOFF_MAX_DEPTH: "0",
        BOT_HANDOFF_MAX_PER_RUN: "10",
      }).handoff,
    ).toEqual({ maxDepth: 0, maxPerRun: 10 });
  });

  test("refuses a cap that is not a whole number", () => {
    expect(() =>
      loadConfig({ ...baseEnvironment, BOT_HANDOFF_MAX_DEPTH: "two" }),
    ).toThrow("BOT_HANDOFF_MAX_DEPTH");
    expect(() =>
      loadConfig({ ...baseEnvironment, BOT_HANDOFF_MAX_PER_RUN: "-1" }),
    ).toThrow("BOT_HANDOFF_MAX_PER_RUN");
    expect(() =>
      loadConfig({ ...baseEnvironment, BOT_HANDOFF_MAX_PER_RUN: "1.5" }),
    ).toThrow("BOT_HANDOFF_MAX_PER_RUN");
  });
});

/**
 * Composio, which a deployment either bought or did not.
 *
 * Unset is the ordinary state and not a degraded one, so the absence has to read as `undefined`
 * rather than as an empty string that later code would have to keep asking about. Trimmed like
 * every other secret here, because a key pasted into a hosting dashboard arrives with whatever
 * whitespace came with it and the vendor would refuse the padded copy.
 */
test("a Composio key is read when set and absent when not", () => {
  expect(loadConfig(baseEnvironment).composioApiKey).toBeUndefined();
  expect(
    loadConfig({ ...baseEnvironment, COMPOSIO_API_KEY: "  ak_example  " })
      .composioApiKey,
  ).toBe("ak_example");
});

test("an Arcade key and a broker provider are read, and an unknown provider is refused", () => {
  const unset = loadConfig(baseEnvironment);
  expect(unset).toHaveProperty("arcadeApiKey", undefined);
  expect(unset).toHaveProperty("brokerProvider", undefined);
  const set = loadConfig({
    ...baseEnvironment,
    ARCADE_API_KEY: "  arc_example  ",
    OPENBOT_BROKER_PROVIDER: " arcade ",
  });
  expect(set.arcadeApiKey).toBe("arc_example");
  expect(set.brokerProvider).toBe("arcade");
  expect(() =>
    loadConfig({ ...baseEnvironment, OPENBOT_BROKER_PROVIDER: "zapier" }),
  ).toThrow(/OPENBOT_BROKER_PROVIDER must be "composio" or "arcade"/);
});