| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { otpInputProps } from "./otp.ts"; |
| 5 | |
| 6 | test("one-time-code fields are findable by password managers and carry no ignore attributes", () => { |
| 7 | for (const recovery of [false, true]) { |
| 8 | const props: Record<string, unknown> = otpInputProps({ id: "totp-code", recovery }); |
| 9 | assert.equal(props.autoComplete, "one-time-code"); |
| 10 | assert.equal(props.id, "totp-code"); |
| 11 | assert.equal(props.name, "code"); |
| 12 | assert.equal(props.type, "text"); |
| 13 | for (const key of Object.keys(props)) assert.doesNotMatch(key, /1p|lpignore|bwignore|form-type/i); |
| 14 | } |
| 15 | }); |
| 16 | |
| 17 | test("an authenticator code field takes six digits, with or without the space", () => { |
| 18 | const props = otpInputProps({ id: "totp-code" }); |
| 19 | assert.equal(props.inputMode, "numeric"); |
| 20 | const pattern = new RegExp(`^(?:${props.pattern})$`); |
| 21 | for (const code of ["123456", "123 456"]) { |
| 22 | assert.match(code, pattern); |
| 23 | assert.ok(code.length <= props.maxLength); |
| 24 | } |
| 25 | }); |
| 26 | |
| 27 | test("a field that takes recovery codes neither cuts nor refuses one", () => { |
| 28 | const props = otpInputProps({ id: "totp-code", recovery: true }); |
| 29 | assert.equal(props.inputMode, "text"); |
| 30 | assert.equal(props.pattern, undefined); |
| 31 | for (const code of ["k7m2q-9xw4d", "K7M2Q 9XW4D", "123456"]) assert.ok(code.length <= props.maxLength); |
| 32 | }); |