// R-P7 interim-UI (AD-28) — LeadAntiSpamAdminPage unit test. Fixture VM via the real // buildLeadAntiSpamVM; asserts the save callback carries edited values + the "on" // boolean convention. i18n mocked. import { fireEvent, render } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { buildLeadAntiSpamVM } from "@hivegrid/view-models"; import { LeadAntiSpamAdminPage, type LeadAntiSpamAdminActions } from "./LeadAntiSpamAdminPage"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }) })); const vm = buildLeadAntiSpamVM({ config: { perSourcePerHourLimit: 60, perIpPerHourLimit: 20, anomalySpikeFactor: 10, mode: "quarantine", disposableDomainEnabled: true, honeypotEnabled: true, fastSubmitEnabled: false, lostAutoDeleteDays: 0 }, canWrite: true, }); function actions(over: Partial = {}): LeadAntiSpamAdminActions { return { onSave: vi.fn(), ...over }; } describe("LeadAntiSpamAdminPage", () => { it("saves the edited config with the 'on' boolean convention", () => { const onSave = vi.fn(); render(); fireEvent.change(document.querySelector("[data-ui-id='ui.leads.antiSpam.perSourcePerHourLimit']")!, { target: { value: "80" } }); fireEvent.click(document.querySelector("[data-ui-id='ui.leads.antiSpam.save']")!); expect(onSave).toHaveBeenCalledTimes(1); const arg = onSave.mock.calls[0][0]; expect(arg.perSourcePerHourLimit).toBe("80"); expect(arg.disposableDomainEnabled).toBe("on"); expect(arg.fastSubmitEnabled).toBeUndefined(); }); it("groups the settings by what they do", () => { // Detection signals, hourly limits, the reaction to a high score and the // retention field are four different decisions — a flat list gave no clue // which knob does what. render(); for (const section of ["detection", "limits", "reaction", "retention"]) { expect(document.querySelector(`[data-ui-id='ui.leads.antiSpam.${section}']`)).not.toBeNull(); } }); it("puts the save button in the header row, not below the last section", () => { // Four sections deep, a footer button sits below the fold — the header row // is where the black primary action lives on the other pages. render(); const save = document.querySelector("[data-ui-id='ui.leads.antiSpam.save']")!; const firstSection = document.querySelector("[data-ui-id='ui.leads.antiSpam.detection']")!; expect(save.compareDocumentPosition(firstSection) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy(); }); it("keeps the checkbox label pinned to the start so hover cannot shift it", () => { // The Subframe checkbox is a justify-between button: stretched to full width // the label sits flush right and snaps left on hover. render(); const box = document.querySelector("[data-ui-id='ui.leads.antiSpam.honeypotEnabled']")!; expect(box.className).toContain("justify-start"); }); it("mirrors the server-side minimums on the number inputs", () => { // updateLeadAntiSpamConfig clamps instead of rejecting, so without a min the // browser would happily send a value that is silently stored as another one. render(); expect(document.querySelector("[data-ui-id='ui.leads.antiSpam.anomalySpikeFactor']")!.getAttribute("min")).toBe("2"); expect(document.querySelector("[data-ui-id='ui.leads.antiSpam.lostAutoDeleteDays']")!.getAttribute("min")).toBe("0"); }); it("follows the view-model when the server stored something else", () => { const { rerender } = render(); const input = () => document.querySelector("[data-ui-id='ui.leads.antiSpam.anomalySpikeFactor']") as HTMLInputElement; fireEvent.change(input(), { target: { value: "1" } }); expect(input().value).toBe("1"); // What the server clamped 1 up to. The form must show that, not the rejected input. const saved = buildLeadAntiSpamVM({ config: { perSourcePerHourLimit: 60, perIpPerHourLimit: 20, anomalySpikeFactor: 2, mode: "quarantine", disposableDomainEnabled: true, honeypotEnabled: true, fastSubmitEnabled: false, lostAutoDeleteDays: 0 }, canWrite: true, }); rerender(); expect(input().value).toBe("2"); }); it("does not offer saving without write permission", () => { const readOnly = buildLeadAntiSpamVM({ config: { perSourcePerHourLimit: 60, perIpPerHourLimit: 20, anomalySpikeFactor: 10, mode: "quarantine", disposableDomainEnabled: true, honeypotEnabled: true, fastSubmitEnabled: false, lostAutoDeleteDays: 0 }, canWrite: false, }); render(); expect(document.querySelector("[data-ui-id='ui.leads.antiSpam.save']")!.hasAttribute("disabled")).toBe(true); }); });