scratch-vm/test/integration/offline-custom-assets.js
Christopher Willis-Ford 840ffb5df0 test: don't use process.exit to end tests
Newer versions of `tap` run more asynchronously, so sometimes using `process.nextTick(process.exit)`
to end a test would prevent the test from completing correctly. Removing all instances of
`process.nextTick(process.exit)` put tests into three categories:
* the test still worked correctly -- no fixup needed.
* the test would hang because the VM's `_steppingInterval` was keeping
  Node alive. These tests call a new `quit()` method which ends the
  stepping interval.
* the `load-extensions` test needed special attention because the "Video
  Sensing" extension starts its own loop using `setTimeout`. I added a
  `_stopLoop()` method on the extension and directly call that from the
  test. I'm not completely happy with this solution but anything more
  general would likely require a change to the extension spec, so I'm
  leaving that as a followup task.
2022-06-07 11:44:06 -07:00

77 lines
2.7 KiB
JavaScript

/**
* @fileoverview
* This integration test ensures that a local upload of a sb2 project pulls
* in assets correctly (from the provided .sb2 file) even if the assets
* are not present on our servers.
*/
const path = require('path');
const fs = require('fs');
const test = require('tap').test;
const AdmZip = require('adm-zip');
const ScratchStorage = require('scratch-storage');
const VirtualMachine = require('../../src/index');
const projectUri = path.resolve(__dirname, '../fixtures/offline-custom-assets.sb2');
const projectZip = AdmZip(projectUri);
const project = Buffer.from(fs.readFileSync(projectUri));
// Custom costume from sb2 file (which was downloaded from offline editor)
// This sound should not be available on our servers
const costume = projectZip.readFile('1.svg');
const costumeData = new Uint8Array(costume);
// Custom sound recording from sb2 file (which was downloaded from offline editor)
// This sound should not be available on our servers
const sound = projectZip.readFile('0.wav');
const soundData = new Uint8Array(sound);
test('offline-custom-assets', t => {
const vm = new VirtualMachine();
// Use a test storage here that does not have any web sources added to it.
const testStorage = new ScratchStorage();
vm.attachStorage(testStorage);
// Evaluate playground data and exit
vm.on('playgroundData', e => {
const threads = JSON.parse(e.threads);
t.ok(threads.length === 0);
vm.quit();
t.end();
});
// Start VM, load project, and run
t.doesNotThrow(() => {
vm.start();
vm.clear();
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(project).then(() => {
// Verify initial state
t.equals(vm.runtime.targets.length, 2);
const costumes = vm.runtime.targets[1].getCostumes();
t.equals(costumes.length, 1);
const customCostume = costumes[0];
t.equals(customCostume.name, 'A_Test_Costume');
const storedCostume = customCostume.asset;
t.type(storedCostume, 'object');
t.deepEquals(storedCostume.data, costumeData);
const sounds = vm.runtime.targets[1].sprite.sounds;
t.equals(sounds.length, 1);
const customSound = sounds[0];
t.equals(customSound.name, 'A_Test_Recording');
const storedSound = customSound.asset;
t.type(storedSound, 'object');
t.deepEquals(storedSound.data, soundData);
vm.greenFlag();
// After two seconds, get playground data and stop
setTimeout(() => {
vm.getPlaygroundData();
vm.stopAll();
}, 2000);
});
});
});