import { generatePassword } from "../password"; const CRYPTO_OK = typeof globalThis.crypto?.getRandomValues === "function"; const describeIfCrypto = CRYPTO_OK ? describe : describe.skip; describeIfCrypto("generatePassword", () => { it("returns exactly the requested length", () => { expect(generatePassword({ length: 16 }).length).toBe(16); expect(generatePassword({ length: 32 }).length).toBe(32); expect(generatePassword({ length: 64 }).length).toBe(64); }); it("draws only from the enabled character class", () => { const pw = generatePassword({ length: 64, includeUppercase: false, includeLowercase: false, includeNumbers: true, includeSymbols: false, }); expect(/^\d+$/.test(pw)).toBe(true); }); it("excludes similar glyphs when requested", () => { const pw = generatePassword({ length: 256, includeUppercase: true, includeLowercase: true, includeNumbers: true, includeSymbols: false, excludeSimilar: true, }); for (const ch of ["i", "l", "1", "L", "o", "0", "O"]) { expect(pw).not.toContain(ch); } }); it("throws when every class is disabled", () => { expect(() => generatePassword({ length: 16, includeUppercase: false, includeLowercase: false, includeNumbers: false, includeSymbols: false, }) ).toThrow(); }); it("draws different output across calls (non-deterministic)", () => { const a = generatePassword({ length: 32 }); const b = generatePassword({ length: 32 }); expect(a).not.toBe(b); }); });