diff --git a/test/unit/engine_target.js b/test/unit/engine_target.js index 986038774..0b628c1b3 100644 --- a/test/unit/engine_target.js +++ b/test/unit/engine_target.js @@ -4,6 +4,7 @@ const Variable = require('../../src/engine/variable'); const adapter = require('../../src/engine/adapter'); const Runtime = require('../../src/engine/runtime'); const events = require('../fixtures/events.json'); +const Cloud = require('../../src/io/cloud'); test('spec', t => { const target = new Target(); @@ -71,6 +72,58 @@ test('createListVariable creates a list', t => { t.end(); }); +test('createVariable calls cloud io device\'s requestCreateCloudVariable', t => { + const runtime = new Runtime(); + // Mock the requestCreateCloudVariable function + let requestCreateCloudWasCalled = false; + runtime.ioDevices.cloud.requestCreateCloudVariable = () => { + requestCreateCloudWasCalled = true; + }; + + const target = new Target(runtime); + target.isStage = true; + target.createVariable('foo', 'bar', Variable.SCALAR_TYPE, true /* isCloud */); + + const variables = target.variables; + t.equal(Object.keys(variables).length, 1); + const variable = variables[Object.keys(variables)[0]]; + t.equal(variable.id, 'foo'); + t.equal(variable.name, 'bar'); + t.equal(variable.type, Variable.SCALAR_TYPE); + t.equal(variable.value, 0); + // isCloud flag doesn't get set by the target createVariable function + t.equal(variable.isCloud, false); + t.equal(requestCreateCloudWasCalled, true); + + t.end(); +}); + +test('createVariable does not call cloud io device\'s requestCreateCloudVariable if target is not stage', t => { + const runtime = new Runtime(); + // Mock the requestCreateCloudVariable function + let requestCreateCloudWasCalled = false; + runtime.ioDevices.cloud.requestCreateCloudVariable = () => { + requestCreateCloudWasCalled = true; + }; + + const target = new Target(runtime); + target.isStage = false; + target.createVariable('foo', 'bar', Variable.SCALAR_TYPE, true /* isCloud */); + + const variables = target.variables; + t.equal(Object.keys(variables).length, 1); + const variable = variables[Object.keys(variables)[0]]; + t.equal(variable.id, 'foo'); + t.equal(variable.name, 'bar'); + t.equal(variable.type, Variable.SCALAR_TYPE); + t.equal(variable.value, 0); + // isCloud flag doesn't get set by the target createVariable function + t.equal(variable.isCloud, false); + t.equal(requestCreateCloudWasCalled, false); + + t.end(); +}); + test('createVariable throws when given invalid type', t => { const target = new Target(); t.throws( diff --git a/test/unit/io_cloud.js b/test/unit/io_cloud.js index d7f2c7843..0991a4f3d 100644 --- a/test/unit/io_cloud.js +++ b/test/unit/io_cloud.js @@ -2,14 +2,18 @@ const test = require('tap').test; const Cloud = require('../../src/io/cloud'); const Target = require('../../src/engine/target'); const Variable = require('../../src/engine/variable'); +const Runtime = require('../../src/engine/runtime'); test('spec', t => { - const cloud = new Cloud(); + const runtime = new Runtime(); + const cloud = new Cloud(runtime); t.type(cloud, 'object'); t.type(cloud.postData, 'function'); t.type(cloud.requestUpdateVariable, 'function'); t.type(cloud.updateCloudVariable, 'function'); + t.type(cloud.requestCreateCloudVariable, 'function'); + t.type(cloud.createCloudVariable, 'function'); t.type(cloud.setProvider, 'function'); t.type(cloud.setStage, 'function'); t.type(cloud.clear, 'function'); @@ -17,7 +21,8 @@ test('spec', t => { }); test('stage and provider are null initially', t => { - const cloud = new Cloud(); + const runtime = new Runtime(); + const cloud = new Cloud(runtime); t.strictEquals(cloud.provider, null); t.strictEquals(cloud.stage, null); @@ -25,7 +30,8 @@ test('stage and provider are null initially', t => { }); test('setProvider sets the provider', t => { - const cloud = new Cloud(); + const runtime = new Runtime(); + const cloud = new Cloud(runtime); const provider = { foo: 'a fake provider' @@ -37,7 +43,7 @@ test('setProvider sets the provider', t => { t.end(); }); -test('postData updates the variable', t => { +test('postData update message updates the variable', t => { const stage = new Target(); const fooVar = new Variable( 'a fake var id', @@ -49,7 +55,8 @@ test('postData updates the variable', t => { t.strictEquals(fooVar.value, 0); - const cloud = new Cloud(); + const runtime = new Runtime(); + const cloud = new Cloud(runtime); cloud.setStage(stage); cloud.postData({varUpdate: { name: 'foo', @@ -59,6 +66,30 @@ test('postData updates the variable', t => { t.end(); }); +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(); +}); + test('requestUpdateVariable calls provider\'s updateVariable function', t => { let updateVariableCalled = false; let mockVarName = ''; @@ -74,7 +105,8 @@ test('requestUpdateVariable calls provider\'s updateVariable function', t => { updateVariable: mockUpdateVariable }; - const cloud = new Cloud(); + const runtime = new Runtime(); + const cloud = new Cloud(runtime); cloud.setProvider(provider); cloud.requestUpdateVariable('foo', 3); t.equals(updateVariableCalled, true); @@ -82,3 +114,31 @@ test('requestUpdateVariable calls provider\'s updateVariable function', t => { t.strictEquals(mockVarValue, 3); t.end(); }); + +test('requestCreateCloudVariable calls provider\'s createVariable function', t => { + 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); + cloud.requestCreateCloudVariable(mockVariable); + t.equals(createVariableCalled, true); + t.strictEquals(mockVarName, 'my var'); + t.strictEquals(mockVarValue, 0); + // Calling requestCreateCloudVariable does not set isCloud flag on variable + t.strictEquals(mockVariable.isCloud, false); + t.end(); +});