1+ import generateColorFromString , {
2+ getHashOfString ,
3+ normalizeHash ,
4+ } from "@/util/generateUserUniqueColor" ;
5+
6+ describe ( 'Color Generation Functions' , ( ) => {
7+ describe ( 'getHashOfString' , ( ) => {
8+ test ( 'returns consistent hash for same input' , ( ) => {
9+ const input = 'test@example.com' ;
10+ const firstHash = getHashOfString ( input ) ;
11+ const secondHash = getHashOfString ( input ) ;
12+ expect ( firstHash ) . toBe ( secondHash ) ;
13+ } ) ;
14+
15+ test ( 'returns different hashes for different inputs' , ( ) => {
16+ const hash1 = getHashOfString ( 'test1@example.com' ) ;
17+ const hash2 = getHashOfString ( 'test2@example.com' ) ;
18+ expect ( hash1 ) . not . toBe ( hash2 ) ;
19+ } ) ;
20+
21+ test ( 'handles empty string' , ( ) => {
22+ const hash = getHashOfString ( '' ) ;
23+ expect ( hash ) . toBe ( 0 ) ;
24+ } ) ;
25+
26+ test ( 'returns positive numbers' , ( ) => {
27+ const inputs = [ 'test' , 'example@mail.com' , 'longeremail@domain.com' ] ;
28+ inputs . forEach ( input => {
29+ const hash = getHashOfString ( input ) ;
30+ expect ( hash ) . toBeGreaterThanOrEqual ( 0 ) ;
31+ } ) ;
32+ } ) ;
33+ } ) ;
34+
35+ describe ( 'normalizeHash' , ( ) => {
36+ test ( 'returns value within specified range' , ( ) => {
37+ const hash = 12345 ;
38+ const min = 0 ;
39+ const max = 360 ;
40+ const normalized = normalizeHash ( hash , min , max ) ;
41+ expect ( normalized ) . toBeGreaterThanOrEqual ( min ) ;
42+ expect ( normalized ) . toBeLessThan ( max ) ;
43+ } ) ;
44+
45+ test ( 'returns integer values' , ( ) => {
46+ const hash = 12345 ;
47+ const normalized = normalizeHash ( hash , 0 , 100 ) ;
48+ expect ( Number . isInteger ( normalized ) ) . toBe ( true ) ;
49+ } ) ;
50+
51+ test ( 'handles different ranges' , ( ) => {
52+ const testCases = [
53+ { hash : 12345 , min : 0 , max : 360 } ,
54+ { hash : 12345 , min : 50 , max : 75 } ,
55+ { hash : 12345 , min : 25 , max : 60 }
56+ ] ;
57+
58+ testCases . forEach ( ( { hash, min, max } ) => {
59+ const normalized = normalizeHash ( hash , min , max ) ;
60+ expect ( normalized ) . toBeGreaterThanOrEqual ( min ) ;
61+ expect ( normalized ) . toBeLessThan ( max ) ;
62+ } ) ;
63+ } ) ;
64+ } ) ;
65+
66+ describe ( 'generateColorFromString' , ( ) => {
67+ test ( 'returns valid HSL color string' , ( ) => {
68+ const color = generateColorFromString ( 'test@example.com' ) ;
69+ expect ( color ) . toMatch ( / ^ h s l \( \d + , \d + % , \d + % \) $ / ) ;
70+ } ) ;
71+
72+ test ( 'returns consistent colors for same input' , ( ) => {
73+ const input = 'test@example.com' ;
74+ const color1 = generateColorFromString ( input ) ;
75+ const color2 = generateColorFromString ( input ) ;
76+ expect ( color1 ) . toBe ( color2 ) ;
77+ } ) ;
78+
79+ test ( 'returns different colors for different inputs' , ( ) => {
80+ const color1 = generateColorFromString ( 'test1@example.com' ) ;
81+ const color2 = generateColorFromString ( 'test2@example.com' ) ;
82+ expect ( color1 ) . not . toBe ( color2 ) ;
83+ } ) ;
84+
85+ test ( 'generates color with correct HSL ranges' , ( ) => {
86+ const color = generateColorFromString ( 'test@example.com' ) ;
87+ const matches = color . match ( / ^ h s l \( ( \d + ) , ( \d + ) % , ( \d + ) % \) $ / ) ;
88+
89+ expect ( matches ) . not . toBeNull ( ) ;
90+ if ( matches ) {
91+ const [ , hue , saturation , lightness ] = matches . map ( Number ) ;
92+
93+ expect ( hue ) . toBeGreaterThanOrEqual ( 0 ) ;
94+ expect ( hue ) . toBeLessThan ( 360 ) ;
95+
96+ expect ( saturation ) . toBeGreaterThanOrEqual ( 50 ) ;
97+ expect ( saturation ) . toBeLessThan ( 75 ) ;
98+
99+ expect ( lightness ) . toBeGreaterThanOrEqual ( 25 ) ;
100+ expect ( lightness ) . toBeLessThan ( 60 ) ;
101+ }
102+ } ) ;
103+
104+ test ( 'handles special characters' , ( ) => {
105+ const specialChars = '!@#$%^&*()_+' ;
106+ expect ( ( ) => generateColorFromString ( specialChars ) ) . not . toThrow ( ) ;
107+ expect ( generateColorFromString ( specialChars ) ) . toMatch ( / ^ h s l \( \d + , \d + % , \d + % \) $ / ) ;
108+ } ) ;
109+
110+ test ( 'handles empty string' , ( ) => {
111+ expect ( ( ) => generateColorFromString ( '' ) ) . not . toThrow ( ) ;
112+ expect ( generateColorFromString ( '' ) ) . toMatch ( / ^ h s l \( \d + , \d + % , \d + % \) $ / ) ;
113+ } ) ;
114+ } ) ;
115+ } ) ;
0 commit comments