// @vitest-environment jsdom import { beforeEach, describe, expect, it } from "vitest"; import { resolveAutofillInteractionTargets } from "./interaction"; describe("resolveAutofillInteractionTargets", () => { beforeEach(() => { document.body.innerHTML = ""; }); it("keeps passive focus gated when no credential form is present", () => { const anchor = document.createElement("input"); anchor.type = "email"; const targets = resolveAutofillInteractionTargets(anchor, []); expect(targets.shouldOpen).toBe(false); expect(targets.usernameEl).toBeNull(); expect(targets.passwordEl).toBeNull(); }); it("prefers detected username and password fields when they exist", () => { const anchor = document.createElement("input"); anchor.type = "email"; const username = document.createElement("input"); username.type = "email"; const password = document.createElement("input"); password.type = "password"; const targets = resolveAutofillInteractionTargets(anchor, [ { type: "username", el: username }, { type: "password", el: password }, ]); expect(targets.shouldOpen).toBe(true); expect(targets.usernameEl).toBe(username); expect(targets.passwordEl).toBe(password); }); it("lets an explicit email-field click open Peach even if detection is partial", () => { const anchor = document.createElement("input"); anchor.type = "email"; const targets = resolveAutofillInteractionTargets(anchor, [], true); expect(targets.shouldOpen).toBe(true); expect(targets.usernameEl).toBe(anchor); expect(targets.passwordEl).toBeNull(); }); it("lets an explicit password-field click open Peach even if detection is partial", () => { const anchor = document.createElement("input"); anchor.type = "password"; const targets = resolveAutofillInteractionTargets(anchor, [], true); expect(targets.shouldOpen).toBe(true); expect(targets.usernameEl).toBeNull(); expect(targets.passwordEl).toBe(anchor); }); });