File size: 3,428 Bytes
ceb943f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Story 7.3: Tests for slug generation utility
import { describe, it, expect } from 'vitest';
import { generateCreatorSlug, ensureUniqueSlug, isValidSlug } from '../slug';

describe('generateCreatorSlug', () => {
  it('should convert to lowercase', () => {
    expect(generateCreatorSlug('Tech Reviews Pro')).toBe('tech-reviews-pro');
  });

  it('should replace spaces with hyphens', () => {
    expect(generateCreatorSlug('Multiple   Spaces')).toBe('multiple-spaces');
  });

  it('should remove special characters', () => {
    expect(generateCreatorSlug('Mr. Beast!!!')).toBe('mr-beast');
    expect(generateCreatorSlug('Tech@Reviews#Pro')).toBe('techreviewspro');
  });

  it('should remove consecutive hyphens', () => {
    expect(generateCreatorSlug('Tech---Reviews')).toBe('tech-reviews');
  });

  it('should trim hyphens from start and end', () => {
    expect(generateCreatorSlug('  -Tech Reviews-  ')).toBe('tech-reviews');
  });

  it('should handle empty string', () => {
    expect(generateCreatorSlug('')).toBe('');
  });

  it('should handle only special characters', () => {
    expect(generateCreatorSlug('!!!')).toBe('');
  });

  it('should handle complex channel names', () => {
    expect(generateCreatorSlug('Linus Tech Tips')).toBe('linus-tech-tips');
    expect(generateCreatorSlug('MKBHD')).toBe('mkbhd');
    expect(generateCreatorSlug('The Verge')).toBe('the-verge');
  });
});

describe('ensureUniqueSlug', () => {
  it('should return original slug if no collision', () => {
    expect(ensureUniqueSlug('tech-reviews', [])).toBe('tech-reviews');
    expect(ensureUniqueSlug('tech-reviews', ['other-slug'])).toBe('tech-reviews');
  });

  it('should append -2 for first collision', () => {
    expect(ensureUniqueSlug('tech-reviews', ['tech-reviews'])).toBe('tech-reviews-2');
  });

  it('should increment counter for multiple collisions', () => {
    expect(ensureUniqueSlug('tech-reviews', ['tech-reviews', 'tech-reviews-2'])).toBe('tech-reviews-3');
    expect(ensureUniqueSlug('tech-reviews', ['tech-reviews', 'tech-reviews-2', 'tech-reviews-3'])).toBe('tech-reviews-4');
  });

  it('should handle non-sequential existing slugs', () => {
    expect(ensureUniqueSlug('tech-reviews', ['tech-reviews', 'tech-reviews-5'])).toBe('tech-reviews-2');
  });
});

describe('isValidSlug', () => {
  it('should validate correct slugs', () => {
    expect(isValidSlug('tech-reviews')).toBe(true);
    expect(isValidSlug('tech-reviews-pro')).toBe(true);
    expect(isValidSlug('mkbhd')).toBe(true);
    expect(isValidSlug('tech123')).toBe(true);
  });

  it('should reject uppercase letters', () => {
    expect(isValidSlug('Tech-Reviews')).toBe(false);
    expect(isValidSlug('MKBHD')).toBe(false);
  });

  it('should reject special characters', () => {
    expect(isValidSlug('tech@reviews')).toBe(false);
    expect(isValidSlug('tech_reviews')).toBe(false);
    expect(isValidSlug('tech.reviews')).toBe(false);
  });

  it('should reject slugs starting or ending with hyphen', () => {
    expect(isValidSlug('-tech-reviews')).toBe(false);
    expect(isValidSlug('tech-reviews-')).toBe(false);
  });

  it('should reject consecutive hyphens', () => {
    expect(isValidSlug('tech--reviews')).toBe(false);
  });

  it('should reject empty string', () => {
    expect(isValidSlug('')).toBe(false);
  });

  it('should reject spaces', () => {
    expect(isValidSlug('tech reviews')).toBe(false);
  });
});