import { generateDeckId, isValidDeckId } from './deck-id'; describe('deck-id', () => { describe('generateDeckId', () => { it('should generate a deck ID with the correct format', () => { const deckId = generateDeckId(); expect(deckId).toMatch(/^[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}$/); }); it('should generate a deck ID with exactly 12 alphanumeric characters (excluding hyphens)', () => { const deckId = generateDeckId(); const charactersOnly = deckId.replace(/-/g, ''); expect(charactersOnly).toHaveLength(12); }); it('should generate a deck ID with exactly 14 characters total (including hyphens)', () => { const deckId = generateDeckId(); expect(deckId).toHaveLength(14); }); it('should contain exactly 2 hyphens', () => { const deckId = generateDeckId(); const hyphens = deckId.split('-').length - 1; expect(hyphens).toBe(2); }); it('should only use lowercase letters and numbers', () => { const deckId = generateDeckId(); expect(deckId).toMatch(/^[a-z0-9-]+$/); }); it('should generate unique IDs on multiple calls', () => { const ids = new Set(); const iterations = 1000; for (let i = 0; i < iterations; i++) { ids.add(generateDeckId()); } // With 36^12 possible combinations, we should never see duplicates in 1000 tries expect(ids.size).toBe(iterations); }); it('should have high entropy (different characters in different positions)', () => { const ids = Array.from({ length: 100 }, () => generateDeckId()); // Check that different positions have variety const firstCharSet = new Set(ids.map(id => id[0])); const lastCharSet = new Set(ids.map(id => id[id.length - 1])); // With 100 samples from 36 possible characters, we expect good variety expect(firstCharSet.size).toBeGreaterThan(10); expect(lastCharSet.size).toBeGreaterThan(10); }); it('should be valid according to isValidDeckId', () => { const deckId = generateDeckId(); expect(isValidDeckId(deckId)).toBe(true); }); }); describe('isValidDeckId', () => { it('should return true for valid deck IDs', () => { expect(isValidDeckId('a3b2-c5d9-e7f1')).toBe(true); expect(isValidDeckId('0000-0000-0000')).toBe(true); expect(isValidDeckId('zzzz-zzzz-zzzz')).toBe(true); expect(isValidDeckId('abcd-1234-efgh')).toBe(true); }); it('should return false for invalid formats', () => { // Wrong length segments expect(isValidDeckId('ab-cd-ef')).toBe(false); expect(isValidDeckId('abcde-1234-efgh')).toBe(false); // Missing hyphens expect(isValidDeckId('abcd1234efgh')).toBe(false); // Wrong number of segments expect(isValidDeckId('abcd-1234')).toBe(false); expect(isValidDeckId('abcd-1234-efgh-ijkl')).toBe(false); // Wrong separator expect(isValidDeckId('abcd_1234_efgh')).toBe(false); expect(isValidDeckId('abcd.1234.efgh')).toBe(false); }); it('should return false for IDs with uppercase letters', () => { expect(isValidDeckId('ABCD-1234-EFGH')).toBe(false); expect(isValidDeckId('Abcd-1234-efgh')).toBe(false); }); it('should return false for IDs with special characters', () => { expect(isValidDeckId('abc!-1234-efgh')).toBe(false); expect(isValidDeckId('abcd-12$4-efgh')).toBe(false); expect(isValidDeckId('abcd-1234-ef@h')).toBe(false); }); it('should return false for empty or null strings', () => { expect(isValidDeckId('')).toBe(false); expect(isValidDeckId(' ')).toBe(false); }); it('should return false for IDs with spaces', () => { expect(isValidDeckId('abcd -1234-efgh')).toBe(false); expect(isValidDeckId('abcd- 1234-efgh')).toBe(false); expect(isValidDeckId(' abcd-1234-efgh')).toBe(false); expect(isValidDeckId('abcd-1234-efgh ')).toBe(false); }); it('should return false for too short IDs', () => { expect(isValidDeckId('abc-123-efg')).toBe(false); }); it('should return false for too long IDs', () => { expect(isValidDeckId('abcde-12345-efghi')).toBe(false); }); }); });