1234567891011import { beforeEach, describe, expect, it } from "vitest";1213import {14 detectCardForm,15 detectLoginFields,16 findOTPDigitBoxGroup,17 findOtpSignalingGroup,18 isAnyCredentialForm,19 isEmailFirstForm,20 isLoginForm,21 isSignupForm,22 isTOTPField,23 isVisible,24} from "./detect";25import { makeFieldVisible, makeInput } from "./_setup";26272829303132beforeEach(() => {33 document.body.innerHTML = "";34});3536373839function form(): HTMLFormElement {40 const f = document.createElement("form");41 document.body.appendChild(f);42 return f;43}4445describe("T-DT01–T-DT03, T-DT06: form-shape classification", () => {46 47 48 it("T-DT01: text + password → detected as a fillable login form", () => {49 const f = form();50 const u = makeInput({ type: "text", name: "username", autocomplete: "username" }, f);51 const p = makeInput({ type: "password", name: "password", autocomplete: "current-password" }, f);52 const fields = detectLoginFields();53 expect(fields.some((x) => x.el === u && x.type === "username")).toBe(true);54 expect(fields.some((x) => x.el === p && x.type === "password")).toBe(true);55 56 expect(isAnyCredentialForm(fields)).toBe(true);57 58 expect(isLoginForm([{ type: "username", el: u }, { type: "password", el: p }])).toBe(true);59 });6061 62 63 64 it("T-DT02: text + new-password + confirm → isSignupForm true, isLoginForm false", () => {65 const f = form();66 makeInput({ type: "text", name: "username" }, f);67 makeInput({ type: "password", name: "new-password", autocomplete: "new-password" }, f);68 makeInput({ type: "password", name: "confirm", autocomplete: "new-password" }, f);69 const fields = detectLoginFields();70 expect(isSignupForm(fields)).toBe(true);71 expect(isLoginForm(fields)).toBe(false);72 });7374 75 76 77 it("T-DT03: email-only form with auth submit → isEmailFirstForm true", () => {78 const f = form();79 makeInput({ type: "email", name: "email", autocomplete: "email" }, f);80 const btn = document.createElement("button");81 btn.type = "submit";82 btn.textContent = "Next";83 f.appendChild(btn);84 const fields = detectLoginFields();85 expect(isEmailFirstForm(fields)).toBe(true);86 });8788 89 90 it("T-DT06: search form → isLoginForm false", () => {91 const f = form();92 makeInput({ type: "search", name: "q", placeholder: "Search" }, f);93 const fields = detectLoginFields();94 expect(isLoginForm(fields)).toBe(false);95 expect(isSignupForm(fields)).toBe(false);96 });97});9899describe("T-DT04: TOTP field detection", () => {100 101 102 it("T-DT04: input[autocomplete=one-time-code] → isTotpField true", () => {103 const f = form();104 const otp = makeInput({ type: "text", autocomplete: "one-time-code", name: "code" }, f);105 expect(isTOTPField(otp)).toBe(true);106 });107108 it("T-DT04b: a plain password field is not a TOTP field", () => {109 const f = form();110 const pw = makeInput({ type: "password", name: "password" }, f);111 expect(isTOTPField(pw)).toBe(false);112 });113});114115describe("T-DT05: credit-card form detection", () => {116 117 118 it("T-DT05: cardnumber + expiry + cvc → detectCardForm returns the field set", () => {119 const f = form();120 const number = makeInput({ autocomplete: "cc-number", name: "cardnumber" }, f);121 const exp = makeInput({ autocomplete: "cc-exp", name: "exp" }, f);122 const cvc = makeInput({ autocomplete: "cc-csc", name: "cvc" }, f);123 const card = detectCardForm();124 expect(card).not.toBeNull();125 expect(card!.number).toBe(number);126 expect(card!.expiryCombo).toBe(exp);127 expect(card!.cvc).toBe(cvc);128 });129130 it("T-DT05b: a login form is not a card form", () => {131 const f = form();132 makeInput({ type: "text", name: "username" }, f);133 makeInput({ type: "password", name: "password" }, f);134 expect(detectCardForm()).toBeNull();135 });136});137138describe("T-DT07–T-DT09: isVisible branches", () => {139 140 141 142143 144 it("T-DT07: zero-size rect → isVisible false", () => {145 const el = makeInput({ type: "text" });146 Object.defineProperty(el, "getBoundingClientRect", {147 configurable: true,148 value: () => ({ width: 0, height: 0, top: 0, left: 0, right: 0, bottom: 0, x: 0, y: 0, toJSON: () => ({}) }),149 });150 expect(isVisible(el)).toBe(false);151 });152153 154 it("T-DT08: display:none → isVisible false", () => {155 const el = makeInput({ type: "text" });156 el.style.display = "none";157 expect(isVisible(el)).toBe(false);158 });159160 161 it("T-DT09: opacity:0 → isVisible false", () => {162 const el = makeInput({ type: "text" });163 el.style.opacity = "0";164 expect(isVisible(el)).toBe(false);165 });166167 it("T-DT09b: a normally-laid-out field → isVisible true (control)", () => {168 const el = makeInput({ type: "text" });169 expect(isVisible(el)).toBe(true);170 });171});172173describe("T-DT10: deep (shadow DOM) detection", () => {174 175 176 177 it("T-DT10: login fields inside an open shadow root are detected", () => {178 const host = document.createElement("div");179 document.body.appendChild(host);180 const root = host.attachShadow({ mode: "open" });181 182 183 184 const wrapper = document.createElement("div");185 root.appendChild(wrapper);186 const user = document.createElement("input");187 user.type = "text";188 user.setAttribute("name", "username");189 user.setAttribute("autocomplete", "username");190 const pass = document.createElement("input");191 pass.type = "password";192 pass.setAttribute("autocomplete", "current-password");193 wrapper.append(user, pass);194 makeFieldVisible(user);195 makeFieldVisible(pass);196197 const fields = detectLoginFields();198 expect(fields.some((f) => f.type === "password")).toBe(true);199 expect(isAnyCredentialForm(fields)).toBe(true);200 });201});202203describe("T-DT11–T-DT12: PCH-01-025 CSS-special-char id resilience", () => {204 205 206 207 208 209 210 211 212 213 214 it("T-DT11: id='user[email]' does not abort detection; the password is still found", () => {215 const f = form();216 makeInput({ type: "text", id: "user[email]", name: "user[email]" }, f);217 makeInput({ type: "password", autocomplete: "current-password" }, f);218 let fields: ReturnType<typeof detectLoginFields> = [];219 expect(() => { fields = detectLoginFields(); }).not.toThrow();220 expect(fields.some((x) => x.type === "password")).toBe(true);221 });222223 it("T-DT12: id=\"it's-me\" does not abort detection", () => {224 const f = form();225 makeInput({ type: "text", id: "it's-me", name: "identifier" }, f);226 makeInput({ type: "password", autocomplete: "current-password" }, f);227 expect(() => detectLoginFields()).not.toThrow();228 });229230 231 232 233 it("T-DT12b: id with an embedded double-quote does not abort detection", () => {234 const f = form();235 makeInput({ type: "text", id: 'a"quote', name: "weird" }, f);236 makeInput({ type: "password", autocomplete: "current-password" }, f);237 let fields: ReturnType<typeof detectLoginFields> = [];238 expect(() => { fields = detectLoginFields(); }).not.toThrow();239 expect(fields.some((x) => x.type === "password")).toBe(true);240 });241});242243describe("T-DT13–T-DT15: label / placeholder / aria-label classification", () => {244 245 246 it("T-DT13: label[for] resolves the field as username", () => {247 const f = form();248 const input = makeInput({ type: "text", id: "login_user" }, f);249 const label = document.createElement("label");250 label.setAttribute("for", "login_user");251 label.textContent = "Username";252 253 254 f.appendChild(label);255 makeInput({ type: "password", autocomplete: "current-password" }, f);256 const fields = detectLoginFields();257 expect(fields.find((x) => x.el === input)?.type).toBe("username");258 });259260 261 it("T-DT14: placeholder='Username' → classified username", () => {262 const f = form();263 const input = makeInput({ type: "text", placeholder: "Username" }, f);264 makeInput({ type: "password", autocomplete: "current-password" }, f);265 const fields = detectLoginFields();266 expect(fields.find((x) => x.el === input)?.type).toBe("username");267 });268269 270 it("T-DT15: aria-label='Email address' → classified", () => {271 const f = form();272 const input = makeInput({ type: "text", "aria-label": "Email address" }, f);273 makeInput({ type: "password", autocomplete: "current-password" }, f);274 const fields = detectLoginFields();275 expect(fields.find((x) => x.el === input)?.type).toBe("username");276 });277});278279describe("T-DT16–T-DT17: edge form shapes", () => {280 281 282 it("T-DT16: form without a submit button is still a fillable login form", () => {283 const f = form();284 const u = makeInput({ type: "text", name: "username", autocomplete: "username" }, f);285 const p = makeInput({ type: "password", autocomplete: "current-password" }, f);286 const fields = detectLoginFields();287 expect(fields.some((x) => x.el === u)).toBe(true);288 expect(fields.some((x) => x.el === p && x.type === "password")).toBe(true);289 expect(isAnyCredentialForm(fields)).toBe(true);290 expect(isLoginForm([{ type: "username", el: u }, { type: "password", el: p }])).toBe(true);291 });292293 294 295 it("T-DT17: old + new + confirm passwords → signup-shaped, not login", () => {296 const f = form();297 makeInput({ type: "password", name: "old-password", autocomplete: "current-password" }, f);298 makeInput({ type: "password", name: "new-password", autocomplete: "new-password" }, f);299 makeInput({ type: "password", name: "confirm-password", autocomplete: "new-password" }, f);300 const fields = detectLoginFields();301 expect(isSignupForm(fields)).toBe(true);302 expect(isLoginForm(fields)).toBe(false);303 });304});305306307308309310describe("T-DT18–T-DT21 (existing findOTPDigitBoxGroup behavior)", () => {311 312 313 it("T-DT18: 6× maxlength=1 numeric inputs → group of 6 returned, first is box[0]", () => {314 const f = form();315 const boxes: HTMLInputElement[] = [];316 for (let i = 0; i < 6; i++) boxes.push(makeInput({ type: "text", maxlength: "1", inputmode: "numeric" }, f));317 const group = findOTPDigitBoxGroup(boxes[2]!);318 expect(group).not.toBeNull();319 expect(group!.length).toBe(6);320 expect(group![0]).toBe(boxes[0]);321 });322323 324 325 326 it("T-DT19: a real one-time-code field classifies as TOTP; maxlength=1 boxes do not", () => {327 const f = form();328 const box = makeInput({ type: "text", maxlength: "1", inputmode: "numeric" }, f);329 const real = makeInput({ type: "text", autocomplete: "one-time-code", name: "otp" }, f);330 expect(isTOTPField(real)).toBe(true);331 expect(isTOTPField(box)).toBe(false);332 });333334 335 it("T-DT21: only 2 single-char boxes → not an OTP group (null)", () => {336 const f = form();337 const boxes = [338 makeInput({ type: "text", maxlength: "1", inputmode: "numeric" }, f),339 makeInput({ type: "text", maxlength: "1", inputmode: "numeric" }, f),340 ];341 expect(findOTPDigitBoxGroup(boxes[0]!)).toBeNull();342 });343});344345346347348349350351352describe("T-DT22–T-DT24: findOtpSignalingGroup (split-OTP boxes lacking maxlength)", () => {353 354 355 356 357 it("T-DT22: 6 one-time-code boxes (no maxlength) → group of 6, anchor is box[0]", () => {358 const f = form();359 const boxes: HTMLInputElement[] = [];360 for (let i = 0; i < 6; i++) {361 boxes.push(makeInput({ type: "text", autocomplete: "one-time-code", name: "otp" }, f));362 }363 expect(isTOTPField(boxes[3]!)).toBe(true);364 const group = findOtpSignalingGroup(boxes[3]!);365 expect(group).not.toBeNull();366 expect(group!.length).toBe(6);367 expect(group![0]).toBe(boxes[0]);368 expect(group![5]).toBe(boxes[5]);369 });370371 372 373 374 it("T-DT23: a lone one-time-code field → null (not a group)", () => {375 const f = form();376 const lone = makeInput({ type: "text", autocomplete: "one-time-code", name: "code" }, f);377 expect(findOtpSignalingGroup(lone)).toBeNull();378 });379380 381 382 it("T-DT24: only 3 one-time-code boxes → null (below 4-box floor)", () => {383 const f = form();384 const boxes: HTMLInputElement[] = [];385 for (let i = 0; i < 3; i++) {386 boxes.push(makeInput({ type: "text", autocomplete: "one-time-code", name: "otp" }, f));387 }388 expect(findOtpSignalingGroup(boxes[0]!)).toBeNull();389 });390391 392 393 394 395 it("T-DT25: a maxlength=1 box is not part of an OTP-signaling group", () => {396 const f = form();397 const boxes: HTMLInputElement[] = [];398 for (let i = 0; i < 6; i++) {399 boxes.push(makeInput({ type: "text", maxlength: "1", inputmode: "numeric", autocomplete: "one-time-code" }, f));400 }401 expect(isTOTPField(boxes[0]!)).toBe(false);402 expect(findOtpSignalingGroup(boxes[0]!)).toBeNull();403 expect(findOTPDigitBoxGroup(boxes[0]!)).not.toBeNull();404 });405});