-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
186 lines (145 loc) · 6.77 KB
/
Copy pathtest.js
File metadata and controls
186 lines (145 loc) · 6.77 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Test suite for Color Contrast Checker
*/
const {
parseColor,
getRelativeLuminance,
getContrastRatio,
checkWCAG,
checkContrast,
rgbToHex
} = require('./index.js');
let testsPassed = 0;
let testsFailed = 0;
function assert(condition, message) {
if (condition) {
testsPassed++;
console.log(`✓ ${message}`);
} else {
testsFailed++;
console.error(`✗ ${message}`);
}
}
function assertThrows(fn, message) {
try {
fn();
testsFailed++;
console.error(`✗ ${message} (expected error but none thrown)`);
} catch (error) {
testsPassed++;
console.log(`✓ ${message}`);
}
}
function assertCloseTo(actual, expected, tolerance, message) {
const diff = Math.abs(actual - expected);
if (diff <= tolerance) {
testsPassed++;
console.log(`✓ ${message} (${actual} ≈ ${expected})`);
} else {
testsFailed++;
console.error(`✗ ${message} (${actual} vs ${expected}, diff: ${diff})`);
}
}
console.log('\n=== Testing Color Parsing ===\n');
// Test hex colors
const hex6 = parseColor('#ff5733');
assert(hex6.r === 255 && hex6.g === 87 && hex6.b === 51, 'Parse 6-digit hex color');
const hex3 = parseColor('#abc');
assert(hex3.r === 170 && hex3.g === 187 && hex3.b === 204, 'Parse 3-digit hex color');
const black = parseColor('#000000');
assert(black.r === 0 && black.g === 0 && black.b === 0, 'Parse black color');
const white = parseColor('#ffffff');
assert(white.r === 255 && white.g === 255 && white.b === 255, 'Parse white color');
// Test RGB colors
const rgb = parseColor('rgb(100, 150, 200)');
assert(rgb.r === 100 && rgb.g === 150 && rgb.b === 200, 'Parse RGB color');
const rgba = parseColor('rgba(50, 100, 150, 0.5)');
assert(rgba.r === 50 && rgba.g === 100 && rgba.b === 150, 'Parse RGBA color (ignore alpha)');
// Test invalid colors
assertThrows(() => parseColor('#gg0000'), 'Reject invalid hex color');
assertThrows(() => parseColor('rgb(300, 100, 50)'), 'Reject invalid RGB values');
assertThrows(() => parseColor('hsl(200, 50%, 50%)'), 'Reject unsupported color format');
console.log('\n=== Testing Relative Luminance ===\n');
// Test known luminance values
const blackLum = getRelativeLuminance({ r: 0, g: 0, b: 0 });
assertCloseTo(blackLum, 0, 0.001, 'Black luminance is 0');
const whiteLum = getRelativeLuminance({ r: 255, g: 255, b: 255 });
assertCloseTo(whiteLum, 1, 0.001, 'White luminance is 1');
// Test gray (should be approximately mid-range)
const grayLum = getRelativeLuminance({ r: 128, g: 128, b: 128 });
assert(grayLum > 0 && grayLum < 1, 'Gray luminance is between 0 and 1');
console.log('\n=== Testing Contrast Ratio ===\n');
// Test maximum contrast (black on white)
const maxContrast = getContrastRatio('#000000', '#ffffff');
assertCloseTo(maxContrast, 21, 0.01, 'Black on white has 21:1 contrast');
// Test minimum contrast (same color)
const minContrast = getContrastRatio('#000000', '#000000');
assertCloseTo(minContrast, 1, 0.01, 'Same color has 1:1 contrast');
// Test known contrast ratios
const ratio1 = getContrastRatio('#767676', '#ffffff');
assertCloseTo(ratio1, 4.54, 0.1, 'Gray on white has ~4.54:1 contrast');
const ratio2 = getContrastRatio('#000000', '#777777');
assertCloseTo(ratio2, 4.69, 0.1, 'Black on gray has ~4.69:1 contrast');
// Test symmetry (order shouldn't matter)
const r1 = getContrastRatio('#ff0000', '#0000ff');
const r2 = getContrastRatio('#0000ff', '#ff0000');
assertCloseTo(r1, r2, 0.001, 'Contrast ratio is symmetric');
console.log('\n=== Testing WCAG Compliance ===\n');
// Test AA compliance for normal text
const aaPass = checkWCAG('#000000', '#ffffff', 'normal');
assert(aaPass.AA === true, 'Black on white passes AA for normal text');
assert(aaPass.AAA === true, 'Black on white passes AAA for normal text');
const aaFail = checkWCAG('#888888', '#999999', 'normal');
assert(aaFail.AA === false, 'Similar grays fail AA for normal text');
assert(aaFail.AAA === false, 'Similar grays fail AAA for normal text');
// Test large text thresholds
const largePass = checkWCAG('#767676', '#ffffff', 'large');
assert(largePass.AA === true, 'Lower contrast passes AA for large text');
const largeFail = checkWCAG('#999999', '#ffffff', 'normal');
assert(largeFail.AA === false, 'Light gray on white fails AA for normal text');
// Test threshold values
const normalCheck = checkWCAG('#000000', '#ffffff', 'normal');
assert(normalCheck.AAThreshold === 4.5, 'AA threshold for normal text is 4.5:1');
assert(normalCheck.AAAThreshold === 7.0, 'AAA threshold for normal text is 7:1');
const largeCheck = checkWCAG('#000000', '#ffffff', 'large');
assert(largeCheck.AAThreshold === 3.0, 'AA threshold for large text is 3:1');
assert(largeCheck.AAAThreshold === 4.5, 'AAA threshold for large text is 4.5:1');
console.log('\n=== Testing Full Contrast Check ===\n');
// Test successful check
const result1 = checkContrast('#000000', '#ffffff');
assert(result1.ratio === 21, 'Result includes contrast ratio');
assert(result1.AA === true, 'Result includes AA status');
assert(result1.AAA === true, 'Result includes AAA status');
assert(result1.suggestions.length === 0, 'No suggestions when passing');
// Test with suggestions
const result2 = checkContrast('#999999', '#ffffff', { level: 'AA' });
assert(result2.AA === false, 'Light gray on white fails AA');
assert(result2.suggestions.length > 0, 'Suggestions provided when failing');
// Test invalid colors
assertThrows(() => checkContrast('invalid', '#ffffff'), 'Reject invalid foreground color');
assertThrows(() => checkContrast('#000000', 'invalid'), 'Reject invalid background color');
console.log('\n=== Testing Color Suggestions ===\n');
// Test that suggestions meet requirements
const failingResult = checkContrast('#aaaaaa', '#ffffff', { level: 'AA', fontSize: 'normal' });
assert(failingResult.suggestions.length > 0, 'Suggestions generated for failing contrast');
failingResult.suggestions.forEach((suggestion, index) => {
assert(suggestion.ratio >= 4.5, `Suggestion ${index + 1} meets AA normal text requirement`);
assert(suggestion.color.startsWith('#'), `Suggestion ${index + 1} color is in hex format`);
assert(suggestion.type === 'foreground' || suggestion.type === 'background',
`Suggestion ${index + 1} has valid type`);
});
console.log('\n=== Testing RGB to Hex Conversion ===\n');
assert(rgbToHex({ r: 255, g: 87, b: 51 }) === '#ff5733', 'Convert RGB to hex correctly');
assert(rgbToHex({ r: 0, g: 0, b: 0 }) === '#000000', 'Convert black to hex');
assert(rgbToHex({ r: 255, g: 255, b: 255 }) === '#ffffff', 'Convert white to hex');
console.log('\n=== Test Summary ===\n');
console.log(`Total Tests: ${testsPassed + testsFailed}`);
console.log(`Passed: ${testsPassed}`);
console.log(`Failed: ${testsFailed}`);
if (testsFailed === 0) {
console.log('\n✓ All tests passed!\n');
process.exit(0);
} else {
console.log(`\n✗ ${testsFailed} test(s) failed\n`);
process.exit(1);
}