117 lines
4.3 KiB
JavaScript
117 lines
4.3 KiB
JavaScript
// Browser simulation test for polyfill
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const vm = require('vm');
|
|
|
|
// Create a realistic browser context
|
|
const context = {
|
|
// Global browser objects
|
|
window: null, // will be set below
|
|
document: {
|
|
currentScript: { src: '', getAttribute: function(a) { return null; } },
|
|
getElementsByTagName: function() { return []; },
|
|
},
|
|
location: { origin: 'http://localhost:3000' },
|
|
console: console,
|
|
Math: Math,
|
|
JSON: JSON,
|
|
Array: Array,
|
|
Object: Object,
|
|
String: String,
|
|
Number: Number,
|
|
Function: Function,
|
|
Error: Error,
|
|
RegExp: RegExp,
|
|
Set: Set,
|
|
Map: Map,
|
|
Promise: Promise,
|
|
setTimeout: setTimeout,
|
|
setInterval: setInterval,
|
|
clearTimeout: clearTimeout,
|
|
clearInterval: clearInterval,
|
|
// Browser APIs used by the code
|
|
localStorage: {
|
|
getItem: function() { return null; },
|
|
setItem: function() {},
|
|
removeItem: function() {},
|
|
},
|
|
};
|
|
// window IS the global object in browsers
|
|
context.window = context;
|
|
|
|
vm.createContext(context);
|
|
|
|
const polyfillCode = fs.readFileSync('src/browser-polyfill.js', 'utf8');
|
|
const constantsCode = fs.readFileSync('src/core/constants.js', 'utf8');
|
|
const rngCode = fs.readFileSync('src/core/rng.js', 'utf8');
|
|
const utilsCode = fs.readFileSync('src/core/utils.js', 'utf8');
|
|
|
|
var results = [];
|
|
function assert(name, condition) {
|
|
results.push((condition ? 'PASS' : 'FAIL') + ': ' + name);
|
|
}
|
|
|
|
// Track which script is loading
|
|
function setScript(src) {
|
|
context.document.currentScript = { src: 'http://localhost:3000' + src, getAttribute: function(a) { return null; } };
|
|
}
|
|
|
|
// Load polyfill
|
|
try {
|
|
vm.runInContext(polyfillCode, context);
|
|
assert('module exists', typeof context.module !== 'undefined');
|
|
assert('require is function', typeof context.require === 'function');
|
|
} catch(e) { console.error('Polyfill error:', e.message); }
|
|
|
|
// Load constants.js
|
|
setScript('/src/core/constants.js');
|
|
try {
|
|
vm.runInContext(constantsCode, context);
|
|
assert('TILE on window', context.window.TILE === 32);
|
|
assert('COLS on window', context.window.COLS === 32);
|
|
assert('ROWS on window', context.window.ROWS === 24);
|
|
assert('TILE_TYPES is object', context.window.TILE_TYPES && typeof context.window.TILE_TYPES === 'object');
|
|
assert('TILE_TYPES.FLOOR=0', context.window.TILE_TYPES && context.window.TILE_TYPES.FLOOR === 0);
|
|
assert('TILE_TYPES.WALL=1', context.window.TILE_TYPES && context.window.TILE_TYPES.WALL === 1);
|
|
assert('T_FLOOR', context.T_FLOOR === 0);
|
|
assert('T_WALL', context.T_WALL === 1);
|
|
assert('COLORS exists', context.COLORS && typeof context.COLORS === 'object');
|
|
assert('SCREEN_TITLE', context.SCREEN_TITLE === 'title');
|
|
} catch(e) { console.error('constants error:', e.message); }
|
|
|
|
// Load rng.js
|
|
setScript('/src/core/rng.js');
|
|
try {
|
|
vm.runInContext(rngCode, context);
|
|
assert('createRNG is function', typeof context.createRNG === 'function');
|
|
assert('mulberry32 is function', typeof context.mulberry32 === 'function');
|
|
} catch(e) { console.error('rng error:', e.message); }
|
|
|
|
// Load utils.js (uses require('./constants'))
|
|
setScript('/src/core/utils.js');
|
|
try {
|
|
vm.runInContext(utilsCode, context);
|
|
assert('createEmptyLayout is function', typeof context.createEmptyLayout === 'function');
|
|
assert('createBorderLayout is function', typeof context.createBorderLayout === 'function');
|
|
assert('isPassable is function', typeof context.isPassable === 'function');
|
|
} catch(e) { console.error('utils error:', e.message); }
|
|
|
|
// Test require resolution
|
|
try {
|
|
context.document.currentScript = { src: 'http://localhost:3000/src/test.js', getAttribute: function() { return null; } };
|
|
var consts = context.require('./core/constants');
|
|
assert('require constants TILE=32', consts.TILE === 32);
|
|
assert('require constants COLS=32', consts.COLS === 32);
|
|
assert('require constants ROWS=24', consts.ROWS === 24);
|
|
assert('require constants TILE_TYPES', consts.TILE_TYPES && consts.TILE_TYPES.FLOOR === 0);
|
|
|
|
var rng = context.require('./core/rng');
|
|
assert('require createRNG', typeof rng.createRNG === 'function');
|
|
} catch(e) { console.error('require error:', e.message); }
|
|
|
|
console.log('\n=== Results ===');
|
|
results.forEach(r => console.log(r));
|
|
var passed = results.filter(r => r.startsWith('PASS')).length;
|
|
var failed = results.filter(r => r.startsWith('FAIL')).length;
|
|
console.log('\n' + passed + ' passed, ' + failed + ' failed');
|
|
process.exit(failed > 0 ? 1 : 0); |