Skip to content

Commit 49633f8

Browse files
authored
Fixed Node Save States, and added a test (#313)
* Fixed Node Save States, and added a test * Added tests to travis
1 parent 203140d commit 49633f8

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ node_js:
77
install:
88
- npm install
99
script:
10-
- npm run prettier:lint && npm run demo:build:apps && npm run test:accuracy:nobuild
10+
- npm run prettier:lint
11+
- npm run demo:build:apps
12+
- npm run test:accuracy:nobuild
13+
- npm run test:integration:nobuild

lib/memory/idb.js

+9
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ if (typeof window !== 'undefined') {
6666
});
6767
}
6868
};
69+
} else {
70+
// Create a mock keyval for node
71+
keyval = {
72+
get: () => {},
73+
set: () => {},
74+
delete: () => {},
75+
clear: () => {},
76+
keys: () => {}
77+
};
6978
}
7079

7180
export const idbKeyval = keyval;

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
"test:perf": "npm run test:performance",
6868
"test:performance": "npx run-s build test:performance:nobuild",
6969
"test:performance:nobuild": "node --experimental-worker node_modules/mocha/bin/_mocha test/performance/performance-test.js --exit",
70+
"test:integration": "npx run-s build test:integration:nobuild",
71+
"test:integration:nobuild": "node --experimental-worker node_modules/mocha/bin/_mocha test/integration/integration-test.js --exit",
7072
"debugger:dev": "npm run debugger:watch",
7173
"debugger:watch": "npx rollup -c -w --environment DEBUGGER,SERVE",
7274
"debugger:build": "npx rollup -c --environment DEBUGGER",

test/integration/integration-test.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)