Skip to content

Automate browser based workflows with AI

39.0 KB
Raw Blame
// @vitest-environment jsdom

import {
  act,
  cleanup,
  fireEvent,
  render,
  screen,
  waitFor,
  within,
} from "@testing-library/react";
import { useEffect, useState } from "react";
import { MemoryRouter } from "react-router-dom";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { CredentialAuthenticatorSupportProvider } from "./CredentialAuthenticatorSupportContext";
import { PasswordCredentialContent } from "./PasswordCredentialContent";

const mockEmailCredentials = vi.hoisted(() => ({
  google: vi.fn(),
  microsoft: vi.fn(),
}));

vi.mock("@/hooks/useGoogleOAuthCredentials", async (importActual) => {
  const actual =
    await importActual<typeof import("@/hooks/useGoogleOAuthCredentials")>();
  return {
    ...actual,
    useGoogleOAuthCredentials: mockEmailCredentials.google,
  };
});

vi.mock("@/hooks/useMicrosoftOAuthCredentials", async (importActual) => {
  const actual =
    await importActual<typeof import("@/hooks/useMicrosoftOAuthCredentials")>();
  return {
    ...actual,
    useMicrosoftOAuthCredentials: mockEmailCredentials.microsoft,
  };
});

type Values = {
  name: string;
  username: string;
  password: string;
  totp: string;
  totp_type: string;
  totp_identifier: string;
};

const INITIAL_VALUES: Values = {
  name: "",
  username: "",
  password: "",
  totp: "",
  totp_type: "none",
  totp_identifier: "",
};

const SAVED_VALUES: Values = {
  name: "Example Login",
  username: "[email protected]",
  password: "",
  totp: "",
  totp_type: "email",
  totp_identifier: "[email protected]",
};

const ENTERPRISE_APPS = {
  label: "Enterprise QR support",
  apps: ["Enterprise Authenticator A", "Enterprise Authenticator B"],
  contactUrl: "https://www.skyvern.com/contact",
  qrCodeTypes: [
    {
      id: "example",
      label: "Enterprise Authenticator A",
    },
    {
      id: "opaque",
      label: "Enterprise Authenticator B",
    },
  ],
  inferQrCodeType: (value: string) => {
    if (value.startsWith("example-authenticator://")) {
      return "example";
    }
    if (value.startsWith("opaque-authenticator://")) {
      return "opaque";
    }
    return null;
  },
  vendorLabels: {
    opaque: "Enterprise Authenticator B",
  },
  description:
    "Scan the QR as usual - Skyvern detects these setup codes automatically.",
};

function installEmailCredentialHooks() {
  mockEmailCredentials.google.mockReturnValue({
    credentials: [
      {
        id: "google-mail",
        organization_id: "org_1",
        credential_name: "Default",
        state: "active",
        scopes_granted: ["https://www.googleapis.com/auth/gmail.readonly"],
        email_address: "[email protected]",
        created_at: "2026-07-30T00:00:00Z",
        modified_at: "2026-07-30T00:00:00Z",
      },
    ],
    isLoading: false,
    isFetching: false,
  });
  mockEmailCredentials.microsoft.mockReturnValue({
    credentials: [],
    isLoading: false,
    isFetching: false,
  });
}

describe("PasswordCredentialContent — additional two-factor methods", () => {
  const additionalMethod = {
    value: "security_device",
    requestType: "none" as const,
    label: "Security Device",
    initialState: { deviceCode: "" },
    renderFields: ({
      state,
      setState,
      configured,
    }: {
      state: Record<string, string | number | boolean>;
      setState: (next: Record<string, string | number | boolean>) => void;
      disabled: boolean;
      isEditMode: boolean;
      configured: boolean;
    }) => (
      <>
        <label>
          Device code
          <input
            aria-label="Device code"
            value={String(state.deviceCode ?? "")}
            onChange={(event) =>
              setState({ ...state, deviceCode: event.target.value })
            }
          />
        </label>
        <span>{configured ? "Configured" : "Not configured"}</span>
      </>
    ),
    validate: () => null,
    onSaved: vi.fn(),
  };

  it("renders methods supplied by the context as additional cards", () => {
    render(
      <CredentialAuthenticatorSupportProvider
        value={{ additionalTwoFactorMethods: [additionalMethod] }}
      >
        <MemoryRouter>
          <PasswordCredentialContent
            values={INITIAL_VALUES}
            onChange={vi.fn()}
            additionalTwoFactorStates={{
              security_device: additionalMethod.initialState,
            }}
            onAdditionalTwoFactorStateChange={vi.fn()}
          />
        </MemoryRouter>
      </CredentialAuthenticatorSupportProvider>,
    );

    fireEvent.click(screen.getByText("Two-Factor Authentication"));
    fireEvent.click(screen.getByText("Security Device"));

    expect(screen.getByLabelText("Device code")).toBeTruthy();
    expect(screen.getByText("Not configured")).toBeTruthy();
  });

  it("marks a response-matched method as configured in edit mode", () => {
    render(
      <CredentialAuthenticatorSupportProvider
        value={{ additionalTwoFactorMethods: [additionalMethod] }}
      >
        <MemoryRouter>
          <PasswordCredentialContent
            values={{ ...INITIAL_VALUES, totp_type: "security_device" }}
            onChange={vi.fn()}
            editMode
            editingGroups={{ name: false, values: true }}
            configuredAdditionalTwoFactorMethod="security_device"
            additionalTwoFactorStates={{
              security_device: additionalMethod.initialState,
            }}
            onAdditionalTwoFactorStateChange={vi.fn()}
          />
        </MemoryRouter>
      </CredentialAuthenticatorSupportProvider>,
    );

    expect(screen.getByText("Security Device")).toBeTruthy();
    expect(screen.getByText("Configured")).toBeTruthy();
  });

  it("does not render an additional card with the default context", () => {
    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={INITIAL_VALUES}
          onChange={vi.fn()}
          additionalTwoFactorStates={{}}
          onAdditionalTwoFactorStateChange={vi.fn()}
        />
      </MemoryRouter>,
    );

    fireEvent.click(screen.getByText("Two-Factor Authentication"));

    expect(screen.queryByText("Security Device")).toBeNull();
  });
});

describe("PasswordCredentialContent — built-in two-factor methods", () => {
  beforeEach(() => {
    installEmailCredentialHooks();
  });

  it("keeps 2FA optional and allows the selected method to be deselected", () => {
    const onChange = vi.fn();
    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={INITIAL_VALUES}
          onChange={onChange}
        />
      </MemoryRouter>,
    );

    fireEvent.click(screen.getByText("Two-Factor Authentication"));
    expect(onChange).not.toHaveBeenCalled();

    const methodNames = ["Authenticator App", "Email", "Text Message"];
    const methodButtons = methodNames.map((name) =>
      screen.getByRole("button", { name }),
    );

    for (const button of methodButtons) {
      expect(button).toBeInstanceOf(HTMLButtonElement);
      expect((button as HTMLButtonElement).type).toBe("button");
      expect(button.tabIndex).toBe(0);
      expect(button.getAttribute("aria-pressed")).toBe("false");
    }

    const emailButton = methodButtons[1]!;
    emailButton.focus();
    expect(document.activeElement).toBe(emailButton);
    fireEvent.click(emailButton);

    expect(emailButton.getAttribute("aria-pressed")).toBe("true");
    expect(methodButtons[0]?.getAttribute("aria-pressed")).toBe("false");

    onChange.mockClear();
    fireEvent.click(screen.getByText("Two-Factor Authentication"));
    expect(onChange).not.toHaveBeenCalled();
    fireEvent.click(screen.getByText("Two-Factor Authentication"));

    fireEvent.click(screen.getByRole("button", { name: "Email" }));

    expect(
      screen
        .getByRole("button", { name: "Email" })
        .getAttribute("aria-pressed"),
    ).toBe("false");
    expect(onChange).toHaveBeenLastCalledWith(
      expect.objectContaining({ totp_type: "none" }),
    );
  });
});

// Mirrors CredentialsModal: starts the form at INITIAL_VALUES, then after mount
// applies the loaded credential via setPasswordCredentialValues — same flow
// that produced SKY-9864.
function ModalLikeHarness({
  appliedValues,
  onChangeSpy,
  editingValues = false,
}: {
  appliedValues: Values | null;
  onChangeSpy: (next: Values) => void;
  editingValues?: boolean;
}) {
  const [values, setValues] = useState<Values>(INITIAL_VALUES);
  useEffect(() => {
    if (appliedValues) {
      setValues(appliedValues);
    }
  }, [appliedValues]);
  return (
    <MemoryRouter>
      <PasswordCredentialContent
        values={values}
        onChange={(next) => {
          onChangeSpy(next);
          setValues(next);
        }}
        editMode
        editingGroups={{ name: false, values: editingValues }}
      />
    </MemoryRouter>
  );
}

describe("PasswordCredentialContent — edit-mode hydration (SKY-9864 regression)", () => {
  beforeEach(() => {
    installEmailCredentialHooks();
  });

  afterEach(() => {
    cleanup();
    vi.unstubAllGlobals();
  });

  it("preserves saved totp_identifier when modal hydrates an email-TOTP credential into a form that started at PASSWORD_CREDENTIAL_INITIAL_VALUES", async () => {
    // Reproduces the bug:
    //   1. CredentialsModal initially renders PasswordCredentialContent with
    //      PASSWORD_CREDENTIAL_INITIAL_VALUES (totp_type="none").
    //   2. The modal's useEffect then setPasswordCredentialValues({...cred...})
    //      with the saved cred — totp_type="email" plus the saved
    //      totp_identifier (e.g. "[email protected]").
    //   3. The sync useEffect inside PasswordCredentialContent flips totpMethod
    //      from default "authenticator" to "email", which triggers the
    //      auto-fill useEffect with methodChanged=true and silently overwrites
    //      the saved totp_identifier with username.
    const onChangeSpy = vi.fn();

    const { rerender } = render(
      <ModalLikeHarness appliedValues={null} onChangeSpy={onChangeSpy} />,
    );

    // Now simulate the modal applying the loaded credential's values via
    // setPasswordCredentialValues (a single render with a non-null appliedValues
    // prop, then the inner useEffect calls setValues).
    await act(async () => {
      rerender(
        <ModalLikeHarness
          appliedValues={SAVED_VALUES}
          onChangeSpy={onChangeSpy}
        />,
      );
    });

    // The component must NOT have called onChange with totp_identifier set to
    // the username — that would silently corrupt the saved value on next save.
    const corruptingCalls = onChangeSpy.mock.calls.filter((call) => {
      const next = call[0] as Values;
      return (
        next.totp_identifier === SAVED_VALUES.username &&
        next.totp_identifier !== SAVED_VALUES.totp_identifier
      );
    });
    expect(corruptingCalls).toEqual([]);
  });

  it("auto-fills totp_identifier with the username when the user explicitly switches the method to Email and the identifier is blank", async () => {
    const onChangeSpy = vi.fn();

    render(
      <ModalLikeHarness
        appliedValues={{
          name: "New cred",
          username: "[email protected]",
          password: "",
          totp: "",
          totp_type: "none",
          totp_identifier: "",
        }}
        onChangeSpy={onChangeSpy}
      />,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Two-Factor Authentication"));
    });
    await act(async () => {
      fireEvent.click(screen.getByText("Email"));
    });

    const autoFillCall = onChangeSpy.mock.calls.find((call) => {
      const next = call[0] as Values;
      return (
        next.totp_type === "email" &&
        next.totp_identifier === "[email protected]"
      );
    });
    expect(autoFillCall).toBeTruthy();
  });

  it("renders the connected account picker for Email and preserves manual entry", async () => {
    const onChangeSpy = vi.fn();

    render(
      <ModalLikeHarness
        appliedValues={{
          name: "Example Login",
          username: "[email protected]",
          password: "",
          totp: "",
          totp_type: "email",
          totp_identifier: "[email protected]",
        }}
        onChangeSpy={onChangeSpy}
        editingValues
      />,
    );

    expect(
      await screen.findByRole("combobox", {
        name: "Connected email account",
      }),
    ).toBeTruthy();
    const modeToggle = screen.getByRole("button", { name: "Enter manually" });
    fireEvent.click(modeToggle);
    const identifierInput = modeToggle
      .closest(".min-w-0")
      ?.querySelector("input");
    expect(identifierInput).toBeTruthy();
    fireEvent.change(identifierInput as HTMLInputElement, {
      target: { value: "[email protected]" },
    });

    expect(onChangeSpy).toHaveBeenCalledWith(
      expect.objectContaining({
        totp_type: "email",
        totp_identifier: "[email protected]",
      }),
    );
  });

  it("enables editing from a locked connected account picker", async () => {
    const onEnableEditValues = vi.fn();

    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "Example Login",
            username: "[email protected]",
            password: "",
            totp: "",
            totp_type: "email",
            totp_identifier: "[email protected]",
          }}
          onChange={vi.fn()}
          editMode
          editingGroups={{ name: false, values: false }}
          onEnableEditValues={onEnableEditValues}
        />
      </MemoryRouter>,
    );

    const picker = await screen.findByRole("combobox", {
      name: "Connected email account",
    });
    const pickerField = picker.closest(".relative");
    expect(pickerField).toBeTruthy();

    const editButton = within(pickerField as HTMLElement).getByRole("button", {
      name: "Edit credential values",
    });
    fireEvent.click(editButton);

    expect(onEnableEditValues).toHaveBeenCalledOnce();
  });

  it("lets users enter an unset authenticator key without clicking the edit button", async () => {
    const onChangeSpy = vi.fn();
    const onEnableEditValues = vi.fn();

    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "Example Login",
            username: "[email protected]",
            password: "",
            totp: "",
            totp_type: "none",
            totp_identifier: "",
          }}
          onChange={onChangeSpy}
          editMode
          editingGroups={{ name: false, values: false }}
          onEnableEditValues={onEnableEditValues}
        />
      </MemoryRouter>,
    );

    fireEvent.click(screen.getByText("Two-Factor Authentication"));
    fireEvent.click(screen.getByRole("button", { name: "Authenticator App" }));

    const authenticatorKeyInput = screen.getByPlaceholderText(
      "e.g. JBSWY3DPEHPK3PXP",
    );
    expect(screen.getByRole("button", { name: "Scan QR" })).toBeTruthy();

    fireEvent.change(authenticatorKeyInput, {
      target: { value: "JBSWY3DPEHPK3PXP" },
    });

    expect(onEnableEditValues).toHaveBeenCalled();
    expect(onChangeSpy).toHaveBeenCalledWith(
      expect.objectContaining({
        totp: "JBSWY3DPEHPK3PXP",
        totp_type: "authenticator",
      }),
    );
  });

  it("keeps a configured authenticator key masked while credential values are locked", () => {
    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "Example Login",
            username: "[email protected]",
            password: "",
            totp: "",
            totp_type: "authenticator",
            totp_identifier: "",
          }}
          onChange={vi.fn()}
          editMode
          editingGroups={{ name: false, values: false }}
          onEnableEditValues={vi.fn()}
        />
      </MemoryRouter>,
    );

    expect(screen.getAllByDisplayValue("••••••••")).toHaveLength(2);
    expect(screen.queryByPlaceholderText("e.g. JBSWY3DPEHPK3PXP")).toBeNull();
  });

  it("does not select a 2FA method when a new credential opens the section", async () => {
    const onChangeSpy = vi.fn();

    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "New cred",
            username: "[email protected]",
            password: "password",
            totp: "",
            totp_type: "none",
            totp_identifier: "",
          }}
          onChange={onChangeSpy}
        />
      </MemoryRouter>,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Two-Factor Authentication"));
    });

    expect(onChangeSpy).not.toHaveBeenCalled();
    expect(
      screen
        .getByRole("button", { name: "Authenticator App" })
        .getAttribute("aria-pressed"),
    ).toBe("false");
  });

  it("keeps Authenticator App selected when the user types a key", async () => {
    const onChangeSpy = vi.fn();

    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "New cred",
            username: "[email protected]",
            password: "password",
            totp: "",
            totp_type: "none",
            totp_identifier: "",
          }}
          onChange={onChangeSpy}
        />
      </MemoryRouter>,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Two-Factor Authentication"));
    });
    fireEvent.click(screen.getByRole("button", { name: "Authenticator App" }));
    fireEvent.change(screen.getByPlaceholderText("e.g. JBSWY3DPEHPK3PXP"), {
      target: { value: "JBSWY3DPEHPK3PXP" },
    });

    const typedCall = onChangeSpy.mock.calls.find((call) => {
      const next = call[0] as Values;
      return (
        next.totp_type === "authenticator" && next.totp === "JBSWY3DPEHPK3PXP"
      );
    });
    expect(typedCall).toBeTruthy();
  });

  it("imports an otpauth URI from an uploaded QR code image", async () => {
    const onChangeSpy = vi.fn();
    const imageBitmap = { close: vi.fn() } as unknown as ImageBitmap;
    class MockBarcodeDetector {
      detect = vi.fn().mockResolvedValue([
        {
          rawValue:
            "otpauth://totp/[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example",
        },
      ]);
    }
    vi.stubGlobal("createImageBitmap", vi.fn().mockResolvedValue(imageBitmap));
    vi.stubGlobal("BarcodeDetector", MockBarcodeDetector);

    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "New cred",
            username: "[email protected]",
            password: "password",
            totp: "",
            totp_type: "none",
            totp_identifier: "",
          }}
          onChange={onChangeSpy}
        />
      </MemoryRouter>,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Two-Factor Authentication"));
    });
    fireEvent.click(screen.getByRole("button", { name: "Authenticator App" }));
    fireEvent.change(screen.getByLabelText("Upload QR code image"), {
      target: {
        files: [new File(["qr"], "totp.png", { type: "image/png" })],
      },
    });

    await waitFor(() => {
      const qrCall = onChangeSpy.mock.calls.find((call) => {
        const next = call[0] as Values;
        return (
          next.totp_type === "authenticator" &&
          next.totp.startsWith("otpauth://totp/")
        );
      });
      expect(qrCall).toBeTruthy();
    });
    expect(
      screen
        .getByRole("button", { name: /Google Authenticator/ })
        .getAttribute("aria-pressed"),
    ).toBe("true");
    expect(
      screen.getByTestId("authenticator-qr-type-detection").textContent,
    ).toBe("Detected from QR");
    expect(imageBitmap.close).toHaveBeenCalled();
  });

  it("imports a non-otpauth QR payload for server-side validation", async () => {
    const onChangeSpy = vi.fn();
    const imageBitmap = { close: vi.fn() } as unknown as ImageBitmap;
    class MockBarcodeDetector {
      detect = vi.fn().mockResolvedValue([
        {
          rawValue: "custom-authenticator://activate?payload=opaque",
        },
      ]);
    }
    vi.stubGlobal("createImageBitmap", vi.fn().mockResolvedValue(imageBitmap));
    vi.stubGlobal("BarcodeDetector", MockBarcodeDetector);

    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "New cred",
            username: "[email protected]",
            password: "password",
            totp: "",
            totp_type: "none",
            totp_identifier: "",
          }}
          onChange={onChangeSpy}
        />
      </MemoryRouter>,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Two-Factor Authentication"));
    });
    fireEvent.click(screen.getByRole("button", { name: "Authenticator App" }));
    fireEvent.change(screen.getByLabelText("Upload QR code image"), {
      target: {
        files: [new File(["qr"], "totp.png", { type: "image/png" })],
      },
    });

    await waitFor(() => {
      const qrCall = onChangeSpy.mock.calls.find((call) => {
        const next = call[0] as Values;
        return (
          next.totp_type === "authenticator" &&
          next.totp === "custom-authenticator://activate?payload=opaque"
        );
      });
      expect(qrCall).toBeTruthy();
    });
    expect(
      screen
        .getByRole("button", { name: /Google Authenticator/ })
        .getAttribute("aria-pressed"),
    ).toBe("false");
    expect(screen.queryByText("Detected from QR")).toBeNull();
    expect(imageBitmap.close).toHaveBeenCalled();
  });

  it("shows an inline fallback when the browser cannot scan QR codes", async () => {
    const onChangeSpy = vi.fn();
    vi.stubGlobal("BarcodeDetector", undefined);

    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "New cred",
            username: "[email protected]",
            password: "password",
            totp: "",
            totp_type: "none",
            totp_identifier: "",
          }}
          onChange={onChangeSpy}
        />
      </MemoryRouter>,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Two-Factor Authentication"));
    });
    fireEvent.click(screen.getByRole("button", { name: "Authenticator App" }));
    fireEvent.change(screen.getByLabelText("Upload QR code image"), {
      target: {
        files: [new File(["qr"], "totp.png", { type: "image/png" })],
      },
    });

    await waitFor(() => {
      expect(
        screen.getByText(
          "QR scanning is not supported by this browser. Paste the setup key or otpauth:// URI instead.",
        ),
      ).toBeTruthy();
    });
  });

  it("shows the QR fallback when the browser rejects QR detection", async () => {
    const onChangeSpy = vi.fn();
    const createImageBitmapMock = vi.fn();
    class MockBarcodeDetector {
      constructor() {
        throw new TypeError("Unsupported barcode format");
      }

      detect = vi.fn();
    }
    vi.stubGlobal("createImageBitmap", createImageBitmapMock);
    vi.stubGlobal("BarcodeDetector", MockBarcodeDetector);

    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "New cred",
            username: "[email protected]",
            password: "password",
            totp: "",
            totp_type: "none",
            totp_identifier: "",
          }}
          onChange={onChangeSpy}
        />
      </MemoryRouter>,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Two-Factor Authentication"));
    });
    fireEvent.click(screen.getByRole("button", { name: "Authenticator App" }));
    fireEvent.change(screen.getByLabelText("Upload QR code image"), {
      target: {
        files: [new File(["qr"], "totp.png", { type: "image/png" })],
      },
    });

    await waitFor(() => {
      expect(
        screen.getByText(
          "QR scanning is not supported by this browser. Paste the setup key or otpauth:// URI instead.",
        ),
      ).toBeTruthy();
    });
    expect(createImageBitmapMock).not.toHaveBeenCalled();
  });

  it("preserves an intentionally-empty totp_identifier when hydrating a saved email-TOTP credential", async () => {
    // Regression for the empty-string false-positive in the username-rename
    // follow-on path: at mount, prevUsername="" and identifier="". After
    // hydration, identifier is still "" — same value as prevUsername, so
    // identifierMatchedPrevUsername=true. Without the prevUsername!=="" guard,
    // the effect silently fires updateValues({ totp_identifier: username }),
    // overwriting a value the user deliberately saved as blank.
    const onChangeSpy = vi.fn();

    const { rerender } = render(
      <ModalLikeHarness appliedValues={null} onChangeSpy={onChangeSpy} />,
    );

    await act(async () => {
      rerender(
        <ModalLikeHarness
          appliedValues={{
            name: "Example Login",
            username: "[email protected]",
            password: "",
            totp: "",
            totp_type: "email",
            totp_identifier: "",
          }}
          onChangeSpy={onChangeSpy}
        />,
      );
    });

    const refillCalls = onChangeSpy.mock.calls.filter((call) => {
      const next = call[0] as Values;
      return next.totp_identifier === "[email protected]";
    });
    expect(refillCalls).toEqual([]);
  });

  it("clears totp_identifier when the user switches the method from Email to Text", async () => {
    // Symmetric to the Text → Email reseed case: an email-shaped identifier
    // (or anything from email mode) isn't a valid phone number, so clicking
    // Text on a saved email-TOTP credential must wipe the field.
    const onChangeSpy = vi.fn();

    render(
      <ModalLikeHarness
        appliedValues={{
          name: "Example Login",
          username: "[email protected]",
          password: "",
          totp: "",
          totp_type: "email",
          totp_identifier: "[email protected]",
        }}
        onChangeSpy={onChangeSpy}
      />,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Text Message"));
    });

    const clearCall = onChangeSpy.mock.calls.find((call) => {
      const next = call[0] as Values;
      return next.totp_type === "text" && next.totp_identifier === "";
    });
    expect(clearCall).toBeTruthy();
  });

  it("reseeds totp_identifier to username when the user switches the method from Text to Email", async () => {
    // Regression for the Text→Email case raised in PR review: a phone
    // number entered under Text mode must NOT silently persist as the
    // email identifier when the user clicks the Email tile, since it's
    // not a valid email identifier.
    const onChangeSpy = vi.fn();

    render(
      <ModalLikeHarness
        appliedValues={{
          name: "Example Login",
          username: "[email protected]",
          password: "",
          totp: "",
          totp_type: "text",
          totp_identifier: "+14155551234",
        }}
        onChangeSpy={onChangeSpy}
      />,
    );

    await act(async () => {
      fireEvent.click(screen.getByText("Email"));
    });

    const reseedCall = onChangeSpy.mock.calls.find((call) => {
      const next = call[0] as Values;
      return (
        next.totp_type === "email" &&
        next.totp_identifier === "[email protected]"
      );
    });
    expect(reseedCall).toBeTruthy();
  });

  it("allows QR upload to set the TOTP payload", async () => {
    const onChangeSpy = vi.fn();
    const close = vi.fn();
    const detect = vi
      .fn()
      .mockResolvedValue([{ rawValue: "decoded-qr-payload" }]);
    class BarcodeDetector {
      detect: typeof detect;

      constructor() {
        this.detect = detect;
      }
    }
    vi.stubGlobal("BarcodeDetector", BarcodeDetector);
    vi.stubGlobal("createImageBitmap", vi.fn().mockResolvedValue({ close }));

    const { container } = render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={{
            name: "Example Login",
            username: "[email protected]",
            password: "password",
            totp: "",
            totp_type: "authenticator",
            totp_identifier: "",
          }}
          onChange={onChangeSpy}
        />
      </MemoryRouter>,
    );
    const input = container.querySelector('input[type="file"]');
    expect(input).toBeTruthy();
    const file = new File(["qr"], "qr.png", { type: "image/png" });

    await act(async () => {
      fireEvent.change(input as HTMLInputElement, {
        target: { files: [file] },
      });
    });

    await waitFor(() =>
      expect(onChangeSpy).toHaveBeenCalledWith(
        expect.objectContaining({
          totp: "decoded-qr-payload",
          totp_type: "authenticator",
        }),
      ),
    );
    expect(close).toHaveBeenCalled();
  });
});

describe("PasswordCredentialContent — supported authenticator copy", () => {
  afterEach(() => {
    cleanup();
    vi.unstubAllGlobals();
  });

  const NEW_VALUES: Values = {
    name: "New cred",
    username: "[email protected]",
    password: "password",
    totp: "",
    totp_type: "none",
    totp_identifier: "",
  };

  function stubQrCodeScan(rawValue: string) {
    const imageBitmap = { close: vi.fn() } as unknown as ImageBitmap;
    class MockBarcodeDetector {
      detect = vi.fn().mockResolvedValue([{ rawValue }]);
    }
    vi.stubGlobal("createImageBitmap", vi.fn().mockResolvedValue(imageBitmap));
    vi.stubGlobal("BarcodeDetector", MockBarcodeDetector);
    return imageBitmap;
  }

  async function uploadQrCodeImage() {
    await act(async () => {
      fireEvent.change(screen.getByLabelText("Upload QR code image"), {
        target: {
          files: [new File(["qr"], "totp.png", { type: "image/png" })],
        },
      });
    });
  }

  async function openAuthenticatorSection(
    enterpriseApps?: typeof ENTERPRISE_APPS,
    onChangeSpy = vi.fn(),
  ) {
    const content = (
      <MemoryRouter>
        <PasswordCredentialContent values={NEW_VALUES} onChange={onChangeSpy} />
      </MemoryRouter>
    );
    render(
      enterpriseApps ? (
        <CredentialAuthenticatorSupportProvider value={{ enterpriseApps }}>
          {content}
        </CredentialAuthenticatorSupportProvider>
      ) : (
        content
      ),
    );
    await act(async () => {
      fireEvent.click(screen.getByText("Two-Factor Authentication"));
    });
    fireEvent.click(screen.getByRole("button", { name: "Authenticator App" }));
    return onChangeSpy;
  }

  it("always lists generic TOTP apps", async () => {
    await openAuthenticatorSection();
    expect(
      screen.getByText(/Google Authenticator, Authy, 1Password/),
    ).toBeTruthy();
  });

  it("omits enterprise QR type options in the default/OSS context", async () => {
    await openAuthenticatorSection();
    expect(
      screen.getByRole("button", { name: /Google Authenticator/ }),
    ).toBeTruthy();
    expect(screen.queryByText("Enterprise Authenticator A")).toBeNull();
    expect(screen.queryByText("Enterprise Authenticator B")).toBeNull();
    expect(
      screen.queryByText(/detects these setup codes automatically/),
    ).toBeNull();
  });

  it("renders Cloud provider QR type options when the context provides apps", async () => {
    await openAuthenticatorSection(ENTERPRISE_APPS);
    const selector = screen.getByTestId("authenticator-qr-type-selector");
    expect(selector).toBeTruthy();
    const keyInput = screen.getByPlaceholderText("e.g. JBSWY3DPEHPK3PXP");
    expect(
      selector.compareDocumentPosition(keyInput) &
        Node.DOCUMENT_POSITION_FOLLOWING,
    ).toBeTruthy();
    expect(
      screen.getByRole("button", { name: /Google Authenticator/ }),
    ).toBeTruthy();
    expect(
      screen.getByRole("button", { name: "Enterprise Authenticator A" }),
    ).toBeTruthy();
    expect(
      screen.getByRole("button", { name: "Enterprise Authenticator B" }),
    ).toBeTruthy();
    expect(
      screen.getByText(/detects these setup codes automatically/),
    ).toBeTruthy();
    expect(
      selector.querySelectorAll('[data-testid="authenticator-type-logo"]'),
    ).toHaveLength(3);
  });

  it("highlights the Cloud provider QR type inferred from an uploaded QR code", async () => {
    const imageBitmap = stubQrCodeScan(
      "example-authenticator://activate?payload=opaque",
    );
    const onChangeSpy = await openAuthenticatorSection(ENTERPRISE_APPS);
    await uploadQrCodeImage();

    await waitFor(() => {
      const qrCall = onChangeSpy.mock.calls.find((call) => {
        const next = call[0] as Values;
        return (
          next.totp_type === "authenticator" &&
          next.totp === "example-authenticator://activate?payload=opaque"
        );
      });
      expect(qrCall).toBeTruthy();
    });
    expect(
      screen
        .getByRole("button", { name: /Enterprise Authenticator A/ })
        .getAttribute("aria-pressed"),
    ).toBe("true");
    expect(
      screen.getByTestId("authenticator-qr-type-detection").textContent,
    ).toBe("Detected from QR");
    expect(imageBitmap.close).toHaveBeenCalled();
  });

  it("announces the inferred type via a polite live region naming the detected app", async () => {
    stubQrCodeScan("example-authenticator://activate?payload=opaque");
    await openAuthenticatorSection(ENTERPRISE_APPS);
    await uploadQrCodeImage();

    const liveRegion = await waitFor(() => {
      const region = screen
        .getByTestId("authenticator-qr-type-detection")
        .closest('[role="status"]');
      expect(region).toBeTruthy();
      return region as HTMLElement;
    });
    expect(liveRegion.getAttribute("aria-live")).toBe("polite");
    expect(liveRegion.textContent).toContain("Detected from QR");
    expect(liveRegion.textContent).toContain("Enterprise Authenticator A");
    expect(
      screen.getByTestId("authenticator-qr-type-detection").textContent,
    ).toBe("Detected from QR");
  });

  it("visually marks only the inferred chip as detected, and clears that marker when the user picks another type", async () => {
    stubQrCodeScan("example-authenticator://activate?payload=opaque");
    await openAuthenticatorSection(ENTERPRISE_APPS);
    await uploadQrCodeImage();

    const inferredButton = await waitFor(() => {
      const button = screen.getByRole("button", {
        name: /Enterprise Authenticator A/,
      });
      expect(button.getAttribute("data-inferred")).toBe("true");
      return button;
    });
    expect(
      screen
        .getByRole("button", { name: /Google Authenticator/ })
        .getAttribute("data-inferred"),
    ).toBeNull();

    await act(async () => {
      fireEvent.click(
        screen.getByRole("button", { name: /Google Authenticator/ }),
      );
    });

    expect(inferredButton.getAttribute("data-inferred")).toBeNull();
    expect(screen.queryByText("Detected from QR")).toBeNull();
    expect(
      screen
        .getByRole("button", { name: /Google Authenticator/ })
        .getAttribute("aria-pressed"),
    ).toBe("true");
  });

  it("does not falsely highlight a Cloud enterprise type for an unknown uploaded QR code", async () => {
    const imageBitmap = stubQrCodeScan(
      "unknown-authenticator://activate?payload=opaque",
    );
    const onChangeSpy = await openAuthenticatorSection(ENTERPRISE_APPS);
    await uploadQrCodeImage();

    await waitFor(() => {
      const qrCall = onChangeSpy.mock.calls.find((call) => {
        const next = call[0] as Values;
        return (
          next.totp_type === "authenticator" &&
          next.totp === "unknown-authenticator://activate?payload=opaque"
        );
      });
      expect(qrCall).toBeTruthy();
    });
    expect(
      screen
        .getByRole("button", { name: /Google Authenticator/ })
        .getAttribute("aria-pressed"),
    ).toBe("false");
    expect(
      screen
        .getByRole("button", { name: "Enterprise Authenticator A" })
        .getAttribute("aria-pressed"),
    ).toBe("false");
    expect(
      screen
        .getByRole("button", { name: "Enterprise Authenticator B" })
        .getAttribute("aria-pressed"),
    ).toBe("false");
    expect(screen.queryByText("Detected from QR")).toBeNull();
    expect(imageBitmap.close).toHaveBeenCalled();
  });
});

describe("PasswordCredentialContent — inline authenticator save error", () => {
  afterEach(() => {
    cleanup();
  });

  const AUTH_VALUES: Values = {
    name: "New cred",
    username: "[email protected]",
    password: "password",
    totp: "otpauth://totp/[email protected]?secret=BAD",
    totp_type: "authenticator",
    totp_identifier: "",
  };

  it("shows enterprise copy with a contact link for an enterprise-required error", () => {
    render(
      <MemoryRouter>
        <CredentialAuthenticatorSupportProvider
          value={{ enterpriseApps: ENTERPRISE_APPS }}
        >
          <PasswordCredentialContent
            values={AUTH_VALUES}
            onChange={vi.fn()}
            authenticatorSaveError={{
              code: "enterprise_required",
              message: "This authenticator requires a Skyvern enterprise plan.",
              vendor: "opaque",
            }}
          />
        </CredentialAuthenticatorSupportProvider>
      </MemoryRouter>,
    );

    const enterpriseMessage = screen.getByText(
      "Enterprise Authenticator B requires a Skyvern enterprise plan.",
    );
    expect(enterpriseMessage.closest(".text-destructive")).toBeNull();
    expect(screen.getByTestId("enterprise-authenticator-upgrade")).toBeTruthy();
    const contactLink = screen.getByRole("link", { name: "Contact us" });
    expect(contactLink.getAttribute("href")).toBe(
      "https://www.skyvern.com/contact",
    );
    const input = screen.getByPlaceholderText("e.g. JBSWY3DPEHPK3PXP");
    expect(input.getAttribute("aria-invalid")).toBe("false");
    expect(input.className).not.toContain("border-destructive");
    expect(input.getAttribute("aria-describedby")).toBeTruthy();
  });

  it("shows actionable no-code-secret copy without a contact link", () => {
    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={AUTH_VALUES}
          onChange={vi.fn()}
          authenticatorSaveError={{
            code: "no_code_secret",
            message:
              "This QR code doesn't contain a code-based setup key. It may enroll a push-approval app or device-bound authenticator. Set up an authenticator app or one-time code instead.",
          }}
        />
      </MemoryRouter>,
    );

    expect(screen.getByText(/push-approval app/)).toBeTruthy();
    expect(screen.queryByRole("link", { name: "Contact us" })).toBeNull();
    const input = screen.getByPlaceholderText("e.g. JBSWY3DPEHPK3PXP");
    expect(input.getAttribute("aria-invalid")).toBe("true");
    expect(input.className).toContain("border-destructive");
    const describedBy = input.getAttribute("aria-describedby");
    expect(describedBy).toBeTruthy();
    expect(document.getElementById(describedBy!)?.textContent).toContain(
      "push-approval app",
    );
  });

  it("keeps the decoded QR value in the field after a save failure", () => {
    render(
      <MemoryRouter>
        <PasswordCredentialContent
          values={AUTH_VALUES}
          onChange={vi.fn()}
          authenticatorSaveError={{
            code: "invalid_authenticator_key",
            message: "Invalid authenticator key.",
          }}
        />
      </MemoryRouter>,
    );

    const input = screen.getByPlaceholderText(
      "e.g. JBSWY3DPEHPK3PXP",
    ) as HTMLInputElement;
    expect(input.value).toBe("otpauth://totp/[email protected]?secret=BAD");
  });
});