|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { getConversationId } from '../../utils/conversations'; |
| 3 | + |
| 4 | +describe('getConversationId', () => { |
| 5 | + describe('ID generation', () => { |
| 6 | + it('should generate a non-empty string', () => { |
| 7 | + const id = getConversationId(); |
| 8 | + expect(id).toBeTruthy(); |
| 9 | + expect(typeof id).toBe('string'); |
| 10 | + expect(id.length).toBeGreaterThan(0); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should generate unique IDs', () => { |
| 14 | + const id1 = getConversationId(); |
| 15 | + const id2 = getConversationId(); |
| 16 | + const id3 = getConversationId(); |
| 17 | + |
| 18 | + expect(id1).not.toBe(id2); |
| 19 | + expect(id1).not.toBe(id3); |
| 20 | + expect(id2).not.toBe(id3); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should generate IDs with reasonable length (nanoid default is 21)', () => { |
| 24 | + const id = getConversationId(); |
| 25 | + // nanoid default length is 21, but could be slightly less if leading hyphens are removed |
| 26 | + expect(id.length).toBeGreaterThanOrEqual(20); |
| 27 | + expect(id.length).toBeLessThanOrEqual(21); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('lowercase requirement', () => { |
| 32 | + it('should generate only lowercase IDs', () => { |
| 33 | + // Test multiple generations to ensure consistency |
| 34 | + for (let i = 0; i < 50; i++) { |
| 35 | + const id = getConversationId(); |
| 36 | + expect(id).toBe(id.toLowerCase()); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it('should not contain any uppercase letters', () => { |
| 41 | + for (let i = 0; i < 50; i++) { |
| 42 | + const id = getConversationId(); |
| 43 | + expect(/[A-Z]/.test(id)).toBe(false); |
| 44 | + } |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('no leading hyphens requirement', () => { |
| 49 | + it('should not start with a hyphen', () => { |
| 50 | + // Test multiple generations to ensure consistency |
| 51 | + for (let i = 0; i < 50; i++) { |
| 52 | + const id = getConversationId(); |
| 53 | + expect(id.startsWith('-')).toBe(false); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + it('should not start with multiple hyphens', () => { |
| 58 | + for (let i = 0; i < 50; i++) { |
| 59 | + const id = getConversationId(); |
| 60 | + expect(/^-+/.test(id)).toBe(false); |
| 61 | + } |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('character set', () => { |
| 66 | + it('should only contain URL-safe characters', () => { |
| 67 | + // nanoid uses A-Za-z0-9_- alphabet |
| 68 | + // After lowercase conversion: a-z0-9_- |
| 69 | + const urlSafePattern = /^[a-z0-9_-]+$/; |
| 70 | + |
| 71 | + for (let i = 0; i < 50; i++) { |
| 72 | + const id = getConversationId(); |
| 73 | + expect(urlSafePattern.test(id)).toBe(true); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('should allow underscores and hyphens (but not leading hyphens)', () => { |
| 78 | + // Generate many IDs to potentially get ones with these characters |
| 79 | + const ids = Array.from({ length: 100 }, () => getConversationId()); |
| 80 | + |
| 81 | + // Check that none start with hyphen |
| 82 | + for (const id of ids) { |
| 83 | + expect(id.startsWith('-')).toBe(false); |
| 84 | + } |
| 85 | + |
| 86 | + // IDs should be valid |
| 87 | + for (const id of ids) { |
| 88 | + expect(id.length).toBeGreaterThan(0); |
| 89 | + expect(/^[a-z0-9_-]+$/.test(id)).toBe(true); |
| 90 | + } |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('collision resistance', () => { |
| 95 | + it('should generate unique IDs even in rapid succession', () => { |
| 96 | + const ids = new Set<string>(); |
| 97 | + const count = 1000; |
| 98 | + |
| 99 | + for (let i = 0; i < count; i++) { |
| 100 | + ids.add(getConversationId()); |
| 101 | + } |
| 102 | + |
| 103 | + // Should have generated 1000 unique IDs |
| 104 | + expect(ids.size).toBe(count); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('consistency', () => { |
| 109 | + it('should always return a valid ID format', () => { |
| 110 | + for (let i = 0; i < 100; i++) { |
| 111 | + const id = getConversationId(); |
| 112 | + |
| 113 | + // Verify all requirements |
| 114 | + expect(typeof id).toBe('string'); |
| 115 | + expect(id.length).toBeGreaterThan(0); |
| 116 | + expect(id).toBe(id.toLowerCase()); |
| 117 | + expect(id.startsWith('-')).toBe(false); |
| 118 | + expect(/^[a-z0-9_-]+$/.test(id)).toBe(true); |
| 119 | + } |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments