2018-11-27 16:06:56 -05:00
|
|
|
const tap = require('tap');
|
2018-10-30 15:26:22 -04:00
|
|
|
const path = require('path');
|
|
|
|
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
|
|
|
|
const VirtualMachine = require('../../src/virtual-machine');
|
2017-04-20 19:17:05 -04:00
|
|
|
const Runtime = require('../../src/engine/runtime');
|
2017-05-30 10:24:09 -04:00
|
|
|
const MonitorRecord = require('../../src/engine/monitor-record');
|
2017-05-19 17:28:00 -04:00
|
|
|
const {Map} = require('immutable');
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2018-11-27 16:06:56 -05:00
|
|
|
tap.tearDown(() => process.nextTick(process.exit));
|
|
|
|
|
|
|
|
const test = tap.test;
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('spec', t => {
|
|
|
|
const r = new Runtime();
|
2016-04-18 17:20:30 -04:00
|
|
|
|
|
|
|
t.type(Runtime, 'function');
|
|
|
|
t.type(r, 'object');
|
2018-10-31 19:20:13 -04:00
|
|
|
|
|
|
|
// Test types of cloud data managing functions
|
|
|
|
t.type(r.hasCloudData, 'function');
|
|
|
|
t.type(r.canAddCloudVariable, 'function');
|
|
|
|
t.type(r.addCloudVariable, 'function');
|
|
|
|
t.type(r.removeCloudVariable, 'function');
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
t.ok(r instanceof Runtime);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2017-05-19 12:28:05 -04:00
|
|
|
|
2017-05-19 17:28:00 -04:00
|
|
|
test('monitorStateEquals', t => {
|
2017-05-19 12:28:05 -04:00
|
|
|
const r = new Runtime();
|
2017-05-19 17:28:00 -04:00
|
|
|
const id = 'xklj4#!';
|
2017-05-24 15:42:29 -04:00
|
|
|
const prevMonitorState = MonitorRecord({
|
2017-05-19 17:28:00 -04:00
|
|
|
id,
|
2017-05-26 13:50:50 -04:00
|
|
|
opcode: 'turtle whereabouts',
|
|
|
|
value: '25'
|
2017-05-19 17:28:00 -04:00
|
|
|
});
|
|
|
|
const newMonitorDelta = Map({
|
|
|
|
id,
|
2017-05-19 12:28:05 -04:00
|
|
|
value: String(25)
|
2017-05-19 17:28:00 -04:00
|
|
|
});
|
|
|
|
r.requestAddMonitor(prevMonitorState);
|
|
|
|
r.requestUpdateMonitor(newMonitorDelta);
|
|
|
|
|
2017-05-26 13:50:50 -04:00
|
|
|
t.equals(true, prevMonitorState === r._monitorState.get(id));
|
2017-05-19 17:28:00 -04:00
|
|
|
t.equals(String(25), r._monitorState.get(id).get('value'));
|
2017-05-19 12:28:05 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-05-19 17:28:00 -04:00
|
|
|
test('monitorStateDoesNotEqual', t => {
|
2017-05-19 12:28:05 -04:00
|
|
|
const r = new Runtime();
|
2017-05-19 17:28:00 -04:00
|
|
|
const id = 'xklj4#!';
|
2017-05-26 13:50:50 -04:00
|
|
|
const params = {seven: 7};
|
2017-05-24 15:42:29 -04:00
|
|
|
const prevMonitorState = MonitorRecord({
|
2017-05-19 17:28:00 -04:00
|
|
|
id,
|
2017-05-26 13:50:50 -04:00
|
|
|
opcode: 'turtle whereabouts',
|
2017-05-24 15:42:29 -04:00
|
|
|
value: '25'
|
2017-05-19 17:28:00 -04:00
|
|
|
});
|
2017-05-19 12:28:05 -04:00
|
|
|
|
|
|
|
// Value change
|
2017-05-19 17:28:00 -04:00
|
|
|
let newMonitorDelta = Map({
|
|
|
|
id,
|
2017-05-19 12:28:05 -04:00
|
|
|
value: String(24)
|
2017-05-19 17:28:00 -04:00
|
|
|
});
|
|
|
|
r.requestAddMonitor(prevMonitorState);
|
|
|
|
r.requestUpdateMonitor(newMonitorDelta);
|
2018-09-12 17:23:01 -04:00
|
|
|
|
2017-05-19 17:28:00 -04:00
|
|
|
t.equals(false, prevMonitorState.equals(r._monitorState.get(id)));
|
|
|
|
t.equals(String(24), r._monitorState.get(id).get('value'));
|
2017-05-19 12:28:05 -04:00
|
|
|
|
|
|
|
// Prop change
|
2017-05-19 17:28:00 -04:00
|
|
|
newMonitorDelta = Map({
|
2017-05-19 12:28:05 -04:00
|
|
|
id: 'xklj4#!',
|
2017-05-26 13:50:50 -04:00
|
|
|
params: params
|
2017-05-19 17:28:00 -04:00
|
|
|
});
|
|
|
|
r.requestUpdateMonitor(newMonitorDelta);
|
|
|
|
|
|
|
|
t.equals(false, prevMonitorState.equals(r._monitorState.get(id)));
|
2017-05-26 13:50:50 -04:00
|
|
|
t.equals(String(24), r._monitorState.get(id).value);
|
|
|
|
t.equals(params, r._monitorState.get(id).params);
|
2017-05-19 12:28:05 -04:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2018-09-12 17:23:01 -04:00
|
|
|
|
|
|
|
test('getLabelForOpcode', t => {
|
|
|
|
const r = new Runtime();
|
|
|
|
|
|
|
|
const fakeExtension = {
|
|
|
|
id: 'fakeExtension',
|
|
|
|
name: 'Fake Extension',
|
|
|
|
blocks: [
|
|
|
|
{
|
|
|
|
info: {
|
|
|
|
opcode: 'foo',
|
|
|
|
json: {},
|
|
|
|
text: 'Foo',
|
|
|
|
xml: ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
info: {
|
|
|
|
opcode: 'foo_2',
|
|
|
|
json: {},
|
|
|
|
text: 'Foo 2',
|
|
|
|
xml: ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
r._blockInfo.push(fakeExtension);
|
|
|
|
|
|
|
|
const result1 = r.getLabelForOpcode('fakeExtension_foo');
|
|
|
|
t.type(result1.category, 'string');
|
|
|
|
t.type(result1.label, 'string');
|
|
|
|
t.equals(result1.label, 'Fake Extension: Foo');
|
|
|
|
|
|
|
|
const result2 = r.getLabelForOpcode('fakeExtension_foo_2');
|
|
|
|
t.type(result2.category, 'string');
|
|
|
|
t.type(result2.label, 'string');
|
|
|
|
t.equals(result2.label, 'Fake Extension: Foo 2');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2018-10-30 15:26:22 -04:00
|
|
|
|
|
|
|
test('Project loaded emits runtime event', t => {
|
|
|
|
const vm = new VirtualMachine();
|
|
|
|
const projectUri = path.resolve(__dirname, '../fixtures/default.sb2');
|
|
|
|
const project = readFileToBuffer(projectUri);
|
|
|
|
let projectLoaded = false;
|
|
|
|
|
|
|
|
vm.runtime.addListener('PROJECT_LOADED', () => {
|
|
|
|
projectLoaded = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
vm.loadProject(project).then(() => {
|
|
|
|
t.equal(projectLoaded, true, 'Project load event emitted');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
2018-10-30 18:54:25 -04:00
|
|
|
|
|
|
|
test('Cloud variable limit allows only 8 cloud variables', t => {
|
|
|
|
// This is a test of just the cloud variable limit mechanism
|
|
|
|
// The functions being tested below need to be used when
|
|
|
|
// creating and deleting cloud variables in the runtime.
|
|
|
|
|
|
|
|
const rt = new Runtime();
|
|
|
|
|
2018-10-31 13:40:05 -04:00
|
|
|
t.equal(rt.hasCloudData(), false);
|
|
|
|
|
2018-10-30 18:54:25 -04:00
|
|
|
for (let i = 0; i < 8; i++) {
|
2018-10-31 13:40:05 -04:00
|
|
|
t.equal(rt.canAddCloudVariable(), true);
|
|
|
|
rt.addCloudVariable();
|
|
|
|
// Adding a cloud variable should change the
|
|
|
|
// result of the hasCloudData check
|
|
|
|
t.equal(rt.hasCloudData(), true);
|
2018-10-30 18:54:25 -04:00
|
|
|
}
|
|
|
|
|
2018-10-31 13:40:05 -04:00
|
|
|
|
2018-10-30 18:54:25 -04:00
|
|
|
// We should be at the cloud variable limit now
|
2018-10-31 13:40:05 -04:00
|
|
|
t.equal(rt.canAddCloudVariable(), false);
|
2018-10-30 18:54:25 -04:00
|
|
|
|
|
|
|
// Removing a cloud variable should allow the addition of exactly one more
|
|
|
|
// when we are at the cloud variable limit
|
2018-10-31 13:40:05 -04:00
|
|
|
rt.removeCloudVariable();
|
2018-10-30 18:54:25 -04:00
|
|
|
|
2018-10-31 13:40:05 -04:00
|
|
|
t.equal(rt.canAddCloudVariable(), true);
|
2018-10-31 19:20:13 -04:00
|
|
|
rt.addCloudVariable();
|
2018-10-31 13:40:05 -04:00
|
|
|
t.equal(rt.canAddCloudVariable(), false);
|
2018-10-30 18:54:25 -04:00
|
|
|
|
|
|
|
// Disposing of the runtime should reset the cloud variable limitations
|
|
|
|
rt.dispose();
|
2018-10-31 13:40:05 -04:00
|
|
|
t.equal(rt.hasCloudData(), false);
|
2018-10-30 18:54:25 -04:00
|
|
|
|
|
|
|
for (let i = 0; i < 8; i++) {
|
2018-10-31 13:40:05 -04:00
|
|
|
t.equal(rt.canAddCloudVariable(), true);
|
|
|
|
rt.addCloudVariable();
|
|
|
|
t.equal(rt.hasCloudData(), true);
|
2018-10-30 18:54:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We should be at the cloud variable limit now
|
2018-10-31 13:40:05 -04:00
|
|
|
t.equal(rt.canAddCloudVariable(), false);
|
2018-10-30 18:54:25 -04:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
|
|
|
|
});
|
2018-11-27 11:37:01 -05:00
|
|
|
|
|
|
|
test('Starting the runtime emits an event', t => {
|
|
|
|
let started = false;
|
|
|
|
const rt = new Runtime();
|
|
|
|
rt.addListener('RUNTIME_STARTED', () => {
|
|
|
|
started = true;
|
|
|
|
});
|
|
|
|
rt.start();
|
|
|
|
t.equal(started, true);
|
|
|
|
t.end();
|
|
|
|
});
|