-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-browser-import.html
More file actions
428 lines (371 loc) Β· 17.4 KB
/
test-browser-import.html
File metadata and controls
428 lines (371 loc) Β· 17.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Codi Browser Import Test</title>
<style>
body {
font-family: 'Courier New', monospace;
background: #1a1a1a;
color: #ffffff;
padding: 20px;
margin: 0;
}
.test-result {
padding: 15px;
margin: 10px 0;
border-radius: 8px;
border: 2px solid;
}
.success {
background: #d4edda;
color: #155724;
border-color: #c3e6cb;
}
.error {
background: #f8d7da;
color: #721c24;
border-color: #f5c6cb;
}
.info {
background: #d1ecf1;
color: #0c5460;
border-color: #bee5eb;
}
pre {
background: #000;
padding: 10px;
border-radius: 4px;
overflow-x: auto;
margin: 10px 0;
}
h1 {
color: #4CAF50;
text-align: center;
}
.test-section {
margin: 20px 0;
padding: 15px;
border: 1px solid #333;
border-radius: 8px;
}
</style>
</head>
<body>
<h1>πΆ Codi Browser Import Test</h1>
<div class="test-section">
<h2>π Version Selection</h2>
<label for="version-select">Test Version: </label>
<select id="version-select">
<option value="latest">Latest Stable</option>
<option value="beta">Latest Beta</option>
<option value="alpha">Latest Alpha</option>
<option value="1.0.39">v1.0.40-beta (Specific)</option>
</select>
<button id="run-tests" style="margin-left: 10px; padding: 5px 15px;">
Run Tests
</button>
<button id="test-local" style="margin-left: 10px; padding: 5px 15px;">
Test Local Build
</button>
</div>
<div id="results"></div>
<script type="module">
const results = document.getElementById('results');
const versionSelect = document.getElementById('version-select');
const runTestsBtn = document.getElementById('run-tests');
const testLocalBtn = document.getElementById('test-local');
function addResult(title, success, message, details = '') {
const div = document.createElement('div');
div.className = `test-result ${success ? 'success' : 'error'}`;
div.innerHTML = `
<h3>${success ? 'β
' : 'β'} ${title}</h3>
<p>${message}</p>
${details ? `<pre>${details}</pre>` : ''}
`;
results.appendChild(div);
}
function addInfo(title, message) {
const div = document.createElement('div');
div.className = 'test-result info';
div.innerHTML = `
<h3>βΉοΈ ${title}</h3>
<p>${message}</p>
`;
results.appendChild(div);
}
function clearResults() {
results.innerHTML = '';
}
async function runVersionTests(version) {
clearResults();
addInfo('Testing Configuration', `Version: ${version} | Timestamp: ${new Date().toLocaleString()}`);
// Test 1: Browser-specific ESM import
addInfo('Test 1', `Attempting ESM import from JSDelivr (browser-specific entry point)...`);
try {
const esmUrl = `https://cdn.jsdelivr.net/npm/codi-test-framework@${version}/src/_codi.browser.js`;
const codi = await import(esmUrl);
addResult(
'JSDelivr ESM Import (Browser Entry)',
true,
'Successfully imported Codi from JSDelivr CDN using browser-specific entry point',
`URL: ${esmUrl}\nVersion: ${codi.version || 'Unknown'}\nAvailable functions: ${Object.keys(codi).join(', ')}`
);
// Test functionality
await testFunctionality(codi, 'ESM Import');
} catch (importError) {
addResult(
'JSDelivr ESM Import (Browser Entry)',
false,
'Failed to import from JSDelivr CDN',
`Error: ${importError.message}\nStack: ${importError.stack}`
);
// Fallback to UNPKG
addInfo('Test 1b', 'Attempting fallback to UNPKG CDN...');
try {
const unpkgUrl = `https://unpkg.com/codi-test-framework@${version}/src/_codi.browser.js`;
const codi = await import(unpkgUrl);
addResult(
'UNPKG ESM Import (Browser Entry)',
true,
'Successfully imported from UNPKG CDN as fallback',
`URL: ${unpkgUrl}\nVersion: ${codi.version || 'Unknown'}\nAvailable functions: ${Object.keys(codi).join(', ')}`
);
await testFunctionality(codi, 'UNPKG Import');
} catch (unpkgError) {
addResult(
'UNPKG ESM Import (Browser Entry)',
false,
'Fallback to UNPKG also failed',
unpkgError.message
);
}
}
// Test 2: IIFE build
addInfo('Test 2', 'Testing IIFE build availability...');
await testIIFEBuild(version);
// Test 3: Primary ESM.sh import (should now work with fixed main entry point)
addInfo('Test 3', 'Testing ESM.sh with main entry point (should now work)...');
try {
const esmUrl = `https://esm.sh/codi-test-framework@${version}`;
const codi = await import(esmUrl);
addResult(
'ESM.sh Import (Main Entry)',
true,
'ESM.sh import succeeded with main entry point - this is the preferred method!',
`URL: ${esmUrl}\nVersion: ${codi.version || 'Unknown'}\nAvailable functions: ${Object.keys(codi).join(', ')}`
);
// Test functionality with esm.sh import
await testFunctionality(codi, 'ESM.sh');
} catch (esmError) {
addResult(
'ESM.sh Import (Main Entry)',
false,
'ESM.sh import failed - this should now work with the fixed main entry point',
`Error: ${esmError.message}\nStack: ${esmError.stack}`
);
}
// Add recommendations
addRecommendations(version);
}
async function testFunctionality(codi, testContext) {
try {
let testPassed = false;
let testDetails = [];
// Test individual functions
const functionsToTest = ['describe', 'it', 'assertEqual', 'assertTrue', 'assertFalse'];
const availableFunctions = functionsToTest.filter(fn => typeof codi[fn] === 'function');
testDetails.push(`Available functions: ${availableFunctions.join(', ')}`);
if (availableFunctions.length === functionsToTest.length) {
// Run a basic test
await codi.runWebTestFunction(async () => {
codi.describe({ name: `${testContext} Test`, id: 'import_test' }, () => {
codi.it({ name: 'should work correctly', parentId: 'import_test' }, () => {
codi.assertEqual(2 + 2, 4, 'Math should work');
codi.assertTrue(typeof window !== 'undefined', 'Window should be available');
codi.assertFalse(typeof process !== 'undefined', 'Process should not be available in browser');
testPassed = true;
});
});
}, { quiet: true, showSummary: false });
testDetails.push('Basic functionality test passed');
} else {
testDetails.push(`Missing functions: ${functionsToTest.filter(fn => !availableFunctions.includes(fn)).join(', ')}`);
}
addResult(
`${testContext} Functionality Test`,
testPassed,
testPassed ? 'All assertion functions work correctly' : 'Some functionality issues detected',
testDetails.join('\n')
);
} catch (funcError) {
addResult(
`${testContext} Functionality Test`,
false,
'Import succeeded but functionality test failed',
`Error: ${funcError.message}\nStack: ${funcError.stack}`
);
}
}
async function testIIFEBuild(version) {
return new Promise((resolve) => {
const script = document.createElement('script');
const scriptUrl = `https://cdn.jsdelivr.net/npm/codi-test-framework@${version}/dist/codi.browser.js`;
script.src = scriptUrl;
script.onload = () => {
if (typeof window.codi !== 'undefined') {
addResult(
'IIFE Build Load',
true,
'IIFE build loaded successfully and codi is available globally',
`URL: ${scriptUrl}\nGlobal codi object available with functions: ${Object.keys(window.codi).join(', ')}\nVersion: ${window.codi.version || 'Unknown'}`
);
// Test IIFE functionality
testIIFEFunctionality();
} else {
addResult(
'IIFE Build Load',
false,
'IIFE build loaded but codi global not found',
`URL: ${scriptUrl}\nScript loaded but window.codi is undefined`
);
}
resolve();
};
script.onerror = (error) => {
addResult(
'IIFE Build Load',
false,
'Failed to load IIFE build from CDN',
`URL: ${scriptUrl}\nThis might indicate the file is not published or CDN is not accessible\nError: ${error}`
);
resolve();
};
document.head.appendChild(script);
});
}
function testIIFEFunctionality() {
try {
if (typeof window.codi === 'undefined') {
throw new Error('window.codi is not available');
}
// Test that individual functions are also available globally
const globalFunctions = ['describe', 'it', 'assertEqual', 'assertTrue', 'assertFalse'];
const availableGlobals = globalFunctions.filter(fn => typeof window[fn] === 'function');
let testPassed = false;
// Run a simple test using global functions
if (availableGlobals.includes('assertEqual')) {
window.assertEqual(3 + 3, 6, 'Global function test');
testPassed = true;
}
addResult(
'IIFE Global Functions Test',
testPassed,
testPassed ? 'Global functions are working correctly' : 'Global functions test failed',
`Available global functions: ${availableGlobals.join(', ')}\nMissing: ${globalFunctions.filter(fn => !availableGlobals.includes(fn)).join(', ')}`
);
} catch (error) {
addResult(
'IIFE Global Functions Test',
false,
'IIFE functionality test failed',
`Error: ${error.message}`
);
}
}
async function testLocalBuild() {
clearResults();
addInfo('Local Build Test', 'Testing locally built bundle...');
// Test local IIFE build
const script = document.createElement('script');
script.src = './dist/codi.browser.js';
script.onload = () => {
if (typeof window.codi !== 'undefined') {
addResult(
'Local IIFE Build',
true,
'Local IIFE build loaded successfully',
`Local build available with functions: ${Object.keys(window.codi).join(', ')}\nVersion: ${window.codi.version || 'Unknown'}`
);
testIIFEFunctionality();
} else {
addResult(
'Local IIFE Build',
false,
'Local IIFE build loaded but codi global not found',
'Check if dist/codi.browser.js exists and is properly built'
);
}
};
script.onerror = () => {
addResult(
'Local IIFE Build',
false,
'Failed to load local IIFE build',
'Make sure you have run "npm run build" and the dist/codi.browser.js file exists'
);
};
document.head.appendChild(script);
}
function addRecommendations(version) {
setTimeout(() => {
const recommendations = document.createElement('div');
recommendations.className = 'test-section';
recommendations.innerHTML = `
<h2>π Recommendations for Version ${version}</h2>
<p><strong>For reliable browser usage:</strong></p>
<ol>
<li><strong>π― Preferred:</strong> Use ESM.sh with main entry point (now browser-compatible):
<br><code>https://esm.sh/codi-test-framework@${version}</code></li>
<li><strong>Alternative:</strong> Use JSDelivr with browser-specific entry point:
<br><code>https://cdn.jsdelivr.net/npm/codi-test-framework@${version}/src/_codi.browser.js</code></li>
<li><strong>Script Tag:</strong> Use IIFE build for script tag inclusion:
<br><code>https://cdn.jsdelivr.net/npm/codi-test-framework@${version}/dist/codi.browser.js</code></li>
<li><strong>Experimental:</strong> For testing beta/alpha versions:
<br><code>https://esm.sh/codi-test-framework@beta</code></li>
<li><strong>Development:</strong> Use local build for offline development</li>
</ol>
<h3>π§ Quick Implementation Examples</h3>
<p><strong>π― ESM Import (Recommended - ESM.sh):</strong></p>
<pre>import * as codi from 'https://esm.sh/codi-test-framework@${version}';</pre>
<p><strong>ESM Import (Alternative - JSDelivr):</strong></p>
<pre>import * as codi from 'https://cdn.jsdelivr.net/npm/codi-test-framework@${version}/src/_codi.browser.js';</pre>
<p><strong>Script Tag:</strong></p>
<pre><script src="https://cdn.jsdelivr.net/npm/codi-test-framework@${version}/dist/codi.browser.js"></script></pre>
<h3>π Automated Build Information</h3>
<p>This version was built using the automated CI/CD pipeline which:</p>
<ul>
<li>β
Runs comprehensive tests (Node.js + Browser)</li>
<li>β
Validates bundle integrity</li>
<li>β
Ensures CDN compatibility (ESM.sh, JSDelivr, UNPKG)</li>
<li>β
Tests main entry point browser compatibility</li>
<li>β
Publishes with appropriate npm tags</li>
</ul>
<h3>β¨ ESM.sh Compatibility</h3>
<p>The main entry point now works seamlessly with ESM.sh by:</p>
<ul>
<li>π§ Using browser-safe dynamic imports</li>
<li>π§ Providing stub functions for Node.js-only features in browser</li>
<li>π§ Avoiding top-level await that can break ESM.sh</li>
<li>π§ Maintaining full compatibility with both browser and Node.js</li>
</ul>
`;
results.appendChild(recommendations);
}, 1000);
}
// Event listeners
runTestsBtn.addEventListener('click', () => {
const selectedVersion = versionSelect.value;
runVersionTests(selectedVersion);
});
testLocalBtn.addEventListener('click', () => {
testLocalBuild();
});
// Auto-run tests for latest version on page load
document.addEventListener('DOMContentLoaded', () => {
runVersionTests('latest');
});
</script>
</body>
</html>