-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-runner.js
More file actions
446 lines (404 loc) · 10.5 KB
/
test-runner.js
File metadata and controls
446 lines (404 loc) · 10.5 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import puppeteer from 'puppeteer';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
Test runner for CSS functions using Puppeteer.
Validates computed styles match expected values.
*/
const tests = [
// Math & Number tests
{
name: '--negate(10px)',
selector: '.test-math',
property: '--test-negate-positive',
expected: 'calc(-1 * 10px)',
},
{
name: '--negate(-10px)',
selector: '.test-math',
property: '--test-negate-negative',
expected: 'calc(-1 * -10px)',
},
{
name: '--lerp(0px, 100px, 0.5)',
selector: '.test-math',
property: '--test-lerp-half',
expected: 'calc(0px + (100px - 0px) * 0.5)',
},
{
name: '--map-range(50, 0, 100, 0px, 200px)',
selector: '.test-math',
property: '--test-map-range',
expected: 'calc(0px + (200px - 0px) * clamp(0, calc((50 - 0) / (100 - 0)), 1))',
},
{
name: '--ratio(8px * 2, 0.55em + 0.45em)',
selector: '.test-math',
property: '--test-ratio-dimension',
expected: 'tan(atan2(8px * 2, 0.55em + 0.45em))',
},
{
name: '--ratio(8 * 2, 5.5 + 4.5)',
selector: '.test-math',
property: '--test-ratio-number',
expected: 'tan(atan2(8 * 2, 5.5 + 4.5))',
},
{
name: '--ratio(80% * 2, 55% + 45%)',
selector: '.test-math',
property: '--test-ratio-percentage',
expected: 'tan(atan2(80% * 2, 55% + 45%))',
},
// Color tests
{
name: '--opacity(red, 50%)',
selector: '.test-color',
property: '--test-opacity-half',
expected: 'rgb(from red r g b / 50%)',
},
{
name: '--black(50%)',
selector: '.test-color',
property: '--test-black-50',
expected: 'rgb(0 0 0 / 50%)',
},
{
name: '--white(50%)',
selector: '.test-color',
property: '--test-white-50',
expected: 'rgb(255 255 255 / 50%)',
},
// Typography tests
{
name: '--modular-scale(1rem, 1.25, 0)',
selector: '.test-typography',
property: '--test-scale-0',
expected: 'calc(1rem * pow(1.25, 0))',
},
{
name: '--line-height-length(16px, 1.5)',
selector: '.test-typography',
property: '--test-line-height',
expected: 'calc(16px * 1.5)',
},
// Spacing tests
{
name: '--spacing(0)',
selector: '.test-spacing',
property: '--test-spacing-0',
expected: 'calc(0.25rem * pow(2, 0))',
},
{
name: '--spacing(1)',
selector: '.test-spacing',
property: '--test-spacing-1',
expected: 'calc(0.25rem * pow(2, 1))',
},
{
name: '--spacing(2)',
selector: '.test-spacing',
property: '--test-spacing-2',
expected: 'calc(0.25rem * pow(2, 2))',
},
{
name: '--spacing(3)',
selector: '.test-spacing',
property: '--test-spacing-3',
expected: 'calc(0.25rem * pow(2, 3))',
},
// Utility tests
{
name: '--px-to-rem(16px)',
selector: '.test-utility',
property: '--test-px-to-rem-16',
expected: 'calc(16px / 16px * 1rem)',
},
{
name: '--px-to-rem(24px)',
selector: '.test-utility',
property: '--test-px-to-rem-24',
expected: 'calc(24px / 16px * 1rem)',
},
{
name: '--rem-to-px(1rem)',
selector: '.test-utility',
property: '--test-rem-to-px-1',
expected: 'calc(1rem / 1rem * 16px)',
},
// Grid tests
{
name: '--grid-span(3)',
selector: '.test-grid',
property: '--test-span-3',
expected: 'span round(clamp(1, 3, 12))',
},
{
name: '--auto-grid(200px, 3) should cap columns',
selector: '.test-grid',
property: '--test-auto-grid-capped',
expected: 'repeat(auto-fit, minmax(max(200px, calc(100% / 3)), 1fr))',
},
// Color function fixes - clamping tests
{
name: '--saturate(red, 10) should clamp chroma',
selector: '.test-color',
property: '--test-saturate-clamped',
expected: 'oklch(from red l clamp(0, calc(c * 10), 0.4) h)',
},
{
name: '--lighten(red, 200%) should clamp to 1',
selector: '.test-color',
property: '--test-lighten-clamped',
expected: 'oklch(from red clamp(0, calc(l + 200% / 100%), 1) c h)',
},
// New line-height-unitless function
{
name: '--line-height-unitless(16px)',
selector: '.test-typography',
property: '--test-line-height-unitless',
expected: 'calc(16px * 1.5 / 1px)',
},
// Edge case tests
{
name: '--negate(0) should return 0',
selector: '.test-edge-cases',
property: '--test-negate-zero',
expected: 'calc(-1 * 0)',
},
{
name: '--spacing(0) should return base value',
selector: '.test-edge-cases',
property: '--test-spacing-zero',
expected: 'calc(0.25rem * pow(2, 0))',
},
{
name: '--spacing(10) should handle large values',
selector: '.test-edge-cases',
property: '--test-spacing-large',
expected: 'calc(0.25rem * pow(2, 10))',
},
{
name: '--lerp at progress 0 returns start value',
selector: '.test-edge-cases',
property: '--test-lerp-start',
expected: 'calc(10px + (50px - 10px) * 0)',
},
{
name: '--lerp at progress 1 returns end value',
selector: '.test-edge-cases',
property: '--test-lerp-end',
expected: 'calc(10px + (50px - 10px) * 1)',
},
{
name: '--opacity with 0% should be fully transparent',
selector: '.test-edge-cases',
property: '--test-opacity-zero',
expected: 'rgb(from red r g b / 0%)',
},
{
name: '--opacity with 100% should be fully opaque',
selector: '.test-edge-cases',
property: '--test-opacity-full',
expected: 'rgb(from red r g b / 100%)',
},
];
/**
* Create test HTML page.
*/
function createTestHTML() {
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Extras - Test Suite</title>
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" href="test.css">
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
padding: 2rem;
}
.test-container {
display: none; /* Hidden elements for testing */
}
</style>
</head>
<body>
<h1>CSS Extras Test Suite</h1>
<p>Running automated tests...</p>
<!-- Test elements -->
<div class="test-container">
<div class="test-math"></div>
<div class="test-color"></div>
<div class="test-typography"></div>
<div class="test-layout"></div>
<div class="test-spacing"></div>
<div class="test-animation"></div>
<div class="test-utility"></div>
<div class="test-grid"></div>
<div class="test-filter"></div>
<div class="test-theme"></div>
<div class="test-theme-dark"></div>
<div class="test-edge-cases"></div>
</div>
</body>
</html>`;
const testHtmlPath = path.join(__dirname, 'test.html');
fs.writeFileSync(testHtmlPath, html);
return testHtmlPath;
}
/**
Run tests using Puppeteer.
*/
async function runTests() {
const testHtmlPath = createTestHTML();
let browser;
try {
browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
// Navigate to test page
await page.goto(`file://${testHtmlPath}`, {
waitUntil: 'networkidle0',
});
console.log('🧪 Running CSS function tests...\n');
let passed = 0;
let failed = 0;
const failures = [];
// Run each test
for (const test of tests) {
try {
// eslint-disable-next-line no-await-in-loop
const result = await page.evaluate((selector, property) => {
// eslint-disable-next-line no-undef
const element = document.querySelector(selector);
if (!element) {
return null;
}
// eslint-disable-next-line no-undef
const computed = getComputedStyle(element);
return computed.getPropertyValue(property);
}, test.selector, test.property);
let isPass = false;
if (test.isRegex && test.expected instanceof RegExp) {
isPass = test.expected.test(result);
} else if (typeof test.expected === 'string') {
// Normalize values for comparison
const normalizedResult = normalizeValue(result);
const normalizedExpected = normalizeValue(test.expected);
isPass = normalizedResult === normalizedExpected;
}
if (isPass) {
console.log(`✅ ${test.name}`);
passed++;
} else {
console.log(`❌ ${test.name}`);
console.log(` Expected: ${test.expected}`);
console.log(` Got: ${result}`);
failed++;
failures.push({
test: test.name,
expected: test.expected.toString(),
actual: result,
});
}
} catch (error) {
console.log(`❌ ${test.name} - Error: ${error.message}`);
failed++;
failures.push({
test: test.name,
error: error.message,
});
}
}
// Summary
console.log('\n' + '='.repeat(50));
console.log('\n📊 Test Results:');
console.log(` Passed: ${passed}/${tests.length}`);
console.log(` Failed: ${failed}/${tests.length}`);
if (failures.length > 0) {
console.log('\n❌ Failed tests:');
for (const failure of failures) {
console.log(` - ${failure.test}`);
if (failure.expected) {
console.log(` Expected: ${failure.expected}`);
console.log(` Actual: ${failure.actual}`);
}
if (failure.error) {
console.log(` Error: ${failure.error}`);
}
}
}
return failed === 0;
} finally {
// Always close browser and clean up test HTML
if (browser) {
await browser.close();
}
if (fs.existsSync(testHtmlPath)) {
fs.unlinkSync(testHtmlPath);
}
}
}
/**
Normalize CSS values for comparison.
*/
function normalizeValue(value) {
if (!value) {
return '';
}
// Trim whitespace
value = value.trim();
// Normalize spacing in rgb/rgba
value = value.replaceAll(/rgba?\s*\(\s*/g, 'rgb(');
value = value.replaceAll(/,\s*/g, ', ');
value = value.replaceAll(/\s*\)/g, ')');
// Normalize grid template columns - remove all whitespace/newlines
if (value.includes('repeat') && value.includes('minmax')) {
value = value.replaceAll(' ', ' ').replaceAll('\n', '').replaceAll('\t', '');
value = value.replaceAll(', ', ', ');
value = value.replaceAll('( ', '(');
value = value.replaceAll(' )', ')');
}
// Normalize 0 values
if (value === '0px') {
value = '0';
}
return value;
}
/**
Install Puppeteer if not already installed.
*/
async function ensurePuppeteer() {
try {
await import('puppeteer');
} catch {
console.log('📦 Installing Puppeteer for testing...');
const {execSync} = await import('node:child_process');
execSync('npm install --save-dev puppeteer', {stdio: 'inherit'});
console.log('✅ Puppeteer installed\n');
}
}
// Main execution
async function main() {
await ensurePuppeteer();
const success = await runTests();
if (success) {
console.log('\n✅ All tests passed!');
process.exit(0);
} else {
console.log('\n❌ Some tests failed');
process.exit(1);
}
}
await main().catch(error => {
console.error('Test runner error:', error);
process.exit(1);
});