password_manager/tests/lib/crypto/crypto.test.js

44 lines
1.5 KiB
JavaScript

import { describe, it, expect } from 'vitest'
import { generatePassword } from '../../../src/lib/crypto/crypto.js'
describe('generatePassword', () => {
it('should produce different passwords on consecutive calls', () => {
const passwords = new Set()
for (let i = 0; i < 50; i++) {
passwords.add(generatePassword({ length: 16 }))
}
// With 50 calls and 94^16 possible values, all should be unique
expect(passwords.size).toBe(50)
})
it('should respect the length option', () => {
for (let len of [4, 8, 16, 32, 64]) {
const pw = generatePassword({ length: len })
expect(pw.length).toBe(len)
}
})
it('should only use allowed character types', () => {
const pw = generatePassword({ uppercase: true, lowercase: false, digits: false, symbols: false })
expect(pw).toMatch(/^[A-Z]+$/)
const pw2 = generatePassword({ uppercase: false, lowercase: true, digits: false, symbols: false })
expect(pw2).toMatch(/^[a-z]+$/)
const pw3 = generatePassword({ uppercase: false, lowercase: false, digits: true, symbols: false })
expect(pw3).toMatch(/^[0-9]+$/)
})
it('should exclude specified characters', () => {
const pw = generatePassword({ length: 32, exclude: '0OIl1' })
for (const ch of '0OIl1') {
expect(pw).not.toContain(ch)
}
})
it('should throw when charset is empty', () => {
expect(() => generatePassword({ uppercase: false, lowercase: false, digits: false, symbols: false }))
.toThrow()
})
})