2018-10-29 00:59:24 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const Cloud = require('../../src/io/cloud');
|
|
|
|
const Target = require('../../src/engine/target');
|
|
|
|
const Variable = require('../../src/engine/variable');
|
2018-11-14 15:46:51 -05:00
|
|
|
const Runtime = require('../../src/engine/runtime');
|
2018-10-29 00:59:24 -04:00
|
|
|
|
|
|
|
test('spec', t => {
|
2018-11-14 15:46:51 -05:00
|
|
|
const runtime = new Runtime();
|
|
|
|
const cloud = new Cloud(runtime);
|
2018-10-29 00:59:24 -04:00
|
|
|
|
|
|
|
t.type(cloud, 'object');
|
|
|
|
t.type(cloud.postData, 'function');
|
|
|
|
t.type(cloud.requestUpdateVariable, 'function');
|
|
|
|
t.type(cloud.updateCloudVariable, 'function');
|
2018-11-20 15:11:12 -05:00
|
|
|
t.type(cloud.requestCreateVariable, 'function');
|
2018-11-14 15:46:51 -05:00
|
|
|
t.type(cloud.createCloudVariable, 'function');
|
2018-10-29 00:59:24 -04:00
|
|
|
t.type(cloud.setProvider, 'function');
|
|
|
|
t.type(cloud.setStage, 'function');
|
|
|
|
t.type(cloud.clear, 'function');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('stage and provider are null initially', t => {
|
2018-11-14 15:46:51 -05:00
|
|
|
const runtime = new Runtime();
|
|
|
|
const cloud = new Cloud(runtime);
|
2018-10-29 00:59:24 -04:00
|
|
|
|
|
|
|
t.strictEquals(cloud.provider, null);
|
|
|
|
t.strictEquals(cloud.stage, null);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('setProvider sets the provider', t => {
|
2018-11-14 15:46:51 -05:00
|
|
|
const runtime = new Runtime();
|
|
|
|
const cloud = new Cloud(runtime);
|
2018-10-29 00:59:24 -04:00
|
|
|
|
|
|
|
const provider = {
|
|
|
|
foo: 'a fake provider'
|
|
|
|
};
|
|
|
|
|
|
|
|
cloud.setProvider(provider);
|
|
|
|
t.strictEquals(cloud.provider, provider);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-11-14 15:46:51 -05:00
|
|
|
test('postData update message updates the variable', t => {
|
2018-10-29 00:59:24 -04:00
|
|
|
const stage = new Target();
|
|
|
|
const fooVar = new Variable(
|
|
|
|
'a fake var id',
|
|
|
|
'foo',
|
|
|
|
Variable.SCALAR_TYPE,
|
|
|
|
true /* isCloud */
|
|
|
|
);
|
|
|
|
stage.variables[fooVar.id] = fooVar;
|
|
|
|
|
|
|
|
t.strictEquals(fooVar.value, 0);
|
|
|
|
|
2018-11-14 15:46:51 -05:00
|
|
|
const runtime = new Runtime();
|
|
|
|
const cloud = new Cloud(runtime);
|
2018-10-29 00:59:24 -04:00
|
|
|
cloud.setStage(stage);
|
|
|
|
cloud.postData({varUpdate: {
|
|
|
|
name: 'foo',
|
|
|
|
value: 3
|
|
|
|
}});
|
|
|
|
t.strictEquals(fooVar.value, 3);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-11-14 15:46:51 -05:00
|
|
|
test('postData create message sets isCloud flag on the variable and updates runtime cloud limit', t => {
|
|
|
|
const stage = new Target();
|
|
|
|
const fooVar = new Variable(
|
|
|
|
'a fake var id',
|
|
|
|
'foo',
|
|
|
|
Variable.SCALAR_TYPE,
|
|
|
|
false /* isCloud */
|
|
|
|
);
|
|
|
|
stage.variables[fooVar.id] = fooVar;
|
|
|
|
|
|
|
|
t.strictEquals(fooVar.value, 0);
|
|
|
|
t.strictEquals(fooVar.isCloud, false);
|
|
|
|
|
|
|
|
const runtime = new Runtime();
|
|
|
|
const cloud = new Cloud(runtime);
|
|
|
|
cloud.setStage(stage);
|
|
|
|
cloud.postData({varCreate: {
|
|
|
|
name: 'foo'
|
|
|
|
}});
|
|
|
|
t.strictEquals(fooVar.isCloud, true);
|
|
|
|
t.strictEquals(runtime.hasCloudData(), true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-10-29 00:59:24 -04:00
|
|
|
test('requestUpdateVariable calls provider\'s updateVariable function', t => {
|
|
|
|
let updateVariableCalled = false;
|
|
|
|
let mockVarName = '';
|
|
|
|
let mockVarValue = '';
|
|
|
|
const mockUpdateVariable = (name, value) => {
|
|
|
|
updateVariableCalled = true;
|
|
|
|
mockVarName = name;
|
|
|
|
mockVarValue = value;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
const provider = {
|
|
|
|
updateVariable: mockUpdateVariable
|
|
|
|
};
|
|
|
|
|
2018-11-14 15:46:51 -05:00
|
|
|
const runtime = new Runtime();
|
|
|
|
const cloud = new Cloud(runtime);
|
2018-10-29 00:59:24 -04:00
|
|
|
cloud.setProvider(provider);
|
|
|
|
cloud.requestUpdateVariable('foo', 3);
|
|
|
|
t.equals(updateVariableCalled, true);
|
|
|
|
t.strictEquals(mockVarName, 'foo');
|
|
|
|
t.strictEquals(mockVarValue, 3);
|
|
|
|
t.end();
|
|
|
|
});
|
2018-11-14 15:46:51 -05:00
|
|
|
|
2018-11-20 15:11:12 -05:00
|
|
|
test('requestCreateVariable calls provider\'s createVariable function', t => {
|
2018-11-14 15:46:51 -05:00
|
|
|
let createVariableCalled = false;
|
|
|
|
const mockVariable = new Variable('a var id', 'my var', Variable.SCALAR_TYPE, false);
|
|
|
|
let mockVarName;
|
|
|
|
let mockVarValue;
|
|
|
|
const mockCreateVariable = (name, value) => {
|
|
|
|
createVariableCalled = true;
|
|
|
|
mockVarName = name;
|
|
|
|
mockVarValue = value;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
const provider = {
|
|
|
|
createVariable: mockCreateVariable
|
|
|
|
};
|
|
|
|
|
|
|
|
const runtime = new Runtime();
|
|
|
|
const cloud = new Cloud(runtime);
|
|
|
|
cloud.setProvider(provider);
|
2018-11-20 15:11:12 -05:00
|
|
|
cloud.requestCreateVariable(mockVariable);
|
2018-11-14 15:46:51 -05:00
|
|
|
t.equals(createVariableCalled, true);
|
|
|
|
t.strictEquals(mockVarName, 'my var');
|
|
|
|
t.strictEquals(mockVarValue, 0);
|
2018-11-20 15:11:12 -05:00
|
|
|
// Calling requestCreateVariable does not set isCloud flag on variable
|
2018-11-14 15:46:51 -05:00
|
|
|
t.strictEquals(mockVariable.isCloud, false);
|
|
|
|
t.end();
|
|
|
|
});
|