File size: 5,437 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
const path = require('path');
const test = require('tap').test;
const makeTestStorage = require('../fixtures/make-test-storage');
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
const VirtualMachine = require('../../src/index');

const cloudVarSimpleUri = path.resolve(__dirname, '../fixtures/cloud_variables_simple.sb2');
const cloudVarLimitUri = path.resolve(__dirname, '../fixtures/cloud_variables_limit.sb2');
const cloudVarExceededLimitUri = path.resolve(__dirname, '../fixtures/cloud_variables_exceeded_limit.sb2');
const cloudVarLocalUri = path.resolve(__dirname, '../fixtures/cloud_variables_local.sb2');

const cloudVarSimple = readFileToBuffer(cloudVarSimpleUri);
const cloudVarLimit = readFileToBuffer(cloudVarLimitUri);
const cloudVarExceededLimit = readFileToBuffer(cloudVarExceededLimitUri);
const cloudVarLocal = readFileToBuffer(cloudVarLocalUri);

test('importing an sb2 project with cloud variables', t => {
    const vm = new VirtualMachine();
    vm.attachStorage(makeTestStorage());

    // Start VM, load project, and run
    vm.start();
    vm.clear();
    vm.setCompatibilityMode(false);
    vm.setTurboMode(false);
    vm.loadProject(cloudVarSimple).then(() => {
        t.equal(vm.runtime.hasCloudData(), true);

        const stage = vm.runtime.targets[0];
        const stageVars = Object.values(stage.variables);
        t.equal(stageVars.length, 1);

        const variable = stageVars[0];
        t.equal(variable.name, '☁ firstCloud');
        t.equal(Number(variable.value), 100); // Though scratch 2 requires
        // cloud variables to be numbers, this is something that happens
        // when the message is being sent to the server rather than on the client
        t.equal(variable.isCloud, true);

        t.end();
    });
});

test('importing an sb2 project with cloud variables at the limit for a project', t => {
    const vm = new VirtualMachine();
    vm.attachStorage(makeTestStorage());

    // Start VM, load project, and run
    vm.start();
    vm.clear();
    vm.setCompatibilityMode(false);
    vm.setTurboMode(false);
    vm.loadProject(cloudVarLimit).then(() => {
        t.equal(vm.runtime.hasCloudData(), true);

        const stage = vm.runtime.targets[0];
        const stageVars = Object.values(stage.variables);

        t.equal(stageVars.length, 10);
        // All of the 8 stage variables should be cloud variables
        t.equal(stageVars.filter(v => v.isCloud).length, 10);

        t.end();
    });
});

test('importing an sb2 project with cloud variables exceeding the limit for a project', t => {
    // This tests a hacked project where additional cloud variables exceeding
    // the project limit have been added.
    const vm = new VirtualMachine();
    vm.attachStorage(makeTestStorage());

    // Start VM, load project, and run
    vm.start();
    vm.clear();
    vm.setCompatibilityMode(false);
    vm.setTurboMode(false);
    vm.loadProject(cloudVarExceededLimit).then(() => {
        t.equal(vm.runtime.hasCloudData(), true);

        const stage = vm.runtime.targets[0];
        const stageVars = Object.values(stage.variables);

        t.equal(stageVars.length, 15);
        // Only 8 of the variables should have the isCloud flag set to true
        t.equal(stageVars.filter(v => v.isCloud).length, 10);

        t.end();
    });
});

test('importing one project after the other resets cloud variable limit', t => {
    const vm = new VirtualMachine();
    vm.attachStorage(makeTestStorage());

    // Start VM, load project, and run
    vm.start();
    vm.clear();
    vm.setCompatibilityMode(false);
    vm.setTurboMode(false);
    vm.loadProject(cloudVarExceededLimit).then(() => {
        t.equal(vm.runtime.canAddCloudVariable(), false);

        vm.loadProject(cloudVarSimple).then(() => {
            const stage = vm.runtime.targets[0];
            const stageVars = Object.values(stage.variables);
            t.equal(stageVars.length, 1);

            const variable = stageVars[0];
            t.equal(variable.name, '☁ firstCloud');
            t.equal(Number(variable.value), 100); // Though scratch 2 requires
            // cloud variables to be numbers, this is something that happens
            // when the message is being sent to the server rather than on the client
            t.equal(variable.isCloud, true);

            t.equal(vm.runtime.canAddCloudVariable(), true);

            t.end();
        });
    });
});

test('local cloud variables get imported as regular variables', t => {
    // This tests a hacked project where a sprite-local variable is
    // has the cloud variable flag set.
    const vm = new VirtualMachine();
    vm.attachStorage(makeTestStorage());

    // Start VM, load project, and run
    vm.start();
    vm.clear();
    vm.setCompatibilityMode(false);
    vm.setTurboMode(false);
    vm.loadProject(cloudVarLocal).then(() => {
        t.equal(vm.runtime.hasCloudData(), false);

        const stage = vm.runtime.targets[0];
        const stageVars = Object.values(stage.variables);

        t.equal(stageVars.length, 0);

        const sprite = vm.runtime.targets[1];
        const spriteVars = Object.values(sprite.variables);

        t.equal(spriteVars.length, 1);
        t.equal(spriteVars[0].isCloud, false);

        t.end();

        process.nextTick(process.exit); // This is needed because this is the end of the last test in this file!!!
    });
});