Skip to content

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

110 KB
Raw Blame
import { afterAll, afterEach, beforeAll, expect, mock, test } from "bun:test";
import { GlobalRegistrator } from "@happy-dom/global-registrator";
import {
  act,
  cleanup,
  fireEvent,
  render,
  waitFor,
  within,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { StrictMode } from "react";

type Invoke = (command: string, args?: unknown) => Promise<unknown>;
type ProgressEvent = {
  step: string;
  ok: boolean;
  detail: string;
  running?: boolean;
  downloadBytes?: number;
};
type Deferred<T> = {
  promise: Promise<T>;
  resolve: (value: T) => void;
  reject: (error: unknown) => void;
};

let invokeCalls: Array<{ command: string; args?: unknown }> = [];
let invokeHandler: Invoke = async () => {
  throw new Error("invoke handler was not installed");
};
const progressListeners = new Set<
  (event: { payload: ProgressEvent }) => void
>();

mock.module("@tauri-apps/api/core", () => ({
  invoke: (command: string, args?: unknown) => {
    invokeCalls.push({ command, args });
    return invokeHandler(command, args);
  },
}));

mock.module("@tauri-apps/api/event", () => ({
  listen: async (
    name: string,
    listener: (event: { payload: ProgressEvent }) => void,
  ) => {
    if (name === "setup:progress") progressListeners.add(listener);
    return () => progressListeners.delete(listener);
  },
}));

mock.module("./Mark", () => ({
  Mark: ({ name }: { name: string }) => <span>{name}</span>,
}));

const { App } = await import("./App");

beforeAll(() => GlobalRegistrator.register());
afterEach(() => {
  invokeCalls = [];
  cleanup();
  progressListeners.clear();
});
afterAll(() => GlobalRegistrator.unregister());

async function renderApp(strictMode = false) {
  let view!: ReturnType<typeof render>;

  await act(async () => {
    view = render(
      strictMode ? (
        <StrictMode>
          <App />
        </StrictMode>
      ) : (
        <App />
      ),
    );
  });

  return view;
}

function setupEvents() {
  return invokeCalls
    .filter((call) => call.command === "record_setup_event")
    .map((call) => call.args);
}

async function enterInstallation(view: Awaited<ReturnType<typeof renderApp>>) {
  await userEvent.click(view.getByRole("button", { name: "Set up OpenBot" }));
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
}

async function completeInstallation(
  view: Awaited<ReturnType<typeof renderApp>>,
) {
  await userEvent.click(view.getByRole("button", { name: "Install OpenBot" }));
  await userEvent.click(
    await view.findByRole("button", { name: "Continue to sign in" }),
  );
}

function installationCalls() {
  return invokeCalls.filter((call) =>
    ["prepare_installation", "prepare_engine"].includes(call.command),
  );
}

async function beginPendingInstallation() {
  setupRootConfiguration("/tmp/install-progress", async () =>
    emptyConfiguration(),
  );
  let preparation = deferred<void>();
  const previous = invokeHandler;
  invokeHandler = async (command, args) =>
    command === "prepare_installation"
      ? preparation.promise
      : previous(command, args);
  const view = await renderApp();
  await enterInstallation(view);
  await userEvent.click(view.getByRole("button", { name: "Install OpenBot" }));
  return {
    view,
    preparation,
    retry: async () => {
      preparation = deferred<void>();
      await userEvent.click(
        view.getByRole("button", { name: "Retry installation" }),
      );
      return preparation;
    },
  };
}

async function emitProgress(payload: ProgressEvent) {
  await act(async () => {
    for (const listener of progressListeners) listener({ payload });
  });
}

test("installation shows its active stage immediately and advances elapsed time", async () => {
  const { view, preparation } = await beginPendingInstallation();
  const engine = within(
    view.getByRole("listitem", { name: "Container engine" }),
  );
  expect(engine.getByText("Checking the software OpenBot needs.")).toBeTruthy();
  expect(engine.getByText("In progress")).toBeTruthy();
  expect(engine.queryByText("✓")).toBeNull();
  await waitFor(() => expect(engine.getByText(/1s elapsed/)).toBeTruthy(), {
    timeout: 1800,
  });
  await act(async () => preparation.resolve());
  expect(engine.getByText("Complete")).toBeTruthy();
  expect(engine.getByText("✓")).toBeTruthy();
  expect(view.queryByText("In progress") === null).toBe(true);
  expect(view.queryByText(/elapsed/)).toBeNull();
});

test("running updates show real download bytes without completing a stage early", async () => {
  const { view, preparation } = await beginPendingInstallation();
  await emitProgress({
    step: "engine",
    ok: true,
    detail: "Docker is answering.",
  });
  await emitProgress({
    step: "dependencies",
    ok: true,
    running: true,
    detail: "Downloading the local runtime.",
    downloadBytes: 2_400_000,
  });
  const dependencies = within(
    view.getByRole("listitem", { name: "Dependencies" }),
  );
  expect(dependencies.getByText("In progress")).toBeTruthy();
  expect(dependencies.getByText(/2.4 MB downloaded/)).toBeTruthy();
  expect(dependencies.queryByText("✓")).toBeNull();
  await emitProgress({
    step: "dependencies",
    ok: true,
    running: true,
    detail: "Downloading the local runtime.",
    downloadBytes: 5_700_000,
  });
  expect(view.getAllByRole("listitem", { name: "Dependencies" })).toHaveLength(
    1,
  );
  expect(dependencies.getByText(/5.7 MB downloaded/)).toBeTruthy();
  expect(dependencies.queryByText(/2.4 MB downloaded/)).toBeNull();
  expect(dependencies.queryByText("Complete")).toBeNull();
  await emitProgress({
    step: "dependencies",
    ok: true,
    running: false,
    detail: "Local runtime installed.",
  });
  expect(dependencies.getByText("Complete")).toBeTruthy();
  expect(dependencies.getByText("✓")).toBeTruthy();
  expect(dependencies.queryByText("In progress")).toBeNull();
  await emitProgress({
    step: "images",
    ok: true,
    running: true,
    detail: "Downloading local software (2/4).",
  });
  expect(view.getByText("Downloading local software (2/4).")).toBeTruthy();
  await act(async () => preparation.resolve());
  expect(view.queryByText("In progress") === null).toBe(true);
});

test("installation failure stops active indicators and retry begins fresh", async () => {
  const { view, preparation, retry } = await beginPendingInstallation();
  await emitProgress({
    step: "engine",
    ok: true,
    running: false,
    detail: "Docker is answering.",
  });
  await emitProgress({
    step: "dependencies",
    ok: true,
    running: true,
    detail: "Installing the local runtime.",
  });
  await act(async () =>
    preparation.reject({ said: "The download was interrupted." }),
  );
  const dependencies = within(
    view.getByRole("listitem", { name: "Dependencies" }),
  );
  expect(dependencies.getByText("Failed")).toBeTruthy();
  expect(dependencies.getByText("✗")).toBeTruthy();
  expect(dependencies.getByText("The download was interrupted.")).toBeTruthy();
  expect(view.queryByText("In progress") === null).toBe(true);
  expect(view.queryByText(/elapsed/)).toBeNull();
  expect(
    within(view.getByRole("listitem", { name: "Container engine" })).getByText(
      "Complete",
    ),
  ).toBeTruthy();
  const secondAttempt = await retry();
  expect(view.queryByRole("listitem", { name: "Dependencies" })).toBeNull();
  expect(view.queryByText("Failed")).toBeNull();
  expect(view.getByText("In progress")).toBeTruthy();
  expect(view.queryByRole("alert")).toBeNull();
  await act(async () => secondAttempt.resolve());
  expect(view.queryByText("In progress") === null).toBe(true);
  expect(
    view.getByRole("heading", { name: "Installation complete" }),
  ).toBeTruthy();
});

test.each([
  [false, false],
  [true, false],
  [false, true],
  [true, true],
])(
  "startup settles active progress (automatic=%s, succeeds=%s)",
  async (automatic, succeeds) => {
    useInterruptedShutdownSetup();
    const launch = deferred<void>();
    const previous = invokeHandler;
    invokeHandler = async (command, args) => {
      if (command === "last_failure" && automatic) return null;
      if (command === "start_stack") return launch.promise;
      return previous(command, args);
    };
    const view = await renderApp();
    if (!automatic)
      await userEvent.click(
        view.getByRole("button", { name: "Start OpenBot" }),
      );
    await emitProgress({
      step: "services",
      ok: true,
      running: true,
      detail: "Starting local services.",
    });
    expect(view.getByText("In progress")).toBeTruthy();
    await act(async () => {
      if (succeeds) launch.resolve();
      else launch.reject({ said: "Local services could not start." });
    });
    expect(view.queryByText("In progress") === null).toBe(true);
    const services = within(view.getByRole("listitem", { name: "Containers" }));
    expect(services.getByText(succeeds ? "Complete" : "Failed")).toBeTruthy();
    expect(services.getByText(succeeds ? "✓" : "✗")).toBeTruthy();
    if (!succeeds)
      expect(
        view.getByRole("button", { name: "Start OpenBot" }),
      ).toHaveProperty("disabled", false);
  },
);

test("installation completes before either sign-in is available", async () => {
  setupRootConfiguration("/tmp/install-before-signin", async () =>
    emptyConfiguration(),
  );
  const preparation = deferred<void>();
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "prepare_installation") return preparation.promise;
    return previous(command, args);
  };
  const view = await renderApp();
  await enterInstallation(view);
  expect(view.getByRole("heading", { name: "Install OpenBot" })).toBeTruthy();
  expect(view.getByLabelText("Where OpenBot lives")).toHaveProperty(
    "value",
    "/tmp/install-before-signin",
  );
  expect(
    view.queryByRole("button", { name: /Sign in|Continue to sign in/i }),
  ).toBeNull();
  await userEvent.click(view.getByRole("button", { name: "Install OpenBot" }));
  expect(installationCalls()).toEqual([
    {
      command: "prepare_installation",
      args: {
        root: "/tmp/install-before-signin",
        harness: { id: "langgraph" },
      },
    },
  ]);
  expect(view.getByLabelText("Where OpenBot lives")).toHaveProperty(
    "disabled",
    true,
  );
  expect(view.getByRole("button", { name: "Back" })).toHaveProperty(
    "disabled",
    true,
  );
  expect(view.queryByRole("heading", { name: "Connect your AI" })).toBeNull();
  expect(
    invokeCalls.some((call) =>
      /sign_in|start_stack|^providers$/.test(call.command),
    ),
  ).toBe(false);
  await act(async () => {
    for (const listener of progressListeners)
      listener({
        payload: {
          step: "dependencies",
          ok: true,
          detail: "Local runtime installed.",
        },
      });
  });
  expect(view.getByText("Local runtime installed.")).toBeTruthy();
  expect(
    view.queryByRole("button", { name: "Continue to sign in" }),
  ).toBeNull();
  await act(async () => preparation.resolve());
  expect(
    view.getByRole("heading", { name: "Installation complete" }),
  ).toBeTruthy();
  expect(
    view
      .getByRole("button", { name: "Repair installation" })
      .closest("details"),
  ).toHaveProperty("open", false);
  expect(view.queryByRole("heading", { name: "Connect your AI" })).toBeNull();
  await userEvent.click(
    view.getByRole("button", { name: "Continue to sign in" }),
  );
  expect(view.getByRole("heading", { name: "Connect your AI" })).toBeTruthy();
});

test("CopilotKit Back preserves the selected AI and connection settings", async () => {
  setupRootConfiguration("/tmp/back-preserves-setup", async () =>
    emptyConfiguration(),
  );
  const view = await renderApp();
  const user = userEvent.setup({ document: view.container.ownerDocument });
  await enterInstallation(view);
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  await userEvent.type(view.getByLabelText("OpenAI API key"), "model-test-key");
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(
    view.getByText("Point at your own Intelligence server"),
  );
  await userEvent.type(view.getByLabelText("Project key"), "project-test-key");
  const api = view.getByLabelText("API URL");
  await user.clear(api);
  await user.type(api, "https://intelligence.example/api");
  const gateway = view.getByLabelText("Gateway WebSocket URL");
  await user.clear(gateway);
  await user.type(gateway, "wss://intelligence.example/ws");
  await userEvent.click(view.getByRole("button", { name: "Back" }));
  expect(view.getByRole("heading", { name: "Connect your AI" })).toBeTruthy();
  expect(view.getByRole("radio", { name: /OpenAI/ })).toHaveProperty(
    "checked",
    true,
  );
  expect(view.getByLabelText("OpenAI API key")).toHaveProperty(
    "value",
    "model-test-key",
  );
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  expect(
    view.getByRole("heading", { name: "Connect to CopilotKit" }),
  ).toBeTruthy();
  await userEvent.click(
    view.getByText("Point at your own Intelligence server"),
  );
  expect(view.getByLabelText("Project key")).toHaveProperty(
    "value",
    "project-test-key",
  );
  expect(view.getByLabelText("API URL")).toHaveProperty(
    "value",
    "https://intelligence.example/api",
  );
  expect(view.getByLabelText("Gateway WebSocket URL")).toHaveProperty(
    "value",
    "wss://intelligence.example/ws",
  );
  expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
    "disabled",
    false,
  );
  expect(
    view.queryByRole("button", { name: "Change AI connection" }) === null,
  ).toBe(true);
  expect(
    view
      .getByRole("button", { name: "Change installation" })
      .closest("details"),
  ).toHaveProperty("open", false);
  expect(installationCalls()).toHaveLength(1);
});

test("failed installation blocks sign-in and retries before reporting completion", async () => {
  setupRootConfiguration("/tmp/install-retry", async () =>
    emptyConfiguration(),
  );
  const previous = invokeHandler;
  let attempts = 0;
  invokeHandler = async (command, args) => {
    if (command === "prepare_installation") {
      if (++attempts === 1)
        throw {
          said: "The download was interrupted.",
          detail: "Synthetic network failure",
        };
      return null;
    }
    return previous(command, args);
  };
  const view = await renderApp();
  await enterInstallation(view);
  await userEvent.click(view.getByRole("button", { name: "Install OpenBot" }));
  expect((await view.findByRole("alert")).textContent).toContain(
    "The download was interrupted.",
  );
  expect(
    view.queryByRole("button", { name: "Continue to sign in" }),
  ).toBeNull();
  expect(
    invokeCalls.some((call) => /sign_in|^providers$/.test(call.command)),
  ).toBe(false);
  await userEvent.click(
    view.getByRole("button", { name: "Retry installation" }),
  );
  expect(
    await view.findByRole("heading", { name: "Installation complete" }),
  ).toBeTruthy();
  expect(view.queryByRole("alert")).toBeNull();
  expect(attempts).toBe(2);
});

test("provider sign-in retries and Back reuse the completed local installation", async () => {
  setupRootConfiguration("/tmp/provider-retry", async () =>
    emptyConfiguration(),
  );
  const previous = invokeHandler;
  let signIns = 0;
  invokeHandler = async (command, args) => {
    if (command === "prepare_installation") return null;
    if (command === "providers")
      return [
        {
          id: "openai",
          name: "OpenAI",
          summary: "Use OpenAI",
          logins: ["plan"],
          mark: null,
          caution: null,
        },
      ];
    if (command === "begin_chatgpt_sign_in") {
      if (++signIns === 1) throw { said: "Sign-in was interrupted." };
      return "https://sign-in.example";
    }
    if (command === "finish_chatgpt_sign_in") return "synthetic-token";
    return previous(command, args);
  };
  const view = await renderApp();
  await enterInstallation(view);
  await completeInstallation(view);
  await userEvent.click(view.getByRole("button", { name: "Back" }));
  expect(
    view.getByRole("heading", { name: "Installation complete" }),
  ).toBeTruthy();
  await userEvent.click(
    view.getByRole("button", { name: "Continue to sign in" }),
  );
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  await userEvent.click(
    view.getByRole("button", { name: "Sign in with OpenAI" }),
  );
  await view.findByText("Sign-in was interrupted.");
  await userEvent.click(
    view.getByRole("button", { name: "Sign in with OpenAI" }),
  );
  await view.findByText(/Signed in to OpenAI/);
  expect(signIns).toBe(2);
  expect(installationCalls()).toHaveLength(1);
});

test("a completed installation can be repaired after a provider reports missing assets", async () => {
  setupRootConfiguration("/tmp/repair-installation", async () =>
    emptyConfiguration(),
  );
  const previous = invokeHandler;
  const repair = deferred<void>();
  let installations = 0;
  invokeHandler = async (command, args) => {
    if (command === "prepare_installation")
      return ++installations === 1 ? null : repair.promise;
    if (command === "providers")
      return [
        {
          id: "openai",
          name: "OpenAI",
          summary: "Use OpenAI",
          logins: ["plan"],
          mark: null,
          caution: null,
        },
      ];
    if (command === "begin_chatgpt_sign_in")
      throw { said: "Return to Install and try again." };
    return previous(command, args);
  };
  const view = await renderApp();
  await enterInstallation(view);
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  await userEvent.click(
    view.getByRole("button", { name: "Sign in with OpenAI" }),
  );
  await view.findByText("Return to Install and try again.");
  await userEvent.click(view.getByRole("button", { name: "Back" }));
  await userEvent.click(view.getByText("Installation options"));
  await userEvent.click(
    view.getByRole("button", { name: "Repair installation" }),
  );
  expect(
    view.queryByRole("button", { name: "Continue to sign in" }),
  ).toBeNull();
  await act(async () => repair.resolve());
  expect(
    await view.findByRole("button", { name: "Continue to sign in" }),
  ).toBeTruthy();
  expect(installations).toBe(2);
});

test("changing the Bot invalidates its completed installation", async () => {
  useBringYourOwnHarnessSetup();
  const view = await renderApp();
  await enterInstallation(view);
  await completeInstallation(view);
  await userEvent.click(view.getByRole("button", { name: "Back" }));
  await userEvent.click(view.getByRole("button", { name: "Back" }));
  await userEvent.click(view.getByText("Choose the agent framework"));
  await userEvent.click(
    view.getByRole("radio", { name: /An agent you already run/ }),
  );
  await userEvent.type(
    view.getByLabelText("AG-UI endpoint"),
    "https://agent.example/ag-ui",
  );
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  expect(
    view.queryByRole("button", { name: "Continue to sign in" }),
  ).toBeNull();
  await completeInstallation(view);
  expect(installationCalls()).toEqual([
    {
      command: "prepare_installation",
      args: { root: "/tmp/openbot-app-test", harness: { id: "langgraph" } },
    },
    {
      command: "prepare_installation",
      args: {
        root: "/tmp/openbot-app-test",
        harness: { id: "byo-url", agentUrl: "https://agent.example/ag-ui" },
      },
    },
  ]);
});

test.each([false, true])(
  "reopening a successful setup starts its saved root and connections without the wizard or Ask (Strict Mode=%s)",
  async (strictMode) => {
    const root = "/tmp/successful-setup-root";
    setupRootConfiguration("/tmp/default-root", async () => ({
      values: {
        INTELLIGENCE_API_URL: "https://own.example/api",
        INTELLIGENCE_GATEWAY_WS_URL: "wss://own.example/ws",
      },
      saved: { ...savedOpenAiConfiguration().saved, model: "open-ai-api-key" },
      launch: { harness: { id: "mastra" } },
    }));
    const launch = deferred<void>();
    const previous = invokeHandler;
    invokeHandler = async (command, args) => {
      if (command === "selected_root") return root;
      if (command === "start_stack") return launch.promise;
      if (command === "show_openbot") return null;
      return previous(command, args);
    };
    const view = await renderApp(strictMode);
    expect(
      view.getByRole("heading", { name: "Starting OpenBot" }),
    ).toBeTruthy();
    expect(
      view.queryByRole("heading", { name: "Connect to CopilotKit" }),
    ).toBeNull();
    expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
    await act(async () => launch.resolve());
    await waitFor(() =>
      expect(invokeCalls).toContainEqual({ command: "show_openbot" }),
    );
    expect(getStartStackPayload()).toEqual({
      root,
      apiKey: "",
      apiUrl: "https://own.example/api",
      gatewayWsUrl: "wss://own.example/ws",
      harness: { id: "mastra" },
      model: { provider: "openai", login: "api-key", saved: true },
    });
    expect(
      invokeCalls.filter((call) => call.command === "start_stack"),
    ).toHaveLength(1);
    expect(
      invokeCalls.some((call) =>
        /prepare_|sign_in|^providers$|^harnesses$|ask_the_bot/.test(
          call.command,
        ),
      ),
    ).toBe(false);
    expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
    expect(view.queryByRole("button", { name: "Ask" })).toBeNull();
  },
);

test("a failed automatic reopen offers recovery without retrying or installing", async () => {
  setupRootConfiguration("/tmp/reopen-failure", async () => ({
    ...savedOpenAiConfiguration(),
    saved: { ...savedOpenAiConfiguration().saved, model: "open-ai-api-key" },
    launch: { harness: { id: "langgraph" } },
  }));
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "start_stack")
      throw { said: "Return to Install and try again." };
    return previous(command, args);
  };
  const view = await renderApp();
  expect((await view.findByRole("alert")).textContent).toContain(
    "Return to Install and try again.",
  );
  expect(
    invokeCalls.filter((call) => call.command === "start_stack"),
  ).toHaveLength(1);
  expect(installationCalls()).toHaveLength(0);
  expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
    "disabled",
    false,
  );
  await userEvent.click(
    view.getByRole("button", { name: "Change installation" }),
  );
  expect(view.getByRole("button", { name: "Install OpenBot" })).toBeTruthy();
});

test("menu Stop retains the installed root for explicit Start without the wizard", async () => {
  const root = "/tmp/menu-stopped-installation";
  setupRootConfiguration(root, async () => ({
    ...savedOpenAiConfiguration(),
    saved: { ...savedOpenAiConfiguration().saved, model: "open-ai-api-key" },
    launch: { harness: { id: "mastra" } },
    autoStart: false,
  }));
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "show_openbot") return null;
    return previous(command, args);
  };
  const view = await renderApp();
  expect(
    view.getByRole("heading", { name: "OpenBot is stopped" }),
  ).toBeTruthy();
  expect(invokeCalls.some((call) => call.command === "start_stack")).toBe(
    false,
  );
  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));
  expect(getStartStackPayload()).toMatchObject({
    root,
    harness: { id: "mastra" },
  });
  expect(invokeCalls).toContainEqual({ command: "show_openbot" });
  expect(installationCalls()).toHaveLength(0);
  expect(view.queryByRole("button", { name: "Ask" })).toBeNull();
});

test("completed local installation resumes at sign-in when it has never launched", async () => {
  setupRootConfiguration("/tmp/installed-before-signin", async () => ({
    ...emptyConfiguration(),
    installation: { harness: { id: "mastra" } },
  }));
  const view = await renderApp();
  expect(view.getByRole("heading", { name: "Connect your AI" })).toBeTruthy();
  expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
  expect(installationCalls()).toHaveLength(0);
  expect(invokeCalls.some((call) => call.command === "start_stack")).toBe(
    false,
  );
});

test.each(["model", "intelligence"])(
  "a runtime %s authentication notification focuses refresh without adopting the running app",
  async (connection) => {
    useInterruptedShutdownSetup();
    const previous = invokeHandler;
    invokeHandler = async (command, args) => {
      if (command === "last_failure")
        return { said: "Connection was refused.", connection };
      if (command === "already_running") return true;
      return previous(command, args);
    };
    const view = await renderApp();
    expect(
      view.getByRole("heading", {
        name:
          connection === "model"
            ? "Refresh your AI connection"
            : "Refresh your CopilotKit connection",
      }),
    ).toBeTruthy();
    expect(
      invokeCalls.some((call) =>
        ["start_stack", "show_openbot", "already_running"].includes(
          call.command,
        ),
      ),
    ).toBe(false);
    expect(installationCalls()).toHaveLength(0);
    expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
  },
);

test.each(["reopen", "runtime"])(
  "organization sign-in on %s retains the installation and opens its app after verification",
  async (source) => {
    const root = "/tmp/organization-installation";
    const authorityUrl = "https://company.example";
    setupRootConfiguration(root, async () => ({
      ...savedOpenAiConfiguration(),
      values: { OPENBOT_ORGANIZATION_AUTH_URL: authorityUrl },
      saved: { ...savedOpenAiConfiguration().saved, model: "open-ai-api-key" },
      launch: { harness: { id: "mastra" } },
    }));
    const previous = invokeHandler;
    let opens = 0;
    invokeHandler = async (command, args) => {
      if (command === "last_failure" && source === "runtime")
        return {
          said: "Your organization sign-in needs to be refreshed.",
          connection: "organization",
        };
      if (command === "show_openbot") {
        if (source === "reopen" && opens++ === 0)
          throw {
            said: "Sign in to your organization again.",
            connection: "organization",
          };
        return null;
      }
      if (command === "begin_organization_sign_in")
        return "https://company.example/api/auth/desktop";
      if (command === "finish_organization_sign_in")
        return {
          id: "employee",
          email: "[email protected]",
          name: "Employee",
          role: "user",
        };
      if (command === "cancel_organization_sign_in") return null;
      return previous(command, args);
    };
    const view = await renderApp();
    expect(
      view.getByRole("heading", { name: "Sign in to your organization" }),
    ).toBeTruthy();
    expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
    expect(view.queryByText(/Step \d of 4/)).toBeNull();
    await userEvent.click(
      view.getByRole("button", { name: "Continue with Google" }),
    );
    expect(invokeCalls).toContainEqual({
      command: "begin_organization_sign_in",
      args: { root, authorityUrl, provider: "google" },
    });
    expect(invokeCalls).toContainEqual({
      command: "finish_organization_sign_in",
      args: { root },
    });
    expect(
      invokeCalls.filter((call) => call.command === "show_openbot"),
    ).toHaveLength(source === "reopen" ? 2 : 1);
    expect(
      invokeCalls.filter((call) => call.command === "start_stack"),
    ).toHaveLength(source === "reopen" ? 1 : 0);
    if (source === "reopen") {
      expect(
        invokeCalls.find((call) => call.command === "start_stack")?.args,
      ).toMatchObject({ root, organizationAuthUrl: authorityUrl });
    }
    expect(installationCalls()).toHaveLength(0);
    expect(view.queryByRole("button", { name: "Ask" })).toBeNull();
  },
);

test.each(["model", "intelligence"])(
  "an unavailable saved %s connection opens only its refresh screen and returns to the existing app",
  async (connection) => {
    const root = "/tmp/installed-refresh";
    setupRootConfiguration(root, async () => ({
      ...savedOpenAiConfiguration(),
      saved: { ...savedOpenAiConfiguration().saved, model: "open-ai-api-key" },
      launch: { harness: { id: "mastra" } },
    }));
    const previous = invokeHandler;
    let starts = 0;
    invokeHandler = async (command, args) => {
      if (command === "start_stack" && starts++ === 0)
        throw {
          said:
            connection === "model"
              ? "That saved OpenAI API key is no longer available."
              : "That saved CopilotKit connection is no longer available. Sign in again or enter a project key.",
          connection,
        };
      if (command === "show_openbot") return null;
      if (command === "begin_intelligence_sign_in")
        return "https://signin.example/fixture";
      if (command === "finish_intelligence_sign_in")
        return [{ id: "fixture", name: "Existing project" }];
      if (command === "intelligence_key_for")
        return "refreshed-intelligence-key";
      return previous(command, args);
    };
    const view = await renderApp();
    expect(
      view.getByRole("heading", {
        name:
          connection === "model"
            ? "Refresh your AI connection"
            : "Refresh your CopilotKit connection",
      }),
    ).toBeTruthy();
    expect(view.getByRole("alert").textContent).toContain(
      "no longer available",
    );
    expect(view.queryByText(/Step \d of 4/)).toBeNull();
    expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
    if (connection === "model") {
      await userEvent.type(
        view.getByLabelText("OpenAI API key"),
        "refreshed-provider-key",
      );
      await userEvent.click(view.getByRole("button", { name: "Continue" }));
    } else {
      await userEvent.click(
        view.getByRole("button", { name: "Sign in to CopilotKit again" }),
      );
      await userEvent.click(
        await view.findByRole("button", { name: "Existing project" }),
      );
      await userEvent.click(
        view.getByRole("button", { name: "Start OpenBot" }),
      );
    }
    const startsMade = invokeCalls.filter(
      (call) => call.command === "start_stack",
    );
    expect(startsMade).toHaveLength(2);
    expect(startsMade[1].args).toMatchObject({
      root,
      harness: { id: "mastra" },
      ...(connection === "model"
        ? { model: { apiKey: "refreshed-provider-key" } }
        : { apiKey: "refreshed-intelligence-key" }),
    });
    expect(invokeCalls).toContainEqual({ command: "show_openbot" });
    expect(installationCalls()).toHaveLength(0);
    expect(view.queryByRole("button", { name: "Ask" })).toBeNull();
  },
);

test("reopening a retained root waits for its saved setup without flashing the wizard", async () => {
  const root = "/tmp/reopen-pending-configuration";
  const configuration = deferred<ReturnType<typeof savedOpenAiConfiguration>>();
  setupRootConfiguration(root, async () => configuration.promise);
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "selected_root") return root;
    return previous(command, args);
  };
  const view = await renderApp();
  expect(view.getByRole("heading", { name: "Opening OpenBot" })).toBeTruthy();
  expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
  await act(async () => configuration.resolve(savedOpenAiConfiguration()));
  // A retained folder with no successful launch still needs the setup screens.
  expect(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  ).toBeTruthy();
  expect(invokeCalls.some((call) => call.command === "start_stack")).toBe(
    false,
  );
});

function useInterruptedShutdownSetup() {
  const root = "/tmp/interrupted-shutdown-root";
  const notice = "OpenBot had trouble shutting down last time.";
  setupRootConfiguration("/tmp/default-root", async () => ({
    values: {
      INTELLIGENCE_API_URL: "https://own.example/api",
      INTELLIGENCE_GATEWAY_WS_URL: "wss://own.example/ws",
    },
    saved: { ...savedOpenAiConfiguration().saved, model: "open-ai-api-key" },
    launch: { harness: { id: "mastra" } },
  }));
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "selected_root") return root;
    if (command === "last_failure") return { said: notice };
    if (command === "show_openbot") return null;
    return previous(command, args);
  };
  return { root, notice };
}

test("an interrupted shutdown restores saved setup for explicit recovery", async () => {
  const { root, notice } = useInterruptedShutdownSetup();
  const view = await renderApp();
  expect(view.getByRole("alert").textContent).toContain(notice);
  expect(view.queryByRole("button", { name: "Set up OpenBot" }) !== null).toBe(
    false,
  );
  expect(
    view.getByRole("heading", { name: "OpenBot is stopped" }),
  ).toBeTruthy();
  const start = view.getByRole("button", { name: "Start OpenBot" });
  expect(start).toHaveProperty("disabled", false);
  expect(invokeCalls.some((call) => call.command === "start_stack")).toBe(
    false,
  );

  await userEvent.click(start);

  expect(getStartStackPayload()).toEqual({
    root,
    apiKey: "",
    apiUrl: "https://own.example/api",
    gatewayWsUrl: "wss://own.example/ws",
    harness: { id: "mastra" },
    model: { provider: "openai", login: "api-key", saved: true },
  });
  expect(
    invokeCalls.filter((call) => call.command === "start_stack"),
  ).toHaveLength(1);
  expect(
    invokeCalls.some((call) =>
      /prepare_|sign_in|^providers$|^harnesses$/.test(call.command),
    ),
  ).toBe(false);
  expect(view.queryByText(notice)).toBeNull();
  expect(invokeCalls).toContainEqual({ command: "show_openbot" });
  expect(view.queryByRole("button", { name: "Ask" }) !== null).toBe(false);
});

test.each(["model", "root", "Intelligence"])(
  "changing the saved %s during recovery only repeats Ask for a different installation",
  async (changed) => {
    useInterruptedShutdownSetup();
    const view = await renderApp();
    const user = userEvent.setup({ document: view.container.ownerDocument });
    if (changed === "model") {
      await user.click(
        view.getByRole("button", { name: "Change AI connection" }),
      );
      await user.type(view.getByLabelText("OpenAI API key"), "new-model-key");
      await user.click(view.getByRole("button", { name: "Continue" }));
    } else if (changed === "root") {
      await user.click(
        view.getByRole("button", { name: "Change installation" }),
      );
      const input = view.getByLabelText("Where OpenBot lives");
      await user.clear(input);
      await user.type(input, "/tmp/changed-recovery-root");
      await completeInstallation(view);
      await user.click(view.getByRole("button", { name: "Continue" }));
    } else {
      await user.click(
        view.getByRole("button", { name: "Change CopilotKit connection" }),
      );
      await user.click(view.getByText("Point at your own Intelligence server"));
      const input = view.getByLabelText("API URL");
      await user.clear(input);
      await user.type(input, "https://different.example/api");
    }

    if (changed !== "model")
      await user.click(view.getByRole("button", { name: "Start OpenBot" }));

    expect(getStartStackPayload()).toMatchObject(
      changed === "root"
        ? { root: "/tmp/changed-recovery-root" }
        : changed === "model"
          ? { model: { apiKey: "new-model-key" } }
          : { apiUrl: "https://different.example/api" },
    );
    expect(invokeCalls.some((call) => call.command === "show_openbot")).toBe(
      changed !== "root",
    );
    expect(view.queryByRole("button", { name: "Ask" }) !== null).toBe(
      changed === "root",
    );
  },
);

test.each(["supervisor", "windows"])(
  "automatic reopen respects the existing %s blocker",
  async (blocker) => {
    setupRootConfiguration("/tmp/reopen-blocked", async () => ({
      ...savedOpenAiConfiguration(),
      saved: { ...savedOpenAiConfiguration().saved, model: "open-ai-api-key" },
      launch: { harness: { id: "langgraph" } },
    }));
    const previous = invokeHandler;
    invokeHandler = async (command, args) => {
      if (blocker === "supervisor" && command === "last_failure")
        return { said: "The supervisor stopped retrying." };
      if (blocker === "windows" && command === "windows_blocker")
        return "wsl-absent";
      if (command === "windows_blocker_instruction")
        return "Install WSL before continuing.";
      return previous(command, args);
    };
    const view = await renderApp();
    await waitFor(() =>
      expect(
        invokeCalls.some((call) => call.command === "already_running"),
      ).toBe(true),
    );
    expect(invokeCalls.some((call) => call.command === "start_stack")).toBe(
      false,
    );
    expect(installationCalls()).toHaveLength(0);
    expect(
      view.getByText(
        blocker === "supervisor"
          ? "The supervisor stopped retrying."
          : "Install WSL before continuing.",
      ),
    ).toBeTruthy();
  },
);

test("setup records telemetry without a consent gate and deduplicates viewed steps", async () => {
  setupRootConfiguration("/tmp/private-setup-root", async () =>
    emptyConfiguration(),
  );
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "record_setup_event") return null;
    return previous(command, args);
  };
  let view!: ReturnType<typeof render>;
  await act(async () => {
    view = render(
      <StrictMode>
        <App />
      </StrictMode>,
    );
  });

  expect(view.queryByRole("checkbox")).toBeNull();
  expect(view.queryByRole("switch")).toBeNull();
  expect(setupEvents()).toEqual([
    { event: { kind: "step_viewed", step: "welcome" } },
  ]);
  await userEvent.click(view.getByRole("button", { name: "Set up OpenBot" }));
  await userEvent.click(view.getByRole("button", { name: "Continue" }));

  expect(setupEvents()).toEqual([
    { event: { kind: "step_viewed", step: "welcome" } },
    { event: { kind: "step_viewed", step: "harness" } },
    { event: { kind: "harness_chosen", harness: "langgraph" } },
    { event: { kind: "step_viewed", step: "install" } },
  ]);
  await userEvent.click(view.getByRole("button", { name: "Back" }));
  expect(setupEvents().at(-1)).toEqual({
    event: { kind: "step_viewed", step: "harness" },
  });
});

test("setup records only model categories and reaches Ask when telemetry is unavailable", async () => {
  useCompatibleEndpointSetup({});
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "record_setup_event") throw new Error("offline");
    return previous(command, args);
  };
  const privateUrl = "https://private-model.example/v1";
  const privateKey = "synthetic-secret-endpoint-key";
  const view = await enterCompatibleEndpoint(privateUrl, privateKey);
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  expect(setupEvents()).toContainEqual({
    event: {
      kind: "model_chosen",
      provider: "compatible",
      credential_path: "api_key",
      custom_base_url: true,
    },
  });
  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));
  await view.findByRole("button", { name: "Ask" });
  expect(setupEvents().at(-1)).toEqual({
    event: { kind: "step_viewed", step: "ask" },
  });
  const serialized = JSON.stringify(setupEvents());
  for (const privateValue of [
    privateUrl,
    privateKey,
    "local-model",
    "/tmp/openbot-app-test",
  ]) {
    expect(serialized).not.toContain(privateValue);
  }
  expect(view.queryByText(/Nothing here leaves this computer/)).toBeNull();
});

type StartStackPayload = {
  root?: unknown;
  apiKey?: unknown;
  apiUrl?: unknown;
  gatewayWsUrl?: unknown;
  harness?: unknown;
  model: {
    provider?: unknown;
    login?: unknown;
    apiKey?: unknown;
    baseUrl?: unknown;
    containerBaseUrl?: unknown;
    model?: unknown;
    saved?: unknown;
  };
};

function isStartStackPayload(value: unknown): value is StartStackPayload {
  return (
    typeof value === "object" &&
    value !== null &&
    "model" in value &&
    typeof value.model === "object" &&
    value.model !== null
  );
}

function getStartStackPayload() {
  const args = invokeCalls.find((call) => call.command === "start_stack")?.args;
  if (!isStartStackPayload(args)) {
    throw new Error("start_stack payload was not captured");
  }
  return args;
}

function deferred<T>(): Deferred<T> {
  let resolve!: (value: T) => void;
  let reject!: (error: unknown) => void;
  const promise = new Promise<T>((resolvePromise, rejectPromise) => {
    resolve = resolvePromise;
    reject = rejectPromise;
  });
  return { promise, resolve, reject };
}

function savedOpenAiConfiguration() {
  return {
    values: {},
    saved: {
      intelligenceApiKey: true,
      modelApiKeys: { openai: true, anthropic: false },
      modelSessions: { openai: false, anthropic: false },
    },
  };
}

function emptyConfiguration() {
  return {
    values: {},
    saved: {
      intelligenceApiKey: false,
      modelApiKeys: { openai: false, anthropic: false },
      modelSessions: { openai: false, anthropic: false },
    },
  };
}

test("leftover database is offered as a fresh start that carries on into Start", async () => {
  useCompatibleEndpointSetup({});
  const previous = invokeHandler;
  const reset = deferred<void>();
  let starts = 0;
  invokeHandler = async (command, args) => {
    if (command === "start_stack" && ++starts === 1)
      throw {
        said: "A previous OpenBot database remains without its encryption key.",
        database_reset: "fixture_postgres-data",
        leftover_reset: ["fixture_postgres-data"],
      };
    if (command === "reset_leftover_database") return reset.promise;
    return previous(command, args);
  };
  const view = await enterCompatibleEndpoint(
    "https://model.example/v1",
    "synthetic-key",
  );
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));
  const resets = () =>
    invokeCalls.filter((call) => call.command === "reset_leftover_database");
  const startFresh = await view.findByRole("button", { name: "Start fresh" });
  // A choice, not a failure: nothing announces that Start did not finish.
  expect(view.queryByRole("alert")).toBeNull();
  expect(view.getByText(/saved chats, Bots, and settings/)).toBeTruthy();
  expect(view.getByText(/permanently deletes/)).toBeTruthy();
  expect(view.getByText(/with its original encryption key/)).toBeTruthy();
  expect(view.getByText("fixture_postgres-data")).toBeTruthy();
  expect(resets()).toHaveLength(0);
  await userEvent.click(startFresh);
  expect(resets()).toEqual([
    {
      command: "reset_leftover_database",
      args: {
        root: "/tmp/openbot-app-test",
        volumes: ["fixture_postgres-data"],
        containers: [],
        confirmed: true,
      },
    },
  ]);
  expect(view.getByRole("button", { name: "Starting fresh…" })).toHaveProperty(
    "disabled",
    true,
  );
  expect(view.getByRole("button", { name: "Working…" })).toHaveProperty(
    "disabled",
    true,
  );
  expect(starts).toBe(1);
  await act(async () => reset.resolve());
  await view.findByRole("button", { name: "Ask" });
  expect(starts).toBe(2);
  expect(installationCalls()).toHaveLength(1);
});

test("starting fresh names every container and volume and removes only that list", async () => {
  useCompatibleEndpointSetup({});
  const previous = invokeHandler;
  const volumes = [
    "openbot-profile-general-assistant",
    "openbot-workspace-general-assistant",
    "openbot_agent-profiles",
  ];
  const containers = [
    "openbot-agent-computer-1",
    "openbot-computer-general-assistant",
  ];
  invokeHandler = async (command, args) => {
    if (command === "start_stack")
      throw {
        said: "OpenBot found data from a previous installation.",
        leftover_reset: volumes,
        leftover_containers: containers,
      };
    if (command === "reset_leftover_database") return null;
    return previous(command, args);
  };
  const view = await enterCompatibleEndpoint(
    "https://model.example/v1",
    "synthetic-key",
  );
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));
  const startFresh = await view.findByRole("button", { name: "Start fresh" });
  expect(view.queryByText(/saved chats/)).toBeNull();
  expect(view.queryByText(/encryption key/)).toBeNull();
  expect(view.getByText(/browser sign-ins/)).toBeTruthy();
  expect(
    view.getByText(/Restore the previous installation’s folder/),
  ).toBeTruthy();
  for (const volume of volumes) expect(view.getByText(volume)).toBeTruthy();
  expect(view.getByText(/stopped containers/)).toBeTruthy();
  for (const container of containers)
    expect(view.getByText(container)).toBeTruthy();
  await userEvent.click(startFresh);
  expect(
    invokeCalls.filter((call) => call.command === "reset_leftover_database"),
  ).toEqual([
    {
      command: "reset_leftover_database",
      args: {
        root: "/tmp/openbot-app-test",
        volumes,
        containers,
        confirmed: true,
      },
    },
  ]);
});

test("a fresh start that cannot delete the leftovers is shown without starting", async () => {
  useCompatibleEndpointSetup({});
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "start_stack")
      throw {
        said: "A previous database remains.",
        database_reset: "fixture_postgres-data",
      };
    if (command === "reset_leftover_database")
      throw {
        said: "The database is still in use. Close the other OpenBot installation first.",
      };
    return previous(command, args);
  };
  const view = await enterCompatibleEndpoint(
    "https://model.example/v1",
    "synthetic-key",
  );
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));
  await userEvent.click(
    await view.findByRole("button", { name: "Start fresh" }),
  );
  expect(await view.findByText(/database is still in use/)).toBeTruthy();
  expect(view.getByRole("alert")).toBeTruthy();
  expect(
    invokeCalls.filter((call) => call.command === "start_stack"),
  ).toHaveLength(1);
  expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
    "disabled",
    false,
  );
});

function setupRootConfiguration(
  rootA: string,
  loadConfiguration: (root: string) => Promise<unknown>,
) {
  invokeHandler = async (command, args) => {
    if (command === "prepare_installation") return null;
    if (command === "detect_engine") {
      return {
        engine: "docker",
        responding: true,
        engine_socket: null,
        detail: "Docker is answering.",
      };
    }
    if (command === "default_root") return rootA;
    if (command === "selected_root") return null;
    if (command === "already_configured") {
      if (
        typeof args !== "object" ||
        args === null ||
        !("root" in args) ||
        typeof args.root !== "string"
      ) {
        throw new Error("already_configured requires a root");
      }
      return loadConfiguration(args.root);
    }
    if (command === "already_running") return false;
    if (command === "windows_blocker") return null;
    if (command === "last_failure") return null;
    if (command === "harnesses") {
      return [
        {
          id: "langgraph",
          name: "LangGraph",
          summary: "Default Bot",
          image: null,
          health_path: null,
          credential: "any-provider",
          maintainer: "first-party",
          mark: null,
          port: 8000,
        },
      ];
    }
    if (command === "providers") {
      return [
        {
          id: "openai",
          name: "OpenAI",
          summary: "Use OpenAI.",
          logins: ["api-key"],
          mark: null,
          caution: null,
        },
      ];
    }
    if (command === "start_stack") return null;
    throw new Error(`unexpected command ${command}`);
  };
}

test("Windows detection failure blocks setup and displays its diagnostic", async () => {
  setupRootConfiguration("/tmp/openbot-windows-detection-test", async () =>
    emptyConfiguration(),
  );
  const setupHandler = invokeHandler;
  const problem = {
    said: "OpenBot could not check Windows virtualization support.",
    detail: "powershell exited with 17: synthetic CIM access denied",
  };
  invokeHandler = async (command, args) => {
    if (command === "windows_blocker") throw problem;
    return setupHandler(command, args);
  };

  const view = await renderApp();
  const alert = await view.findByRole("alert");
  expect(alert.textContent).toContain(problem.said);
  await userEvent.click(view.getByText("Technical details"));
  expect(alert.textContent).toContain(problem.detail);
  expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
  expect(view.queryByRole("button", { name: "Start OpenBot" })).toBeNull();
  expect(
    view.queryByText(/firmware settings|wsl --install|wsl --update/),
  ).toBeNull();
  expect(
    invokeCalls.some((call) => call.command === "windows_blocker_instruction"),
  ).toBe(false);
});

test("a failed Windows blocker instruction is visible instead of an empty blocker", async () => {
  setupRootConfiguration("/tmp/openbot-windows-detection-test", async () =>
    emptyConfiguration(),
  );
  const setupHandler = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "windows_blocker") return "wsl-absent";
    if (command === "windows_blocker_instruction") {
      throw { said: "The blocker instruction could not be read." };
    }
    return setupHandler(command, args);
  };
  const view = await renderApp();
  expect((await view.findByRole("alert")).textContent).toContain(
    "The blocker instruction could not be read.",
  );
  expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
});

test("a successfully detected missing WSL feature keeps its setup instruction", async () => {
  setupRootConfiguration("/tmp/openbot-windows-detection-test", async () =>
    emptyConfiguration(),
  );
  const setupHandler = invokeHandler;
  const instruction =
    "Run wsl --install, restart Windows, and start OpenBot again.";
  invokeHandler = async (command, args) => {
    if (command === "windows_blocker") return "wsl-absent";
    if (command === "windows_blocker_instruction") return instruction;
    return setupHandler(command, args);
  };
  const view = await renderApp();
  expect(await view.findByText(instruction)).toBeTruthy();
  expect(view.queryByRole("alert")).toBeNull();
  expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
});

test("disabled Virtual Machine Platform displays its feature-specific fix and blocks setup", async () => {
  setupRootConfiguration("/tmp/openbot-vmp-detection-test", async () =>
    emptyConfiguration(),
  );
  const setupHandler = invokeHandler;
  const instruction =
    "Virtual Machine Platform is switched off. Open Windows Terminal or PowerShell as an administrator, run `dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart`, restart Windows, and start OpenBot again.";
  invokeHandler = async (command, args) => {
    if (command === "windows_blocker")
      return "virtual-machine-platform-disabled";
    if (command === "windows_blocker_instruction") {
      expect(args).toEqual({ blocker: "virtual-machine-platform-disabled" });
      return instruction;
    }
    return setupHandler(command, args);
  };
  const view = await renderApp();
  expect(await view.findByText(instruction)).toBeTruthy();
  expect(
    view.getByRole("heading", {
      name: "Virtual Machine Platform is switched off",
    }),
  ).toBeTruthy();
  expect(view.queryByRole("alert")).toBeNull();
  expect(view.queryByRole("button", { name: "Set up OpenBot" })).toBeNull();
  expect(view.queryByRole("button", { name: "Start OpenBot" })).toBeNull();
  expect(
    invokeCalls.some((call) =>
      ["prepare_installation", "prepare_engine", "start_stack"].includes(
        call.command,
      ),
    ),
  ).toBe(false);
});

type ExistingConfigurationValues = {
  INTELLIGENCE_API_KEY?: string;
  INTELLIGENCE_API_URL?: string;
  INTELLIGENCE_GATEWAY_WS_URL?: string;
  OPENAI_API_KEY?: string;
  ANTHROPIC_API_KEY?: string;
  OPENAI_BASE_URL?: string;
};

function useCompatibleEndpointSetup(
  existingValues: ExistingConfigurationValues,
) {
  invokeHandler = async (command) => {
    if (command === "prepare_installation") return null;
    if (command === "detect_engine") {
      return {
        engine: "docker",
        responding: true,
        engine_socket: null,
        detail: "Docker is answering.",
      };
    }
    if (command === "default_root") return "/tmp/openbot-app-test";
    if (command === "already_configured") {
      return {
        values: existingValues,
        saved: {
          intelligenceApiKey: true,
          modelApiKeys: { openai: true, anthropic: false },
          modelSessions: { openai: false, anthropic: false },
        },
      };
    }
    if (command === "already_running") return false;
    if (command === "windows_blocker") return null;
    if (command === "last_failure") return null;
    if (command === "harnesses") {
      return [
        {
          id: "langgraph",
          name: "LangGraph",
          summary: "Default Bot",
          image: null,
          health_path: null,
          credential: "any-provider",
          maintainer: "first-party",
          mark: null,
          port: 8000,
        },
      ];
    }
    if (command === "providers") {
      return [
        {
          id: "openai-compatible",
          name: "OpenAI-compatible",
          summary: "Use your own endpoint.",
          logins: ["endpoint"],
          mark: null,
          caution: null,
        },
      ];
    }
    if (command === "start_stack") return null;
    throw new Error(`unexpected command ${command}`);
  };
}

async function enterCompatibleEndpoint(
  baseUrl: string,
  endpointKey = "",
  containerBaseUrl = "",
) {
  const view = await renderApp();

  await userEvent.click(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  );
  await userEvent.click(await view.findByRole("button", { name: "Continue" }));
  await completeInstallation(view);
  await userEvent.click(
    await view.findByRole("radio", { name: /OpenAI-compatible/ }),
  );
  await userEvent.type(view.getByLabelText("Base URL"), baseUrl);
  if (containerBaseUrl) {
    await userEvent.type(
      view.getByLabelText("Container Base URL, if different"),
      containerBaseUrl,
    );
  }
  await userEvent.type(view.getByLabelText("Model name"), "local-model");
  if (endpointKey) {
    await userEvent.type(
      view.getByLabelText("API key, if the endpoint needs one"),
      endpointKey,
    );
  }
  return view;
}

async function startWithCompatibleEndpoint(
  endpointKey = "",
  baseUrl = "https://models.example/v1",
  containerBaseUrl = "",
) {
  const view = await enterCompatibleEndpoint(
    baseUrl,
    endpointKey,
    containerBaseUrl,
  );
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(
    await view.findByRole("button", { name: "Start OpenBot" }),
  );
}

function useSavedCompatibleEndpointSetup(
  baseUrl = "https://models.example/v1",
  model = "saved-model",
  keyed = true,
  savedModel = "compatible-endpoint",
  containerBaseUrl: string | undefined = undefined,
) {
  useCompatibleEndpointSetup({});
  const setupHandler = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "already_configured") {
      return {
        values: {
          OPENAI_BASE_URL: baseUrl,
          ...(containerBaseUrl === undefined
            ? {}
            : { OPENAI_CONTAINER_BASE_URL: containerBaseUrl }),
          BOT_MODEL: model,
        },
        saved: {
          intelligenceApiKey: true,
          model: savedModel,
          modelApiKeys: { compatible: keyed },
          modelSessions: {},
        },
      };
    }
    return setupHandler(command, args);
  };
}

test.each([true, false])(
  "saved compatible endpoint reaches Start with scoped public fields (keyed=%s)",
  async (keyed) => {
    useSavedCompatibleEndpointSetup(undefined, undefined, keyed);
    const view = await renderApp();
    await userEvent.click(view.getByRole("button", { name: "Set up OpenBot" }));
    await userEvent.click(view.getByRole("button", { name: "Continue" }));
    await completeInstallation(view);
    expect(view.getByLabelText("Base URL")).toHaveProperty(
      "value",
      "https://models.example/v1",
    );
    expect(view.getByLabelText("Model name")).toHaveProperty(
      "value",
      "saved-model",
    );
    expect(
      view.getByLabelText("API key, if the endpoint needs one"),
    ).toHaveProperty("value", "");
    await userEvent.click(view.getByRole("button", { name: "Continue" }));
    expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
      "disabled",
      false,
    );
    await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));
    expect(
      invokeCalls.filter((call) => call.command === "start_stack"),
    ).toHaveLength(1);
    expect(getStartStackPayload()).toEqual({
      root: "/tmp/openbot-app-test",
      apiKey: "",
      apiUrl: "https://api.intelligence.copilotkit.ai",
      gatewayWsUrl: "wss://realtime.intelligence.copilotkit.ai",
      harness: { id: "langgraph" },
      model: {
        provider: "openai-compatible",
        login: "endpoint",
        baseUrl: "https://models.example/v1",
        model: "saved-model",
        ...(keyed ? { saved: true } : {}),
      },
    });
  },
);

test.each([
  ["", "saved-model"],
  ["https://models.example/v1", ""],
  ["https://models.example/v1", "   "],
  ["ftp://models.example/v1", "saved-model"],
  ["https://", "saved-model"],
])(
  "incomplete saved endpoint stays on the provider screen (%s, %s)",
  async (baseUrl, model) => {
    useSavedCompatibleEndpointSetup(baseUrl, model);
    const view = await renderApp();
    await userEvent.click(view.getByRole("button", { name: "Set up OpenBot" }));
    await userEvent.click(view.getByRole("button", { name: "Continue" }));
    await completeInstallation(view);
    expect(view.getByRole("button", { name: "Continue" })).toHaveProperty(
      "disabled",
      true,
    );
    await userEvent.click(view.getByRole("button", { name: "Continue" }));
    expect(view.queryByRole("button", { name: "Start OpenBot" })).toBeNull();
    expect(invokeCalls.some((call) => call.command === "start_stack")).toBe(
      false,
    );
  },
);

test("an unsupported saved model kind does not become a startable endpoint", async () => {
  useSavedCompatibleEndpointSetup(
    undefined,
    undefined,
    true,
    "unsupported-model",
  );
  const view = await renderApp();
  await userEvent.click(view.getByRole("button", { name: "Set up OpenBot" }));
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await completeInstallation(view);
  expect(view.getByRole("button", { name: "Continue" })).toHaveProperty(
    "disabled",
    true,
  );
  expect(view.queryByRole("button", { name: "Start OpenBot" })).toBeNull();
  expect(invokeCalls.some((call) => call.command === "start_stack")).toBe(
    false,
  );
});

test("Change the model after an Ask failure stops the stack and reaches the provider picker", async () => {
  invokeHandler = async (command) => {
    if (command === "prepare_installation") return null;
    if (command === "detect_engine") {
      return {
        engine: "docker",
        responding: true,
        engine_socket: null,
        detail: "Docker is answering.",
      };
    }
    if (command === "default_root") return "/tmp/openbot-app-test";
    if (command === "already_configured") {
      return {
        values: {
          INTELLIGENCE_API_KEY: "ck-test",
          OPENAI_API_KEY: "sk-test",
        },
        saved: {
          intelligenceApiKey: true,
          modelApiKeys: { openai: true, anthropic: false },
          modelSessions: { openai: false, anthropic: false },
        },
      };
    }
    if (command === "already_running") return false;
    if (command === "windows_blocker") return null;
    if (command === "last_failure") return null;
    if (command === "harnesses") {
      return [
        {
          id: "langgraph",
          name: "LangGraph",
          summary: "Default Bot",
          image: null,
          health_path: null,
          credential: "any-provider",
          maintainer: "first-party",
          mark: null,
          port: 8000,
        },
      ];
    }
    if (command === "providers") {
      return [
        {
          id: "openai",
          name: "OpenAI",
          summary: "Use OpenAI.",
          logins: ["api-key"],
          mark: null,
          caution: null,
        },
      ];
    }
    if (command === "start_stack") return null;
    if (command === "ask_the_bot") {
      throw { said: "The model could not answer.", detail: "401" };
    }
    if (command === "stop_stack") return null;
    throw new Error(`unexpected command ${command}`);
  };

  const view = await renderApp();

  await userEvent.click(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  );
  await userEvent.click(await view.findByRole("button", { name: "Continue" }));
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await waitFor(() =>
    expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
      "disabled",
      false,
    ),
  );

  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));
  await userEvent.click(await view.findByRole("button", { name: "Ask" }));
  await userEvent.click(
    await view.findByRole("button", { name: "Change the model" }),
  );

  await waitFor(() =>
    expect(invokeCalls.some((call) => call.command === "stop_stack")).toBe(
      true,
    ),
  );
  expect(view.getByRole("heading", { name: "Connect your AI" })).toBeTruthy();
  expect(view.getByRole("radio", { name: /OpenAI/ })).toBeTruthy();
  expect(view.queryByRole("button", { name: "Stop OpenBot" })).toBeNull();
});

test("empty Intelligence projects keep sign-in retryable while Start waits for a project key", async () => {
  let projectLists = 0;
  invokeHandler = async (command) => {
    if (command === "prepare_installation") return null;
    if (command === "detect_engine") {
      return {
        engine: "docker",
        responding: true,
        engine_socket: null,
        detail: "Docker is answering.",
      };
    }
    if (command === "default_root") return "/tmp/openbot-app-test";
    if (command === "already_configured") {
      return {
        values: {
          OPENAI_API_KEY: "sk-test",
        },
        saved: {
          intelligenceApiKey: false,
          modelApiKeys: { openai: true, anthropic: false },
          modelSessions: { openai: false, anthropic: false },
        },
      };
    }
    if (command === "already_running") return false;
    if (command === "windows_blocker") return null;
    if (command === "last_failure") return null;
    if (command === "harnesses") {
      return [
        {
          id: "langgraph",
          name: "LangGraph",
          summary: "Default Bot",
          image: null,
          health_path: null,
          credential: "any-provider",
          maintainer: "first-party",
          mark: null,
          port: 8000,
        },
      ];
    }
    if (command === "providers") {
      return [
        {
          id: "openai",
          name: "OpenAI",
          summary: "Use OpenAI.",
          logins: ["api-key"],
          mark: null,
          caution: null,
        },
      ];
    }
    if (command === "begin_intelligence_sign_in") {
      return "https://copilotkit.test/sign-in";
    }
    if (command === "finish_intelligence_sign_in") {
      projectLists += 1;
      if (projectLists === 1) return [];
      return [{ id: "project-1", name: "Project One" }];
    }
    throw new Error(`unexpected command ${command}`);
  };

  const view = await renderApp();

  await userEvent.click(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  );
  await userEvent.click(await view.findByRole("button", { name: "Continue" }));
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(
    await view.findByRole("button", { name: "Sign in to CopilotKit" }),
  );

  expect(
    await view.findByText("That account has no projects yet.", {
      exact: false,
    }),
  ).toBeTruthy();
  expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
    "disabled",
    true,
  );

  await userEvent.click(view.getByRole("button", { name: "Sign in again" }));

  expect(await view.findByRole("button", { name: "Project One" })).toBeTruthy();

  await userEvent.click(
    view.getByText("Point at your own Intelligence server"),
  );
  await userEvent.type(view.getByLabelText("Project key"), "ck-test");
  await waitFor(() =>
    expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
      "disabled",
      false,
    ),
  );
});

async function enterProjectSelection() {
  setupRootConfiguration("/tmp/create-project", async () =>
    emptyConfiguration(),
  );
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "begin_intelligence_sign_in")
      return "https://copilotkit.test/sign-in";
    if (command === "finish_intelligence_sign_in") return [];
    if (command === "intelligence_key_for") return "created-project-key";
    return previous(command, args);
  };
  const view = await renderApp();
  await enterInstallation(view);
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  await userEvent.type(view.getByLabelText("OpenAI API key"), "model-test-key");
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(
    view.getByRole("button", { name: "Sign in to CopilotKit" }),
  );
  return view;
}

test("creating a named project connects it only after the user submits", async () => {
  const view = await enterProjectSelection();
  const creation = deferred<{ id: string; name: string }>();
  const previous = invokeHandler;
  invokeHandler = async (command, args) =>
    command === "create_intelligence_project"
      ? creation.promise
      : previous(command, args);
  expect(
    invokeCalls.some((call) => call.command === "create_intelligence_project"),
  ).toBe(false);
  const create = view.getByRole("button", { name: "Create project" });
  expect(create).toHaveProperty("disabled", true);
  await userEvent.type(
    view.getByLabelText("New project name"),
    "  OpenBot workspace  ",
  );
  await userEvent.click(create);
  expect(invokeCalls).toContainEqual({
    command: "create_intelligence_project",
    args: { name: "OpenBot workspace" },
  });
  expect(
    view.getByRole("button", { name: "Creating project…" }),
  ).toHaveProperty("disabled", true);
  expect(view.getByLabelText("New project name")).toHaveProperty(
    "disabled",
    true,
  );
  await act(async () =>
    creation.resolve({ id: "new-project", name: "OpenBot workspace" }),
  );
  expect(view.getByText("Connected to CopilotKit.")).toBeTruthy();
  expect(invokeCalls).toContainEqual({
    command: "intelligence_key_for",
    args: { project: "new-project" },
  });
  expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
    "disabled",
    false,
  );
});

test("project creation failure preserves the name and retries without losing a created project", async () => {
  const view = await enterProjectSelection();
  const previous = invokeHandler;
  let creations = 0;
  let provisions = 0;
  invokeHandler = async (command, args) => {
    if (command === "create_intelligence_project") {
      if (++creations === 1)
        throw { said: "The project could not be created." };
      return { id: "new-project", name: "OpenBot workspace" };
    }
    if (command === "intelligence_key_for" && ++provisions === 1)
      throw { said: "The project key could not be created." };
    return previous(command, args);
  };
  await userEvent.type(
    view.getByLabelText("New project name"),
    "OpenBot workspace",
  );
  await userEvent.click(view.getByRole("button", { name: "Create project" }));
  expect(view.getByRole("alert").textContent).toContain(
    "The project could not be created.",
  );
  expect(view.getByLabelText("New project name")).toHaveProperty(
    "value",
    "OpenBot workspace",
  );
  expect(view.getByRole("button", { name: "Create project" })).toHaveProperty(
    "disabled",
    false,
  );
  await userEvent.click(view.getByRole("button", { name: "Create project" }));
  expect(view.getByRole("alert").textContent).toContain(
    "The project key could not be created.",
  );
  await userEvent.click(
    view.getByRole("button", { name: "OpenBot workspace" }),
  );
  expect(view.getByText("Connected to CopilotKit.")).toBeTruthy();
  expect(creations).toBe(2);
});

test("mount navigates to OpenBot only when the selected root is already owned and running", async () => {
  const root = "/tmp/openbot-owned-running-root";
  setupRootConfiguration(root, async () => emptyConfiguration());
  const setupHandler = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "already_running") {
      expect(args).toEqual({ root });
      return true;
    }
    return setupHandler(command, args);
  };

  await renderApp();
  await waitFor(() =>
    expect(invokeCalls).toContainEqual({ command: "show_openbot" }),
  );
});

test("mount leaves setup visible when the shared port answers without selected root ownership", async () => {
  const root = "/tmp/openbot-unowned-running-root";
  setupRootConfiguration(root, async () => emptyConfiguration());

  const view = await renderApp();

  expect(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  ).toBeTruthy();
  expect(invokeCalls.some((call) => call.command === "show_openbot")).toBe(
    false,
  );
});

for (const staleProbe of [false, true]) {
  test(`recovery mount keeps setup available after ${staleProbe ? "a stale positive" : "a negative"} adoption probe`, async () => {
    setupRootConfiguration("/tmp/openbot-worker-recovery", async () =>
      emptyConfiguration(),
    );
    const setupHandler = invokeHandler;
    const failure = {
      said: "Part of OpenBot (worker) stopped and could not be started again. Try starting OpenBot once more.",
    };
    invokeHandler = async (command, args) => {
      if (command === "already_running") return staleProbe;
      if (command === "last_failure") return failure;
      if (command === "show_openbot") throw failure;
      return setupHandler(command, args);
    };
    const view = await renderApp();
    await waitFor(() =>
      expect(
        invokeCalls.some((call) => call.command === "already_running"),
      ).toBe(true),
    );
    if (staleProbe) {
      await waitFor(() =>
        expect(
          invokeCalls.some((call) => call.command === "show_openbot"),
        ).toBe(true),
      );
    }
    expect(view.getByRole("button", { name: "Set up OpenBot" })).toBeTruthy();
    expect(view.getByRole("alert").textContent).toContain(failure.said);
    expect(view.queryByRole("button", { name: "Stop OpenBot" })).toBeNull();
  });
}

test("saved startup credentials enable Start without raw protected secrets on mount", async () => {
  invokeHandler = async (command) => {
    if (command === "prepare_installation") return null;
    if (command === "detect_engine") {
      return {
        engine: "docker",
        responding: true,
        engine_socket: null,
        detail: "Docker is answering.",
      };
    }
    if (command === "default_root") return "/tmp/openbot-app-test";
    if (command === "already_configured") {
      return {
        values: {},
        saved: {
          intelligenceApiKey: true,
          modelApiKeys: { openai: true, anthropic: false },
          modelSessions: { openai: false, anthropic: false },
        },
      };
    }
    if (command === "already_running") return false;
    if (command === "windows_blocker") return null;
    if (command === "last_failure") return null;
    if (command === "harnesses") {
      return [
        {
          id: "langgraph",
          name: "LangGraph",
          summary: "Default Bot",
          image: null,
          health_path: null,
          credential: "any-provider",
          maintainer: "first-party",
          mark: null,
          port: 8000,
        },
      ];
    }
    if (command === "providers") {
      return [
        {
          id: "openai",
          name: "OpenAI",
          summary: "Use OpenAI.",
          logins: ["api-key"],
          mark: null,
          caution: null,
        },
      ];
    }
    throw new Error(`unexpected command ${command}`);
  };

  const view = await renderApp();

  await userEvent.click(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  );
  await userEvent.click(await view.findByRole("button", { name: "Continue" }));
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  await userEvent.click(view.getByRole("button", { name: "Continue" }));

  await waitFor(() =>
    expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
      "disabled",
      false,
    ),
  );
  expect(
    invokeCalls.filter((call) => call.command === "already_configured"),
  ).toHaveLength(1);
});

async function chooseModelAfterRootEdit(
  view: Awaited<ReturnType<typeof renderApp>>,
  apiKey?: string,
) {
  expect(view.queryByRole("button", { name: "Start OpenBot" })).toBeNull();
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  if (apiKey)
    await userEvent.type(view.getByLabelText("OpenAI API key"), apiKey);
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
}

test("root edits reload saved configuration for that root and ignore stale saved responses", async () => {
  const rootA = "/tmp/openbot-root-a";
  const rootB = "/tmp/openbot-root-b";
  const rootC = "/tmp/openbot-root-c";
  const savedForRootA = deferred<ReturnType<typeof savedOpenAiConfiguration>>();
  const emptyForRootB = deferred<ReturnType<typeof emptyConfiguration>>();
  const savedForRootC = deferred<ReturnType<typeof savedOpenAiConfiguration>>();

  setupRootConfiguration(rootA, async (requestedRoot) => {
    if (requestedRoot === rootA) return savedForRootA.promise;
    if (requestedRoot === rootB) return emptyForRootB.promise;
    if (requestedRoot === rootC) return savedForRootC.promise;
    throw new Error(`unexpected already_configured root ${requestedRoot}`);
  });

  const view = await renderApp();
  await act(async () => {
    savedForRootA.resolve(savedOpenAiConfiguration());
  });

  await userEvent.click(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  );
  await userEvent.click(await view.findByRole("button", { name: "Continue" }));
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  expect(view.getByText(/A saved OpenAI API key will be used/)).toBeTruthy();
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await waitFor(() =>
    expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
      "disabled",
      false,
    ),
  );

  await userEvent.click(
    view.getByRole("button", { name: "Change installation" }),
  );
  const rootField = view.getByLabelText("Where OpenBot lives");
  const user = userEvent.setup({
    document: view.container.ownerDocument,
  });
  await user.clear(rootField);
  await user.type(rootField, rootB);
  await act(async () => {
    rootField.blur();
  });

  await waitFor(() =>
    expect(
      invokeCalls.filter((call) => call.command === "already_configured"),
    ).toContainEqual({ command: "already_configured", args: { root: rootB } }),
  );
  expect(
    view.queryByRole("button", { name: "Continue to sign in" }),
  ).toBeNull();

  await user.clear(rootField);
  await user.type(rootField, rootC);
  await act(async () => {
    rootField.blur();
  });

  await waitFor(() =>
    expect(
      invokeCalls.filter((call) => call.command === "already_configured"),
    ).toContainEqual({ command: "already_configured", args: { root: rootC } }),
  );
  await act(async () => {
    emptyForRootB.resolve(emptyConfiguration());
  });
  expect(
    view.queryByRole("button", { name: "Continue to sign in" }),
  ).toBeNull();

  await act(async () => {
    savedForRootC.resolve(savedOpenAiConfiguration());
  });
  await chooseModelAfterRootEdit(view);
  await waitFor(() =>
    expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
      "disabled",
      false,
    ),
  );
  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));

  expect(getStartStackPayload()).toMatchObject({
    root: rootC,
    model: {
      provider: "openai",
      login: "api-key",
      saved: true,
    },
  });
});

test("same-process setup remount prefers the retained selected root", async () => {
  const rootA = "/tmp/openbot-default-root";
  const rootB = "/tmp/openbot-retained-root";
  setupRootConfiguration(rootA, async (requestedRoot) => {
    if (requestedRoot !== rootB)
      throw new Error(`unexpected already_configured root ${requestedRoot}`);
    return savedOpenAiConfiguration();
  });
  const setupHandler = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "selected_root") return rootB;
    return setupHandler(command, args);
  };

  const view = await renderApp();
  await userEvent.click(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  );
  await userEvent.click(await view.findByRole("button", { name: "Continue" }));
  expect(view.getByLabelText("Where OpenBot lives")).toHaveProperty(
    "value",
    rootB,
  );
  await completeInstallation(view);
  await userEvent.click(await view.findByRole("radio", { name: /OpenAI/ }));
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(
    await view.findByRole("button", { name: "Start OpenBot" }),
  );

  expect(
    invokeCalls.filter((call) => call.command === "already_configured"),
  ).toEqual([{ command: "already_configured", args: { root: rootB } }]);
  expect(getStartStackPayload().root).toBe(rootB);
});

test.each([
  {
    name: "initial folder reload",
    pendingRoot: "a",
    finalRoot: "b",
    savedModel: false,
  },
  {
    name: "blur-started load",
    pendingRoot: "b",
    finalRoot: "c",
    savedModel: true,
  },
  {
    name: "away-and-back edit",
    pendingRoot: "a",
    finalRoot: "a",
    savedModel: false,
  },
])(
  "root edits invalidate the $name before blur",
  async ({ pendingRoot, finalRoot, savedModel }) => {
    const rootA = "/tmp/openbot-root-a";
    const rootB = "/tmp/openbot-root-b";
    const currentRoot = `/tmp/openbot-root-${finalRoot}`;
    const requests: Array<{
      root: string;
      response: Deferred<ReturnType<typeof savedOpenAiConfiguration>>;
    }> = [];
    setupRootConfiguration(rootA, async (root) => {
      const response = deferred<ReturnType<typeof savedOpenAiConfiguration>>();
      requests.push({ root, response });
      return response.promise;
    });

    const view = await renderApp();
    const user = userEvent.setup({ document: view.container.ownerDocument });
    // Bootstrap must finish checking installation before exposing any wizard controls.
    await act(async () =>
      requests[0].response.resolve(
        savedModel ? savedOpenAiConfiguration() : emptyConfiguration(),
      ),
    );
    await user.click(
      await view.findByRole("button", { name: "Set up OpenBot" }),
    );
    await user.click(await view.findByRole("button", { name: "Continue" }));
    await completeInstallation(view);
    await user.click(await view.findByRole("radio", { name: /OpenAI/ }));
    expect(
      Boolean(view.queryByText(/A saved OpenAI API key will be used/)),
    ).toBe(savedModel);
    if (!savedModel) {
      await user.type(
        view.getByLabelText("OpenAI API key"),
        "sk-synthetic-current-model",
      );
    }
    await user.click(view.getByRole("button", { name: "Continue" }));
    const previousStart = view.getByRole("button", { name: "Start OpenBot" });
    expect(previousStart).toHaveProperty("disabled", !savedModel);
    await user.click(view.getByRole("button", { name: "Change installation" }));
    const rootField = view.getByLabelText("Where OpenBot lives");

    if (pendingRoot === "a") {
      await user.click(rootField);
      await act(async () => rootField.blur());
    }

    await user.clear(rootField);
    await user.type(rootField, rootB);
    if (pendingRoot === "b") {
      await act(async () => rootField.blur());
    }
    if (currentRoot !== rootB) {
      await user.clear(rootField);
      await user.type(rootField, currentRoot);
    }
    const pending = requests[requests.length - 1];
    expect(pending.root).toBe(`/tmp/openbot-root-${pendingRoot}`);
    const requestCountBeforeBlur = requests.length;

    function expectBlockedSetup() {
      expect(rootField).toHaveProperty("value", currentRoot);
      expect(view.container.ownerDocument.activeElement === rootField).toBe(
        true,
      );
      expect(
        view.queryByRole("button", { name: "Continue to sign in" }),
      ).toBeNull();
      expect(view.queryByRole("button", { name: "Start OpenBot" })).toBeNull();
      expect(requests).toHaveLength(requestCountBeforeBlur);
      expect(invokeCalls.some((call) => call.command === "start_stack")).toBe(
        false,
      );
    }

    expectBlockedSetup();
    await act(async () =>
      pending.response.resolve({
        ...savedOpenAiConfiguration(),
        values: {
          INTELLIGENCE_API_KEY: "ck-synthetic-stale",
          INTELLIGENCE_API_URL: "https://stale.example/api",
          INTELLIGENCE_GATEWAY_WS_URL: "wss://stale.example/ws",
        },
      }),
    );
    expectBlockedSetup();

    if (savedModel) {
      // A different root's Intelligence connection cannot restore the model cleared by an edit.
      await act(async () => rootField.blur());
      await act(async () =>
        requests[requests.length - 1].response.resolve({
          ...emptyConfiguration(),
          saved: { ...emptyConfiguration().saved, intelligenceApiKey: true },
        }),
      );
      await completeInstallation(view);
      await user.click(await view.findByRole("radio", { name: /OpenAI/ }));
      expect(
        view.queryByText(/A saved OpenAI API key will be used/),
      ).toBeNull();
      expect(view.getByRole("button", { name: "Continue" })).toHaveProperty(
        "disabled",
        true,
      );
      await user.click(view.getByRole("button", { name: "Back" }));
      await user.click(view.getByLabelText("Where OpenBot lives"));
      await act(async () => view.getByLabelText("Where OpenBot lives").blur());
    } else {
      await act(async () => rootField.blur());
    }
    expect(requests[requests.length - 1].root).toBe(currentRoot);
    await act(async () =>
      requests[requests.length - 1].response.resolve({
        ...savedOpenAiConfiguration(),
        values: {
          INTELLIGENCE_API_URL: "https://current.example/api",
          INTELLIGENCE_GATEWAY_WS_URL: "wss://current.example/ws",
        },
      }),
    );
    if (savedModel) {
      await user.click(
        view.getByRole("button", { name: "Continue to sign in" }),
      );
      await user.click(await view.findByRole("radio", { name: /OpenAI/ }));
      await user.click(view.getByRole("button", { name: "Continue" }));
    } else {
      await chooseModelAfterRootEdit(view, "sk-synthetic-current-model");
    }
    expect(
      view.getByText(
        "A saved CopilotKit connection will be checked when you start.",
      ),
    ).toBeTruthy();
    await user.click(view.getByText("Point at your own Intelligence server"));
    expect(view.getByLabelText("Project key")).toHaveProperty("value", "");
    expect(view.getByLabelText("API URL")).toHaveProperty(
      "value",
      "https://current.example/api",
    );
    expect(view.getByLabelText("Gateway WebSocket URL")).toHaveProperty(
      "value",
      "wss://current.example/ws",
    );
    const currentStart = view.getByRole("button", { name: "Start OpenBot" });
    expect(currentStart).toHaveProperty("disabled", false);
    await user.click(currentStart);
    expect(
      invokeCalls.filter((call) => call.command === "start_stack"),
    ).toHaveLength(1);
    expect(getStartStackPayload()).toEqual({
      root: currentRoot,
      apiKey: "",
      apiUrl: "https://current.example/api",
      gatewayWsUrl: "wss://current.example/ws",
      harness: { id: "langgraph" },
      model: savedModel
        ? { provider: "openai", login: "api-key", saved: true }
        : {
            provider: "openai",
            login: "api-key",
            apiKey: "sk-synthetic-current-model",
          },
    });
  },
);

function useBringYourOwnHarnessSetup() {
  invokeHandler = async (command) => {
    if (command === "prepare_installation") return null;
    if (command === "detect_engine") {
      return {
        engine: "docker",
        responding: true,
        engine_socket: null,
        detail: "Docker is answering.",
      };
    }
    if (command === "default_root") return "/tmp/openbot-app-test";
    if (command === "already_configured") {
      return {
        values: {},
        saved: {
          intelligenceApiKey: true,
          modelApiKeys: { openai: false, anthropic: false },
          modelSessions: { openai: false, anthropic: false },
        },
      };
    }
    if (command === "already_running") return false;
    if (command === "windows_blocker") return null;
    if (command === "last_failure") return null;
    if (command === "harnesses") {
      return [
        {
          id: "langgraph",
          name: "LangGraph",
          summary: "Default Bot",
          image: null,
          health_path: null,
          credential: "any-provider",
          maintainer: "first-party",
          mark: null,
          port: 8000,
        },
        {
          id: "byo-url",
          name: "An agent you already run",
          summary: "Give its address.",
          image: null,
          health_path: null,
          credential: "their-endpoint",
          maintainer: "community",
          mark: null,
          port: null,
        },
      ];
    }
    if (command === "providers") {
      return [
        {
          id: "openai-compatible",
          name: "OpenAI-compatible",
          summary: "Use your own endpoint.",
          logins: ["endpoint"],
          mark: null,
          caution: null,
        },
      ];
    }
    if (command === "start_stack") return null;
    throw new Error(`unexpected command ${command}`);
  };
}

async function enterBringYourOwnHarnessEndpoint(agentUrl: string) {
  const view = await renderApp();

  await userEvent.click(
    await view.findByRole("button", { name: "Set up OpenBot" }),
  );
  await userEvent.click(await view.findByText("Choose the agent framework"));
  await userEvent.click(
    await view.findByRole("radio", { name: /An agent you already run/ }),
  );

  const continueFromHarness = view.getByRole("button", { name: "Continue" });
  expect(continueFromHarness).toHaveProperty("disabled", true);
  const agentEndpoint = view.getByLabelText("AG-UI endpoint");
  if (agentUrl) {
    await userEvent.type(agentEndpoint, agentUrl.replaceAll("[", "[["));
  }
  expect(agentEndpoint).toHaveProperty("value", agentUrl);
  return view;
}

test.each([
  "",
  "http://",
  "https://",
  "httpx://models.example/v1",
  "httpfoo://models.example/v1",
  "https://exa mple.example/v1",
  "HTTP://agent.example/ag-ui",
  "https:agent.example/ag-ui",
])("bring-your-own agent refuses startup for URL %s", async (agentUrl) => {
  useBringYourOwnHarnessSetup();
  const view = await enterBringYourOwnHarnessEndpoint(agentUrl);

  const continueButton = view.getByRole("button", { name: "Continue" });
  expect(continueButton).toHaveProperty("disabled", true);
  await userEvent.click(continueButton);
  expect(view.getByRole("heading", { name: "Your first Bot" })).toBeTruthy();
  expect(view.queryByRole("heading", { name: "Connect your AI" })).toBeNull();
  expect(view.queryByRole("button", { name: "Start OpenBot" })).toBeNull();
  expect(invokeCalls.filter((call) => call.command === "start_stack")).toEqual(
    [],
  );
});

test.each([
  ["https://agent.example/ag-ui", "https://agent.example/ag-ui"],
  ["http://localhost:11434/v1", "http://localhost:11434/v1"],
  ["https://models.example/v1", "https://models.example/v1"],
  ["https://bücher.example/ag-ui", "https://bücher.example/ag-ui"],
  ["http://[::1]:8000/ag-ui", "http://[::1]:8000/ag-ui"],
  ["  http://localhost:8000/ag-ui  ", "http://localhost:8000/ag-ui"],
])(
  "bring-your-own agent collects a distinct AG-UI endpoint for startup: %s",
  async (agentUrl, expectedAgentUrl) => {
    useBringYourOwnHarnessSetup();
    const view = await enterBringYourOwnHarnessEndpoint(agentUrl);

    const continueFromHarness = view.getByRole("button", { name: "Continue" });
    expect(continueFromHarness).toHaveProperty("disabled", false);
    await userEvent.click(continueFromHarness);
    await completeInstallation(view);
    expect(
      await view.findByRole("heading", { name: "Connect your AI" }),
    ).toBeTruthy();
    await userEvent.click(
      await view.findByRole("radio", { name: /OpenAI-compatible/ }),
    );
    await userEvent.type(
      view.getByLabelText("Base URL"),
      "https://provider.example/v1",
    );
    await userEvent.type(view.getByLabelText("Model name"), "local-model");
    await userEvent.click(view.getByRole("button", { name: "Continue" }));
    await userEvent.click(
      await view.findByRole("button", { name: "Start OpenBot" }),
    );

    const payload = getStartStackPayload();
    expect(
      invokeCalls.filter((call) => call.command === "start_stack"),
    ).toHaveLength(1);
    expect(payload).toEqual({
      root: "/tmp/openbot-app-test",
      apiKey: "",
      apiUrl: "https://api.intelligence.copilotkit.ai",
      gatewayWsUrl: "wss://realtime.intelligence.copilotkit.ai",
      harness: { id: "byo-url", agentUrl: expectedAgentUrl },
      model: {
        provider: "openai-compatible",
        login: "endpoint",
        baseUrl: "https://provider.example/v1",
        model: "local-model",
      },
    });
    expect(payload.model).not.toHaveProperty("apiKey");
  },
);

test.each([
  "http://",
  "https://",
  "httpx://models.example/v1",
  "httpfoo://models.example/v1",
  "https://exa mple.example/v1",
])("custom compatible endpoint refuses startup for URL %s", async (baseUrl) => {
  useCompatibleEndpointSetup({});
  const view = await enterCompatibleEndpoint(baseUrl);

  const continueButton = view.getByRole("button", { name: "Continue" });
  expect(continueButton).toHaveProperty("disabled", true);
  await userEvent.click(continueButton);
  expect(view.getByRole("heading", { name: "Connect your AI" })).toBeTruthy();
  expect(view.queryByRole("button", { name: "Start OpenBot" })).toBeNull();
  expect(invokeCalls.filter((call) => call.command === "start_stack")).toEqual(
    [],
  );
});

test.each(["http://localhost:11434/v1", "https://models.example/v1"])(
  "custom compatible endpoint startup for URL %s does not submit a saved OpenAI API key",
  async (baseUrl) => {
    useCompatibleEndpointSetup({
      OPENAI_API_KEY: "sk-synthetic-openai",
    });

    await startWithCompatibleEndpoint("", baseUrl);

    const payload = getStartStackPayload();
    expect(
      invokeCalls.filter((call) => call.command === "start_stack"),
    ).toHaveLength(1);
    expect(payload).toEqual({
      root: "/tmp/openbot-app-test",
      apiKey: "",
      apiUrl: "https://api.intelligence.copilotkit.ai",
      gatewayWsUrl: "wss://realtime.intelligence.copilotkit.ai",
      harness: { id: "langgraph" },
      model: {
        provider: "openai-compatible",
        login: "endpoint",
        baseUrl,
        model: "local-model",
      },
    });
    expect(payload.model).not.toHaveProperty("apiKey");
    expect(JSON.stringify(payload)).not.toContain("sk-synthetic-openai");
  },
);

test("custom compatible endpoint startup can route containers to a different public URL", async () => {
  useCompatibleEndpointSetup({});

  await startWithCompatibleEndpoint(
    "",
    "http://127.0.0.1:11434/v1",
    "http://ollama:11434/v1",
  );

  const payload = getStartStackPayload();
  expect(payload.model).toEqual({
    provider: "openai-compatible",
    login: "endpoint",
    baseUrl: "http://127.0.0.1:11434/v1",
    containerBaseUrl: "http://ollama:11434/v1",
    model: "local-model",
  });
});

test("saved compatible endpoint restores the optional container URL", async () => {
  useSavedCompatibleEndpointSetup(
    "http://127.0.0.1:11434/v1",
    "qwen3-vl:2b",
    false,
    "compatible-endpoint",
    "http://ollama:11434/v1",
  );

  const view = await renderApp();
  await userEvent.click(view.getByRole("button", { name: "Set up OpenBot" }));
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await completeInstallation(view);

  expect(
    view.getByLabelText("Container Base URL, if different"),
  ).toHaveProperty("value", "http://ollama:11434/v1");
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(
    await view.findByRole("button", { name: "Start OpenBot" }),
  );

  expect(getStartStackPayload().model).toMatchObject({
    provider: "openai-compatible",
    login: "endpoint",
    baseUrl: "http://127.0.0.1:11434/v1",
    containerBaseUrl: "http://ollama:11434/v1",
    model: "qwen3-vl:2b",
  });
});

test("custom compatible endpoint startup submits an explicitly typed endpoint key", async () => {
  useCompatibleEndpointSetup({
    OPENAI_API_KEY: "sk-synthetic-openai",
  });

  await startWithCompatibleEndpoint("endpoint-key");

  const payload = getStartStackPayload();
  expect(payload.model).toMatchObject({
    provider: "openai-compatible",
    login: "endpoint",
    apiKey: "endpoint-key",
    baseUrl: "https://models.example/v1",
    model: "local-model",
  });
  expect(JSON.stringify(payload)).not.toContain("sk-synthetic-openai");
});

for (const provider of [
  { id: "openai", name: "OpenAI" },
  { id: "anthropic", name: "Anthropic" },
] as const) {
  test.each(["fresh", "saved"])(
    `${provider.name} %s plan startup omits a key typed before switching login tabs`,
    async (session) => {
      const planToken = `synthetic-${provider.id}-plan-token`;
      const hiddenKey = `sk-synthetic-${provider.id}-hidden`;
      setupRootConfiguration("/tmp/openbot-app-test", async () => ({
        ...emptyConfiguration(),
        saved: {
          ...emptyConfiguration().saved,
          intelligenceApiKey: true,
          modelSessions: { [provider.id]: session === "saved" },
        },
      }));
      const setupHandler = invokeHandler;
      invokeHandler = async (command, args) => {
        if (command === "providers") {
          return [
            {
              ...provider,
              summary: `Use ${provider.name}.`,
              logins: ["plan", "api-key"],
              mark: null,
              caution: null,
            },
          ];
        }
        if (session === "fresh") {
          const signIn = provider.id === "openai" ? "chatgpt" : "claude";
          if (command === `begin_${signIn}_sign_in`)
            return "https://sign-in.example";
          if (command === `finish_${signIn}_sign_in`) return planToken;
        }
        return setupHandler(command, args);
      };

      const view = await renderApp();
      await userEvent.click(
        await view.findByRole("button", { name: "Set up OpenBot" }),
      );
      await userEvent.click(
        await view.findByRole("button", { name: "Continue" }),
      );
      await completeInstallation(view);
      await userEvent.click(
        await view.findByRole("radio", { name: new RegExp(provider.name) }),
      );
      await userEvent.click(view.getByRole("tab", { name: "Use an API key" }));
      await userEvent.type(
        view.getByLabelText(`${provider.name} API key`),
        hiddenKey,
      );
      await userEvent.click(
        view.getByRole("tab", { name: "Sign in with my plan" }),
      );
      if (session === "fresh") {
        await userEvent.click(
          view.getByRole("button", { name: `Sign in with ${provider.name}` }),
        );
        if (provider.id === "anthropic") {
          await userEvent.type(
            await view.findByLabelText("Code from your browser"),
            "synthetic-code",
          );
          await userEvent.click(
            view.getByRole("button", { name: "Finish signing in" }),
          );
        }
      }
      await view.findByText(
        new RegExp(
          session === "saved"
            ? `A saved ${provider.name} sign-in will be checked`
            : `Signed in to ${provider.name}`,
        ),
      );
      await userEvent.click(view.getByRole("button", { name: "Continue" }));
      await userEvent.click(
        await view.findByRole("button", { name: "Start OpenBot" }),
      );

      const payload = getStartStackPayload();
      expect(payload.model).not.toHaveProperty("apiKey");
      expect(payload.model).toEqual({
        provider: provider.id,
        login: "plan",
        ...(session === "saved" ? { saved: true } : { token: planToken }),
      });
      expect(JSON.stringify(payload)).not.toContain(hiddenKey);
    },
  );

  test(`saved ${provider.name} plan session enables Start without raw protected secrets on mount`, async () => {
    invokeHandler = async (command) => {
      if (command === "prepare_installation") return null;
      if (command === "detect_engine") {
        return {
          engine: "docker",
          responding: true,
          engine_socket: null,
          detail: "Docker is answering.",
        };
      }
      if (command === "default_root") return "/tmp/openbot-app-test";
      if (command === "already_configured") {
        return {
          values: {},
          saved: {
            intelligenceApiKey: true,
            modelApiKeys: { openai: false, anthropic: false },
            modelSessions: {
              openai: provider.id === "openai",
              anthropic: provider.id === "anthropic",
            },
          },
        };
      }
      if (command === "already_running") return false;
      if (command === "windows_blocker") return null;
      if (command === "last_failure") return null;
      if (command === "harnesses") {
        return [
          {
            id: "langgraph",
            name: "LangGraph",
            summary: "Default Bot",
            image: null,
            health_path: null,
            credential: "any-provider",
            maintainer: "first-party",
            mark: null,
            port: 8000,
          },
        ];
      }
      if (command === "providers") {
        return [
          {
            id: provider.id,
            name: provider.name,
            summary: `Use ${provider.name}.`,
            logins: ["plan", "api-key"],
            mark: null,
            caution: null,
          },
        ];
      }
      if (command === "start_stack") return null;
      throw new Error(`unexpected command ${command}`);
    };

    const view = await renderApp();

    await userEvent.click(
      await view.findByRole("button", { name: "Set up OpenBot" }),
    );
    await userEvent.click(
      await view.findByRole("button", { name: "Continue" }),
    );
    await completeInstallation(view);
    await userEvent.click(
      await view.findByRole("radio", { name: new RegExp(provider.name) }),
    );
    expect(
      view.getByText(
        new RegExp(`A saved ${provider.name} sign-in will be checked`),
      ),
    ).toBeTruthy();
    await userEvent.click(view.getByRole("button", { name: "Continue" }));

    await waitFor(() =>
      expect(
        view.getByRole("button", { name: "Start OpenBot" }),
      ).toHaveProperty("disabled", false),
    );
    await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));

    expect(
      invokeCalls.filter((call) => call.command === "already_configured"),
    ).toEqual([
      {
        command: "already_configured",
        args: { root: "/tmp/openbot-app-test" },
      },
    ]);
    for (const call of invokeCalls) {
      const args = JSON.stringify(call.args ?? {});
      expect(args).not.toContain("OPENAI_API_KEY");
      expect(args).not.toContain("CLAUDE_CODE_OAUTH_TOKEN");
    }
    expect(
      invokeCalls.find((call) => call.command === "start_stack")?.args,
    ).toMatchObject({
      root: "/tmp/openbot-app-test",
      apiKey: "",
      model: {
        provider: provider.id,
        login: "plan",
        saved: true,
      },
      harness: { id: "langgraph" },
    });
  });
}

for (const provider of [
  { id: "openai", name: "OpenAI", plan: "ChatGPT" },
  { id: "anthropic", name: "Anthropic", plan: "Claude" },
]) {
  for (const login of ["plan", "api-key"] as const) {
    test(`unknown legacy ${provider.name} ${login} and Intelligence reuse stays passive until Start`, async () => {
      setupRootConfiguration("/tmp/synthetic-legacy-root", async () => ({
        values: {},
        saved: {},
      }));
      const setupHandler = invokeHandler;
      invokeHandler = async (command, args) => {
        if (command === "providers")
          return [
            {
              ...provider,
              summary: "Synthetic provider",
              logins: ["plan", "api-key"],
              mark: null,
              caution: null,
            },
          ];
        if (command === "start_stack")
          throw {
            said: "Synthetic saved credential is unavailable.",
            detail: "Synthetic denial",
          };
        return setupHandler(command, args);
      };
      const view = await renderApp();
      await userEvent.click(
        await view.findByRole("button", { name: "Set up OpenBot" }),
      );
      await userEvent.click(
        await view.findByRole("button", { name: "Continue" }),
      );
      await completeInstallation(view);
      await userEvent.click(
        await view.findByRole("radio", { name: new RegExp(provider.name) }),
      );
      if (login === "api-key")
        await userEvent.click(
          view.getByRole("tab", { name: "Use an API key" }),
        );
      await userEvent.click(
        view.getByRole("button", {
          name:
            login === "plan"
              ? `Use a saved ${provider.plan} sign-in`
              : `Use a saved ${provider.name} API key`,
        }),
      );
      await userEvent.click(view.getByRole("button", { name: "Continue" }));
      expect(
        view.getByRole("button", { name: "Start OpenBot" }),
      ).toHaveProperty("disabled", true);
      await userEvent.click(
        view.getByRole("button", { name: "Use a saved connection" }),
      );
      expect(view.queryByText("Connected to CopilotKit.")).toBeNull();
      // Returning to the provider screen retains deliberate reuse without signing in automatically.
      await userEvent.click(view.getByRole("button", { name: "Back" }));
      await userEvent.click(
        await view.findByRole("button", { name: "Continue" }),
      );
      expect(
        invokeCalls.some((call) =>
          /sign_in|start_stack|ask_the_bot/.test(call.command),
        ),
      ).toBe(false);
      await userEvent.click(
        view.getByRole("button", { name: "Start OpenBot" }),
      );
      await view.findByText("Synthetic saved credential is unavailable.");
      expect(getStartStackPayload()).toMatchObject({
        apiKey: "",
        model: { provider: provider.id, login, saved: true },
      });
      expect(getStartStackPayload().model).not.toHaveProperty("token");
      expect(getStartStackPayload().model).not.toHaveProperty("apiKey");
      expect(
        view.getByRole("button", { name: "Sign in to CopilotKit again" }),
      ).toBeTruthy();
    });
  }
}

test("Start credential failures do not expose a restore action", async () => {
  useCompatibleEndpointSetup({
    INTELLIGENCE_API_KEY: "synthetic-intelligence",
  });
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "start_stack")
      throw {
        said: "Saved credential needs authorization.",
        detail: "synthetic item refusal",
        [["reco", "very"].join("")]: {
          ticket: "synthetic-one-use",
          operation: "read",
          setting: "INTELLIGENCE_API_KEY",
          label: "Restore access to saved setup",
          explanation:
            "This Mac is protecting a credential from your saved OpenBot setup. Restoring access may ask macOS to confirm this app. Your saved data stays in place.",
        },
      };
    return previous(command, args);
  };
  const view = await enterCompatibleEndpoint(
    "https://models.example/v1",
    "synthetic-model-key",
  );
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));

  expect(
    await view.findByText("Saved credential needs authorization."),
  ).toBeTruthy();
  expect(
    view.queryByRole("button", { name: "Restore access to saved setup" }),
  ).toBeNull();
  expect(
    invokeCalls.some((c) => c.command === ["reco", "ver_credential"].join("")),
  ).toBe(false);
  expect(
    invokeCalls.filter(
      (c) => c.command === ["cancel", "_credential", "_reco", "very"].join(""),
    ),
  ).toEqual([]);
});

test("the Enter that finishes a composed character does not ask the Bot", async () => {
  useCompatibleEndpointSetup({});
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "ask_the_bot") return "42";
    return previous(command, args);
  };
  const view = await enterCompatibleEndpoint("https://models.example/v1");
  await userEvent.click(view.getByRole("button", { name: "Continue" }));
  await userEvent.click(view.getByRole("button", { name: "Start OpenBot" }));
  const question = await view.findByLabelText("Your question");
  // Keys land on the focused field.
  await userEvent.click(question);

  // Japanese, Chinese and Korean are typed through an input method, where Enter confirms the
  // character being built. Chromium marks that keydown `isComposing`; the macOS WebKit webview
  // sends it after compositionend with key code 229. Neither should send the question.
  await act(async () => {
    fireEvent.keyDown(question, { key: "Enter", isComposing: true });
    fireEvent.keyDown(question, { key: "Enter", keyCode: 229 });
  });

  expect(invokeCalls.filter((call) => call.command === "ask_the_bot")).toEqual(
    [],
  );
});

async function enterSelfHostedIntelligence() {
  const view = await enterProjectSelection();
  await userEvent.click(
    view.getByText("Point at your own Intelligence server"),
  );
  const user = userEvent.setup({ document: window.document });
  await user.clear(view.getByLabelText("API URL"));
  await user.type(
    view.getByLabelText("API URL"),
    "https://intelligence.example.test",
  );
  return view;
}

test("self-hosted Intelligence sign-in selects a project and receives its provisioned key", async () => {
  const view = await enterSelfHostedIntelligence();
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "begin_self_hosted_intelligence_sign_in")
      return [{ id: "42", name: "Self-hosted team" }];
    if (command === "finish_self_hosted_intelligence_sign_in")
      return "self-hosted-project-key";
    return previous(command, args);
  };
  await userEvent.click(
    view.getByRole("button", { name: "Sign in to Intelligence" }),
  );
  expect(invokeCalls).toContainEqual({
    command: "begin_self_hosted_intelligence_sign_in",
    args: {
      root: "/tmp/create-project",
      apiUrl: "https://intelligence.example.test",
    },
  });
  expect(view.queryByRole("button", { name: "Create project" })).toBeNull();
  await userEvent.click(
    await view.findByRole("button", { name: "Self-hosted team" }),
  );
  expect(invokeCalls).toContainEqual({
    command: "finish_self_hosted_intelligence_sign_in",
    args: { root: "/tmp/create-project", project: "42" },
  });
  expect(await view.findByText("Connected to Intelligence.")).toBeTruthy();
  expect(view.getByLabelText("Project key")).toHaveProperty(
    "value",
    "self-hosted-project-key",
  );
  expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
    "disabled",
    false,
  );
});

test("cancelling self-hosted login ignores a late project response and keeps manual setup available", async () => {
  const view = await enterSelfHostedIntelligence();
  const projects = deferred<{ id: string; name: string }[]>();
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "begin_self_hosted_intelligence_sign_in")
      return projects.promise;
    if (command === "cancel_self_hosted_intelligence_sign_in") return;
    return previous(command, args);
  };
  await userEvent.click(
    view.getByRole("button", { name: "Sign in to Intelligence" }),
  );
  expect(view.getByRole("status").textContent).toContain(
    "Intelligence browser window",
  );
  expect(view.getByLabelText("API URL")).toHaveProperty("disabled", true);
  await userEvent.click(
    view.getByRole("button", { name: "Cancel Intelligence sign-in" }),
  );
  expect(
    invokeCalls.some(
      (call) => call.command === "cancel_self_hosted_intelligence_sign_in",
    ),
  ).toBe(true);
  await act(async () =>
    projects.resolve([{ id: "42", name: "Cancelled team" }]),
  );
  expect(view.queryByRole("button", { name: "Cancelled team" })).toBeNull();
  expect(view.getByLabelText("API URL")).toHaveProperty("disabled", false);
  await userEvent.type(view.getByLabelText("Project key"), "manual-key");
  expect(view.getByRole("button", { name: "Start OpenBot" })).toHaveProperty(
    "disabled",
    false,
  );
});

test("self-hosted provisioning failure offers a fresh sign-in without retaining a stale picker", async () => {
  const view = await enterSelfHostedIntelligence();
  const previous = invokeHandler;
  invokeHandler = async (command, args) => {
    if (command === "begin_self_hosted_intelligence_sign_in")
      return [{ id: "42", name: "Team" }];
    if (command === "finish_self_hosted_intelligence_sign_in")
      throw { said: "Your account cannot create the Learning container." };
    return previous(command, args);
  };
  await userEvent.click(
    view.getByRole("button", { name: "Sign in to Intelligence" }),
  );
  await userEvent.click(await view.findByRole("button", { name: "Team" }));
  expect(view.getByRole("alert").textContent).toContain(
    "cannot create the Learning container",
  );
  expect(view.queryByRole("button", { name: "Team" })).toBeNull();
  expect(
    view.getByRole("button", { name: "Sign in to Intelligence" }),
  ).toHaveProperty("disabled", false);
  expect(view.getByLabelText("Project key")).toHaveProperty("value", "");
});