Skip to content

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

135 KB
Raw Blame
import { execFileSync } from "node:child_process";
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, renameSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
import { tmpdir, userInfo } from "node:os";
import { dirname, join } from "node:path";
import { pathToFileURL } from "node:url";

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { botThreadLimitText, defaultBotThreads, maxConcurrentBotThreads, maxConcurrentChildThreads } from "./bot-thread-limits.ts";
import type { JsonValue } from "./schema.ts";
import { imageSlots } from "./image-generation.ts";

import { cacheUntilConfigChanges,
  changedInstanceIds,
  customMcpServers,
  engineLaunches,
  DATA_DIR,
  ensureDirs,
  instanceConfigs,
  isValidSshAlias,
  loadBrowserProfileIdAliases,
  loadConfig,
  localVmIdleTimeoutMinutes,
  localVmMaxInstances,
  localVmMode,
  parseConfigPatch,
  perBotLocalVmLimitProblem,
  parseStoredConfig,
  persistableInstanceConfigs,
  roomTurnTimeoutMinutes,
  mcpCallTimeoutMinutes,
  threadEventLogMaxBytes,
  threadEventLogRetentionDays,
  showToolCallsEnabled,
  saveConfig,
  skillAuthoringEnabled,
  sharedComputersEnabled,
  builtInBrowserEnabled,
  browserProfilePartitionId,
  browserProfilePartitionTarget,
  browserProfileReplacementConflict,
  browserProfileRoutingConflict,
  stripControlPlaneEnv,
  stripWorkspaceCredentialEnv,
  syncCredentialEnv,
  vpsSshAlias,
  browserEngineAttachCdpUrl,
  withInstanceCli,
  WORKSPACE_CREDENTIAL_ENV,
  PROVIDER_CREDENTIAL_ENV,
  mergeOpenCodeProviderKeys,
  openCodeProviderKeys,
  OPENCODE_PROVIDER_KEY_LIMIT,
  type AppConfig,
} from "./config.ts";

const reloaded = (patch: object, cfg: AppConfig = {}) =>
  changedInstanceIds(engineLaunches(instanceConfigs(cfg), {}), engineLaunches(instanceConfigs({ ...cfg, ...patch } as AppConfig), {}));

describe("configuration boundaries", () => {
  it("keeps the optional promotions preference in stored config and patches", () => {
    expect(parseStoredConfig({ features: { promotions: false } }).features).toEqual({ promotions: false });
    expect(parseConfigPatch({ features: { promotions: true } }).features).toEqual({ promotions: true });
    expect(() => parseConfigPatch({ features: { promotions: "off" } })).toThrow();
  });
  it("loads and takes a retired features.teamBoard without keeping it: the Team Board has no switch", () => {
    expect(parseStoredConfig({ features: { teamBoard: true, threadTabs: true } }).features).toEqual({ threadTabs: true });
    expect(parseConfigPatch({ features: { teamBoard: false } }).features).toEqual({});
  });

  it("requires an explicit backup to opt into automatic recovery without reloading engines", () => {
    expect(parseStoredConfig({}).automaticRecovery).toBeUndefined();
    expect(parseConfigPatch({ automaticRecovery: { enabled: false } })).toEqual({ automaticRecovery: { enabled: false } });
    const automaticRecovery = { enabled: true, backup: { instanceId: "codex", model: "backup", effort: "high" } };
    expect(parseStoredConfig({ automaticRecovery }).automaticRecovery).toEqual(automaticRecovery);
    expect(parseConfigPatch({ automaticRecovery }).automaticRecovery).toEqual(automaticRecovery);
    expect(reloaded({ automaticRecovery })).toEqual([]);
    const invalidSettings: JsonValue[] = [{ enabled: true }, { backup: { instanceId: "codex", model: "m" } },
      { enabled: "true" }, { enabled: true, backup: { instanceId: "", model: "m" } },
      { enabled: true, backup: { instanceId: "codex", model: "m", effort: "high", variant: "v" } },
      { enabled: false, maxRetries: 2 }];
    for (const invalid of invalidSettings) {
      expect(() => parseConfigPatch({ automaticRecovery: invalid })).toThrow("automaticRecovery");
    }
  });
  it("accepts shared user context, including clearing, without reloading providers", () => {
    const profile = { aboutMe: "I prefer short answers.\nMy time zone is Europe/Berlin." };
    expect(parseConfigPatch({ profile })).toEqual({ profile });
    expect(parseStoredConfig({ profile })).toEqual({ profile });
    expect(parseConfigPatch({ profile: { aboutMe: "" } })).toEqual({ profile: { aboutMe: "" } });
    expect(reloaded({ profile })).toEqual([]);
    expect(() => parseConfigPatch({ profile: { aboutMe: "x".repeat(24_001) } })).toThrow();
  });
  it("accepts rules for every bot next to About me, including clearing, without reloading providers", () => {
    const everyBotRules = "Answer in British English.\nNever book anything without asking.";
    expect(parseConfigPatch({ everyBotRules })).toEqual({ everyBotRules });
    expect(parseStoredConfig({ everyBotRules })).toEqual({ everyBotRules });
    expect(parseConfigPatch({ everyBotRules: "" })).toEqual({ everyBotRules: "" });
    expect(reloaded({ everyBotRules })).toEqual([]);
    expect(() => parseConfigPatch({ everyBotRules: "x".repeat(24_001) })).toThrow();
  });
  it("keeps the profile photo to a stored attachment picture, never an outside address or SVG", () => {
    const avatarUrl = "/api/attachments/5f0c2b1e-8a43-4c1d-9e7a-2b6f1d3c4e5a.webp";
    expect(parseConfigPatch({ profile: { avatarUrl } })).toEqual({ profile: { avatarUrl } });
    expect(parseStoredConfig({ profile: { name: "Ada", avatarUrl } })).toEqual({ profile: { name: "Ada", avatarUrl } });
    expect(parseConfigPatch({ profile: { avatarUrl: "" } })).toEqual({ profile: { avatarUrl: "" } });
    for (const outside of ["https://tracker.example/me.png", "/api/attachments/me.svg", "javascript:alert(1)", "/api/attachments/../config.json", "data:image/png;base64,AAAA"]) {
      expect(() => parseConfigPatch({ profile: { avatarUrl: outside } }), outside).toThrow();
    }
  });
  it("validates context budgets and keeps changes independent of provider reload", () => {
    const context = { autoCompact: false, compactAt: 0.7, rebuildBytes: 32_000 };
    expect(parseStoredConfig({ context })).toEqual({ context });
    expect(parseConfigPatch({ context })).toEqual({ context });
    expect(reloaded({ context })).toEqual([]);
    for (const value of [0, -1, "10", null, Infinity]) {
      expect(() => parseConfigPatch({ context: { compactAt: value } })).toThrow();
    }
    for (const value of [512, 1_024.1, 1_000_001]) {
      expect(() => parseConfigPatch({ context: { rebuildBytes: value } })).toThrow();
    }
  });
  it("accepts the replay tool-history switch as a boolean, without reloading providers", () => {
    for (const replayToolHistory of [true, false]) {
      const context = { replayToolHistory };
      expect(parseStoredConfig({ context })).toEqual({ context });
      expect(parseConfigPatch({ context })).toEqual({ context });
      expect(reloaded({ context })).toEqual([]);
    }
    for (const value of ["1", 1, "true", null]) {
      expect(() => parseConfigPatch({ context: { replayToolHistory: value } })).toThrow();
    }
  });
  it("keeps Fish Audio and ElevenLabs voice credentials separate", () => {
    const parsed = parseConfigPatch({
      tts: { provider: "fish", key: "eleven-key", fishKey: "fish-key", voice: "fish-voice" },
    });
    expect(parsed.tts).toEqual({
      provider: "fish",
      key: "eleven-key",
      fishKey: "fish-key",
      voice: "fish-voice",
    });
    expect(() => parseConfigPatch({ tts: { provider: "unknown" } })).toThrow("provider");
  });

  it("keeps Cartesia speech settings and trims its own API key", () => {
    const tts = { provider: "cartesia", cartesiaKey: "fixture-cartesia-key", cartesiaModel: "sonic-3.5", cartesiaLanguage: "nl", voice: "voice-dutch" };
    expect(parseStoredConfig({ tts }).tts).toEqual(tts);
    expect(parseConfigPatch({ tts: { ...tts, cartesiaKey: ` ${tts.cartesiaKey} ` } }).tts).toEqual(tts);
  });

  it("stores OpenAI readout without changing live-call settings or enabling readout", () => {
    const patch = { tts: { provider: "openai", voice: "marin" }, openai: { key: "fixture-key" } };
    expect(parseConfigPatch(patch)).toEqual(patch);
    expect(parseStoredConfig(patch)).toEqual(patch);
  });

  it("accepts only known Fish Audio speech models", () => {
    for (const fishModel of ["s2.1-pro", "s2.1-pro-free", "drama-3"]) {
      expect(parseConfigPatch({ tts: { fishModel } }).tts).toEqual({ fishModel });
    }
    expect(() => parseConfigPatch({ tts: { fishModel: "s1" } })).toThrow("fishModel");
  });

  it("accepts only supported voice playback rates", () => {
    for (const playbackRate of [0.75, 1, 1.25, 1.5, 1.75, 2]) {
      expect(parseConfigPatch({ tts: { playbackRate } }).tts).toEqual({ playbackRate });
    }
    expect(() => parseConfigPatch({ tts: { playbackRate: 1.1 } })).toThrow("playbackRate");
  });

  it("validates optional Fish Audio synthesis settings", () => {
    const tts = { fishSynthesis: { temperature: 0, topP: 1, speed: 0.5, mp3Bitrate: 128 } };
    expect(parseConfigPatch({ tts }).tts).toEqual(tts);
    expect(parseStoredConfig({ tts }).tts).toEqual(tts);
    const invalid: Record<string, number>[] = [{ temperature: -0.1 }, { topP: 1.1 }, { speed: 3 }, { mp3Bitrate: 1 }];
    for (const fishSynthesis of invalid) {
      expect(() => parseConfigPatch({ tts: { fishSynthesis } })).toThrow("fishSynthesis");
    }
  });

  it("keeps how the person is heard and the language they speak, and refuses anything else", () => {
    expect(parseConfigPatch({ tts: { hearWith: "elevenlabs", hearLanguage: " ES " } }).tts).toEqual({ hearWith: "elevenlabs", hearLanguage: "es" });
    expect(parseConfigPatch({ tts: { hearWith: "device", hearLanguage: "" } }).tts).toEqual({ hearWith: "device", hearLanguage: "" });
    expect(parseStoredConfig({ tts: { key: "el-own" } }).tts).toEqual({ key: "el-own" });
    expect(() => parseConfigPatch({ tts: { hearWith: "fish" } })).toThrow("hearWith");
    expect(() => parseConfigPatch({ tts: { hearLanguage: "english" } })).toThrow("hearLanguage");
  });

  it("defaults to what each host holds, and a saved number wins except over a plan's limit", () => {
    const GB = 1024 ** 3;
    const desktop = (gb: number) => ({ memoryGate: false, totalBytes: gb * GB, oneThreadPerBot: false });
    for (const [gb, threads] of [[2, 3], [4, 3], [8, 4], [16, 8], [26, 13], [32, 16], [128, 16]] as const) expect(maxConcurrentBotThreads({}, desktop(gb))).toBe(threads);
    expect(maxConcurrentBotThreads({}, desktop(15.8)), "a 16 GB machine reports a little less").toBe(8);
    for (const gb of [2, 8]) expect(maxConcurrentBotThreads({}, { memoryGate: true, totalBytes: gb * GB, oneThreadPerBot: false }), "the memory gate decides").toBe(16);
    expect(maxConcurrentBotThreads({ threads: { maxConcurrentPerBot: 2 } }, desktop(64))).toBe(2);
    expect(maxConcurrentBotThreads({ threads: { maxConcurrentPerBot: 2 } }, { memoryGate: true, totalBytes: 8 * GB, oneThreadPerBot: false })).toBe(2);
    const starter = { memoryGate: true, totalBytes: 2 * GB, oneThreadPerBot: true };
    expect(maxConcurrentBotThreads({}, starter)).toBe(1);
    expect(maxConcurrentBotThreads({ threads: { maxConcurrentPerBot: 8 } }, starter), "a plan's limit is not a preference").toBe(1);
    expect(maxConcurrentChildThreads({}, starter), "a bot's own turn is never held behind itself").toBe(16);
    expect(maxConcurrentChildThreads({ threads: { maxConcurrentPerBot: 2 } }, desktop(64)), "only a plan's limit lets children through").toBe(2);
    expect(botThreadLimitText({}, starter)).toContain("Starter runs one thread at a time per bot");
    expect(botThreadLimitText({ threads: { maxConcurrentPerBot: 5 } }, desktop(16))).toBe("this bot has reached its limit of 5 parallel threads — wait for one to finish");
    expect(parseStoredConfig({ threads: { maxConcurrentPerBot: 16 } })).toEqual({ threads: { maxConcurrentPerBot: 16 } });
    expect(maxConcurrentBotThreads(parseConfigPatch({ threads: { maxConcurrentPerBot: 1 } }))).toBe(1);
    for (const value of [0, -1, 17, 1.5, "10", null]) {
      expect(() => parseConfigPatch({ threads: { maxConcurrentPerBot: value } })).toThrow("threads.maxConcurrentPerBot");
    }
  });

  it("caps per-thread event logs only when a size is configured", () => {
    expect(threadEventLogMaxBytes({})).toBeNull();
    expect(threadEventLogMaxBytes({ threads: { maxConcurrentPerBot: 3 } })).toBeNull();
    const parsed = parseStoredConfig({ threads: { maxConcurrentPerBot: 3, eventLogMaxBytes: 50 * 1024 * 1024 } });
    expect(threadEventLogMaxBytes(parsed)).toBe(50 * 1024 * 1024);
    expect(parseConfigPatch({ threads: { eventLogMaxBytes: 50 * 1024 * 1024 } })).toEqual({ threads: { eventLogMaxBytes: 50 * 1024 * 1024 } });
    expect(parseConfigPatch({ threads: { eventLogMaxBytes: null } })).toEqual({ threads: { eventLogMaxBytes: null } });
    for (const value of [0, -1, 256 * 1024 - 1, 1.5, "1000"]) {
      expect(() => parseConfigPatch({ threads: { maxConcurrentPerBot: 3, eventLogMaxBytes: value } })).toThrow("threads.eventLogMaxBytes");
    }
  });

  it("keeps thread event logs forever unless a retention window is configured", () => {
    expect(threadEventLogRetentionDays({})).toBeNull();
    expect(threadEventLogRetentionDays(parseStoredConfig({ threads: { maxConcurrentPerBot: 2 } }))).toBeNull();
    const configured = parseStoredConfig({ threads: { maxConcurrentPerBot: 2, eventLogRetentionDays: 30 } });
    expect(threadEventLogRetentionDays(configured)).toBe(30);
    expect(parseConfigPatch({ threads: { eventLogRetentionDays: 30 } })).toEqual({ threads: { eventLogRetentionDays: 30 } });
    expect(parseConfigPatch({ threads: { eventLogRetentionDays: null } })).toEqual({ threads: { eventLogRetentionDays: null } });
    for (const value of [0, -1, 1.5, "30", 3660]) {
      expect(() => parseConfigPatch({ threads: { maxConcurrentPerBot: 2, eventLogRetentionDays: value } })).toThrow("threads.eventLogRetentionDays");
    }
  });

  it("keeps avatar providers and credentials separate, normalizing a router endpoint", () => {
    const parsed = parseConfigPatch({ imageGen: {
      provider: "custom", key: "openai-kept", customApiKey: "router-only",
      customUrl: "http://127.0.0.1:4000/v1/", customModel: " local/image ",
    } });
    expect(parsed.imageGen).toEqual({ primary: { provider: "custom" }, customModel: "local/image", key: "openai-kept", customApiKey: "router-only", customUrl: "http://127.0.0.1:4000/v1" });
    expect(parseStoredConfig({ imageGen: { key: "legacy" } }).imageGen).toEqual({ key: "legacy" });
    expect(() => parseConfigPatch({ imageGen: { provider: "unknown" } })).toThrow("provider");
    expect(parseConfigPatch({ imageGen: { primary: { provider: "gemini", model: " gemini-2.5-flash-image " }, fallback: null, maxPerTurn: 3 } }).imageGen)
      .toEqual({ primary: { provider: "gemini", model: "gemini-2.5-flash-image" }, fallback: null, maxPerTurn: 3 });
    expect(() => parseConfigPatch({ imageGen: { primary: { provider: "cursor" } } })).toThrow("primary");
    expect(() => parseConfigPatch({ imageGen: { maxPerTurn: 11 } })).toThrow("maxPerTurn");
    expect(parseConfigPatch({ imageGen: { provider: "xai" } }).imageGen).toEqual({ primary: { provider: "xai" } });
    for (const pasted of ["sk-or-v1-0123456789abcdef0123456789abcdef", "sk-proj-abcdefghijklmnop", "xai-abcdefghijklmnopqrstuvwx", "AIzaSyA-abcdefghijklmnopqrstuvwxyz012"]) {
      expect(() => parseConfigPatch({ imageGen: { primary: { provider: "openrouter", model: pasted } } })).toThrow("API key");
      expect(() => parseConfigPatch({ imageGen: { fallback: { provider: "openai", model: pasted } } })).toThrow("API key");
      expect(() => parseConfigPatch({ imageGen: { provider: "custom", customModel: pasted } })).toThrow("API key");
    }
    expect(parseConfigPatch({ imageGen: { primary: { provider: "openrouter", model: "openai/gpt-image-2" } } }).imageGen).toEqual({ primary: { provider: "openrouter", model: "openai/gpt-image-2" } });
    expect(() => parseConfigPatch({ imageGen: { customUrl: "https://user:[email protected]/v1" } })).toThrow("customUrl");
    const childEnv = { OMB_CUSTOM_IMAGE_KEY: "must-not-reach-bot" };
    stripWorkspaceCredentialEnv(childEnv);
    expect(childEnv).not.toHaveProperty("OMB_CUSTOM_IMAGE_KEY");
  });
  it("persists a custom domain but excludes it from generic config patches", () => {
    expect(parseStoredConfig({ customDomain: "https://bots.example.com" })).toEqual({ customDomain: "https://bots.example.com" });
    expect(parseConfigPatch({ customDomain: "https://unverified.example.com", language: "en" })).toEqual({ language: "en" });
    expect(parseStoredConfig({ customDomain: "" })).toEqual({ customDomain: "" });
  });
  it("keeps supported stored settings and drops unrelated top-level data", () => {
    expect(
      parseStoredConfig({
        profile: { name: "Ada", email: "[email protected]" },
        instances: { claude: { driver: "claudeAgent", config: { cli: "/opt/claude" } } },
        unrelated: { secret: "not part of the config contract" },
      }),
    ).toEqual({
      profile: { name: "Ada", email: "[email protected]" },
      instances: { claude: { driver: "claudeAgent", config: { cli: "/opt/claude" } } },
    });
  });

  it("loads a config that still has the removed cloud-overflow and idle-release keys, and drops them", () => {
    const stored = {
      language: "en",
      features: { browser: true, computerClaimIdleRelease: true, cloudOverflow: true },
      cloudOverflow: { perSecondCostUsd: 0.0004, idleStopMs: 300_000, allowlistedThreads: ["t1"] },
    };
    expect(parseStoredConfig(stored)).toEqual({ language: "en", features: { browser: true } });
    expect(parseConfigPatch(stored)).toEqual({ language: "en", features: { browser: true } });
  });

  it("loads a config that still switches on chief escalations, and drops the switch", () => {
    const stored = { language: "en", features: { browser: true, chiefEscalations: true } };
    expect(parseStoredConfig(stored)).toEqual({ language: "en", features: { browser: true } });
    expect(parseConfigPatch(stored)).toEqual({ language: "en", features: { browser: true } });
  });

  it("rejects malformed stored instances and API patches", () => {
    expect(() => parseStoredConfig({ instances: { claude: { driver: 42 } } })).toThrow("instances.claude.driver");
    expect(() => parseStoredConfig({ browserProfiles: [{ id: "../evil", name: "Unsafe" }] })).toThrow(
      "browserProfiles.0.id",
    );
    expect(() => parseConfigPatch({ opencodeGo: { apiKey: 42 } })).toThrow("opencodeGo.apiKey");
    expect(() => parseConfigPatch({ profile: [] })).toThrow("profile");
  });

  it("accepts and normalizes a default model selection", () => {
    const input = { defaultModelSelection: { instanceId: " codex ", model: " chosen-model ", effort: "high" } };
    const expected = { defaultModelSelection: { instanceId: "codex", model: "chosen-model", effort: "high" } };
    expect(parseStoredConfig(input)).toEqual(expected);
    expect(parseConfigPatch(input)).toEqual(expected);
  });

  it("accepts a new-bot effort default and clears it with null", () => {
    expect(parseStoredConfig({ newBots: { effort: "medium" } })).toEqual({ newBots: { effort: "medium" } });
    expect(parseConfigPatch({ newBots: { effort: "medium" } })).toEqual({ newBots: { effort: "medium" } });
    expect(parseConfigPatch({ newBots: { effort: null } })).toEqual({ newBots: { effort: null } });
    expect(() => parseConfigPatch({ newBots: { effort: "turbo" } })).toThrow("newBots");
    expect(() => parseConfigPatch({ newBots: { approvalMode: "full" } })).toThrow("newBots");
  });

  it("round-trips an opaque model variant without converting omission to none", () => {
    const defaultModelSelection = { instanceId: "opencodeGo", model: "provider/model", variant: "minimal" };
    expect(parseConfigPatch({ defaultModelSelection })).toEqual({ defaultModelSelection });
    expect(parseStoredConfig({ defaultModelSelection })).toEqual({ defaultModelSelection });
    expect(parseConfigPatch({ defaultModelSelection: { instanceId: "opencodeGo", model: "provider/model" } }))
      .toEqual({ defaultModelSelection: { instanceId: "opencodeGo", model: "provider/model" } });
  });

  it.each<JsonValue>([
    null,
    "codex/model",
    {},
    { instanceId: "codex" },
    { instanceId: "", model: "model" },
    { instanceId: "codex", model: "   " },
    { instanceId: "codex", model: 42 },
    { instanceId: "codex", model: "model", effort: "turbo" },
    { instanceId: "opencodeGo", model: "model", variant: "" },
    { instanceId: "opencodeGo", model: "model", variant: " low " },
    { instanceId: "opencodeGo", model: "model", variant: "low", effort: "high" },
  ])("rejects an invalid default model selection: %j", (defaultModelSelection) => {
    expect(() => parseStoredConfig({ defaultModelSelection })).toThrow("defaultModelSelection");
    expect(() => parseConfigPatch({ defaultModelSelection })).toThrow("defaultModelSelection");
  });


  it("canonicalizes legacy browser profile ids without dropping other stored settings", () => {
    expect(parseStoredConfig({
      profile: { name: "Ada", email: "[email protected]" },
      rooms: { turnTimeoutMinutes: 20 },
      features: { browser: true },
      browserProfiles: [
        { id: "Work", name: " Work " },
        { id: "Work", name: "Second workspace" },
      ],
      instances: { claude: { driver: "claudeAgent", config: { cli: "/opt/claude" } } },
    })).toEqual({
      profile: { name: "Ada", email: "[email protected]" },
      rooms: { turnTimeoutMinutes: 20 },
      features: { browser: true },
      browserProfiles: [
        { id: "work", name: "Work", partitionId: "Work" },
        { id: "work-2", name: "Second workspace" },
      ],
      instances: { claude: { driver: "claudeAgent", config: { cli: "/opt/claude" } } },
    });
  });

  it("preserves an unambiguous uppercase profile's exact durable partition", () => {
    const config = parseStoredConfig({
      browserProfiles: [{ id: "ClientA", name: "Client A" }],
    });
    expect(config.browserProfiles).toEqual([
      { id: "clienta", name: "Client A", partitionId: "ClientA" },
    ]);
    expect(browserProfilePartitionId(config.browserProfiles![0]!)).toBe("ClientA");
    expect(browserProfilePartitionTarget(config, "clienta")).toEqual({
      profileId: "clienta",
      partitionId: "ClientA",
    });
  });

  it("isolates case-colliding legacy profiles instead of sharing an account", () => {
    const config = parseStoredConfig({
      browserProfiles: [
        { id: "Work", name: "Uppercase" },
        { id: "work", name: "Canonical" },
      ],
    });
    expect(config.browserProfiles).toEqual([
      { id: "work-2", name: "Uppercase" },
      { id: "work", name: "Canonical" },
    ]);
    const partitions = config.browserProfiles!.map(browserProfilePartitionId);
    expect(new Set(partitions.map((id) => id.toLowerCase())).size).toBe(partitions.length);
  });

  it("reserves explicit suffix ids while isolating a case collision", () => {
    const config = parseStoredConfig({
      browserProfiles: [
        { id: "Work", name: "Uppercase" },
        { id: "work", name: "Canonical" },
        { id: "work-2", name: "Explicit suffix" },
      ],
    });
    expect(config.browserProfiles).toEqual([
      { id: "work-3", name: "Uppercase" },
      { id: "work", name: "Canonical" },
      { id: "work-2", name: "Explicit suffix" },
    ]);
    const partitions = config.browserProfiles!.map(browserProfilePartitionId);
    expect(new Set(partitions.map((id) => id.toLowerCase())).size).toBe(3);
  });

  it("round-trips migrated partition aliases idempotently", () => {
    const once = parseStoredConfig({
      browserProfiles: [
        { id: "ClientA", name: "Client A" },
        { id: "Personal", name: "Personal" },
      ],
    });
    expect(parseStoredConfig(JSON.parse(JSON.stringify(once)))).toEqual(once);
  });

  it("keeps explicit suffix partitions owned by their own canonical profile", () => {
    const once = parseStoredConfig({
      browserProfiles: [
        { id: "Foo", name: "Case collision" },
        { id: "FOO-2", name: "Explicit suffix" },
        { id: "foo", name: "Canonical" },
      ],
    });
    expect(once.browserProfiles).toEqual([
      { id: "foo-3", name: "Case collision" },
      { id: "foo-2", name: "Explicit suffix", partitionId: "FOO-2" },
      { id: "foo", name: "Canonical" },
    ]);
    const profiles = once.browserProfiles!;
    for (const [index, profile] of profiles.entries()) {
      const partition = browserProfilePartitionId(profile).toLowerCase();
      expect(
        profiles.some((candidate, candidateIndex) => candidateIndex !== index && candidate.id === partition),
      ).toBe(false);
    }
    expect(parseStoredConfig(JSON.parse(JSON.stringify(once)))).toEqual(once);
  });

  it("keeps legacy Guest isolated from an explicit Guest-2 profile", () => {
    const once = parseStoredConfig({
      browserProfiles: [
        { id: "Guest", name: "Legacy guest account" },
        { id: "Guest-2", name: "Explicit guest suffix" },
      ],
    });
    expect(once.browserProfiles).toEqual([
      { id: "guest-3", name: "Legacy guest account", partitionId: "Guest" },
      { id: "guest-2", name: "Explicit guest suffix", partitionId: "Guest-2" },
    ]);
    expect(parseStoredConfig(JSON.parse(JSON.stringify(once)))).toEqual(once);
  });

  it("repairs a prior cross-mapped partition without moving either exact legacy account", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({
      browserProfiles: [
        { id: "foo-2", name: "First", partitionId: "foo-2-2" },
        { id: "foo-2-2", name: "Second", partitionId: "FOO-2" },
        { id: "foo", name: "Canonical" },
      ],
    }));
    try {
      const once = loadConfig();
      expect(once.browserProfiles).toEqual([
        { id: "foo-2-3", name: "First", partitionId: "foo-2-2" },
        { id: "foo-2-2-2", name: "Second", partitionId: "FOO-2" },
        { id: "foo", name: "Canonical" },
      ]);
      const aliases = loadBrowserProfileIdAliases();
      const first = browserProfilePartitionTarget(once, aliases.get("foo-2")!);
      const second = browserProfilePartitionTarget(once, aliases.get("foo-2-2")!);
      expect(first).toEqual({ profileId: "foo-2-3", partitionId: "foo-2-2" });
      expect(second).toEqual({ profileId: "foo-2-2-2", partitionId: "FOO-2" });
      // bots.json may be migrated before config.json is rewritten: a second hydration must leave migrated references fixed.
      expect(aliases.get(first!.profileId) ?? first!.profileId).toBe(first!.profileId);
      expect(aliases.get(second!.profileId) ?? second!.profileId).toBe(second!.profileId);
      expect(parseStoredConfig(JSON.parse(JSON.stringify(once)))).toEqual(once);
    } finally {
      rmSync(path, { force: true });
    }
  });

  it("keeps chained partition aliases fixed across repeated raw-config hydration", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({
      browserProfiles: [
        { id: "foo", name: "First", partitionId: "Bar" },
        { id: "bar", name: "Second", partitionId: "Baz" },
      ],
    }));
    try {
      const once = loadConfig();
      expect(once.browserProfiles).toEqual([
        { id: "foo", name: "First", partitionId: "Bar" },
        { id: "bar-2", name: "Second", partitionId: "Baz" },
      ]);
      const aliases = loadBrowserProfileIdAliases();
      const hydrate = (id: string) => aliases.get(id) ?? id;
      expect(hydrate("foo")).toBe("foo");
      expect(hydrate(hydrate("foo"))).toBe("foo");
      expect(hydrate("bar")).toBe("bar-2");
      expect(hydrate(hydrate("bar"))).toBe("bar-2");

      const first = browserProfilePartitionTarget(once, hydrate("foo"));
      const second = browserProfilePartitionTarget(once, hydrate("bar"));
      expect(first).toEqual({ profileId: "foo", partitionId: "Bar" });
      expect(second).toEqual({ profileId: "bar-2", partitionId: "Baz" });
      expect(parseStoredConfig(JSON.parse(JSON.stringify(once)))).toEqual(once);
    } finally {
      rmSync(path, { force: true });
    }
  });

  it("blocks a new logical id from claiming another profile's retained partition", () => {
    const profiles = [
      { id: "client-repaired", name: "Existing", partitionId: "Client" },
      { id: "client", name: "New account" },
    ];
    expect(browserProfileRoutingConflict(profiles)).toMatch(/already used by another durable session/i);
  });

  it("blocks same-write reuse of a legacy partition that is being erased", () => {
    expect(browserProfileReplacementConflict(
      [{ id: "legacy-client", name: "Legacy", partitionId: "Client" }],
      [{ id: "client", name: "New account" }],
    )).toMatch(/delete it first, then add/i);
  });

  it("truncates 40-character collision suffixes without stealing an explicit id", () => {
    const base = "a".repeat(40);
    const explicitSuffix = `${"a".repeat(38)}-2`;
    const once = parseStoredConfig({
      browserProfiles: [
        { id: base.toUpperCase(), name: "Case collision" },
        { id: explicitSuffix.toUpperCase(), name: "Explicit suffix" },
        { id: base, name: "Canonical" },
      ],
    });
    expect(once.browserProfiles).toEqual([
      { id: `${"a".repeat(38)}-3`, name: "Case collision" },
      { id: explicitSuffix, name: "Explicit suffix", partitionId: explicitSuffix.toUpperCase() },
      { id: base, name: "Canonical" },
    ]);
    expect(once.browserProfiles!.every((profile) => profile.id.length <= 40)).toBe(true);
    expect(parseStoredConfig(JSON.parse(JSON.stringify(once)))).toEqual(once);
  });

  it("accepts only a simple VPS SSH config alias and exposes no credentials", () => {
    expect(isValidSshAlias("production-vps")).toBe(true);
    expect(isValidSshAlias("prod; reboot")).toBe(false);
    expect(() => parseConfigPatch({ vps: { sshAlias: "prod; reboot" } })).toThrow("vps.sshAlias");
    expect(parseConfigPatch({ vps: { sshAlias: "production-vps" } })).toEqual({
      vps: { sshAlias: "production-vps" },
    });
    expect(vpsSshAlias({ vps: { sshAlias: "production-vps" } })).toBe("production-vps");
    expect(vpsSshAlias({ vps: { sshAlias: "-bad" } })).toBeNull();
  });

  it("keeps named VPS servers apart from the default and accepts only plain names and simple aliases", () => {
    const servers = [{ name: "Home lab", sshAlias: "home-lab" }, { name: "Office", sshAlias: "office.vps" }];
    expect(parseConfigPatch({ vpsServers: servers })).toEqual({ vpsServers: servers });
    expect(parseConfigPatch({ vpsServers: [{ name: "  Home lab ", sshAlias: "home-lab" }] })).toEqual({ vpsServers: [{ name: "Home lab", sshAlias: "home-lab" }] });
    expect(() => parseConfigPatch({ vpsServers: [{ name: "Bad", sshAlias: "prod; reboot" }] })).toThrow("vpsServers");
    expect(() => parseConfigPatch({ vpsServers: [{ name: " ", sshAlias: "home-lab" }] })).toThrow("vpsServers");
    expect(() => parseConfigPatch({ vpsServers: [{ name: "Line\nbreak", sshAlias: "home-lab" }] })).toThrow("vpsServers");
    expect(() => parseConfigPatch({ vpsServers: [...servers, { name: "Again", sshAlias: "home-lab" }] })).toThrow("own SSH alias");
    expect(() => parseConfigPatch({ vpsServers: Array.from({ length: 21 }, (_, index) => ({ name: `S${index}`, sshAlias: `s${index}` })) })).toThrow("vpsServers");
    // A hand-edited bad entry is dropped on load, never the whole file.
    expect(parseStoredConfig({ vpsServers: [...servers, { name: "Bad", sshAlias: "-bad" }, "junk"] })).toEqual({ vpsServers: servers });
    expect(parseStoredConfig({ vpsServers: "home-lab" })).toEqual({});
    expect(parseStoredConfig({ vpsServers: Array.from({ length: 25 }, (_, index) => ({ name: `S${index}`, sshAlias: `s${index}` })) }).vpsServers).toHaveLength(20);
    expect(() => parseConfigPatch({ vpsServers: [{ name: "Lab\u202e", sshAlias: "home-lab" }] })).toThrow("vpsServers");
    expect(parseStoredConfig({ vpsServers: [...servers, { name: "Copy", sshAlias: "home-lab" }] })).toEqual({ vpsServers: servers });
    expect(parseStoredConfig({})).toEqual({});
  });

  it("validates browserEngine.attachCdpUrl as a bare CDP port or an http(s)/ws(s) URL, and forwards it only when configured", () => {
    expect(parseStoredConfig({})).toEqual({});
    expect(browserEngineAttachCdpUrl({})).toBeNull();
    for (const value of ["9333", "1", "65535", "http://127.0.0.1:9333", "https://cdp.internal:9333/", "ws://127.0.0.1:9333/devtools/browser/abc"]) {
      expect(parseStoredConfig({ browserEngine: { attachCdpUrl: value } })).toEqual({ browserEngine: { attachCdpUrl: value } });
      expect(parseConfigPatch({ browserEngine: { attachCdpUrl: value } })).toEqual({ browserEngine: { attachCdpUrl: value } });
      expect(browserEngineAttachCdpUrl({ browserEngine: { attachCdpUrl: value } })).toBe(value);
    }
    for (const value of ["0", "70000", "not-a-url", "ftp://127.0.0.1:9333", "javascript:alert(1)"]) {
      expect(() => parseConfigPatch({ browserEngine: { attachCdpUrl: value } })).toThrow("browserEngine.attachCdpUrl");
    }
    expect(parseConfigPatch({ browserEngine: { attachCdpUrl: "" } })).toEqual({ browserEngine: { attachCdpUrl: "" } });
    expect(browserEngineAttachCdpUrl({ browserEngine: { attachCdpUrl: "" } })).toBeNull();
  });

  it("keeps a private per-profile Chrome address while migrating a legacy partition", () => {
    const configured = { id: "Scout", name: "Scout Chrome", browser: "chrome", cdpUrl: " http://127.0.0.1:9333 " };
    expect(parseStoredConfig({ browserProfiles: [configured] }).browserProfiles).toEqual([
      { ...configured, id: "scout", partitionId: "Scout", cdpUrl: "http://127.0.0.1:9333" },
    ]);
    for (const cdpUrl of ["", "70000", "ftp://localhost:9333", "https://", "https://somehost:70000"]) {
      expect(() => parseStoredConfig({ browserProfiles: [{ ...configured, cdpUrl }] })).toThrow("browserProfiles.0.cdpUrl");
    }
    expect(() => parseStoredConfig({ browserProfiles: [{ id: "scout", name: configured.name, cdpUrl: configured.cdpUrl }] })).toThrow("browserProfiles.0.cdpUrl");
    expect(() => parseConfigPatch({ browserProfiles: [{ ...configured, id: "scout" }] })).toThrow();
  });

  it("takes browserEngine.idleTimeoutMinutes as whole minutes from 0 (never) up to the engine's own hour, and clears on null patch", () => {
    for (const value of [0, 1, 10, 60]) {
      expect(parseStoredConfig({ browserEngine: { idleTimeoutMinutes: value } })).toEqual({ browserEngine: { idleTimeoutMinutes: value } });
      expect(parseConfigPatch({ browserEngine: { idleTimeoutMinutes: value } })).toEqual({ browserEngine: { idleTimeoutMinutes: value } });
    }
    expect(parseConfigPatch({ browserEngine: { idleTimeoutMinutes: null } })).toEqual({ browserEngine: { idleTimeoutMinutes: null } });
    expect(() => parseStoredConfig({ browserEngine: { idleTimeoutMinutes: null } })).toThrow("browserEngine.idleTimeoutMinutes");
    for (const value of [61, 2.5, -1, "10"]) expect(() => parseConfigPatch({ browserEngine: { idleTimeoutMinutes: value } })).toThrow();
    expect(parseConfigPatch({ browserEngine: { attachCdpUrl: "9333", idleTimeoutMinutes: 5 } })).toEqual({ browserEngine: { attachCdpUrl: "9333", idleTimeoutMinutes: 5 } });

    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({ browserEngine: { engine: "chrome", idleTimeoutMinutes: 0 } }));
    try {
      saveConfig({ browserEngine: { idleTimeoutMinutes: null } });
      expect(JSON.parse(readFileSync(path, "utf8")).browserEngine).toEqual({ engine: "chrome" });
    } finally {
      rmSync(path, { force: true });
    }
  });

  it("keeps a browser profile's My Chrome choice through saving, loading and migration, and nothing else", () => {
    const mine = { id: "profile-mine", name: "Mine", browser: "chrome" };
    expect(parseConfigPatch({ browserProfiles: [mine, { id: "profile-work", name: "Work" }] })).toEqual({ browserProfiles: [mine, { id: "profile-work", name: "Work" }] });
    expect(parseStoredConfig({ browserProfiles: [mine] }).browserProfiles).toEqual([mine]);
    expect(parseStoredConfig({ browserProfiles: [{ id: "Mine", name: "Mine", browser: "chrome" }] }).browserProfiles)
      .toEqual([{ id: "mine", name: "Mine", browser: "chrome", partitionId: "Mine" }]);
    for (const browser of ["edge", "firefox", "", true]) {
      expect(() => parseConfigPatch({ browserProfiles: [{ ...mine, browser }] })).toThrow("browserProfiles.0.browser");
    }
  });

  it("accepts a persisted global room turn timeout and supplies the legacy default", () => {
    expect(parseStoredConfig({ rooms: { turnTimeoutMinutes: 20 } })).toEqual({
      rooms: { turnTimeoutMinutes: 20 },
    });
    expect(roomTurnTimeoutMinutes({ rooms: { turnTimeoutMinutes: 20 } })).toBe(20);
    expect(roomTurnTimeoutMinutes({})).toBe(5);
  });

  it.each([0, 1.5, 1441, "20", null])(
    "rejects an invalid room turn timeout: %j",
    (turnTimeoutMinutes) => {
      expect(() => parseConfigPatch({ rooms: { turnTimeoutMinutes } })).toThrow(
        "rooms.turnTimeoutMinutes",
      );
    },
  );

  it("accepts a persisted MCP call timeout and supplies the legacy default", () => {
    expect(parseStoredConfig({ mcp: { callTimeoutMinutes: 30 } })).toEqual({
      mcp: { callTimeoutMinutes: 30 },
    });
    expect(mcpCallTimeoutMinutes({ mcp: { callTimeoutMinutes: 30 } })).toBe(30);
    expect(mcpCallTimeoutMinutes({})).toBe(10);
  });

  it.each([0, 0.5, 61, 1440, "20", null])(
    "rejects an invalid MCP call timeout: %j",
    (callTimeoutMinutes) => {
      expect(() => parseConfigPatch({ mcp: { callTimeoutMinutes } })).toThrow(
        "mcp.callTimeoutMinutes",
      );
    },
  );

  it("rejects unknown keys under the mcp config section", () => {
    expect(() => parseConfigPatch({ mcp: { callTimeoutMinutes: 10, surprise: 1 } })).toThrow("mcp");
  });

  it("preserves shared Local VM behavior by default and accepts bounded per-bot mode", () => {
    expect(localVmMode({})).toBe("shared");
    expect(localVmMaxInstances({})).toBe(2);
    expect(parseConfigPatch({ localVm: { mode: "per-bot", maxInstances: 4 } })).toEqual({
      localVm: { mode: "per-bot", maxInstances: 4 },
    });
    expect(parseConfigPatch({ localVm: { maxInstances: 5 } })).toEqual({
      localVm: { maxInstances: 5 },
    });
    expect(parseConfigPatch({ localVm: { mode: "per-bot", maxInstances: 8 } })).toEqual({
      localVm: { mode: "per-bot", maxInstances: 8 },
    });
    expect(localVmMode({ localVm: { mode: "per-bot" } })).toBe("per-bot");
    expect(localVmMaxInstances({ localVm: { maxInstances: 3 } })).toBe(3);
  });

  it("names the per-bot desktop limit once every allowed desktop exists", () => {
    const cfg = { localVm: { mode: "per-bot" as const, maxInstances: 8 } };
    expect(perBotLocalVmLimitProblem(cfg, 7)).toBeNull();
    expect(perBotLocalVmLimitProblem(cfg, 8)).toBe("The per-bot Local VM limit is 8 — delete an unused bot VM");
    expect(perBotLocalVmLimitProblem({}, 2)).toBe(
      "The per-bot Local VM limit is 2 — delete an unused bot VM or raise the limit in App Settings",
    );
  });

  it("accepts pool mode with the same bounded seat count", () => {
    expect(parseConfigPatch({ localVm: { mode: "pool", maxInstances: 4 } })).toEqual({
      localVm: { mode: "pool", maxInstances: 4 },
    });
    expect(localVmMode({ localVm: { mode: "pool" } })).toBe("pool");
    expect(localVmMode({})).toBe("shared");
  });

  it("keeps skill authoring on by default with an explicit opt-out, and the browser off by default", () => {
    expect(skillAuthoringEnabled({})).toBe(true);
    expect(skillAuthoringEnabled({ features: {} })).toBe(true);
    expect(skillAuthoringEnabled({ features: { skillAuthoring: true } })).toBe(true);
    expect(parseConfigPatch({ features: { skillAuthoring: false } })).toEqual({
      features: { skillAuthoring: false },
    });
    expect(skillAuthoringEnabled({ features: { skillAuthoring: false } })).toBe(false);
    expect(parseConfigPatch({ features: { skillRecorder: true } })).toEqual({ features: {} });
    expect(builtInBrowserEnabled({})).toBe(true);
    expect(builtInBrowserEnabled({ features: { skillAuthoring: true } })).toBe(true);
    expect(parseConfigPatch({ features: { browser: false } })).toEqual({ features: { browser: false } });
    expect(builtInBrowserEnabled({ features: { browser: false } })).toBe(false);
    expect(builtInBrowserEnabled({ features: { browser: true } })).toBe(true);
    const cloudHome = { OMB_CLOUD_ROLE: "home", OMB_CLOUD_MACHINE_ID: "3f9c2a4e-8b1d-4c6e-9a7f-2d5e8c1b0a93" };
    expect(builtInBrowserEnabled({}, { OMB_PUBLIC_URL: "https://selfhosted.example.test" })).toBe(true);
    expect(builtInBrowserEnabled({}, cloudHome)).toBe(true);
    expect(builtInBrowserEnabled({ features: { browser: false } }, cloudHome)).toBe(false);
    expect(parseConfigPatch({ browserProfiles: [{ id: "work", name: " Work " }] })).toEqual({
      browserProfiles: [{ id: "work", name: "Work" }],
    });
    expect(() => parseConfigPatch({ browserProfiles: [{ id: "../evil", name: "x" }] })).toThrow(/browserProfiles.*id/i);
    expect(() => parseConfigPatch({ browserProfiles: [{ id: "Work", name: "Work" }] })).toThrow(/browserProfiles.*id/i);
    expect(() => parseConfigPatch({
      browserProfiles: [{ id: "work", name: "Work", partitionId: "OtherAccount" }],
    })).toThrow(/browserProfiles/i);
    expect(() => parseConfigPatch({ browserProfiles: [{ id: "ok", name: "" }] })).toThrow(/browserProfiles.*name/i);
    expect(() => parseConfigPatch({
      browserProfiles: [{ id: "work", name: "Work" }, { id: "work", name: "Work again" }],
    })).toThrow(/browserProfiles.*id.*duplicated/i);
    expect(() => parseConfigPatch({ features: { skillAuthoring: "yes" } })).toThrow(
      "features.skillAuthoring",
    );
  });

  it("accepts only off, ask and auto for sharing learned skills", () => {
    expect(parseConfigPatch({ features: { shareLearnedSkills: "off" } })).toEqual({ features: { shareLearnedSkills: "off" } });
    expect(() => parseConfigPatch({ features: { shareLearnedSkills: "always" } })).toThrow("features.shareLearnedSkills");
  });

  it("keeps computer sharing off unless config.json explicitly turns it on", () => {
    expect(sharedComputersEnabled({})).toBe(false);
    expect(sharedComputersEnabled({ features: {} })).toBe(false);
    expect(sharedComputersEnabled({ features: { skillAuthoring: true } })).toBe(false);
    expect(sharedComputersEnabled({ features: { sharedComputers: false } })).toBe(false);
    expect(sharedComputersEnabled({ features: { sharedComputers: true } })).toBe(true);
    expect(parseConfigPatch({ features: { sharedComputers: true } })).toEqual({
      features: { sharedComputers: true },
    });
    expect(() => parseConfigPatch({ features: { sharedComputers: "yes" } })).toThrow(
      "features.sharedComputers",
    );
  });

  it("drops the retired routines-in-conversation switch instead of refusing the config", () => {
    expect(parseConfigPatch({ features: { routinesInConversation: true, showToolCalls: true } })).toEqual({
      features: { showToolCalls: true },
    });
  });

  it("keeps tool-call chips off by default and accepts an explicit opt-in", () => {
    expect(showToolCallsEnabled({})).toBe(false);
    expect(parseConfigPatch({ features: { showToolCalls: true } })).toEqual({
      features: { showToolCalls: true },
    });
    expect(showToolCallsEnabled({ features: { showToolCalls: true } })).toBe(true);
  });

  it.each([0, 1.5, 9, "2", null])("rejects an invalid per-bot VM limit: %j", (maxInstances) => {
    expect(() => parseConfigPatch({ localVm: { maxInstances } })).toThrow("localVm.maxInstances");
  });

  it("keeps the 8-hour Local VM idle timeout by default and accepts a bounded override", () => {
    expect(localVmIdleTimeoutMinutes({})).toBe(480);
    expect(localVmIdleTimeoutMinutes({ localVm: { mode: "per-bot" } })).toBe(480);
    expect(parseStoredConfig({ localVm: { mode: "per-bot", maxInstances: 3 } })).toMatchObject({
      localVm: { mode: "per-bot", maxInstances: 3 },
    });
    for (const idleTimeoutMinutes of [5, 30, 1_440]) {
      expect(parseConfigPatch({ localVm: { idleTimeoutMinutes } })).toEqual({ localVm: { idleTimeoutMinutes } });
      expect(localVmIdleTimeoutMinutes({ localVm: { idleTimeoutMinutes } })).toBe(idleTimeoutMinutes);
    }
    expect(parseStoredConfig({ localVm: { idleTimeoutMinutes: 30 } })).toMatchObject({ localVm: { idleTimeoutMinutes: 30 } });
  });

  it.each([0, 4, 1.5, 1_441, "30", null])("rejects an invalid Local VM idle timeout: %j", (idleTimeoutMinutes) => {
    expect(() => parseConfigPatch({ localVm: { idleTimeoutMinutes } })).toThrow("localVm.idleTimeoutMinutes");
    expect(() => parseStoredConfig({ localVm: { idleTimeoutMinutes } })).toThrow("localVm.idleTimeoutMinutes");
  });

  it.each(["one-per-bot", "windows", 1, null])("rejects an invalid Local VM mode: %j", (mode) => {
    expect(() => parseConfigPatch({ localVm: { mode } })).toThrow("localVm.mode");
  });
});

describe("a config.json saved with a byte order mark", () => {
  // Windows PowerShell's Set-Content -Encoding UTF8 and Notepad's "UTF-8 with
  // BOM" put U+FEFF before the first brace, which JSON.parse refuses.
  const bom = "\uFEFF";

  it("loads instead of being ignored", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, bom + JSON.stringify({ budgets: { monthlyUsd: 25, warnAtPercent: 70 } }, null, 2));
    const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
    try {
      expect(loadConfig().budgets).toEqual({ monthlyUsd: 25, warnAtPercent: 70 });
      expect(warn.mock.calls.some(([line]) => String(line).includes("ignoring"))).toBe(false);
    } finally {
      warn.mockRestore();
      rmSync(path, { force: true });
    }
  });

  it("keeps every other key when a setting is saved", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, bom + JSON.stringify({
      xai: { key: "xai-fixture" },
      mcpServers: { notes: { command: "npx", args: ["notes-mcp"] } },
    }, null, 2));
    try {
      saveConfig({ budgets: { monthlyUsd: 25, warnAtPercent: 70 } });
      const disk = JSON.parse(readFileSync(path, "utf8"));
      expect(disk).toMatchObject({
        xai: { key: "xai-fixture" },
        mcpServers: { notes: { command: "npx", args: ["notes-mcp"] } },
        budgets: { monthlyUsd: 25, warnAtPercent: 70 },
      });
    } finally {
      rmSync(path, { force: true });
    }
  });

  it("keeps the browser's allow-list when the owner flips its local-sites switch", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({ browserEngine: { engine: "chrome", egress: { allow: ["10.0.0.5"] } } }));
    try {
      saveConfig({ browserEngine: { egress: { localNetwork: true } } });
      expect(JSON.parse(readFileSync(path, "utf8")).browserEngine).toEqual({ engine: "chrome", egress: { allow: ["10.0.0.5"], localNetwork: true } });
      saveConfig({ browserEngine: { egress: { localNetwork: false } } });
      expect(JSON.parse(readFileSync(path, "utf8")).browserEngine).toEqual({ engine: "chrome", egress: { allow: ["10.0.0.5"], localNetwork: false } });
    } finally {
      rmSync(path, { force: true });
    }
  });
});

describe("loadConfig between changes", () => {
  it("hands every caller its own copy, and sees a save or an edit made outside the app", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    const settle = () => { const past = new Date(Date.now() - 60_000); utimesSync(path, past, past); };
    writeFileSync(path, JSON.stringify({ budgets: { monthlyUsd: 10, warnAtPercent: 50 } }));
    settle();
    try {
      const first = loadConfig();
      first.budgets!.monthlyUsd = 999;
      expect(loadConfig().budgets).toEqual({ monthlyUsd: 10, warnAtPercent: 50 });
      saveConfig({ budgets: { monthlyUsd: 20, warnAtPercent: 50 } });
      settle();
      expect(loadConfig().budgets).toEqual({ monthlyUsd: 20, warnAtPercent: 50 });
      writeFileSync(path, JSON.stringify({ budgets: { monthlyUsd: 30, warnAtPercent: 50 } }));
      settle();
      expect(loadConfig().budgets).toEqual({ monthlyUsd: 30, warnAtPercent: 50 });
    } finally {
      rmSync(path, { force: true });
    }
  });
});

describe("saving the image providers", () => {
  it("keeps the model typed for Custom when the base moves to another provider and comes back", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({}));
    try {
      saveConfig(parseConfigPatch({ imageGen: { primary: { provider: "custom", model: "local/image" } } }));
      saveConfig(parseConfigPatch({ imageGen: { primary: { provider: "openai" } } }));
      saveConfig(parseConfigPatch({ imageGen: { primary: { provider: "custom" } } }));
      expect(imageSlots(JSON.parse(readFileSync(path, "utf8"))).primary).toEqual({ provider: "custom", model: "local/image" });
    } finally {
      rmSync(path, { force: true });
    }
  });

  it("drops a model that was a key once that key is replaced, and refuses a model equal to a saved key", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({ imageGen: { customApiKey: "gateway-old", customUrl: "http://127.0.0.1:4000/v1", customModel: "gateway-old",
      primary: { provider: "custom", model: "gateway-old" }, fallback: { provider: "openai", model: "gateway-old" } } }));
    try {
      saveConfig(parseConfigPatch({ imageGen: { customApiKey: "gateway-new" } }));
      const saved = readFileSync(path, "utf8");
      expect(saved).not.toContain("gateway-old");
      expect(imageSlots(JSON.parse(saved)).primary.model).not.toBe("gateway-old");
      expect(() => saveConfig(parseConfigPatch({ imageGen: { primary: { provider: "custom", model: "gateway-new" } } }))).toThrow("API key");
      expect(() => saveConfig(parseConfigPatch({ imageGen: { fallback: { provider: "openrouter", model: "gateway-new" } } }))).toThrow("API key");
    } finally {
      rmSync(path, { force: true });
    }
  });

  it("keeps the base and the keys when the backup is cleared", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({ imageGen: { key: "openai-fixture", primary: { provider: "openai" } } }));
    try {
      saveConfig(parseConfigPatch({ imageGen: { fallback: { provider: "xai" } } }));
      expect(JSON.parse(readFileSync(path, "utf8")).imageGen).toEqual({ key: "openai-fixture", primary: { provider: "openai" }, fallback: { provider: "xai" } });
      saveConfig(parseConfigPatch({ imageGen: { fallback: null } }));
      saveConfig(parseConfigPatch({ imageGen: { fallback: null } }));
      expect(JSON.parse(readFileSync(path, "utf8")).imageGen).toEqual({ key: "openai-fixture", primary: { provider: "openai" } });
    } finally {
      rmSync(path, { force: true });
    }
  });
});

describe("saving the newer sections", () => {
  it("persists the Anthropic key, the spend limit and the price list, section by section", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({ xai: { key: "xai-fixture" } }));
    try {
      saveConfig({ anthropic: { key: "sk-ant-fixture" } });
      saveConfig({ budgets: { monthlyUsd: 25, warnAtPercent: 70 } });
      saveConfig({ billing: { currency: "EUR", prices: { default: { inputPerMillion: 1, outputPerMillion: 2 } } } });
      saveConfig({ billing: { prices: { "gpt-5": { inputPerMillion: 3, outputPerMillion: 4 } } } });
      const disk = JSON.parse(readFileSync(path, "utf8"));
      expect(disk).toMatchObject({
        xai: { key: "xai-fixture" },
        anthropic: { key: "sk-ant-fixture" },
        budgets: { monthlyUsd: 25, warnAtPercent: 70 },
        billing: { currency: "EUR", prices: { "gpt-5": { inputPerMillion: 3, outputPerMillion: 4 } } },
      });
      expect(disk.billing.prices.default).toBeUndefined();
    } finally {
      rmSync(path, { force: true });
    }
  });

  it("clears a thread event-log knob with null while other keys survive", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({}));
    try {
      saveConfig({ threads: { maxConcurrentPerBot: 3, eventLogRetentionDays: 30, eventLogMaxBytes: 50 * 1024 * 1024 } });
      saveConfig({ threads: { eventLogRetentionDays: null } });
      let disk = JSON.parse(readFileSync(path, "utf8"));
      expect(disk.threads).toEqual({ maxConcurrentPerBot: 3, eventLogMaxBytes: 50 * 1024 * 1024 });
      saveConfig({ threads: { eventLogMaxBytes: null } });
      disk = JSON.parse(readFileSync(path, "utf8"));
      expect(disk.threads).toEqual({ maxConcurrentPerBot: 3 });
      expect(parseStoredConfig(disk).threads).toEqual({ maxConcurrentPerBot: 3 });
      expect(threadEventLogRetentionDays(parseStoredConfig(disk))).toBeNull();
      expect(threadEventLogMaxBytes(parseStoredConfig(disk))).toBeNull();
    } finally {
      rmSync(path, { force: true });
    }
  });

  it("seeds what this machine holds when the first threads save is an event-log knob, so an older version can still read the file", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({}));
    try {
      saveConfig({ threads: { eventLogRetentionDays: 30 } });
      const disk = JSON.parse(readFileSync(path, "utf8"));
      expect(disk.threads).toEqual({ maxConcurrentPerBot: defaultBotThreads(), eventLogRetentionDays: 30 });
      expect(parseStoredConfig(disk).threads).toEqual({ maxConcurrentPerBot: defaultBotThreads(), eventLogRetentionDays: 30 });
    } finally {
      rmSync(path, { force: true });
    }
  });
});

describe("default fleet", () => {
  it("adds Groq to product fleets and keeps its workspace key away from other accounts", () => {
    const cfg: AppConfig = { groq: { key: "groq-workspace", url: "https://proxy.example.test/openai/v1" }, instances: {
      codex: { driver: "codex" },
      groqProxy: { driver: "groq", config: { url: "https://another.example.test/v1" } },
      groqOwn: { driver: "groq", environment: { GROQ_API_KEY: "groq-own" } },
    } };
    const map = instanceConfigs(cfg);
    expect(map.groq).toEqual({ driver: "groq", config: { url: cfg.groq!.url }, environment: { GROQ_API_KEY: "groq-workspace" } });
    expect(map.codex.environment).toEqual({});
    expect(map.groqProxy.environment).toEqual({});
    expect(map.groqOwn.environment).toEqual({ GROQ_API_KEY: "groq-own" });
    expect(cfg.instances).not.toHaveProperty("groq");
    expect(instanceConfigs({ instances: { standalone: { driver: "fake" } } })).not.toHaveProperty("groq");
  });

  it("adds ModelGo to product fleets and sends its workspace key only to ModelGo's own host", () => {
    const cfg: AppConfig = { modelgo: { key: "modelgo-workspace" }, instances: {
      codex: { driver: "codex" },
      modelgoCustomHost: { driver: "modelgo", config: { url: "https://another.example.test/v1" } },
      modelgoSameHost: { driver: "modelgo", config: { url: "https://api.modelgo.ai/v1/" } },
      modelgoOwn: { driver: "modelgo", environment: { MODELGO_API_KEY: "modelgo-own" } },
    } };
    const map = instanceConfigs(cfg);
    expect(map.modelgo.environment).toEqual({ MODELGO_API_KEY: "modelgo-workspace" });
    expect(map.codex.environment).toEqual({});
    expect(map.modelgoCustomHost.environment).toEqual({});
    expect(map.modelgoSameHost.environment).toEqual({ MODELGO_API_KEY: "modelgo-workspace" });
    expect(map.modelgoOwn.environment).toEqual({ MODELGO_API_KEY: "modelgo-own" });
  });

  it("preserves an existing compatible account named Groq when adding the built-in provider", () => {
    const map = instanceConfigs({ groq: { key: "workspace-key" }, instances: {
      codex: { driver: "codex" },
      groq: { driver: "openai-compat", config: { url: "https://existing.example.test/v1" }, environment: { OPENAI_COMPAT_API_KEY: "own-key" } },
    } });
    expect(map.groq).toEqual({ driver: "openai-compat", config: { url: "https://existing.example.test/v1" }, environment: { OPENAI_COMPAT_API_KEY: "own-key" } });
  });

  it("accepts clearing a Groq key and removes it from child environments", () => {
    expect(parseConfigPatch({ groq: { key: "" } })).toEqual({ groq: { key: "" } });
    const env = { OMB_GROQ_API_KEY: "groq-fixture", KEEP: "yes" };
    stripWorkspaceCredentialEnv(env);
    expect(env).toEqual({ KEEP: "yes" });
  });

  it("adds Meta to product fleets and keeps its workspace key away from other accounts", () => {
    const cfg: AppConfig = { meta: { key: "meta-workspace", url: "https://proxy.example.test/v1" }, instances: {
      codex: { driver: "codex" },
      metaProxy: { driver: "meta", config: { url: "https://another.example.test/v1" } },
      metaOwn: { driver: "meta", environment: { META_API_KEY: "meta-own" } },
    } };
    const map = instanceConfigs(cfg);
    expect(map.meta).toEqual({ driver: "meta", config: { url: cfg.meta!.url }, environment: { META_API_KEY: "meta-workspace" } });
    expect(map.codex.environment).toEqual({});
    expect(map.metaProxy.environment).toEqual({});
    expect(map.metaOwn.environment).toEqual({ META_API_KEY: "meta-own" });
    expect(cfg.instances).not.toHaveProperty("meta");
    expect(instanceConfigs({ instances: { standalone: { driver: "fake" } } })).not.toHaveProperty("meta");
  });

  it("adds DeepSeek Harness to product fleets and hands its key only to that engine", () => {
    const cfg: AppConfig = { deepseek: { key: "ds-workspace" }, instances: {
      codex: { driver: "codex" },
      kimi: { driver: "kimiAgent" },
      deepseekOwn: { driver: "deepseekAgent", environment: { DEEPSEEK_API_KEY: "ds-own" } },
    } };
    const map = instanceConfigs(cfg);
    expect(map.deepseek).toEqual({ driver: "deepseekAgent", environment: { DEEPSEEK_API_KEY: "ds-workspace" } });
    expect(map.codex.environment).toEqual({});
    expect(map.kimi.environment).toEqual({});
    expect(map.deepseekOwn.environment).toEqual({ DEEPSEEK_API_KEY: "ds-own" });
    expect(cfg.instances).not.toHaveProperty("deepseek");
    expect(instanceConfigs({ instances: { standalone: { driver: "fake" } } })).not.toHaveProperty("deepseek");
  });

  it("accepts clearing a DeepSeek key and removes it from child environments", () => {
    expect(parseConfigPatch({ deepseek: { key: "" } })).toEqual({ deepseek: { key: "" } });
    const env = { OMB_DEEPSEEK_API_KEY: "ds-fixture", DEEPSEEK_API_KEY: "ds-ambient", KEEP: "yes" };
    stripWorkspaceCredentialEnv(env);
    expect(env).toEqual({ DEEPSEEK_API_KEY: "ds-ambient", KEEP: "yes" });
    expect(PROVIDER_CREDENTIAL_ENV).toContain("DEEPSEEK_API_KEY");
  });

  it("accepts clearing a Meta key and removes it from child environments", () => {
    expect(parseConfigPatch({ meta: { key: "" } })).toEqual({ meta: { key: "" } });
    const env = { OMB_META_API_KEY: "meta-fixture", META_API_KEY: "muse-code-login", KEEP: "yes" };
    stripWorkspaceCredentialEnv(env);
    expect(env).toEqual({ META_API_KEY: "muse-code-login", KEEP: "yes" });
  });

  it("keeps OpenCode's existing Groq key separate from the built-in Groq key", () => {
    const map = instanceConfigs({ groq: { key: "groq-workspace" },
      opencodeGo: { providerKeys: { GROQ_API_KEY: "groq-opencode" } } });
    expect(map.groq.environment).toEqual({ GROQ_API_KEY: "groq-workspace" });
    expect(map.opencodeGo.environment).toEqual({ GROQ_API_KEY: "groq-opencode" });
    expect(map.codex.environment).toEqual({});
  });

  it("adds a separate ChatGPT plan account without copying Codex credentials", () => {
    const cfg: AppConfig = { instances: { codex: { driver: "codex", config: { cli: "/fixture/codex" }, environment: { CODEX_HOME: "/other-account", OPENAI_API_KEY: "not-for-plan" } } } };
    expect(instanceConfigs(cfg).chatgpt).toMatchObject({ driver: "codex", displayName: "ChatGPT plan", config: { cli: "/fixture/codex", authMode: "chatgpt-plan" }, environment: {} });
    expect(cfg.instances).not.toHaveProperty("chatgpt");
    expect(instanceConfigs({ instances: { standalone: { driver: "fake" } } })).not.toHaveProperty("chatgpt");
  });
  it("adds Mistral to product fleets and scopes its saved credential to Mistral", () => {
    const map = instanceConfigs({ mistral: { key: "mistral-fixture" }, instances: { codex: { driver: "codex" } } });
    expect(map.mistral).toEqual({ driver: "mistral", environment: { MISTRAL_API_KEY: "mistral-fixture" } });
    expect(map.codex.environment).toEqual({});
    expect(instanceConfigs({ instances: { standalone: { driver: "fake" } } })).not.toHaveProperty("mistral");
    expect(parseConfigPatch({ mistral: { key: "" } })).toEqual({ mistral: { key: "" } });
    const env = { MISTRAL_API_KEY: "mistral-fixture", KEEP: "yes" };
    stripWorkspaceCredentialEnv(env);
    expect(env).toEqual({ KEEP: "yes" });
  });

  it("ships Qwen and Hermes as custom-only engines", () => {
    const map = instanceConfigs({});
    expect(map.qwen).toEqual({ driver: "qwenAgent", environment: {} });
    expect(map.hermes).toEqual({ driver: "hermesAgent", environment: {} });
  });

  it("ships Cursor as a default-fleet subscription engine", () => {
    const map = instanceConfigs({});
    expect(map.cursor).toEqual({ driver: "cursorAgent", environment: {} });
  });

  it("carries the saved OpenAI-compatible URL into the live default instance", () => {
    const map = instanceConfigs({
      openaiCompat: { key: "secret", url: "https://models.example.test/v1" },
    });
    expect(map.openaiCompat.config).toEqual({ url: "https://models.example.test/v1" });
    expect(map.openaiCompat.environment).toEqual({
      OPENAI_COMPAT_API_KEY: "secret",
      OPENAI_COMPAT_URL: "https://models.example.test/v1",
    });
  });

  it("hands a saved Anthropic key only to Claude instances, as the variable the CLI reads", () => {
    const map = instanceConfigs({ anthropic: { key: "sk-ant-fixture", url: "https://anthropic-proxy.example.test" } });
    expect(map.claude.environment).toEqual({ ANTHROPIC_API_KEY: "sk-ant-fixture", ANTHROPIC_BASE_URL: "https://anthropic-proxy.example.test" });
    expect(map.codex.environment).toEqual({});
    expect(map.openaiCompat.environment).toEqual({});
    expect(instanceConfigs({ anthropic: { url: "https://only-a-url.example.test" } }).claude.environment).toEqual({});
    expect(parseConfigPatch({ anthropic: { key: "sk-ant-new" } })).toEqual({ anthropic: { key: "sk-ant-new" } });
  });

  it("never hands the workspace Anthropic key to a Claude instance with its own endpoint or credential", () => {
    const map = instanceConfigs({
      anthropic: { key: "sk-ant-workspace", url: "https://anthropic-proxy.example.test" },
      instances: {
        claude: { driver: "claudeAgent" },
        router: { driver: "claudeAgent", environment: { ANTHROPIC_BASE_URL: "https://router.example.test", ANTHROPIC_AUTH_TOKEN: "router-token" } },
        keyed: { driver: "claudeAgent", environment: { ANTHROPIC_API_KEY: "sk-ant-own" } },
        bedrock: { driver: "claudeAgent", environment: { CLAUDE_CODE_USE_BEDROCK: "1" } },
      },
    });
    expect(map.claude.environment).toEqual({ ANTHROPIC_API_KEY: "sk-ant-workspace", ANTHROPIC_BASE_URL: "https://anthropic-proxy.example.test" });
    expect(map.router.environment).toEqual({ ANTHROPIC_BASE_URL: "https://router.example.test", ANTHROPIC_AUTH_TOKEN: "router-token" });
    expect(map.keyed.environment).toEqual({ ANTHROPIC_API_KEY: "sk-ant-own" });
    expect(map.bedrock.environment).toEqual({ CLAUDE_CODE_USE_BEDROCK: "1" });
  });

  it("never hands a workspace API key to an API-key instance with its own endpoint or key", () => {
    const map = instanceConfigs({
      openaiCompat: { key: "sk-or-WORKSPACE" },
      mistral: { key: "mistral-WORKSPACE" },
      xai: { key: "xai-WORKSPACE" },
      instances: {
        claude: { driver: "claudeAgent" },
        openaiCompat: { driver: "openai-compat" },
        sameUrl: { driver: "openai-compat", config: { url: "https://openrouter.ai/api/v1/" } },
        groq: { driver: "openai-compat", config: { url: "https://third-party.example.test/v1" } },
        ownKey: { driver: "openai-compat", config: { key: "sk-own" } },
        ownVariable: { driver: "openai-compat", config: { apiKeyEnv: "GROQ_API_KEY" } },
        mistral: { driver: "mistral" },
        mistralProxy: { driver: "mistral", config: { url: "https://mistral-proxy.example.test/v1" } },
        grokApi: { driver: "grok" },
        grokProxy: { driver: "grok", config: { url: "https://xai-proxy.example.test/v1" } },
        grokOwn: { driver: "grok", environment: { XAI_API_KEY: "xai-own" } },
      },
    });
    expect(map.openaiCompat.environment).toMatchObject({ OPENAI_COMPAT_API_KEY: "sk-or-WORKSPACE" });
    expect(map.sameUrl.environment).toMatchObject({ OPENAI_COMPAT_API_KEY: "sk-or-WORKSPACE" });
    for (const id of ["groq", "ownKey", "ownVariable"]) expect(map[id].environment, id).toEqual({});
    expect(map.mistral.environment).toEqual({ MISTRAL_API_KEY: "mistral-WORKSPACE" });
    expect(map.mistralProxy.environment).toEqual({});
    expect(map.grokApi.environment).toEqual({ XAI_API_KEY: "xai-WORKSPACE" });
    expect(map.grokProxy.environment).toEqual({});
    expect(map.grokOwn.environment).toEqual({ XAI_API_KEY: "xai-own" });
  });

  it.each(["openai", "openrouter", "xaiApi", "claudeApi"])("preserves a custom driver's own routing when its id is %s", (id) => {
    const map = instanceConfigs({
      mistral: { key: "mistral-WORKSPACE" },
      instances: {
        [id]: { driver: "mistral", config: { url: "https://custom.example.test/v1" }, environment: { MISTRAL_API_KEY: "mistral-own" } },
      },
    });
    expect(map[id].driver).toBe("mistral");
    expect(map[id].config).toEqual({ url: "https://custom.example.test/v1" });
    expect(map[id].environment).toEqual({ MISTRAL_API_KEY: "mistral-own" });
  });

  it.each([
    ["openai", "OMB_OPENAI_API_KEY", "https://api.openai.com/v1"],
    ["openrouter", "OMB_OPENROUTER_API_KEY", "https://openrouter.ai/api/v1"],
  ])("respects same-driver endpoint and credential overrides for %s", (id, keyEnv, defaultUrl) => {
    const workspace = { openai: { key: "openai-WORKSPACE" }, openrouter: { key: "openrouter-WORKSPACE" } };
    for (const config of [
      { url: "https://third-party.example.test/v1" },
      { key: "instance-own" },
      { apiKeyEnv: "INSTANCE_OWN_KEY" },
    ]) {
      const map = instanceConfigs({ ...workspace, instances: { [id]: { driver: "openai-compat", config } } });
      expect(map[id].environment, JSON.stringify(config)).toEqual({});
    }
    const keyed = instanceConfigs({
      ...workspace,
      instances: { [id]: { driver: "openai-compat", environment: { [keyEnv]: "instance-own" } } },
    });
    expect(keyed[id].environment).toEqual({ [keyEnv]: "instance-own" });

    const inherited = instanceConfigs({
      ...workspace,
      openaiCompat: { key: "compat-WORKSPACE", url: "https://workspace-router.example.test/v1" },
      instances: { [id]: { driver: "openai-compat", config: { url: `${defaultUrl}/`, apiKeyEnv: keyEnv } } },
    });
    expect(inherited[id].environment).toEqual({ [keyEnv]: workspace[id as "openai" | "openrouter"].key });
  });

  it("respects same-driver routing overrides for the Claude and xAI API instances", () => {
    const map = instanceConfigs({
      anthropic: { key: "anthropic-WORKSPACE", everyClaudeBot: false },
      xai: { key: "xai-WORKSPACE" },
      instances: {
        claudeApi: { driver: "claudeAgent", environment: { ANTHROPIC_BASE_URL: "https://claude-router.example.test" } },
        xaiApi: { driver: "grok", config: { url: "https://xai-router.example.test/v1" } },
      },
    });
    expect(map.claudeApi.environment).toEqual({ ANTHROPIC_BASE_URL: "https://claude-router.example.test" });
    expect(map.xaiApi.environment).toEqual({});
  });

  it("preserves a per-instance OpenAI-compatible URL override", () => {
    const map = instanceConfigs({
      openaiCompat: { url: "https://workspace.example.test/v1" },
      instances: {
        custom: {
          driver: "openai-compat",
          config: { url: "https://instance.example.test/v1", apiKeyEnv: "CUSTOM_KEY" },
        },
      },
    });
    expect(map.custom.config).toEqual({
      url: "https://instance.example.test/v1",
      apiKeyEnv: "CUSTOM_KEY",
    });
  });

  it("keeps an explicit empty routing provider while other instances inherit the workspace pin", () => {
    const config: AppConfig = {
      openaiCompat: { provider: "workspace-provider" },
      instances: {
        isolated: { driver: "openai-compat", config: { provider: "" } },
        inherited: { driver: "openai-compat" },
        pinned: { driver: "openai-compat", config: { provider: "instance-provider" } },
      },
    };
    const instances = instanceConfigs(config);
    expect(instances.isolated.config).toEqual({ provider: "" });
    expect(instances.inherited.config).toEqual({ provider: "workspace-provider" });
    expect(instances.pinned.config).toEqual({ provider: "instance-provider" });
    expect(config.instances?.isolated.config).toEqual({ provider: "" });
    expect(config.instances?.inherited.config).toBeUndefined();
  });

  it("does not retain an injected OpenAI-compatible URL across config refreshes", () => {
    const config: AppConfig = {
      openaiCompat: { url: "https://first.example.test/v1" },
      instances: {
        custom: { driver: "openai-compat" },
      },
    };

    expect(instanceConfigs(config).custom.config).toEqual({
      url: "https://first.example.test/v1",
    });
    config.openaiCompat = { url: "https://second.example.test/v1" };
    expect(instanceConfigs(config).custom.config).toEqual({
      url: "https://second.example.test/v1",
    });
    expect(config.instances?.custom.config).toBeUndefined();
  });

  it("adds missing custom-only engines onto an existing product fleet", () => {
    const map = instanceConfigs({ instances: { claude: { driver: "claudeAgent" } } });
    expect(map.claude.driver).toBe("claudeAgent");
    expect(map.qwen?.driver).toBe("qwenAgent");
    expect(map.hermes?.driver).toBe("hermesAgent");
    expect(map.cursor?.driver).toBe("cursorAgent");
    expect(map.openaiCompat?.driver).toBe("openai-compat");
  });

  it("does not expand a one-off shadow fleet", () => {
    const map = instanceConfigs({ instances: { ghost: { driver: "not-a-real-driver" } } });
    expect(Object.keys(map)).toEqual(["ghost"]);
  });
});

describe("Instance CLI override", () => {
  it("sets, replaces, and clears config.cli on a default-fleet instance", () => {
    const cfg: AppConfig = {};
    const set = withInstanceCli(cfg, "claude", "/opt/claude-2.1/bin/claude");
    expect(set.ok).toBe(true);
    expect(set.config.instances!.claude.config).toEqual({ cli: "/opt/claude-2.1/bin/claude" });

    const replaced = withInstanceCli(set.config, "claude", "~/bin/claude");
    expect(replaced.config.instances!.claude.config).toEqual({ cli: "~/bin/claude" });

    const cleared = withInstanceCli(replaced.config, "claude", "");
    expect(cleared.config.instances!.claude.config).toBeUndefined();
  });

  it("preserves sibling config keys when clearing only cli", () => {
    const cfg: AppConfig = {
      instances: { claude: { driver: "claudeAgent", config: { cli: "/x/claude", permissionMode: "bypassPermissions" } } },
    };
    const cleared = withInstanceCli(cfg, "claude", "");
    expect(cleared.config.instances!.claude.config).toEqual({ permissionMode: "bypassPermissions" });
  });

  it("leaves the original config untouched and rejects unknown instances", () => {
    const cfg: AppConfig = { instances: { codex: { driver: "codex" } } };
    const result = withInstanceCli(cfg, "codex", "/new/codex");
    expect(result.config.instances!.codex.config).toEqual({ cli: "/new/codex" });
    expect(cfg.instances!.codex.config).toBeUndefined();

    expect(withInstanceCli(cfg, "nope", "/x").ok).toBe(false);
  });

  it("never persists the credential env instanceConfigs injects", () => {
    const cfg: AppConfig = {
      xai: { key: "SECRET-XAI" },
      box: { token: "SECRET-BOAT" },
      opencodeGo: { apiKey: "SECRET-OCG" },
      instances: {
        claude: { driver: "claudeAgent" },
        grokApi: { driver: "grok" },
        opencode: { driver: "opencodeGo" },
      },
    };
    const set = withInstanceCli(cfg, "claude", "/opt/claude");
    expect(set.ok).toBe(true);
    for (const entry of Object.values(set.config.instances!)) {
      expect(entry.environment ?? {}).toEqual({});
    }
    const custom = { instances: { claude: { driver: "claudeAgent", environment: { MY_FLAG: "1" } } } };
    const kept = withInstanceCli(custom, "claude", "/x");
    expect(kept.config.instances!.claude.environment).toEqual({ MY_FLAG: "1" });
  });

  it("preserves explicit instance credentials even when workspace injection shadows them", () => {
    const cfg: AppConfig = {
      xai: { key: "fixture-shared-xai" },
      instances: {
        ownKey: { driver: "grok", environment: { XAI_API_KEY: "fixture-instance-xai", MY_FLAG: "1" } },
        sameCredential: { driver: "grok", environment: { XAI_API_KEY: "fixture-shared-xai" } },
        injectedOnly: { driver: "grok" },
      },
    };
    const instances = persistableInstanceConfigs(cfg);
    expect(instances.ownKey.environment).toEqual({ XAI_API_KEY: "fixture-instance-xai", MY_FLAG: "1" });
    expect(instances.sameCredential.environment).toEqual({ XAI_API_KEY: "fixture-shared-xai" });
    expect(instances.injectedOnly.environment).toBeUndefined();
    instances.ownKey.environment!.MY_FLAG = "changed";
    expect(cfg.instances!.ownKey.environment!.MY_FLAG).toBe("1");
  });

  it("an added server at its own address keeps its own model list, not the workspace model", () => {
    const cfg: AppConfig = {
      openaiCompat: { url: "https://first.example.test/v1", model: "fugu", provider: "first-provider" },
      instances: {
        "openai-compat-added": { driver: "openai-compat", config: { url: "http://127.0.0.1:1234/v1", key: "fixture-added", catalog: "lmstudio" } },
        sameHost: { driver: "openai-compat", config: { url: "https://first.example.test/v1/", key: "fixture-same" } },
        inherited: { driver: "openai-compat" },
      },
    };
    const instances = instanceConfigs(cfg);
    expect(instances["openai-compat-added"].config).toEqual({ url: "http://127.0.0.1:1234/v1", key: "fixture-added", catalog: "lmstudio" });
    expect(instances.sameHost.config).toEqual({ url: "https://first.example.test/v1/", key: "fixture-same", model: "fugu", provider: "first-provider" });
    expect(instances.inherited.config).toEqual({ url: "https://first.example.test/v1", model: "fugu", provider: "first-provider" });
  });

  it("saving another engine's CLI does not freeze inherited API endpoint or model settings", () => {
    const cfg: AppConfig = {
      openaiCompat: { url: "https://first.example.test/v1", model: "first-model", provider: "first-provider" },
      instances: {
        claude: { driver: "claudeAgent" },
        openaiCompat: { driver: "openai-compat", config: { tools: false } },
        inherited: { driver: "openai-compat" },
        custom: { driver: "openai-compat", config: { url: "https://custom.example.test/v1", provider: "", key: "fixture-custom" } },
      },
    };
    const updated = withInstanceCli(cfg, "claude", "/fixture/claude").config;
    expect(updated.instances!.openaiCompat.config).toEqual({ tools: false });
    expect(updated.instances!.inherited.config).toBeUndefined();
    expect(updated.instances!.custom.config).toEqual(cfg.instances!.custom.config);
    updated.openaiCompat = { url: "https://second.example.test/v1", model: "second-model", provider: "second-provider" };
    expect(instanceConfigs(updated).openaiCompat.config).toEqual({ tools: false, ...updated.openaiCompat });
    const persisted = persistableInstanceConfigs(cfg);
    (persisted.custom.config as Record<string, unknown>).url = "https://changed.example.test/v1";
    expect(cfg.instances!.custom.config).toMatchObject({ url: "https://custom.example.test/v1" });
  });

  it("validates openaiCompat.customHeaders in config and propagates them only to non-overridden non-built-in compat instances", () => {
    const parsed = parseStoredConfig({
      openaiCompat: {
        url: "https://gw.example/v1",
        customHeaders: { enabled: true, headers: [{ name: "X-Maus-Thread-ID", value: "{threadId}" }] },
      },
    });
    expect(parsed.openaiCompat?.customHeaders).toEqual({
      enabled: true,
      headers: [{ name: "X-Maus-Thread-ID", value: "{threadId}", required: true, secret: false }],
    });
    expect(() => parseConfigPatch({
      openaiCompat: { customHeaders: { enabled: true, headers: [{ name: "Authorization", value: "Bearer evil" }] } },
    })).toThrow();
    expect(() => parseConfigPatch({
      openaiCompat: { customHeaders: { enabled: true, headers: [{ name: "X-Bad", value: "a\r\nb" }] } },
    })).toThrow();

    const cfg: AppConfig = {
      openaiCompat: parsed.openaiCompat,
      instances: {
        openaiCompat: { driver: "openai-compat", config: {} },
        inherited: { driver: "openai-compat" },
        openai: { driver: "openai-compat", config: { url: "https://api.openai.com/v1", apiKeyEnv: "OMB_OPENAI_API_KEY" } },
        openrouter: { driver: "openai-compat", config: { url: "https://openrouter.ai/api/v1", apiKeyEnv: "OMB_OPENROUTER_API_KEY" } },
        "openai-compat-added": { driver: "openai-compat", config: { url: "https://generativelanguage.googleapis.com/v1beta/openai", key: "gemini-key" } },
        override: { driver: "openai-compat", config: { url: "https://other.example/v1", customHeaders: { enabled: false, headers: [] } } },
      },
    };
    const resolved = instanceConfigs(cfg);
    expect(resolved.openaiCompat!.config).toMatchObject({ customHeaders: parsed.openaiCompat!.customHeaders });
    expect(resolved.inherited!.config).toMatchObject({ customHeaders: parsed.openaiCompat!.customHeaders });
    expect(resolved.openai!.config).not.toHaveProperty("customHeaders");
    expect(resolved.openrouter!.config).not.toHaveProperty("customHeaders");
    expect(resolved["openai-compat-added"]!.config).not.toHaveProperty("customHeaders");
    expect(resolved.override!.config).toMatchObject({ customHeaders: { enabled: false, headers: [] } });
  });
});

describe("OpenCode Go configuration", () => {
  it("injects the key only into OpenCode Go instances", () => {
    const cfg: AppConfig = {
      opencodeGo: { apiKey: "secret-value" },
      instances: {
        opencode: { driver: "opencodeGo" },
        grok: { driver: "grokAgent" },
      },
    };

    const instances = instanceConfigs(cfg);
    expect(instances.opencode.environment).toEqual({ OPENCODE_API_KEY: "secret-value" });
    expect(instances.grok.environment).toEqual({});
  });

  it("hands the saved provider keys to OpenCode instances only, under their own names", () => {
    const cfg: AppConfig = {
      opencodeGo: { apiKey: "ocg", providerKeys: { VENICE_API_KEY: "venice-secret", ANTHROPIC_API_KEY: "own-anthropic" } },
      instances: {
        opencode: { driver: "opencodeGo", environment: { VENICE_API_KEY: "instance-venice" } },
        grok: { driver: "grokAgent" },
        codex: { driver: "codex" },
        compat: { driver: "openai-compat" },
      },
    };
    const instances = instanceConfigs(cfg);
    expect(instances.opencode.environment).toEqual({
      OPENCODE_API_KEY: "ocg", VENICE_API_KEY: "venice-secret", ANTHROPIC_API_KEY: "own-anthropic",
    });
    for (const id of ["grok", "codex", "compat"]) {
      expect(JSON.stringify(instances[id].environment ?? {})).not.toMatch(/venice-secret|own-anthropic/);
    }
    expect(persistableInstanceConfigs(cfg).opencode.environment).toEqual({ VENICE_API_KEY: "instance-venice" });
  });

  it("skips a hand-edited provider key OpenCode could not be given, without dropping the file", () => {
    const stored = parseStoredConfig({
      profile: { name: "Ada" },
      opencodeGo: { providerKeys: {
        VENICE_API_KEY: "venice-secret", venice_api_key: "lower", OPENCODE_API_KEY: "shadow", OMB_CLOUD_BOAT_TOKEN: "relay",
        BOX_TOKEN: "boat", NODE_OPTIONS: "--require x", GROQ_API_KEY: "has space",
        DEEPSEEK_API_KEY: 42, TOGETHER_API_KEY: null, FIREWORKS_API_KEY: { key: "nested" },
      } },
    });
    expect(stored.profile?.name).toBe("Ada");
    expect(openCodeProviderKeys(stored)).toEqual({ VENICE_API_KEY: "venice-secret" });
    expect(instanceConfigs({ ...stored, instances: { opencode: { driver: "opencodeGo" } } }).opencode.environment)
      .toEqual({ VENICE_API_KEY: "venice-secret" });
    const notAMap = parseStoredConfig({ profile: { name: "Ada" }, opencodeGo: { apiKey: "ocg", providerKeys: "VENICE_API_KEY=x" } });
    expect(notAMap.profile?.name).toBe("Ada");
    expect(notAMap.opencodeGo).toEqual({ apiKey: "ocg", providerKeys: {} });
  });
});

describe("saving OpenCode provider keys", () => {
  const saved: AppConfig = { opencodeGo: { providerKeys: { VENICE_API_KEY: "old-venice", GROQ_API_KEY: "groq" } } };

  it("preserves Cline provider keys for OpenCode without sharing its saved key with other engines", () => {
    const keys = { CLINE_API_KEY: "cline-provider-fixture" };
    expect(mergeOpenCodeProviderKeys({}, keys)).toEqual({ ok: true, keys });
    const instances = instanceConfigs({ opencodeGo: { providerKeys: keys }, instances: {
      opencode: { driver: "opencodeGo" }, cline: { driver: "clineAgent" }, grok: { driver: "grokAgent" },
    } });
    expect(instances.opencode.environment).toMatchObject(keys);
    expect(instances.cline.environment).toEqual({});
    expect(instances.grok.environment).toEqual({});
  });

  it("saves, replaces and removes one name at a time, keeping the rest", () => {
    expect(mergeOpenCodeProviderKeys(saved, { VENICE_API_KEY: "  new-venice ", DEEPSEEK_API_KEY: "deep" }))
      .toEqual({ ok: true, keys: { DEEPSEEK_API_KEY: "deep", GROQ_API_KEY: "groq", VENICE_API_KEY: "new-venice" } });
    expect(mergeOpenCodeProviderKeys(saved, { GROQ_API_KEY: "" })).toEqual({ ok: true, keys: { VENICE_API_KEY: "old-venice" } });
    expect(mergeOpenCodeProviderKeys(saved, { NOPE_KEY: "" })).toEqual({ ok: true, keys: saved.opencodeGo!.providerKeys });
    expect(mergeOpenCodeProviderKeys({}, { ANTHROPIC_API_KEY: "sk-ant" })).toEqual({ ok: true, keys: { ANTHROPIC_API_KEY: "sk-ant" } });
  });

  it("refuses names OpenCode does not read or that OpenMaus keeps, with the fix", () => {
    const refused = (name: string) => {
      const result = mergeOpenCodeProviderKeys(saved, { [name]: "a-key" });
      expect(result.ok, name).toBe(false);
      return result.ok ? "" : result.error;
    };
    for (const name of ["venice_api_key", "VENICE", "NODE_OPTIONS", "PATH", "LD_PRELOAD", "1_API_KEY", "__proto__", `${"A".repeat(70)}_KEY`]) {
      expect(refused(name)).toContain("such as VENICE_API_KEY");
    }
    expect(refused("OPENCODE_API_KEY")).toContain("OpenCode API key box");
    for (const name of ["OPENCODE_SERVER_KEY", "OMB_CLOUD_BOAT_TOKEN", "OMB_LICENSE_KEY", "BOX_TOKEN", "COMPOSIO_API_KEY",
      "OPENAI_COMPAT_API_KEY", "XAI_API_KEY", "MISTRAL_API_KEY", "CURSOR_API_KEY", "FACTORY_API_KEY"]) {
      expect(refused(name)).toBe(`OpenMaus keeps ${name} for itself, so OpenCode can't be given it here. Put this key in opencode.json instead.`);
    }
  });

  it("refuses a value that is not a key, and more than the limit", () => {
    for (const value of ["two words", "line\nbreak", "x".repeat(4097)]) {
      const result = mergeOpenCodeProviderKeys(saved, { VENICE_API_KEY: value });
      expect(result).toEqual({ ok: false, error: "That doesn't look like a key. Paste only the key, with no spaces." });
    }
    const full: AppConfig = { opencodeGo: { providerKeys: Object.fromEntries(
      Array.from({ length: OPENCODE_PROVIDER_KEY_LIMIT }, (_, index) => [`P${index}_API_KEY`, `key-${index}`])) } };
    expect(mergeOpenCodeProviderKeys(full, { P0_API_KEY: "replaced" }).ok).toBe(true);
    expect(mergeOpenCodeProviderKeys(full, { EXTRA_API_KEY: "one-too-many" })).toEqual({
      ok: false, error: `OpenCode can hold up to ${OPENCODE_PROVIDER_KEY_LIMIT} provider keys. Remove one you no longer use, then add this one.`,
    });
  });

  it("is a patch the config route accepts, and changing it reloads the engines", () => {
    expect(parseConfigPatch({ opencodeGo: { providerKeys: { VENICE_API_KEY: "v", GROQ_API_KEY: "" } } }))
      .toEqual({ opencodeGo: { providerKeys: { VENICE_API_KEY: "v", GROQ_API_KEY: "" } } });
    expect(() => parseConfigPatch({ opencodeGo: { providerKeys: { VENICE_API_KEY: 42 } } })).toThrow("opencodeGo.providerKeys");
    expect(reloaded({ opencodeGo: { providerKeys: { VENICE_API_KEY: "v" } } })).toEqual(["opencodeGo"]);
    expect(reloaded({ opencodeGo: { providerKeys: {} } })).toEqual([]);
  });
});

describe("credential env narrowing", () => {
  it("injects each credential only into the driver that consumes it", () => {
    const cfg: AppConfig = {
      xai: { key: "SECRET-XAI" },
      box: { token: "SECRET-BOAT" },
      opencodeGo: { apiKey: "SECRET-OCG" },
      instances: {
        grokApi: { driver: "grok" },
        opencode: { driver: "opencodeGo" },
        claude: { driver: "claudeAgent" },
        codex: { driver: "codex" },
      },
    };
    const instances = instanceConfigs(cfg);
    expect(instances.grokApi.environment).toEqual({ XAI_API_KEY: "SECRET-XAI" });
    expect(instances.opencode.environment).toEqual({ OPENCODE_API_KEY: "SECRET-OCG" });
    for (const entry of Object.values(instances)) expect(Object.values(entry.environment ?? {})).not.toContain("SECRET-BOAT");
    expect(instances.claude.environment).toEqual({});
    expect(instances.codex.environment).toEqual({});
  });

  it("hands no credential to any default-fleet CLI engine, and the Boat key to none at all", () => {
    const cfg: AppConfig = { xai: { key: "SECRET-XAI" }, box: { token: "SECRET-BOAT" } };
    const instances = instanceConfigs(cfg);
    for (const [id, entry] of Object.entries(instances)) {
      if (id === "xaiApi") expect(entry.environment).toEqual({ XAI_API_KEY: "SECRET-XAI" });
      else expect(entry.environment).toEqual({});
    }
  });

  it("gives each provider's own instance only its own key", () => {
    const cfg: AppConfig = {
      openai: { key: "SECRET-OPENAI" },
      openrouter: { key: "SECRET-OPENROUTER" },
      openaiCompat: { key: "SECRET-COMPAT", url: "https://api.groq.com/openai/v1", model: "llama" },
    };
    const instances = instanceConfigs(cfg);
    expect(instances.openai.environment).toEqual({ OMB_OPENAI_API_KEY: "SECRET-OPENAI" });
    expect(instances.openrouter.environment).toEqual({ OMB_OPENROUTER_API_KEY: "SECRET-OPENROUTER" });
    expect(instances.openaiCompat.environment).toEqual({ OPENAI_COMPAT_API_KEY: "SECRET-COMPAT", OPENAI_COMPAT_URL: "https://api.groq.com/openai/v1" });
    expect(instances.openai.config).toEqual({ url: "https://api.openai.com/v1", apiKeyEnv: "OMB_OPENAI_API_KEY", catalog: "openai" });
    expect(instances.openrouter.config).toEqual({ url: "https://openrouter.ai/api/v1", apiKeyEnv: "OMB_OPENROUTER_API_KEY" });
    expect(instances.openai.access).toBe("api");
  });

  it("runs only Claude (API key) on a key saved from Settings, and every Claude bot when asked", () => {
    const own = instanceConfigs({ anthropic: { key: "SECRET-ANT", everyClaudeBot: false } });
    expect(own.claudeApi.environment).toEqual({ ANTHROPIC_API_KEY: "SECRET-ANT" });
    expect(own.claude.environment).toEqual({});
    for (const anthropic of [{ key: "SECRET-ANT" }, { key: "SECRET-ANT", everyClaudeBot: true }]) {
      const every = instanceConfigs({ anthropic });
      expect(every.claude.environment, "a plain Claude bot runs on the key").toEqual({ ANTHROPIC_API_KEY: "SECRET-ANT" });
      expect(every.claudeApi.environment, "a bot already set to Claude (API key) keeps running on it").toEqual({ ANTHROPIC_API_KEY: "SECRET-ANT" });
    }
    const routed = instanceConfigs({ anthropic: { key: "SECRET-ANT", url: "https://gateway.example.test", everyClaudeBot: true } });
    expect(routed.claudeApi.environment).toEqual({ ANTHROPIC_API_KEY: "SECRET-ANT", ANTHROPIC_BASE_URL: "https://gateway.example.test" });
    expect(instanceConfigs({ anthropic: { everyClaudeBot: true } }).claudeApi.environment, "no key, nothing injected").toEqual({});
  });

  it("keeps the built-in routing of a provider instance in a saved fleet", () => {
    const instances = instanceConfigs({
      openaiCompat: { key: "SECRET-COMPAT", url: "https://api.groq.com/openai/v1" },
      instances: { claude: { driver: "claudeAgent" }, openai: { driver: "openai-compat", displayName: "OpenAI" } },
    });
    expect(instances.openai.config).toEqual({ url: "https://api.openai.com/v1", apiKeyEnv: "OMB_OPENAI_API_KEY", catalog: "openai" });
    expect(instances.openai.environment).toEqual({});
  });

  it("keeps a per-instance environment while layering the credential on top", () => {
    const cfg: AppConfig = {
      xai: { key: "SECRET-XAI" },
      instances: { grokApi: { driver: "grok", environment: { MY_FLAG: "1" } } },
    };
    expect(instanceConfigs(cfg).grokApi.environment).toEqual({ MY_FLAG: "1", XAI_API_KEY: "SECRET-XAI" });
  });
});

describe("legacy feature flag migration", () => {
  const path = join(DATA_DIR, "config.json");
  const readDisk = () => JSON.parse(readFileSync(path, "utf8"));

  beforeEach(() => {
    mkdirSync(DATA_DIR, { recursive: true });
    rmSync(path, { force: true });
  });
  afterEach(() => {
    rmSync(path, { force: true });
  });

  it("carries a legacy skillRecorder opt-in over to skillAuthoring once, at startup", () => {
    writeFileSync(path, JSON.stringify({ profile: { name: "Ada" }, features: { skillRecorder: true, browser: false } }));
    ensureDirs();
    expect(readDisk()).toEqual({ profile: { name: "Ada" }, features: { browser: false, skillAuthoring: true } });
    expect(skillAuthoringEnabled(loadConfig())).toBe(true);
    if (process.platform !== "win32") expect(statSync(path).mode & 0o777).toBe(0o600);
    const written = readFileSync(path, "utf8");
    ensureDirs();
    expect(readFileSync(path, "utf8")).toBe(written);
  });

  it("keeps a legacy opt-out off and removes the old key", () => {
    writeFileSync(path, JSON.stringify({ features: { skillRecorder: false } }));
    ensureDirs();
    expect(readDisk()).toEqual({ features: { skillAuthoring: false } });
    expect(skillAuthoringEnabled(loadConfig())).toBe(false);
  });

  it("lets an explicit skillAuthoring value win over the legacy key", () => {
    writeFileSync(path, JSON.stringify({ features: { skillRecorder: true, skillAuthoring: false } }));
    ensureDirs();
    expect(readDisk()).toEqual({ features: { skillAuthoring: false } });
    expect(skillAuthoringEnabled(loadConfig())).toBe(false);
  });

  it("treats a non-boolean legacy value as off and a null features block as absent", () => {
    writeFileSync(path, JSON.stringify({ features: { skillRecorder: "yes" } }));
    ensureDirs();
    expect(readDisk()).toEqual({ features: { skillAuthoring: false } });
    const nulled = JSON.stringify({ features: null });
    writeFileSync(path, nulled);
    ensureDirs();
    expect(readFileSync(path, "utf8")).toBe(nulled);
  });

  it("leaves a config without the legacy key untouched", () => {
    for (const disk of [{ profile: { name: "Ada" } }, { features: {} }, { features: { skillAuthoring: true } }]) {
      const raw = JSON.stringify(disk);
      writeFileSync(path, raw);
      ensureDirs();
      expect(readFileSync(path, "utf8")).toBe(raw);
    }
  });

  it("does not create a config file or throw on a fresh or unreadable install", () => {
    ensureDirs();
    expect(() => readFileSync(path, "utf8")).toThrow();
    writeFileSync(path, "{not json");
    expect(() => ensureDirs()).not.toThrow();
    expect(readFileSync(path, "utf8")).toBe("{not json");
  });
});

describe("credential env preference", () => {
  const VARS = [
    "XAI_API_KEY",
    "OMB_GROQ_API_KEY",
    "OMB_META_API_KEY",
    "OPENAI_COMPAT_API_KEY",
    "OPENAI_COMPAT_URL",
    "OPENAI_COMPAT_MODEL",
    "OPENAI_COMPAT_PROVIDER",
    "BOX_TOKEN",
    "OPENCODE_API_KEY",
    "OMB_TTS_KEY",
    "OMB_FISH_AUDIO_API_KEY",
    "OMB_OPENAI_IMAGE_KEY",
    "COMPOSIO_API_KEY",
  ] as const;
  let saved: Record<string, string | undefined>;

  beforeEach(() => {
    saved = Object.fromEntries(VARS.map((name) => [name, process.env[name]]));
    for (const name of VARS) delete process.env[name];
    mkdirSync(DATA_DIR, { recursive: true });
    rmSync(join(DATA_DIR, "config.json"), { force: true });
  });
  afterEach(() => {
    for (const name of VARS) {
      if (saved[name] === undefined) delete process.env[name];
      else process.env[name] = saved[name];
    }
    rmSync(join(DATA_DIR, "config.json"), { force: true });
  });

  it("prefers env over the config file for every credential", () => {
    // The desktop shell hands secrets over as env and leaves the file without them: env must win over a leftover plaintext value.
    writeFileSync(
      join(DATA_DIR, "config.json"),
      JSON.stringify({
        xai: { key: "file-xai", url: "https://api.example.test/v1" },
        box: { token: "file-box" },
        opencodeGo: { apiKey: "file-ocg" },
        tts: { key: "file-tts", fishKey: "file-fish", voice: "narrator" },
        imageGen: { key: "file-image" },
      }),
    );
    process.env.XAI_API_KEY = "env-xai";
    process.env.BOX_TOKEN = "env-box";
    process.env.OPENCODE_API_KEY = "env-ocg";
    process.env.OMB_TTS_KEY = "env-tts";
    process.env.OMB_FISH_AUDIO_API_KEY = "env-fish";
    process.env.OMB_OPENAI_IMAGE_KEY = "env-image";
    const cfg = loadConfig();
    expect(cfg.xai).toEqual({ key: "env-xai", url: "https://api.example.test/v1" });
    expect(cfg.box).toEqual({ token: "env-box" });
    expect(cfg.opencodeGo).toEqual({ apiKey: "env-ocg" });
    expect(cfg.tts).toEqual({ key: "env-tts", fishKey: "env-fish", voice: "narrator" });
    expect(cfg.imageGen).toEqual({ key: "env-image" });
  });

  it("uses a preset voice only when the person has not chosen one or another provider", () => {
    process.env.OMB_TTS_DEFAULT_VOICE = " preset-voice ";
    expect(loadConfig().tts?.voice).toBe("preset-voice");
    writeFileSync(join(DATA_DIR, "config.json"), JSON.stringify({ tts: { voice: "chosen" } }));
    expect(loadConfig().tts?.voice).toBe("chosen");
    writeFileSync(join(DATA_DIR, "config.json"), JSON.stringify({ tts: { provider: "system" } }));
    expect(loadConfig().tts?.voice).toBeUndefined();
    writeFileSync(join(DATA_DIR, "config.json"), JSON.stringify({ tts: { provider: "elevenlabs" } }));
    expect(loadConfig().tts?.voice).toBe("preset-voice");
  });

  it("never takes Cloud Pro's included tokens for the person's own keys, in config or an engine's environment", () => {
    const included = {
      OMB_CLOUD_BOAT_URL: "https://cloud.example.test/api/cloud/services/boat/api/box/v1",
      OMB_CLOUD_BOAT_TOKEN: "box_omb_included-relay-token",
      OMB_CLOUD_VOICE_URL: "https://cloud.example.test/api/cloud/services/voice/v1",
      OMB_CLOUD_VOICE_TOKEN: "omb_voice_included-relay-token",
      OMB_CLOUD_DECIDER_URL: "https://cloud.example.test/api/cloud/services/decider",
      OMB_CLOUD_DECIDER_TOKEN: "omb_decide_included-relay-token",
    };
    for (const [name, value] of Object.entries(included)) vi.stubEnv(name, value);
    try {
      const cfg = loadConfig();
      expect(cfg.box?.token).toBeUndefined();
      expect(cfg.tts?.key).toBeUndefined();
      expect(cfg.decider?.key).toBeUndefined();
      for (const entry of Object.values(instanceConfigs(cfg))) {
        for (const value of Object.values(entry.environment ?? {})) expect(Object.values(included)).not.toContain(value);
      }
      saveConfig({ tts: { voice: "chosen" }, box: { token: "" }, decider: { enabled: true, jobs: { roomRouting: true } } });
      const disk = readFileSync(join(DATA_DIR, "config.json"), "utf8");
      const runtime = JSON.stringify([loadConfig(), instanceConfigs(loadConfig()), persistableInstanceConfigs(loadConfig())]);
      for (const token of [included.OMB_CLOUD_BOAT_TOKEN, included.OMB_CLOUD_VOICE_TOKEN, included.OMB_CLOUD_DECIDER_TOKEN]) {
        expect(disk).not.toContain(token);
        expect(runtime).not.toContain(token);
      }
      process.env.BOX_TOKEN = "box_own";
      process.env.OMB_TTS_KEY = "sk-own";
      expect(loadConfig()).toMatchObject({ box: { token: "box_own" }, tts: { key: "sk-own" } });
      expect(JSON.stringify(instanceConfigs(loadConfig()))).not.toContain("box_own");
    } finally {
      vi.unstubAllEnvs();
    }
  });

  it("saves and removes the verified domain without replacing existing settings", () => {
    saveConfig({ profile: { name: "Workspace owner" }, customDomain: "https://bots.example.com" });
    expect(loadConfig().customDomain).toBe("https://bots.example.com");
    saveConfig({ language: "en" });
    expect(loadConfig().customDomain).toBe("https://bots.example.com");
    saveConfig({ customDomain: "" });
    expect(loadConfig()).toMatchObject({ customDomain: "", language: "en", profile: { name: "Workspace owner" } });
  });

  it("saves, trims and clears the computer name a paired phone shows", () => {
    expect(parseConfigPatch({ computerName: "  AdamPC  " })).toEqual({ computerName: "AdamPC" });
    expect(() => parseConfigPatch({ computerName: "x".repeat(64) })).toThrow();
    saveConfig({ profile: { name: "Workspace owner" }, computerName: "AdamPC" });
    expect(loadConfig()).toMatchObject({ computerName: "AdamPC", profile: { name: "Workspace owner" } });
    saveConfig({ computerName: "" });
    expect(loadConfig()).toMatchObject({ computerName: "", profile: { name: "Workspace owner" } });
  });

  it("merges onboarding progress like any other section", () => {
    saveConfig({ onboarding: { completedAt: "2026-09-09T10:00:00.000Z", version: 1 } });
    saveConfig({ onboarding: { hintsSeen: ["computer"] } });
    expect(loadConfig().onboarding).toEqual({
      completedAt: "2026-09-09T10:00:00.000Z",
      version: 1,
      hintsSeen: ["computer"],
    });
    expect(() => parseConfigPatch({ onboarding: { hintsSeen: ["x".repeat(61)] } })).toThrow();
    expect(() => parseConfigPatch({ onboarding: { unknown: true } })).toThrow();
  });

  it("replaces automatic recovery atomically, clears an omitted backup and keeps unrelated settings", () => {
    const backup = { instanceId: "codex", model: "backup", effort: "high" as const };
    saveConfig({ automaticRecovery: { enabled: true, backup }, profile: { name: "Recovery fixture" } });
    expect(loadConfig().automaticRecovery).toEqual({ enabled: true, backup });
    saveConfig({ automaticRecovery: { enabled: true, backup: { instanceId: "claude", model: "other" } } });
    expect(loadConfig().automaticRecovery).toEqual({ enabled: true, backup: { instanceId: "claude", model: "other" } });
    expect(() => saveConfig({ automaticRecovery: { enabled: true } })).toThrow();
    expect(loadConfig().automaticRecovery?.backup?.model).toBe("other");
    saveConfig({ automaticRecovery: { enabled: false } });
    expect(loadConfig().automaticRecovery).toEqual({ enabled: false });
    expect(loadConfig().profile).toEqual({ name: "Recovery fixture" });
  });

  it("persists a context change without losing the other context preferences", () => {
    saveConfig({ context: { rebuildBytes: 32_000, autoCompact: false } });
    saveConfig({ context: { compactAt: 0.75 } });
    expect(loadConfig().context).toEqual({ rebuildBytes: 32_000, autoCompact: false, compactAt: 0.75 });
  });

  it("persists the replay tool-history switch beside the other context preferences", () => {
    saveConfig({ context: { rebuildBytes: 32_000 } });
    expect(loadConfig().context?.replayToolHistory).toBeUndefined();
    saveConfig({ context: { replayToolHistory: true } });
    expect(loadConfig().context).toEqual({ rebuildBytes: 32_000, replayToolHistory: true });
  });

  it("falls back to the config file when the env var is unset (dev mode)", () => {
    writeFileSync(
      join(DATA_DIR, "config.json"),
      JSON.stringify({ xai: { key: "file-xai" }, tts: { key: "file-tts" }, imageGen: { key: "file-image" } }),
    );
    const cfg = loadConfig();
    expect(cfg.xai?.key).toBe("file-xai");
    expect(cfg.tts?.key).toBe("file-tts");
    expect(cfg.imageGen?.key).toBe("file-image");
  });

  it("persists a default selection without changing the fleet or unrelated settings", () => {
    const path = join(DATA_DIR, "config.json");
    const existing = {
      profile: { name: "Ada" },
      instances: { customCodex: { driver: "codex", config: { cli: "/opt/custom-codex" } } },
      openaiCompat: { key: "fixture-key", url: "https://models.example.test/v1" },
      futureSetting: { keep: true },
    };
    writeFileSync(path, JSON.stringify(existing));
    const selection = { instanceId: "customCodex", model: "fixture-model", effort: "high" as const };

    saveConfig({ defaultModelSelection: selection });
    expect(loadConfig().defaultModelSelection).toEqual(selection);
    expect(JSON.parse(readFileSync(path, "utf8"))).toEqual({ ...existing, defaultModelSelection: selection });

    saveConfig({ profile: { email: "[email protected]" } });
    expect(loadConfig().defaultModelSelection).toEqual(selection);

    const replacement = { instanceId: "claude", model: "different-model" };
    saveConfig({ defaultModelSelection: replacement });
    expect(loadConfig().defaultModelSelection).toEqual(replacement);
    expect(loadConfig().profile).toEqual({ name: "Ada", email: "[email protected]" });
    expect(loadConfig().instances).toEqual(existing.instances);
  });

  it("rejects oversized default model changes before writing the template", () => {
    saveConfig({ newBotDefaults: { profile: { modelSelection: { instanceId: "codex", model: "valid" } }, memory: {}, skills: [], routines: [] } });
    const path = join(DATA_DIR, "config.json");
    const before = readFileSync(path, "utf8");
    for (const selection of [{ instanceId: "x".repeat(201), model: "valid" }, { instanceId: "codex", model: "x".repeat(501) }]) {
      expect(() => saveConfig({ defaultModelSelection: selection })).toThrow();
      expect(readFileSync(path, "utf8")).toBe(before);
      expect(loadConfig().newBotDefaults?.profile.modelSelection?.model).toBe("valid");
    }
  });

  it("rejects a valid model selection that would overflow the complete defaults template", () => {
    const defaults = { profile: { modelSelection: { instanceId: "codex", model: "valid" } },
      memory: { "memory/a.md": "a".repeat(225_000), "memory/b.md": "b".repeat(225_000),
        "memory/c.md": "c".repeat(225_000), "memory/d.md": "" }, skills: [], routines: [] };
    defaults.memory["memory/d.md"] = "d".repeat(899_990 - Buffer.byteLength(JSON.stringify(defaults), "utf8"));
    saveConfig({ newBotDefaults: defaults });
    const path = join(DATA_DIR, "config.json");
    const before = readFileSync(path, "utf8");
    expect(() => saveConfig({ defaultModelSelection: { instanceId: "codex", model: "m".repeat(500) } })).toThrow("900 KB");
    expect(readFileSync(path, "utf8")).toBe(before);
    expect(loadConfig().newBotDefaults).toEqual(defaults);
  });

  it("replaces instance membership and known settings while preserving retained extension fields", () => {
    const path = join(DATA_DIR, "config.json");
    writeFileSync(path, JSON.stringify({
      instances: {
        retained: {
          driver: "fixture-future-driver",
          displayName: "Old label",
          environment: { FIXTURE_TOKEN: "fixture-stored-token" },
          config: { cli: "/fixture/old-cli" },
          futureSetting: { keep: true },
        },
        removed: { driver: "fixture-removed-driver", futureSetting: { remove: true } },
      },
    }));
    const instances = persistableInstanceConfigs(loadConfig());
    delete instances.removed;
    delete instances.retained.displayName;
    delete instances.retained.environment;
    delete instances.retained.config;

    saveConfig({ instances }, { replaceInstances: true });
    expect(JSON.parse(readFileSync(path, "utf8")).instances).toEqual({
      retained: { driver: "fixture-future-driver", futureSetting: { keep: true } },
    });

    saveConfig({ instances: {} }, { replaceInstances: true });
    expect(JSON.parse(readFileSync(path, "utf8")).instances).toEqual({});
  });

  it.each(["https://next.example.test/v1", ""])("an explicit workspace URL save (%s) repairs the shared default connection", (url) => {
    const path = join(DATA_DIR, "config.json");
    const instances = {
      openaiCompat: { driver: "openai-compat", config: { url: "https://old.example.test/v1", tools: false, model: "selected", provider: "" } },
      custom: { driver: "openai-compat", config: { url: "https://custom.example.test/v1", key: "fixture-custom" } },
      customShared: { driver: "openai-compat", config: { url: "https://shared.example.test/v1" } },
    };
    writeFileSync(path, JSON.stringify({ openaiCompat: { url, key: "fixture-shared" }, instances }));
    saveConfig({ openaiCompat: { url } });
    const disk = JSON.parse(readFileSync(path, "utf8"));
    expect(disk.instances.openaiCompat.config).toEqual({ tools: false, model: "selected", provider: "" });
    expect(disk.instances.custom).toEqual(instances.custom);
    expect(disk.instances.customShared).toEqual(instances.customShared);
    expect(disk.openaiCompat).toEqual({ url, key: "fixture-shared" });
    expect(instanceConfigs(loadConfig()).openaiCompat.config).toEqual({
      tools: false, model: "selected", provider: "", ...(url ? { url } : {}),
    });
  });

  it.each([
    { config: { key: "fixture-private" } },
    { config: { apiKeyEnv: "CUSTOM_API_KEY" } },
    { environment: { OPENAI_COMPAT_API_KEY: "fixture-private" } },
  ])("keeps an independently credentialed default connection when the workspace URL changes: %j", (override) => {
    const entry = { driver: "openai-compat", ...override, config: { url: "https://private.example.test/v1", ...override.config } };
    const path = join(DATA_DIR, "config.json");
    writeFileSync(path, JSON.stringify({ instances: { openaiCompat: entry } }));
    saveConfig({ openaiCompat: { url: "https://workspace.example.test/v1" } });
    expect(JSON.parse(readFileSync(path, "utf8")).instances.openaiCompat).toEqual(entry);
  });

  it("preserves explicit instance URLs on unrelated saves and in a combined configuration patch", () => {
    const path = join(DATA_DIR, "config.json");
    const entry = { driver: "openai-compat", config: { url: "https://explicit.example.test/v1" } };
    writeFileSync(path, JSON.stringify({ instances: { openaiCompat: entry } }));
    saveConfig({ openaiCompat: { key: "fixture-replacement" }, profile: { name: "Fixture" } });
    expect(JSON.parse(readFileSync(path, "utf8")).instances.openaiCompat).toEqual(entry);
    saveConfig({ openaiCompat: { url: "https://workspace.example.test/v1" }, instances: { openaiCompat: entry } });
    expect(JSON.parse(readFileSync(path, "utf8")).instances.openaiCompat).toEqual(entry);
  });

  it.each(["openai-compat", "future-driver"])("limits URL repair to the default OpenAI-compatible driver (%s)", (driver) => {
    const path = join(DATA_DIR, "config.json");
    const entry = { driver, config: { url: "https://old.example.test/v1" }, futureSetting: { keep: true } };
    writeFileSync(path, JSON.stringify({ instances: { openaiCompat: entry } }));
    saveConfig({ openaiCompat: { url: "https://next.example.test/v1" } });
    expect(JSON.parse(readFileSync(path, "utf8")).instances.openaiCompat).toEqual({
      ...entry, config: driver === "openai-compat" ? {} : entry.config,
    });
  });

  it("preserves per-profile CDP targets through settings edits", () => {
    const profile = { id: "scout", name: "Scout", browser: "chrome", cdpUrl: "9333" };
    writeFileSync(join(DATA_DIR, "config.json"), JSON.stringify({ browserProfiles: [profile] }));
    saveConfig({ browserProfiles: [{ id: "scout", name: "Scout Chrome", browser: "chrome" }] });
    expect(loadConfig().browserProfiles).toEqual([{ ...profile, name: "Scout Chrome" }]);
    saveConfig({ browserProfiles: [{ id: "scout", name: "Scout Built-in" }] });
    expect(loadConfig().browserProfiles).toEqual([{ id: "scout", name: "Scout Built-in" }]);
  });

  it("loads legacy browser profiles without resetting config and canonicalizes them on the next write", () => {
    const path = join(DATA_DIR, "config.json");
    writeFileSync(path, JSON.stringify({
      xai: { key: "file-xai", url: "https://api.example.test/v1" },
      profile: { name: "Ada" },
      features: { browser: true },
      browserProfiles: [
        { id: "Client", name: "Client one" },
        { id: "Client", name: "Client two" },
      ],
      futureSetting: { keep: true },
    }));

    expect(loadConfig()).toMatchObject({
      xai: { key: "file-xai", url: "https://api.example.test/v1" },
      profile: { name: "Ada" },
      features: { browser: true },
      browserProfiles: [
        { id: "client", name: "Client one", partitionId: "Client" },
        { id: "client-2", name: "Client two" },
      ],
    });

    saveConfig({ features: { showToolCalls: true } });
    const persisted = JSON.parse(readFileSync(path, "utf8"));
    expect(persisted).toMatchObject({
      xai: { key: "file-xai", url: "https://api.example.test/v1" },
      profile: { name: "Ada" },
      features: { browser: true, showToolCalls: true },
      browserProfiles: [
        { id: "client", name: "Client one", partitionId: "Client" },
        { id: "client-2", name: "Client two" },
      ],
      futureSetting: { keep: true },
    });

    saveConfig({ browserProfiles: [
      { id: "client", name: "Renamed client" },
      { id: "client-2", name: "Client two" },
    ] });
    const renamed = JSON.parse(readFileSync(path, "utf8"));
    expect(renamed.browserProfiles).toEqual([
      { id: "client", name: "Renamed client", partitionId: "Client" },
      { id: "client-2", name: "Client two" },
    ]);
  });

  it("treats a blanked file field as absent when env supplies the secret", () => {
    // The desktop shell may leave "" behind (a key cleared mid-session); the env-injected value must still win.
    writeFileSync(join(DATA_DIR, "config.json"), JSON.stringify({ xai: { key: "" } }));
    process.env.XAI_API_KEY = "env-xai";
    expect(loadConfig().xai?.key).toBe("env-xai");
  });

  it("syncCredentialEnv keeps process.env in step with a credential save", () => {
    process.env.XAI_API_KEY = "boot-injected";
    process.env.BOX_TOKEN = "boot-injected";
    process.env.COMPOSIO_API_KEY = "boot-injected";
    syncCredentialEnv({
      xai: { key: "just-saved" },
      composio: { apiKey: "ak_just_saved" },
      box: { token: "" },
      profile: { name: "Ada" },
    });
    expect(process.env.XAI_API_KEY).toBe("just-saved");
    expect(process.env.COMPOSIO_API_KEY).toBe("ak_just_saved");
    expect(process.env.BOX_TOKEN).toBeUndefined();
    expect(process.env.OMB_TTS_KEY).toBeUndefined();
    expect(process.env.OMB_FISH_AUDIO_API_KEY).toBeUndefined();
  });

  it("saves and clears the built-in Groq key without changing an operator's Groq key", () => {
    vi.stubEnv("GROQ_API_KEY", "groq-operator");
    try {
      syncCredentialEnv({ groq: { key: "groq-workspace" } });
      expect(loadConfig().groq?.key).toBe("groq-workspace");
      syncCredentialEnv({ groq: { key: "" } });
      expect(loadConfig().groq?.key).toBeUndefined();
      expect(process.env.GROQ_API_KEY).toBe("groq-operator");
    } finally { vi.unstubAllEnvs(); }
  });

  it("saves and clears the built-in DeepSeek key without changing an operator's DEEPSEEK_API_KEY", () => {
    vi.stubEnv("DEEPSEEK_API_KEY", "ds-operator");
    try {
      syncCredentialEnv({ deepseek: { key: "ds-workspace" } });
      expect(loadConfig().deepseek?.key).toBe("ds-workspace");
      syncCredentialEnv({ deepseek: { key: "" } });
      expect(loadConfig().deepseek?.key).toBeUndefined();
      expect(process.env.DEEPSEEK_API_KEY).toBe("ds-operator");
    } finally { vi.unstubAllEnvs(); }
  });

  it("saves and clears the built-in Meta key without changing a Muse Code META_API_KEY", () => {
    vi.stubEnv("META_API_KEY", "meta-operator");
    try {
      syncCredentialEnv({ meta: { key: "meta-workspace" } });
      expect(loadConfig().meta?.key).toBe("meta-workspace");
      syncCredentialEnv({ meta: { key: "" } });
      expect(loadConfig().meta?.key).toBeUndefined();
      expect(process.env.META_API_KEY).toBe("meta-operator");
    } finally { vi.unstubAllEnvs(); }
  });

  it("syncCredentialEnv updates Fish Audio without replacing ElevenLabs", () => {
    process.env.OMB_TTS_KEY = "eleven-kept";
    process.env.OMB_FISH_AUDIO_API_KEY = "fish-old";
    syncCredentialEnv({ tts: { fishKey: "fish-new" } });
    expect(process.env.OMB_TTS_KEY).toBe("eleven-kept");
    expect(process.env.OMB_FISH_AUDIO_API_KEY).toBe("fish-new");
  });

  it("syncCredentialEnv keeps model and provider env in step with a save", () => {
    process.env.OPENAI_COMPAT_MODEL = "boot-model";
    process.env.OPENAI_COMPAT_PROVIDER = "boot-provider";
    syncCredentialEnv({
      openaiCompat: { model: "vendor/just-saved", provider: "fireworks" },
    });
    expect(process.env.OPENAI_COMPAT_MODEL).toBe("vendor/just-saved");
    expect(process.env.OPENAI_COMPAT_PROVIDER).toBe("fireworks");
  });

  it("syncCredentialEnv clears model and provider env on an empty-string save", () => {
    process.env.OPENAI_COMPAT_MODEL = "boot-model";
    process.env.OPENAI_COMPAT_PROVIDER = "boot-provider";
    syncCredentialEnv({ openaiCompat: { model: "", provider: "" } });
    expect(process.env.OPENAI_COMPAT_MODEL).toBeUndefined();
    expect(process.env.OPENAI_COMPAT_PROVIDER).toBeUndefined();
  });

  it("syncCredentialEnv leaves model and provider env untouched when absent from the patch", () => {
    process.env.OPENAI_COMPAT_MODEL = "boot-model";
    process.env.OPENAI_COMPAT_PROVIDER = "boot-provider";
    syncCredentialEnv({ openaiCompat: { key: "just-saved" } });
    expect(process.env.OPENAI_COMPAT_MODEL).toBe("boot-model");
    expect(process.env.OPENAI_COMPAT_PROVIDER).toBe("boot-provider");
  });
});

describe("workspace credential env strip", () => {
  it("removes every workspace credential from a child env in place", () => {
    const env = {
      PATH: "/usr/bin",
      MY_FLAG: "1",
      ...Object.fromEntries(WORKSPACE_CREDENTIAL_ENV.map((name) => [name, "secret"])),
    };
    stripWorkspaceCredentialEnv(env);
    expect(env).toEqual({ PATH: "/usr/bin", MY_FLAG: "1" });
  });

  it("keeps a hosted tenant's control-plane secrets out of every child env, and only those", () => {
    // What a hosting control plane and a fleet put in the server's env; OMB_CLOUD_FUTURE_SECRET stands for a name added later.
    const operator = {
      OMB_CLOUD_READY_TOKEN: "ready", OMB_CLOUD_BOOTSTRAP: "bootstrap", OMB_CLOUD_GATEWAY_TOKEN: "gateway",
      OMB_CLOUD_MODELS: "models", OMB_CLOUD_REVISION: "revision", OMB_CLOUD_FUTURE_SECRET: "later",
      OMB_LICENSE_KEY: "license", OMB_INSTALLATION_CREDENTIAL: "fleet", omb_cloud_ready_token: "windows-spelling",
    };
    // What an engine deliberately receives (the hosted model token as its provider key), plus look-alike names.
    const engine = {
      PATH: "/usr/bin", ANTHROPIC_API_KEY: "hosted-token", ANTHROPIC_AUTH_TOKEN: "hosted-token",
      ANTHROPIC_BASE_URL: "https://admin.example.test/api/gateway/w/anthropic", OPENMAUSBOT_COMPANY_API_KEY: "hosted-token",
      OMB_MANAGED_CODEX_TOKEN: "hosted-token", CODEX_HOME: "/data/codex", OMB_HOOK_TOKEN_FILE: "/data/hook-tokens/a.token",
      OMB_CLOUDFLARED_PATH: "/usr/local/bin/cloudflared", OMB_CLOUD: "not-prefixed", MY_OMB_CLOUD_NOTE: "user",
    };
    for (const strip of [stripControlPlaneEnv, stripWorkspaceCredentialEnv]) {
      const env: Record<string, string | undefined> = { ...operator, ...engine };
      strip(env);
      expect(env).toEqual(engine);
    }
  });

  it("covers in-process secrets and private app-state paths", () => {
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("BOX_TOKEN");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_TTS_KEY");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_CLOUD_BOAT_TOKEN");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_CLOUD_VOICE_TOKEN");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_CLOUD_DECIDER_TOKEN");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_CLOUD_AI_TOKEN");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_FISH_AUDIO_API_KEY");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_OPENAI_IMAGE_KEY");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_BROWSER_CONNECTION");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_USER_DATA");
  });
});

describe("customMcpServers", () => {
  const cfg = (mcpServers: Record<string, unknown>) =>
    ({ mcpServers }) as Parameters<typeof customMcpServers>[0];

  it("normalizes a valid stdio entry and defaults args/env", () => {
    expect(
      customMcpServers(
        cfg({
          notes: { command: "npx", args: ["-y", "@x/notes-mcp"], env: { NOTES_TOKEN: "t" } },
          bare: { command: "/usr/local/bin/server" },
        }),
      ),
    ).toEqual({
      notes: { command: "npx", args: ["-y", "@x/notes-mcp"], env: { NOTES_TOKEN: "t" } },
      bare: { command: "/usr/local/bin/server", args: [], env: {} },
    });
  });

  it("returns {} when the section is absent", () => {
    expect(customMcpServers({} as Parameters<typeof customMcpServers>[0])).toEqual({});
  });

  it("narrows to a bot's own list when one is given, and to nothing for an empty list", () => {
    const all = cfg({ notes: { command: "a" }, linear: { command: "b" }, off: { command: "c", enabled: false } });
    expect(Object.keys(customMcpServers(all))).toEqual(["notes", "linear"]);
    expect(Object.keys(customMcpServers(all, ["linear", "gone"]))).toEqual(["linear"]);
    expect(customMcpServers(all, [])).toEqual({});
    expect(customMcpServers(all, ["off"])).toEqual({});
  });

  it("skips disabled entries silently", () => {
    expect(customMcpServers(cfg({ off: { command: "x", enabled: false } }))).toEqual({});
  });

  it("skips reserved names — a custom entry can never shadow a built-in", () => {
    const out = customMcpServers(
      cfg({
        ogb: { command: "evil" },
        computer: { command: "evil" },
        agents: { command: "evil" },
        fine: { command: "ok" },
      }),
    );
    expect(Object.keys(out)).toEqual(["fine"]);
  });

  it("skips names that could not survive every driver's namespace", () => {
    const out = customMcpServers(
      cfg({
        "Bad.Name": { command: "x" },
        "1leading-digit": { command: "x" },
        "way-too-long-name-way-too-long-name-x": { command: "x" },
        good_name: { command: "x" },
      }),
    );
    expect(Object.keys(out)).toEqual(["good_name"]);
  });

  it("passes a url transport through as streamable HTTP", () => {
    expect(customMcpServers(cfg({ api: { url: "https://x/mcp" } }))).toEqual({ api: { type: "http", url: "https://x/mcp", headers: {} } });
  });

  it("never hands a url server's sign-in app or its secret to an engine", () => {
    expect(customMcpServers(cfg({ api: { url: "https://x/mcp", oauth: { clientId: "app", clientSecret: "shh" } } })))
      .toEqual({ api: { type: "http", url: "https://x/mcp", headers: {} } });
  });

  it("skips malformed entries without dropping the valid ones", () => {
    const out = customMcpServers(
      cfg({
        broken: { args: ["no-command"] },
        alsoBad: "not-an-object",
        keeper: { command: "ok" },
      }),
    );
    expect(Object.keys(out)).toEqual(["keeper"]);
  });
});

describe("changedInstanceIds", () => {
  it("replaces only the engines whose launch config a save changed", () => {
    const saved: AppConfig = { mistral: { key: "m1" }, openaiCompat: { key: "c1", url: "https://compat.example/v1" } };
    expect(reloaded({ mistral: { key: "m1" }, openaiCompat: { key: "c1", url: "https://compat.example/v1" } }, saved)).toEqual([]);
    expect(reloaded({ mistral: { key: "m2" } }, saved)).toEqual(["mistral", "mistralVibe"]);
    expect(reloaded({ openaiCompat: { key: "c2", url: "https://compat.example/v1" } }, saved)).toEqual(["openaiCompat"]);
    expect(reloaded({ openaiCompat: { key: "c1", url: "https://other.example/v1" } }, saved)).toEqual(["openaiCompat"]);
    expect(reloaded({ anthropic: { key: "a" } }, saved), "Claude (API key) runs on the key too").toEqual(["claude", "claudeApi"]);
  });
  it("replaces nothing for sections no driver reads", () => {
    expect(reloaded({ onboarding: { hintsSeen: ["tour.composer"] } })).toEqual([]);
    expect(reloaded({ profile: {}, language: "de", tts: {}, features: {} })).toEqual([]);
    expect(reloaded({ box: { token: "boat" }, composio: { apiKey: "c" }, budgets: { monthlyUsd: 5 } })).toEqual([]);
  });
  it("replaces an engine whose driver reads a saved key from the env", () => {
    const own = instanceConfigs({ instances: { own: { driver: "mistral", config: { url: "http://127.0.0.1:9/v1" } } }, mistral: { key: "m1" } });
    expect(own.own!.environment?.MISTRAL_API_KEY).toBeUndefined();
    expect(changedInstanceIds(engineLaunches(own, { MISTRAL_API_KEY: "m1" }), engineLaunches(own, { MISTRAL_API_KEY: "m1" }))).toEqual([]);
    expect(changedInstanceIds(engineLaunches(own, { MISTRAL_API_KEY: "m1" }), engineLaunches(own, { MISTRAL_API_KEY: "m2" }))).toEqual(["own"]);
    const fleet = instanceConfigs({});
    const compat = (env: Record<string, string>) => changedInstanceIds(engineLaunches(fleet, {}), engineLaunches(fleet, env));
    expect(compat({ OPENAI_COMPAT_URL: "https://compat.example/v1" })).toEqual(["openaiCompat"]);
    expect(compat({ OMB_OPENAI_API_KEY: "o" })).toEqual(["openai"]);
    expect(compat({ BOX_TOKEN: "boat", COMPOSIO_API_KEY: "c" })).toEqual([]);
  });
  it("names an engine that was added or removed", () => {
    expect(changedInstanceIds({ a: { driver: "fake" } }, { b: { driver: "fake" } })).toEqual(["a", "b"]);
    expect(changedInstanceIds({ a: { driver: "fake", config: { x: 1 } } }, { a: { driver: "fake", config: { x: 1 } } })).toEqual([]);
  });
});

describe("customMcpServers with url entries", () => {
  it("passes remote servers through in the engines' shape, next to commands", () => {
    const cfg = {
      mcpServers: {
        docs: { type: "sse", url: "https://docs.example/sse", headers: { Authorization: "Bearer t" } },
        notes: { command: "npx" },
        broken: { url: "not-an-address" },
      },
    } as Parameters<typeof customMcpServers>[0];
    expect(customMcpServers(cfg)).toEqual({
      docs: { type: "sse", url: "https://docs.example/sse", headers: { Authorization: "Bearer t" } },
      notes: { command: "npx", args: [], env: {} },
    });
  });
});

describe("live settings", () => {
  it("accepts idle minutes from 1 to 60 only", () => {
    expect(() => parseConfigPatch({ live: { idleMinutes: 0 } })).toThrow();
    expect(() => parseConfigPatch({ live: { idleMinutes: 61 } })).toThrow();
    expect(() => parseConfigPatch({ live: { idleMinutes: 2.5 } })).toThrow();
    expect(parseConfigPatch({ live: { idleMinutes: 60, readTypedReplies: false } })).toMatchObject({ live: { idleMinutes: 60, readTypedReplies: false } });
  });
  it("accepts the OpenAI standby idle timeout only from 5 to 60 whole seconds", () => {
    expect(() => parseConfigPatch({ live: { standbyIdleSeconds: 4 } })).toThrow();
    expect(() => parseConfigPatch({ live: { standbyIdleSeconds: 61 } })).toThrow();
    expect(() => parseConfigPatch({ live: { standbyIdleSeconds: 15.5 } })).toThrow();
    expect(parseConfigPatch({ live: { standby: true, standbyIdleSeconds: 60 } })).toMatchObject({ live: { standby: true, standbyIdleSeconds: 60 } });
  });
  it("does not reload providers for live changes", () => {
    expect(reloaded({ live: { idleMinutes: 3 } })).toEqual([]);
  });

  describe("saving settings from PATCH /api/live/settings", () => {
    const path = join(DATA_DIR, "config.json");
    let envKey: string | undefined;
    beforeEach(() => {
      envKey = process.env.OMB_OPENAI_LIVE_KEY;
      delete process.env.OMB_OPENAI_LIVE_KEY;
      mkdirSync(DATA_DIR, { recursive: true });
      rmSync(path, { force: true });
    });
    afterEach(() => {
      if (envKey === undefined) delete process.env.OMB_OPENAI_LIVE_KEY;
      else process.env.OMB_OPENAI_LIVE_KEY = envKey;
      rmSync(path, { force: true });
    });

    it("keeps the Live key when only settings change", () => {
      saveConfig({ live: { key: "sk-keep" } });
      saveConfig({ live: { idleMinutes: 9 } });
      expect(loadConfig().live).toMatchObject({ key: "sk-keep", idleMinutes: 9 });
    });

    it("keeps a key from the desktop credential store, which reaches the harness as env", () => {
      // the desktop leaves an empty tombstone in the file and hands the key over as env
      saveConfig({ live: { key: "" } });
      process.env.OMB_OPENAI_LIVE_KEY = "sk-from-keychain";
      saveConfig({ live: { readTypedReplies: false, voice: "cedar" } });
      expect(loadConfig().live).toEqual({ key: "sk-from-keychain", readTypedReplies: false, voice: "cedar" });
      expect(JSON.parse(readFileSync(path, "utf8")).live).toEqual({ key: "", readTypedReplies: false, voice: "cedar" });
    });
  });
});

describe("loadConfig with an unusable config.json", () => {
  const path = join(DATA_DIR, "config.json");
  let warn: ReturnType<typeof vi.spyOn>;
  beforeEach(() => {
    mkdirSync(DATA_DIR, { recursive: true });
    rmSync(path, { force: true });
    warn = vi.spyOn(console, "warn").mockImplementation(() => {});
    loadConfig(); // reset the once-per-problem memory with a clean run
    warn.mockClear();
  });
  afterEach(() => {
    warn.mockRestore();
    rmSync(path, { force: true });
  });

  it("stays quiet on a first run with no file", () => {
    expect(loadConfig().instances).toBeUndefined();
    expect(warn).not.toHaveBeenCalled();
  });

  it("names the file and the failing field when one schema error drops the whole file", () => {
    // threads without maxConcurrentPerBot fails the stored schema, which drops
    // every other section (here: the Claude instance) along with it.
    writeFileSync(path, JSON.stringify({
      instances: { claude: { driver: "claudeAgent", displayName: "Claude (work)" } },
      threads: { eventLogMaxBytes: 1_000_000 },
    }));
    expect(loadConfig().instances).toBeUndefined();
    expect(warn).toHaveBeenCalledTimes(1);
    const message = String(warn.mock.calls[0]?.[0]);
    expect(message).toContain(path);
    expect(message).toContain("maxConcurrentPerBot");
  });

  it("warns about invalid JSON too, and only once while the file stays broken", () => {
    writeFileSync(path, "{ not json");
    loadConfig();
    loadConfig();
    expect(warn).toHaveBeenCalledTimes(1);
    expect(String(warn.mock.calls[0]?.[0])).toContain("using defaults");
  });

  it("never logs credential fragments from a JSON parser error", () => {
    writeFileSync(path, "sk-fixture");
    loadConfig();
    expect(warn).toHaveBeenCalledTimes(1);
    expect(String(warn.mock.calls[0]?.[0])).toContain("invalid JSON");
    expect(String(warn.mock.calls[0]?.[0])).not.toContain("sk-fixture");
  });

  it("warns again after a repaired or removed file becomes broken", () => {
    for (const recovered of ["{}", null]) {
      writeFileSync(path, "{ not json");
      loadConfig();
      warn.mockClear();
      if (recovered === null) rmSync(path);
      else writeFileSync(path, recovered);
      loadConfig();
      expect(warn).not.toHaveBeenCalled();
      writeFileSync(path, "{ not json");
      loadConfig();
      expect(warn).toHaveBeenCalledTimes(1);
      warn.mockClear();
    }
  });
});

describe("a value derived from config.json", () => {
  const path = join(DATA_DIR, "config.json");
  const members = (config: AppConfig) => config.signIn?.members ?? [];
  /** Written a minute ago: a running server's file, not one mid-save. */
  const writeSettled = (signIn: AppConfig["signIn"], minutesAgo = 1) => {
    writeFileSync(path, JSON.stringify({ signIn }));
    const when = new Date(Date.now() - minutesAgo * 60_000);
    utimesSync(path, when, when);
  };
  beforeEach(() => { mkdirSync(DATA_DIR, { recursive: true }); rmSync(path, { force: true }); });
  afterEach(() => { rmSync(path, { force: true }); });

  it("reads config.json once while the file is unchanged", () => {
    writeSettled({ members: ["[email protected]"] });
    const derive = vi.fn(members);
    const read = cacheUntilConfigChanges(derive);
    for (let i = 0; i < 5; i++) expect(read()).toEqual(["[email protected]"]);
    expect(derive).toHaveBeenCalledTimes(1);
  });

  it("sees an outside edit on the next call, in place or renamed over the file", () => {
    writeSettled({ members: ["[email protected]", "[email protected]"] });
    const read = cacheUntilConfigChanges(members);
    expect(read()).toEqual(["[email protected]", "[email protected]"]);
    // In place, as a hand edit does: the size and the time change.
    writeSettled({ members: ["[email protected]"] }, 2);
    expect(read()).toEqual(["[email protected]"]);
    // Renamed over the file, as the CLI and the fleet agent write it: same size and time, only the file identity differs.
    const before = statSync(path);
    writeFileSync(`${path}.next`, JSON.stringify({ signIn: { members: ["[email protected]"] } }));
    utimesSync(`${path}.next`, before.atime, before.mtime);
    renameSync(`${path}.next`, path);
    expect(statSync(path).size).toBe(before.size);
    expect(read()).toEqual(["[email protected]"]);
  });

  it("sees this process's own save at once", () => {
    writeSettled({ members: ["[email protected]"] });
    const read = cacheUntilConfigChanges(members);
    expect(read()).toEqual(["[email protected]"]);
    saveConfig({ signIn: { members: [] } });
    expect(read()).toEqual([]);
  });

  it("reads a file saved in the last moments every time, so a second save in the same clock tick is not missed", () => {
    const tick = new Date();
    writeFileSync(path, JSON.stringify({ signIn: { members: ["[email protected]"] } }));
    utimesSync(path, tick, tick);
    const read = cacheUntilConfigChanges(members);
    expect(read()).toEqual(["[email protected]"]);
    // Same size, same file, same modification time: nothing in stat changed.
    writeFileSync(path, JSON.stringify({ signIn: { members: ["[email protected]"] } }));
    utimesSync(path, tick, tick);
    expect(read()).toEqual(["[email protected]"]);
  });

  it("never keeps the old value once the file is gone", () => {
    writeSettled({ members: ["[email protected]"] });
    const read = cacheUntilConfigChanges(members);
    expect(read()).toEqual(["[email protected]"]);
    rmSync(path);
    expect(read()).toEqual([]);
  });

  it("reads a file loadConfig() could not use on every call, never keeping its defaults", () => {
    const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
    try {
      writeFileSync(path, "{ not json");
      const when = new Date(Date.now() - 60_000);
      utimesSync(path, when, when);
      const derive = vi.fn(members);
      const read = cacheUntilConfigChanges(derive);
      expect(read()).toEqual([]);
      expect(read()).toEqual([]);
      expect(derive).toHaveBeenCalledTimes(2);
    } finally {
      warn.mockRestore();
    }
  });

  it.skipIf(process.platform === "win32")("sees a change of permissions or owner on the next call", () => {
    writeSettled({ members: ["[email protected]"] });
    const derive = vi.fn(members);
    const read = cacheUntilConfigChanges(derive);
    read();
    // Linux stamps ctime from a clock that ticks every 1 to 10 ms, so a chmod in the write's own tick keeps its ctime.
    Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
    // chmod and chown leave the size, the time and the file the same.
    chmodSync(path, 0o600);
    read();
    expect(derive).toHaveBeenCalledTimes(2);
  });

  it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
    "uses the list again once a file the server could not read is readable",
    () => {
      const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
      try {
        writeSettled({ members: ["[email protected]"] });
        chmodSync(path, 0o000);
        const read = cacheUntilConfigChanges(members);
        expect(read()).toEqual([]);
        expect(read()).toEqual([]);
        chmodSync(path, 0o600);
        expect(read()).toEqual(["[email protected]"]);
      } finally {
        chmodSync(path, 0o600);
        warn.mockRestore();
      }
    },
  );
});

describe("X research's included relay token", () => {
  it("still loads a config.json saved with the old own treg token, and keeps that token out of the config", () => {
    const path = join(DATA_DIR, "config.json");
    mkdirSync(DATA_DIR, { recursive: true });
    writeFileSync(path, JSON.stringify({ budgets: { monthlyUsd: 25, warnAtPercent: 70 }, treg: { token: "treg_old" } }));
    const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
    try {
      const loaded = loadConfig();
      expect(loaded.budgets).toEqual({ monthlyUsd: 25, warnAtPercent: 70 });
      expect(JSON.stringify(loaded)).not.toContain("treg_old");
      expect(warn.mock.calls.some(([line]) => String(line).includes("ignoring"))).toBe(false);
    } finally {
      warn.mockRestore();
      rmSync(path, { force: true });
    }
  });

  it("keeps the plan's research token and the person's own treg key as workspace credentials that never reach an engine", () => {
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_CLOUD_X_TOKEN");
    expect(WORKSPACE_CREDENTIAL_ENV).toContain("OMB_TREG_KEY");
  });
});

describe("DATA_DIR outside the suite's throwaway home", () => {
  function childDataDir(env: NodeJS.ProcessEnv): string {
    const script = "const m = await import(process.env.OMB_CONFIG_URL); process.stdout.write(m.DATA_DIR);";
    const { OMB_DATA_DIR: _dataDir, VITEST: _vitest, NODE_ENV: _nodeEnv, ...inherited } = process.env;
    return execFileSync(process.execPath, ["--experimental-strip-types", "--no-warnings", "--input-type=module", "-e", script], {
      env: { ...inherited, OMB_CONFIG_URL: pathToFileURL(join(import.meta.dirname, "config.ts")).href, ...env }, encoding: "utf8",
    });
  }

  it("never resolves to the person's real data folder when a test runs without the setup file", () => {
    const realHome = userInfo().homedir;
    const dataDir = childDataDir({ HOME: realHome, USERPROFILE: realHome, VITEST: "true" });
    try {
      expect(dataDir).not.toBe(join(realHome, ".openmausbot"));
      expect(dataDir.startsWith(tmpdir())).toBe(true);
    } finally {
      // Only ever a throwaway folder: a broken guard would hand back the real home here.
      const throwaway = dirname(dataDir);
      if (throwaway.startsWith(join(realpathSync(tmpdir()), "omb-test-home-")) || throwaway.startsWith(join(tmpdir(), "omb-test-home-"))) {
        rmSync(throwaway, { recursive: true, force: true });
      }
    }
  });

  it("treats an empty OMB_DATA_DIR as unset", () => {
    const home = mkdtempSync(join(tmpdir(), "omb-config-home-"));
    try {
      expect(childDataDir({ HOME: home, USERPROFILE: home, OMB_DATA_DIR: "" })).toBe(join(home, ".openmausbot"));
    } finally {
      rmSync(home, { recursive: true, force: true });
    }
  });

  it("keeps the home folder outside tests", () => {
    const home = mkdtempSync(join(tmpdir(), "omb-config-home-"));
    try {
      expect(childDataDir({ HOME: home, USERPROFILE: home })).toBe(join(home, ".openmausbot"));
    } finally {
      rmSync(home, { recursive: true, force: true });
    }
  });
});

describe("a key the desktop app hands the server as an environment variable", () => {
  afterEach(() => vi.unstubAllEnvs());

  it.each([
    ["OMB_OPENAI_API_KEY", "openai", "OMB_OPENAI_API_KEY"],
    ["OMB_ANTHROPIC_API_KEY", "claudeApi", "ANTHROPIC_API_KEY"],
    ["OMB_OPENROUTER_API_KEY", "openrouter", "OMB_OPENROUTER_API_KEY"],
    ["MISTRAL_API_KEY", "mistral", "MISTRAL_API_KEY"],
    ["CEREBRAS_API_KEY", "cerebras", "CEREBRAS_API_KEY"],
    ["OMB_GROQ_API_KEY", "groq", "GROQ_API_KEY"],
    ["OMB_GREENFERENCE_API_KEY", "greenference", "GREENFERENCE_API_KEY"],
    ["OMB_META_API_KEY", "meta", "META_API_KEY"],
    ["OMB_DEEPSEEK_API_KEY", "deepseek", "DEEPSEEK_API_KEY"],
    ["OPENAI_COMPAT_API_KEY", "openaiCompat", "OPENAI_COMPAT_API_KEY"],
  ])("reaches its engine: %s", (variable, instanceId, engineEnv) => {
    vi.stubEnv(variable, `fixture-${variable}`);
    expect(instanceConfigs(loadConfig())[instanceId]?.environment?.[engineEnv]).toBe(`fixture-${variable}`);
  });
});