Files
scratch-vm/test/unit/virtual-machine_tw.js
T
Thomas Weber 9f2106a290 Remove variable value safe stringification
I thought this may have been part of fixing a sb2 -> sb3 corruption issue.
But from commit log, it seems this was added to prevent extensions from making mistakes.
With the scratch-parser change, those mistakes are now considered features.
Seems safe to just do remove these checks all the time.
Closes https://github.com/TurboWarp/scratch-vm/pull/332
2026-03-02 22:43:29 -06:00

94 lines
3.1 KiB
JavaScript

const test = require('tap').test;
const VirtualMachine = require('../../src/virtual-machine');
const RenderedTarget = require('../../src/sprites/rendered-target');
const Sprite = require('../../src/sprites/sprite');
const Variable = require('../../src/engine/variable');
test('emitTargetsUpdate targetList is lazy', t => {
const vm = new VirtualMachine();
let calledToJSON = false;
vm.runtime.targets = [{
toJSON () {
calledToJSON = true;
return {};
}
}];
let targetsUpdateEvent;
vm.on('targetsUpdate', e => {
targetsUpdateEvent = e;
});
vm.emitTargetsUpdate();
t.equal(calledToJSON, false);
void targetsUpdateEvent.targetList; // should trigger lazy compute
t.equal(calledToJSON, true);
t.end();
});
test('values in lists and variables are serialized as-is', t => {
const vm = new VirtualMachine();
const sprite = new Sprite();
const target = new RenderedTarget(sprite, vm.runtime);
target.variables.var1 = new Variable('var', 'test var', Variable.SCALAR_TYPE, false);
target.variables.var1.value = null;
target.variables.var2 = new Variable('var2', 'test var', Variable.SCALAR_TYPE, false);
target.variables.var2.value = undefined;
target.variables.var3 = new Variable('var3', 'test var', Variable.SCALAR_TYPE, false);
target.variables.var3.value = {};
target.variables.var4 = new Variable('var4', 'test var', Variable.SCALAR_TYPE, false);
target.variables.var4.value = 1;
target.variables.var5 = new Variable('var5', 'test var', Variable.SCALAR_TYPE, false);
target.variables.var5.value = 'abc';
target.variables.var6 = new Variable('var6', 'test var', Variable.SCALAR_TYPE, false);
target.variables.var6.value = false;
target.variables.list = new Variable('list', 'test list', Variable.LIST_TYPE, false);
target.variables.list.value = ['abc', false, 1, null, undefined, {}];
vm.runtime.addTarget(target);
const json = JSON.parse(vm.toJSON());
t.deepEqual(json.targets[0].variables.var1[1], null);
t.deepEqual(json.targets[0].variables.var2[1], null); // undefined becomes null due to JSON limitations
t.deepEqual(json.targets[0].variables.var3[1], {});
t.deepEqual(json.targets[0].variables.var4[1], 1);
t.deepEqual(json.targets[0].variables.var5[1], 'abc');
t.deepEqual(json.targets[0].variables.var6[1], false);
t.deepEqual(json.targets[0].lists.list[1], ['abc', false, 1, null, null, {}]);
t.end();
});
test('addSound error handling when sprite does not exist', async t => {
t.plan(1);
const vm = new VirtualMachine();
const id = 'Inva1id5pri731D$!';
try {
await vm.addSound({
thisObjectDoesNotMatter: true
}, id);
} catch (e) {
if (e && e.message === `No target with ID: ${id}`) {
t.pass();
}
}
t.end();
});
test('convertToPackagedRuntime forwards to runtime', t => {
t.plan(1);
const vm = new VirtualMachine();
vm.runtime.convertToPackagedRuntime = () => {
t.pass();
};
vm.convertToPackagedRuntime();
t.end();
});