|
| 1 | +// Test the general WasmBoy Library |
| 2 | + |
| 3 | +// Common test functions |
| 4 | +const commonTest = require('../common-test'); |
| 5 | + |
| 6 | +// Wasm Boy library |
| 7 | +const WasmBoy = require('../../dist/wasmboy.wasm.cjs.js').WasmBoy; |
| 8 | + |
| 9 | +// File management |
| 10 | +const fs = require('fs'); |
| 11 | + |
| 12 | +// Assertion |
| 13 | +const assert = require('assert'); |
| 14 | + |
| 15 | +// Initialize wasmBoy headless, with a speed option |
| 16 | +const WASMBOY_INITIALIZE_OPTIONS = { |
| 17 | + headless: true, |
| 18 | + gameboySpeed: 100.0, |
| 19 | + isGbcEnabled: true |
| 20 | +}; |
| 21 | + |
| 22 | +// Function for playing WasmBoy for a short amount of time |
| 23 | +const playWasmBoy = () => { |
| 24 | + let playResolve = undefined; |
| 25 | + const playPromise = new Promise(resolve => { |
| 26 | + playResolve = resolve; |
| 27 | + }); |
| 28 | + |
| 29 | + WasmBoy.play().then(() => { |
| 30 | + setTimeout(() => { |
| 31 | + WasmBoy.pause().then(() => { |
| 32 | + playResolve(); |
| 33 | + }); |
| 34 | + }, 100); |
| 35 | + }); |
| 36 | + |
| 37 | + return playPromise; |
| 38 | +}; |
| 39 | + |
| 40 | +// Path to roms we want to test |
| 41 | +const testRomsPath = './test/performance/testroms'; |
| 42 | + |
| 43 | +// Print our version |
| 44 | +console.log(`WasmBoy version: ${WasmBoy.getVersion()}`); |
| 45 | + |
| 46 | +describe('WasmBoy Lib', () => { |
| 47 | + // Define our wasmboy instance |
| 48 | + // Not using arrow functions, as arrow function timeouts were acting up |
| 49 | + beforeEach(function(done) { |
| 50 | + // Set a timeout of 5000, takes a while for wasm module to parse |
| 51 | + this.timeout(75000); |
| 52 | + |
| 53 | + // Read the test rom a a Uint8Array and pass to wasmBoy |
| 54 | + const testRomArray = new Uint8Array(fs.readFileSync(`${testRomsPath}/back-to-color/back-to-color.gbc`)); |
| 55 | + |
| 56 | + // Reset WasmBoy, and then load the rom |
| 57 | + WasmBoy.reset(WASMBOY_INITIALIZE_OPTIONS) |
| 58 | + .then(() => { |
| 59 | + return WasmBoy.loadROM(testRomArray); |
| 60 | + }) |
| 61 | + .then(() => { |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should be able to save/load state', async () => { |
| 67 | + // Play a snippet of WasmBoy |
| 68 | + await playWasmBoy(); |
| 69 | + |
| 70 | + // Save State |
| 71 | + const saveState = await WasmBoy.saveState(); |
| 72 | + |
| 73 | + // Play a snippet of WasmBoy |
| 74 | + await playWasmBoy(); |
| 75 | + |
| 76 | + // Load State |
| 77 | + await WasmBoy.loadState(saveState); |
| 78 | + |
| 79 | + // Save State |
| 80 | + const saveStateTwo = await WasmBoy.saveState(); |
| 81 | + |
| 82 | + // Save State should be the same |
| 83 | + const saveStateInternalState = new Uint8Array(saveState.wasmboyMemory.wasmBoyInternalState); |
| 84 | + const saveStateTwoInternalState = new Uint8Array(saveState.wasmboyMemory.wasmBoyInternalState); |
| 85 | + assert(saveStateInternalState[0] === saveStateTwoInternalState[0], true); |
| 86 | + }); |
| 87 | +}); |
0 commit comments