Spaces:
Running
Running
File size: 7,416 Bytes
30c32c8 |
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 |
const tap = require('tap');
const path = require('path');
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
const VirtualMachine = require('../../src/virtual-machine');
const Runtime = require('../../src/engine/runtime');
const MonitorRecord = require('../../src/engine/monitor-record');
const {Map} = require('immutable');
tap.tearDown(() => process.nextTick(process.exit));
const test = tap.test;
test('spec', t => {
const r = new Runtime();
t.type(Runtime, 'function');
t.type(r, 'object');
// 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');
t.ok(r instanceof Runtime);
t.end();
});
test('monitorStateEquals', t => {
const r = new Runtime();
const id = 'xklj4#!';
const prevMonitorState = MonitorRecord({
id,
opcode: 'turtle whereabouts',
value: '25'
});
const newMonitorDelta = Map({
id,
value: String(25)
});
r.requestAddMonitor(prevMonitorState);
r.requestUpdateMonitor(newMonitorDelta);
t.equals(true, prevMonitorState === r._monitorState.get(id));
t.equals(String(25), r._monitorState.get(id).get('value'));
t.end();
});
test('monitorStateDoesNotEqual', t => {
const r = new Runtime();
const id = 'xklj4#!';
const params = {seven: 7};
const prevMonitorState = MonitorRecord({
id,
opcode: 'turtle whereabouts',
value: '25'
});
// Value change
let newMonitorDelta = Map({
id,
value: String(24)
});
r.requestAddMonitor(prevMonitorState);
r.requestUpdateMonitor(newMonitorDelta);
t.equals(false, prevMonitorState.equals(r._monitorState.get(id)));
t.equals(String(24), r._monitorState.get(id).get('value'));
// Prop change
newMonitorDelta = Map({
id: 'xklj4#!',
params: params
});
r.requestUpdateMonitor(newMonitorDelta);
t.equals(false, prevMonitorState.equals(r._monitorState.get(id)));
t.equals(String(24), r._monitorState.get(id).value);
t.equals(params, r._monitorState.get(id).params);
t.end();
});
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();
});
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();
});
});
test('Cloud variable limit allows only 10 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();
t.equal(rt.hasCloudData(), false);
for (let i = 0; i < 10; i++) {
t.equal(rt.canAddCloudVariable(), true);
rt.addCloudVariable();
// Adding a cloud variable should change the
// result of the hasCloudData check
t.equal(rt.hasCloudData(), true);
}
// We should be at the cloud variable limit now
t.equal(rt.canAddCloudVariable(), false);
// Removing a cloud variable should allow the addition of exactly one more
// when we are at the cloud variable limit
rt.removeCloudVariable();
t.equal(rt.canAddCloudVariable(), true);
rt.addCloudVariable();
t.equal(rt.canAddCloudVariable(), false);
// Disposing of the runtime should reset the cloud variable limitations
rt.dispose();
t.equal(rt.hasCloudData(), false);
for (let i = 0; i < 10; i++) {
t.equal(rt.canAddCloudVariable(), true);
rt.addCloudVariable();
t.equal(rt.hasCloudData(), true);
}
// We should be at the cloud variable limit now
t.equal(rt.canAddCloudVariable(), false);
t.end();
});
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();
});
test('Runtime cannot be started while already running', t => {
const rt = new Runtime();
rt.start(); // Start the first time
// Set up a flag/listener to check if it can be started again
let started = false;
rt.addListener('RUNTIME_STARTED', () => {
started = true;
});
// Starting again should not emit another event
rt.start();
t.equal(started, false);
t.end();
});
test('setCompatibilityMode restarts if it was already running', t => {
const rt = new Runtime();
rt.start(); // Start the first time
// Set up a flag/listener to check if it gets started again
let started = false;
rt.addListener('RUNTIME_STARTED', () => {
started = true;
});
rt.setCompatibilityMode(true);
// TW: We make an intentional API change here. Changing compatibility mode won't emit a RUNTIME_STARTED
// if the runtime is already running.
t.equal(started, false);
t.end();
});
test('setCompatibilityMode does not restart if it was not running', t => {
const rt = new Runtime();
let started = false;
rt.addListener('RUNTIME_STARTED', () => {
started = true;
});
rt.setCompatibilityMode(true);
t.equal(started, false);
t.end();
});
test('Disposing the runtime emits an event', t => {
let disposed = false;
const rt = new Runtime();
rt.addListener('RUNTIME_DISPOSED', () => {
disposed = true;
});
rt.dispose();
t.equal(disposed, true);
t.end();
});
test('Clock is reset on runtime dispose', t => {
const rt = new Runtime();
const c = rt.ioDevices.clock;
let simulatedTime = 0;
c._projectTimer = {
timeElapsed: () => simulatedTime,
start: () => {
simulatedTime = 0;
}
};
t.ok(c.projectTimer() === 0);
simulatedTime += 1000;
t.ok(c.projectTimer() === 1);
rt.dispose();
// When the runtime is disposed, the clock should be reset
t.ok(c.projectTimer() === 0);
t.end();
});
|