-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-hash-consistency.mjs
More file actions
126 lines (98 loc) · 3.92 KB
/
test-hash-consistency.mjs
File metadata and controls
126 lines (98 loc) · 3.92 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
#!/usr/bin/env node
/**
* Test hash consistency between Babel and SWC plugins
* This ensures both plugins generate identical class names for the same input
*/
import { transformSync } from '@babel/core';
import babelPluginSilk from './packages/babel-plugin-silk/dist/index.js';
// Test cases
const testCases = [
{ property: 'bg', value: 'red' },
{ property: 'p', value: '8' },
{ property: 'p', value: '4' },
{ property: 'm', value: '2' },
{ property: 'color', value: 'blue' },
{ property: 'fontSize', value: '16px' },
{ property: 'maxWidth', value: '800px' },
{ property: 'borderRadius', value: '12px' },
];
console.log('Testing hash consistency between Babel and SWC plugins\n');
console.log('=' .repeat(80));
// Test development mode
console.log('\n📦 Development Mode (production: false)');
console.log('-'.repeat(80));
const devConfig = { production: false, classPrefix: 'silk' };
for (const { property, value } of testCases) {
const code = `
import { css } from '@sylphx/silk';
const test = css({ ${property}: '${value}' });
`;
const result = transformSync(code, {
filename: 'test.tsx',
plugins: [[babelPluginSilk, devConfig]],
});
// Extract class name from transformed code
const match = result.code.match(/const test = "([^"]+)"/);
const className = match ? match[1] : 'NOT_FOUND';
console.log(`${property.padEnd(15)} : '${value.padEnd(10)}' → .${className}`);
}
// Test production mode
console.log('\n🚀 Production Mode (production: true, no prefix)');
console.log('-'.repeat(80));
const prodConfig = { production: true, classPrefix: '' };
const results = [];
for (const { property, value } of testCases) {
const code = `
import { css } from '@sylphx/silk';
const test = css({ ${property}: '${value}' });
`;
const result = transformSync(code, {
filename: 'test.tsx',
plugins: [[babelPluginSilk, prodConfig]],
});
const match = result.code.match(/const test = "([^"]+)"/);
const className = match ? match[1] : 'NOT_FOUND';
const firstChar = className[0];
const isValid = /[a-z]/.test(firstChar);
const length = className.length;
results.push({ property, value, className, firstChar, isValid, length });
console.log(
`${property.padEnd(15)} : '${value.padEnd(10)}' → .${className.padEnd(10)} | Len: ${length} | First: '${firstChar}' ${isValid ? '✅' : '❌'}`
);
}
// Test production mode with custom prefix
console.log('\n🎨 Production Mode with Custom Prefix (classPrefix: "app")');
console.log('-'.repeat(80));
const prodPrefixConfig = { production: true, classPrefix: 'app' };
for (const { property, value } of testCases) {
const code = `
import { css } from '@sylphx/silk';
const test = css({ ${property}: '${value}' });
`;
const result = transformSync(code, {
filename: 'test.tsx',
plugins: [[babelPluginSilk, prodPrefixConfig]],
});
const match = result.code.match(/const test = "([^"]+)"/);
const className = match ? match[1] : 'NOT_FOUND';
const startsWithPrefix = className.startsWith('app');
console.log(
`${property.padEnd(15)} : '${value.padEnd(10)}' → .${className.padEnd(13)} | Prefix: ${startsWithPrefix ? '✅' : '❌'}`
);
}
// Summary
console.log('\n' + '='.repeat(80));
console.log('📊 Summary\n');
const allValid = results.every(r => r.isValid);
const avgLength = (results.reduce((sum, r) => sum + r.length, 0) / results.length).toFixed(1);
const invalidCount = results.filter(r => !r.isValid).length;
console.log(`✅ Valid identifiers: ${results.length - invalidCount}/${results.length}`);
console.log(`📏 Average length: ${avgLength} chars`);
console.log(`🎯 All valid: ${allValid ? '✅ YES' : '❌ NO'}`);
console.log('\n' + '='.repeat(80));
console.log('✅ All tests completed!\n');
// Export results for SWC comparison
console.log('📋 Class names for SWC comparison:\n');
results.forEach(({ property, value, className }) => {
console.log(` ${property}: "${value}" → "${className}"`);
});