Skip to content

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

30.2 KB
Raw Blame
// @vitest-environment happy-dom
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const fixture = vi.hoisted(() => ({ api: vi.fn(), dispatch: vi.fn(), admin: true as boolean | null, config: null as { cloudHome?: boolean; team?: { id: string; name: string } } | null }));
vi.mock("@/state/store", async (importOriginal) => ({
  ...await importOriginal<typeof import("@/state/store")>(),
  api: fixture.api,
  useStore: () => ({ state: { bots: [{ id: "pepper", name: "Pepper" }, { id: "quill", name: "Quill" }], config: fixture.config }, dispatch: fixture.dispatch }),
}));
vi.mock("@/lib/use-owner-or-admin", () => ({ useOwnerOrAdmin: () => fixture.admin }));
const { VaultSettings } = await import("./VaultSettings");

(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
let host: HTMLDivElement;
let root: Root;

const CARD_LOGIN = { id: "l1", site: "example.com", hosts: ["example.com", "login.microsoftonline.com"], exactHost: false, username: "[email protected]", hasTotp: true, source: "card", bots: ["pepper"], createdAt: 1, lastUsedAt: Date.now() - 120_000 };
const SHARED_LOGIN = { id: "l2", site: "example.org", hosts: ["crm.example.org"], exactHost: false, username: "team", hasTotp: false, source: "settings", bots: "all", createdAt: 2 };
const TENANT_LOGIN = { id: "l3", site: "okta.com", hosts: ["acme.okta.com"], exactHost: true, username: "ops", hasTotp: false, source: "settings", bots: "all", createdAt: 3 };
const VAULT_LOGIN = { id: "1password:v1:i1:example.com", site: "example.com", hosts: ["vault.example.com"], exactHost: false, username: "vault-user", hasTotp: false, source: "1password", bots: "all", createdAt: 0 };
const OFF = { connected: false };

beforeEach(() => {
  fixture.api.mockReset();
  fixture.dispatch.mockReset();
  fixture.admin = true;
  fixture.config = null;
  host = document.createElement("div");
  document.body.append(host);
  root = createRoot(host);
});
afterEach(() => {
  act(() => root.unmount());
  host.remove();
});

async function render() {
  await act(async () => root.render(createElement(VaultSettings)));
  await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
}
const button = (label: string) => [...document.body.querySelectorAll("button")].find((candidate) => (candidate.getAttribute("aria-label") ?? candidate.textContent) === label)!;
/** The password manager's token is asked for in a small dialog over Settings. */
const inDialog = (label: string) => [...document.body.querySelectorAll("[data-settings-dialog] button")].find((candidate) => candidate.textContent === label) as HTMLButtonElement;
async function click(label: string) { await act(async () => button(label).click()); }
async function type(name: string, value: string) {
  const input = document.body.querySelector<HTMLInputElement>(`input[name="${name}"]`)!;
  await act(async () => {
    Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, value);
    input.dispatchEvent(new Event("input", { bubbles: true }));
  });
}
async function submit() {
  await act(async () => { host.querySelector<HTMLFormElement>("[data-login-form]")!.requestSubmit(); });
  await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
}

describe("Settings → Vault: logins", () => {
  it("lists each login by site, the sign-in hosts it also fills on, username, 2FA, source, who may use it and last use", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [CARD_LOGIN, SHARED_LOGIN, TENANT_LOGIN], onePassword: OFF });
    await render();
    expect(fixture.api).toHaveBeenCalledWith("/api/vault");
    const rows = [...host.querySelectorAll("[data-login-row]")].map((row) => row.textContent);
    expect(rows[0]).toContain("example.com");
    expect(rows[0]).toContain("[email protected]");
    expect(rows[0]).toContain("Also signs in on login.microsoftonline.com");
    expect(rows[0]).toContain("2FA");
    expect(rows[0]).toContain("Saved from a chat · Pepper · Used 2 min ago");
    expect(rows[1]).toContain("Added here · All bots · Not used yet");
    expect(rows[1]).toContain("Also signs in on crm.example.org");
    expect(rows[2]).toContain("Only on acme.okta.com");
    expect(host.querySelector("[data-logins-list]")!.querySelectorAll("input")).toHaveLength(0);
  });

  it("adds a login for the bots chosen, and keeps nothing it was typed once saved", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [], onePassword: OFF }).mockResolvedValueOnce({ login: SHARED_LOGIN }).mockResolvedValueOnce({ logins: [SHARED_LOGIN], onePassword: OFF });
    await render();
    expect(host.textContent).toContain("Nothing saved yet.");
    await click("Add");
    await click("Add a login");
    await type("site", "crm.example.org");
    await type("username", "team");
    await type("password", "fake-typed-pass");
    await act(async () => { [...host.querySelectorAll<HTMLInputElement>('input[type="radio"]')][1]!.click(); });
    await act(async () => { host.querySelector<HTMLInputElement>('fieldset input[type="checkbox"]')!.click(); });
    await submit();
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins", { method: "POST", body: JSON.stringify({ site: "crm.example.org", username: "team", password: "fake-typed-pass", bots: ["pepper"] }) });
    expect(host.querySelector("[data-login-form]")).toBeNull();
    expect(host.innerHTML).not.toContain("fake-typed-pass");
  });

  it("keeps a new login to its exact address when asked", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [], onePassword: OFF }).mockResolvedValueOnce({ login: TENANT_LOGIN }).mockResolvedValueOnce({ logins: [TENANT_LOGIN], onePassword: OFF });
    await render();
    await click("Add");
    await click("Add a login");
    await type("site", "acme.okta.com");
    await type("password", "fake-tenant-pass");
    const exact = host.querySelector<HTMLInputElement>('input[name="exactHost"]')!;
    expect(exact.checked).toBe(false);
    expect(exact.closest("label")!.textContent).toBe("Only on this exact address, not elsewhere on its site");
    await act(async () => { exact.click(); });
    await submit();
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins", { method: "POST", body: JSON.stringify({ site: "acme.okta.com", username: "", password: "fake-tenant-pass", bots: "all", exactHost: true }) });
  });

  it("replaces a password, keeps the 2FA key unless asked, and changes who may use it", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF }).mockResolvedValueOnce({ login: CARD_LOGIN }).mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF });
    await render();
    await click("Change the login for example.com");
    expect(host.querySelector<HTMLInputElement>('input[name="password"]')!.value).toBe("");
    await type("password", "fake-new-pass");
    await act(async () => { host.querySelector<HTMLInputElement>('input[type="radio"]')!.click(); });
    await submit();
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/l1", { method: "PATCH", body: JSON.stringify({ password: "fake-new-pass", bots: "all" }) });
  });

  it("keeps a login to its sign-in hosts, or lets it fill on its whole site again", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF }).mockResolvedValueOnce({ login: CARD_LOGIN }).mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF });
    await render();
    await click("Change the login for example.com");
    const exact = host.querySelector<HTMLInputElement>('input[name="exactHost"]')!;
    expect(exact.closest("label")!.textContent).toBe("Only on example.com, login.microsoftonline.com, not elsewhere on example.com");
    await act(async () => { exact.click(); });
    await submit();
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/l1", { method: "PATCH", body: JSON.stringify({ bots: ["pepper"], exactHost: true }) });
  });

  it("moves a login to another site, and sends the site only when it changed", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [SHARED_LOGIN], onePassword: OFF }).mockResolvedValueOnce({ login: SHARED_LOGIN }).mockResolvedValueOnce({ logins: [SHARED_LOGIN], onePassword: OFF });
    await render();
    await click("Change the login for example.org");
    expect(host.querySelector<HTMLInputElement>('input[name="site"]')!.value).toBe("crm.example.org");
    await type("site", "d1abc.cloudfront.net");
    await submit();
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/l2", { method: "PATCH", body: JSON.stringify({ site: "d1abc.cloudfront.net", bots: "all" }) });
  });

  it("keeps every sign-in host when the site is only written differently", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF }).mockResolvedValueOnce({ login: CARD_LOGIN }).mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF });
    await render();
    await click("Change the login for example.com");
    await type("site", "https://EXAMPLE.com/login");
    await submit();
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/l1", { method: "PATCH", body: JSON.stringify({ bots: ["pepper"] }) });
  });

  it("deletes only on a second click", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF }).mockResolvedValueOnce({ ok: true }).mockResolvedValueOnce({ logins: [], onePassword: OFF });
    await render();
    await click("Delete the login for example.com");
    expect(fixture.api).toHaveBeenCalledTimes(1);
    await click("Delete for good");
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/l1", { method: "DELETE" });
  });

  it("connects 1Password with a masked token it then forgets, lists its logins read-only, and disconnects", async () => {
    const on = { connected: true, vaults: [{ id: "v1", title: "Shared with OMB", items: 1, bots: "all" }], items: 1 };
    fixture.api
      .mockResolvedValueOnce({ logins: [], onePassword: OFF })
      .mockResolvedValueOnce({ onePassword: on })
      .mockResolvedValueOnce({ logins: [VAULT_LOGIN], onePassword: on })
      .mockResolvedValueOnce({ onePassword: OFF })
      .mockResolvedValueOnce({ logins: [], onePassword: OFF });
    await render();
    expect(host.querySelector('input[name="onePasswordToken"]'), "no token field on the page itself").toBeNull();
    await click("Connect 1Password");
    expect(document.body.querySelector("[data-settings-dialog]")!.textContent).toContain("create a service account that can read one vault only");
    const field = document.body.querySelector<HTMLInputElement>('input[name="onePasswordToken"]')!;
    expect(field.type).toBe("password");
    await type("onePasswordToken", "ops_fake-typed-token");
    await act(async () => inDialog("Connect 1Password").click());
    await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/1password", { method: "POST", body: JSON.stringify({ token: "ops_fake-typed-token" }) });
    expect(host.querySelector("[data-onepassword]")!.textContent).toContain("Connected to Shared with OMB · 1 logins, keys and secrets");
    expect(host.innerHTML).not.toContain("ops_fake-typed-token");
    const row = host.querySelector('[data-login-row="example.com"]')!;
    expect(row.textContent).toContain("From 1Password · All bots");
    expect(row.querySelectorAll("button")).toHaveLength(0);
    await click("Disconnect");
    await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
    expect(fixture.api).toHaveBeenNthCalledWith(4, "/api/logins/1password", { method: "DELETE" });
    await click("Connect 1Password");
    expect(document.body.querySelector<HTMLInputElement>('input[name="onePasswordToken"]')!.value).toBe("");
  });

  it("keeps a 1Password vault to the bots chosen for it, on the same screen", async () => {
    const vault = { id: "v1", title: "Shared with OMB", items: 2, bots: "all" };
    const on = { connected: true, vaults: [vault], items: 2 };
    const narrowed = { ...on, vaults: [{ ...vault, bots: ["quill"] }] };
    fixture.api
      .mockResolvedValueOnce({ logins: [VAULT_LOGIN], onePassword: on })
      .mockResolvedValueOnce({ onePassword: narrowed })
      .mockResolvedValueOnce({ logins: [{ ...VAULT_LOGIN, bots: ["quill"] }], onePassword: narrowed });
    await render();
    const row = () => host.querySelector('[data-vault="Shared with OMB"]')!;
    expect(row().textContent).toContain("2 website logins · All bots");
    await click("Change who can use Shared with OMB");
    await act(async () => { [...row().querySelectorAll<HTMLInputElement>('input[type="radio"]')][1]!.click(); });
    await act(async () => { [...row().querySelectorAll<HTMLInputElement>('input[type="checkbox"]')][1]!.click(); });
    await click("Save changes");
    await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/1password/vaults/v1", { method: "PATCH", body: JSON.stringify({ bots: ["quill"] }) });
    expect(row().textContent).toContain("2 website logins · Quill");
    expect(host.querySelector('[data-login-row="example.com"]')!.textContent).toContain("From 1Password · Quill");
  });

  it("keeps one 1Password item to its own bots ahead of its vault's, and hands it back to the vault's choice", async () => {
    const on = { connected: true, vaults: [{ id: "v1", title: "Shared with OMB", items: 1, bots: "all" }], items: 1 };
    const follows = { ...VAULT_LOGIN, onePassword: { item: "v1:i1", ownBots: false } };
    const own = { ...VAULT_LOGIN, bots: ["quill"], onePassword: { item: "v1:i1", ownBots: true } };
    fixture.api
      .mockResolvedValueOnce({ logins: [follows], onePassword: on })
      .mockResolvedValueOnce({ logins: [own], onePassword: on })
      .mockResolvedValueOnce({ logins: [own], onePassword: on })
      .mockResolvedValueOnce({ logins: [follows], onePassword: on })
      .mockResolvedValueOnce({ logins: [follows], onePassword: on });
    await render();
    const row = () => host.querySelector('[data-login-row="example.com"]')!;
    const editor = () => host.querySelector('[data-onepassword-item="v1:i1"]')!;
    const flush = () => act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
    expect(row().textContent).toContain("From 1Password · All bots (its vault's choice)");
    await click("Change who can use example.com");
    expect(host.querySelector("[data-login-form]"), "a 1Password item's password is not changed here").toBeNull();
    expect(button("Use the vault's choice"), "it already follows its vault").toBeUndefined();
    await act(async () => { [...editor().querySelectorAll<HTMLInputElement>('input[type="radio"]')][1]!.click(); });
    await act(async () => { [...editor().querySelectorAll<HTMLInputElement>('input[type="checkbox"]')][1]!.click(); });
    await click("Save changes");
    await flush();
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/1password/items/v1/i1", { method: "PATCH", body: JSON.stringify({ bots: ["quill"] }) });
    expect(row().textContent).toContain("From 1Password · Quill");
    expect(row().textContent).not.toContain("vault's choice");
    await click("Change who can use example.com");
    await click("Use the vault's choice");
    await flush();
    expect(fixture.api).toHaveBeenNthCalledWith(4, "/api/logins/1password/items/v1/i1", { method: "PATCH", body: JSON.stringify({ bots: null }) });
    expect(row().textContent).toContain("From 1Password · All bots (its vault's choice)");
    expect(host.querySelector("[data-onepassword-item]")).toBeNull();
  });

  it("says why 1Password refused a token", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [], onePassword: OFF }).mockRejectedValueOnce(new Error("1Password did not accept the token."));
    await render();
    await click("Connect 1Password");
    await type("onePasswordToken", "ops_fake-wrong");
    await act(async () => inDialog("Connect 1Password").click());
    await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
    expect(document.body.querySelector("[data-settings-dialog] [role=alert]")?.textContent).toBe("1Password did not accept the token.");
  });

  it("on a Team Cloud, has an admin connect 1Password for the whole team, with the setup to use in one line", async () => {
    const hint = "Use a dedicated vault and a read-only service account.";
    fixture.config = { team: { id: "acme", name: "Acme" } };
    fixture.api.mockResolvedValueOnce({ logins: [], secrets: [], onePassword: OFF });
    await render();
    expect(host.querySelector("[data-onepassword-team]")!.textContent).toBe(`For the whole team: its bots use it in shared and private chats. ${hint}`);
    await click("Connect 1Password");
    expect(document.body.querySelector("[data-settings-dialog]")!.textContent).toContain(hint);
    await act(async () => root.unmount());
    root = createRoot(host);
    fixture.config = null;
    fixture.api.mockResolvedValueOnce({ logins: [], secrets: [], onePassword: OFF });
    await render();
    expect(host.querySelector("[data-onepassword]"), "off a team: the same row").not.toBeNull();
    expect(host.textContent, "off a team").not.toContain(hint);
  });

  it("connects Bitwarden with its server, API key and master password, sent once and forgotten, with the setup to use in one line", async () => {
    const on = { connected: true, server: "vw.example.test", account: "[email protected]", items: 2 };
    const linked = { id: "bitwarden:i1", name: "Stripe", fields: [], source: "linked", provider: "bitwarden", bots: "all", readOnly: true };
    const login = { ...VAULT_LOGIN, id: "bitwarden:i2:github.com", site: "github.com", hosts: ["github.com"], source: "bitwarden" };
    fixture.config = { team: { id: "acme", name: "Acme" } };
    fixture.api
      .mockResolvedValueOnce({ logins: [], secrets: [], onePassword: OFF, bitwarden: OFF })
      .mockResolvedValueOnce({ bitwarden: on })
      .mockResolvedValueOnce({ logins: [login], secrets: [linked], onePassword: OFF, bitwarden: on })
      .mockResolvedValueOnce({ bitwarden: OFF })
      .mockResolvedValueOnce({ logins: [], secrets: [], onePassword: OFF, bitwarden: OFF });
    await render();
    expect(host.querySelector("[data-bitwarden-team]")!.textContent).toContain("Use a dedicated Bitwarden account that can only see one collection.");
    await click("Connect Bitwarden");
    const dialog = () => document.body.querySelector("[data-settings-dialog]")!;
    expect(dialog().querySelector("[data-bitwarden-guide]")!.textContent).toMatch(/^Use a dedicated Bitwarden account that can only see one collection\./u);
    expect(dialog().querySelector('input[name="bitwardenServerUrl"]'), "no address field for bitwarden.com").toBeNull();
    await act(async () => {
      const select = dialog().querySelector<HTMLSelectElement>('select[name="bitwardenServer"]')!;
      select.value = "self-hosted";
      select.dispatchEvent(new Event("change", { bubbles: true }));
    });
    await type("bitwardenServerUrl", "https://vw.example.test");
    await type("bitwardenClientId", "user.fake-id");
    await type("bitwardenClientSecret", "fake-client-secret");
    await type("bitwardenPassword", "fake-master");
    for (const name of ["bitwardenClientSecret", "bitwardenPassword"]) expect(document.body.querySelector<HTMLInputElement>(`input[name="${name}"]`)!.type).toBe("password");
    await act(async () => [...dialog().querySelectorAll("button")].find((one) => one.textContent === "Connect Bitwarden")!.click());
    await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/bitwarden", { method: "POST",
      body: JSON.stringify({ serverUrl: "https://vw.example.test", clientId: "user.fake-id", clientSecret: "fake-client-secret", password: "fake-master" }) });
    expect(host.querySelector("[data-bitwarden-line]")!.textContent).toBe("Connected as [email protected] · 2 logins and secrets");
    expect(host.innerHTML).not.toContain("fake-master");
    expect(host.querySelector('[data-secret-row="Stripe"]')!.textContent).toContain("From Bitwarden · All bots");
    const row = host.querySelector('[data-login-row="github.com"]')!;
    expect(row.textContent).toContain("From Bitwarden");
    expect(row.querySelectorAll("button"), "read-only here").toHaveLength(0);
    await click("Disconnect Bitwarden");
    await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
    expect(fixture.api).toHaveBeenNthCalledWith(4, "/api/logins/bitwarden", { method: "DELETE" });
    await click("Connect Bitwarden");
    expect(document.body.querySelector<HTMLInputElement>('input[name="bitwardenPassword"]')!.value).toBe("");
  });

  it("shows no Bitwarden row where the server does not offer it", async () => {
    fixture.api.mockResolvedValueOnce({ logins: [], secrets: [], onePassword: OFF });
    await render();
    expect(host.querySelector("[data-onepassword]")).not.toBeNull();
    expect(host.querySelector("[data-bitwarden]")).toBeNull();
  });

  it("is for admins only", async () => {
    fixture.admin = false;
    await render();
    expect(fixture.api).not.toHaveBeenCalled();
    expect(host.textContent).toContain("Only an admin of this installation can see and change the Vault.");
  });

  it("shows a Team Cloud member only the secrets they gave bots in their own chats, which they can delete and nothing more", async () => {
    fixture.admin = false;
    fixture.config = { team: { id: "acme", name: "Acme" } };
    const own = { id: "s9", name: "Stripe secret key", source: "card", bots: ["pepper"], createdAt: 1, updatedAt: 1 };
    fixture.api.mockResolvedValueOnce({ logins: [], secrets: [own], onePassword: OFF }).mockResolvedValueOnce({ ok: true }).mockResolvedValueOnce({ logins: [], secrets: [], onePassword: OFF });
    await render();
    expect(fixture.api).toHaveBeenCalledWith("/api/vault");
    expect(host.textContent).toContain("Only you can use them, in your own chats with that bot");
    expect([...host.querySelectorAll("[data-secret-row]")].map((row) => row.textContent)).toEqual([expect.stringContaining("Saved from a chat · Pepper")]);
    for (const absent of ["Add", "Change Stripe secret key"]) expect(button(absent), absent).toBeUndefined();
    for (const selector of ["[data-onepassword]", "[data-vault-keys]", "[data-vault-filter]"]) expect(host.querySelector(selector), selector).toBeNull();
    await click("Delete Stripe secret key");
    await click("Delete for good");
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/vault/secrets/s9", { method: "DELETE" });
    expect(host.textContent).toContain("A key you type into a bot's card in your own chat shows here.");
  });
});

describe("Settings → Vault: keys and secrets", () => {
  const SAVED = { id: "s1", name: "Stripe secret key", source: "card", bots: ["pepper"], createdAt: 1, updatedAt: 1 };
  const LINKED = { id: "1password:v1:i9", name: "Notion token", fields: [], source: "linked", provider: "1password", bots: "all", readOnly: true };
  const listed = (secrets: unknown[] = [SAVED, LINKED]) => ({ logins: [CARD_LOGIN], secrets, onePassword: OFF });
  const shown = () => [...host.querySelectorAll("[data-login-row], [data-secret-row]")].map((row) => row.getAttribute("data-login-row") ?? row.getAttribute("data-secret-row"));
  async function fill(selector: string, value: string) {
    const field = host.querySelector<HTMLInputElement | HTMLTextAreaElement>(selector)!;
    const proto = field instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype;
    await act(async () => {
      Object.getOwnPropertyDescriptor(proto, "value")!.set!.call(field, value);
      field.dispatchEvent(new Event("input", { bubbles: true }));
    });
  }
  async function send(form: string) {
    await act(async () => { host.querySelector<HTMLFormElement>(`[data-${form}]`)!.requestSubmit(); });
    await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
  }

  it("lists saved and linked secrets beside logins by name only, and finds them by search and by kind", async () => {
    fixture.api.mockResolvedValueOnce(listed());
    await render();
    expect(shown()).toEqual(["example.com", "Notion token", "Stripe secret key"]);
    expect(host.querySelector('[data-secret-row="Stripe secret key"]')!.textContent).toContain("Saved from a chat · Pepper · Not used yet");
    const linked = host.querySelector('[data-secret-row="Notion token"]')!;
    expect(linked.textContent).toContain("From 1Password · All bots");
    expect(linked.querySelectorAll("button")).toHaveLength(0);
    expect(host.querySelector("[data-logins-list]")!.querySelectorAll("input")).toHaveLength(0);
    await fill("[data-vault-search]", "notion");
    expect(shown()).toEqual(["Notion token"]);
    await fill("[data-vault-search]", "nothing like it");
    expect(host.querySelector("[data-vault-none]")!.textContent).toBe("Nothing matches your search.");
    await fill("[data-vault-search]", "");
    await act(async () => host.querySelector<HTMLButtonElement>('[data-vault-filter="logins"]')!.click());
    expect(shown()).toEqual(["example.com"]);
    await act(async () => host.querySelector<HTMLButtonElement>('[data-vault-filter="secrets"]')!.click());
    expect(shown()).toEqual(["Notion token", "Stripe secret key"]);
  });

  it("adds a secret for the bots chosen, and keeps nothing it was typed once saved", async () => {
    fixture.api.mockResolvedValueOnce(listed([])).mockResolvedValueOnce({ secret: SAVED }).mockResolvedValueOnce(listed());
    await render();
    await click("Add");
    await click("Add a secret");
    await fill('input[name="secretName"]', " Stripe secret key ");
    await fill('input[name="secretValue"]', "sk_test_fake-typed-secret");
    expect(host.querySelector<HTMLInputElement>('input[name="secretValue"]')!.type).toBe("password");
    await send("secret-form");
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/vault/secrets", { method: "POST", body: JSON.stringify({ name: "Stripe secret key", value: "sk_test_fake-typed-secret", bots: "all" }) });
    expect(host.querySelector("[data-secret-form]")).toBeNull();
    expect(host.innerHTML).not.toContain("sk_test_fake-typed-secret");
  });

  it("renames a secret and changes who may use it without touching its value, and deletes it on a second click", async () => {
    fixture.api.mockResolvedValueOnce(listed()).mockResolvedValueOnce({ secret: SAVED }).mockResolvedValueOnce(listed())
      .mockResolvedValueOnce({ ok: true }).mockResolvedValueOnce(listed([LINKED]));
    await render();
    await click("Change Stripe secret key");
    expect(host.querySelector<HTMLInputElement>('input[name="secretValue"]')!.value).toBe("");
    await fill('input[name="secretName"]', "Stripe live key");
    await act(async () => { host.querySelector<HTMLInputElement>('[data-secret-form] input[type="radio"]')!.click(); });
    await send("secret-form");
    expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/vault/secrets/s1", { method: "PATCH", body: JSON.stringify({ name: "Stripe live key", bots: "all" }) });
    await click("Delete Stripe secret key");
    expect(fixture.api).toHaveBeenCalledTimes(3);
    await click("Delete for good");
    expect(fixture.api).toHaveBeenNthCalledWith(4, "/api/vault/secrets/s1", { method: "DELETE" });
  });

  it("says which MCP servers lost their key when a secret is deleted", async () => {
    fixture.api.mockResolvedValueOnce(listed()).mockResolvedValueOnce({ ok: true, keysOff: ["stripe", "notion"] }).mockResolvedValueOnce(listed([LINKED]));
    await render();
    await click("Delete Stripe secret key");
    await click("Delete for good");
    expect(host.querySelector("[data-vault-keys-off]")?.textContent).toContain("stripe, notion");
  });

  it("imports a pasted .env file as one secret per line", async () => {
    fixture.api.mockImplementation(async (path: string) => (path === "/api/vault" ? listed([]) : { secret: SAVED }));
    await render();
    await click("Add");
    await click("Paste a .env file");
    await fill('textarea[name="envText"]', "# keys\nexport STRIPE_SECRET_KEY=sk_test_fake-one\nNOTION_TOKEN=\"fake two\"\n");
    await send("env-form");
    const posts = fixture.api.mock.calls.filter(([, init]) => (init as RequestInit | undefined)?.method === "POST").map(([, init]) => JSON.parse(String((init as RequestInit).body)));
    expect(posts).toEqual([{ name: "STRIPE_SECRET_KEY", value: "sk_test_fake-one", bots: "all" }, { name: "NOTION_TOKEN", value: "fake two", bots: "all" }]);
    expect(host.querySelector<HTMLTextAreaElement>('textarea[name="envText"]')!.value).toBe("");
    expect(host.querySelector("[data-env-form] [role=status]")!.textContent).toBe("Saved to the Vault: 2.");
  });

  it("sends keys the app uses itself to API keys", async () => {
    fixture.api.mockResolvedValueOnce(listed([]));
    await render();
    expect(host.querySelector("[data-vault-keys]")!.textContent).toContain("AI and app keys");
    await act(async () => host.querySelector<HTMLButtonElement>("[data-vault-keys] button")!.click());
    expect(fixture.dispatch).toHaveBeenCalledWith({ type: "toggleAppSettings", open: true, section: "connections" });
  });
});

describe("Settings → Vault with My Cloud", () => {
  afterEach(() => { delete (window as { ogb?: unknown }).ogb; });
  const where = (site: string) => host.querySelector(`[data-login-row="${site}"] [data-login-where]`)?.textContent;

  it("without My Cloud says nothing of sides; on My Cloud itself says its logins are its own and sends nothing", async () => {
    (window as { ogb?: unknown }).ogb = { cloudLogins: { state: vi.fn().mockResolvedValue({ cloud: null }), send: vi.fn() } };
    fixture.api.mockResolvedValueOnce({ logins: [SHARED_LOGIN], onePassword: OFF });
    await render();
    expect(host.querySelector("[data-logins-sides]")).toBeNull();
    expect(host.querySelector("[data-also-cloud]")).toBeNull();
    expect(where("example.org")).toBeUndefined();
    act(() => root.unmount());
    root = createRoot(host);
    delete (window as { ogb?: unknown }).ogb;
    fixture.config = { cloudHome: true };
    fixture.api.mockResolvedValueOnce({ logins: [SHARED_LOGIN], onePassword: OFF });
    await render();
    expect(host.querySelector("[data-logins-cloud-own]")!.textContent).toContain("come here by themselves");
    expect(host.querySelector("[data-also-cloud]")).toBeNull();
  });
});