File size: 3,014 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
/**
 * This test ensures that the VM gracefully handles an sb3 project with
 * a missing sound. The project should load without error.
 * TODO: handle missing or corrupted sounds by replacing the missing sound data
 * with the empty sound file but keeping the info about the original missing / corrupted sound
 * so that user data does not get overwritten / lost.
 */
const path = require('path');
const tap = require('tap');
const makeTestStorage = require('../fixtures/make-test-storage');
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
const VirtualMachine = require('../../src/index');
const {serializeSounds} = require('../../src/serialization/serialize-assets');

const projectUri = path.resolve(__dirname, '../fixtures/missing_sound.sb3');
const project = readFileToBuffer(projectUri);

const missingSoundAssetId = '78618aadd225b1db7bf837fa17dc0568';

let vm;

tap.beforeEach(() => {
    const storage = makeTestStorage();

    vm = new VirtualMachine();
    vm.attachStorage(storage);

    return vm.loadProject(project);
});

const test = tap.test;

test('loading sb3 project with missing sound file', t => {
    t.equal(vm.runtime.targets.length, 2);
    
    const stage = vm.runtime.targets[0];
    t.ok(stage.isStage);

    const catSprite = vm.runtime.targets[1];
    t.equal(catSprite.getSounds().length, 1);
    
    const missingSound = catSprite.getSounds()[0];
    t.equal(missingSound.name, 'Boop Sound Recording');
    // Sound should have original data but no asset
    const defaultSoundAssetId = vm.runtime.storage.defaultAssetId.Sound;
    t.equal(missingSound.assetId, defaultSoundAssetId);
    t.equal(missingSound.dataFormat, 'wav');

    // Runtime should have info about broken asset
    t.ok(missingSound.broken);
    t.equal(missingSound.broken.assetId, missingSoundAssetId);

    t.end();
});

test('load and then save sb3 project with missing sound file', t => {
    const resavedProject = JSON.parse(vm.toJSON());

    t.equal(resavedProject.targets.length, 2);
    
    const stage = resavedProject.targets[0];
    t.ok(stage.isStage);

    const catSprite = resavedProject.targets[1];
    t.equal(catSprite.name, 'Sprite1');
    t.equal(catSprite.sounds.length, 1);
    
    const missingSound = catSprite.sounds[0];
    t.equal(missingSound.name, 'Boop Sound Recording');
    // Costume should have both default sound data (e.g. "Gray Question Sound" ^_^) and original data
    t.equal(missingSound.assetId, missingSoundAssetId);
    t.equal(missingSound.dataFormat, 'wav');
    // Test that we didn't save any data about the costume being broken
    t.notOk(missingSound.broken);

    t.end();
});

test('serializeCostume does not save data for missing costume', t => {
    const soundDescs = serializeSounds(vm.runtime);
    
    t.equal(soundDescs.length, 1); // Should only have one sound, the pop sound for the stage
    t.not(soundDescs[0].fileName, `${missingSoundAssetId}.wav`);

    t.end();
    process.nextTick(process.exit);
});