diff --git a/nodes/errors.js b/nodes/errors.js index bd881dbf..edd90d11 100644 --- a/nodes/errors.js +++ b/nodes/errors.js @@ -151,6 +151,7 @@ module.exports = { INVALID_REGISTER_NUMBER, QUBITS_FROM_DIFFERENT_CIRCUITS, SAME_QUBIT_RECEIVED_TWICE, + NOT_BIT_STRING, BLOCH_SPHERE_WITH_MEASUREMENT, GREATER_THAN_TWO, INPUT_ODD_INTEGER, diff --git a/nodes/quantum-algorithms/grovers/grovers.js b/nodes/quantum-algorithms/grovers/grovers.js index 4b378b32..7ca6da4c 100644 --- a/nodes/quantum-algorithms/grovers/grovers.js +++ b/nodes/quantum-algorithms/grovers/grovers.js @@ -10,6 +10,7 @@ module.exports = function(RED) { function GroversNode(config) { RED.nodes.createNode(this, config); this.name = config.name || 'Grovers'; + const node = this; this.on('input', async function(msg, send, done) { let error = errors.validateGroversInput(msg); diff --git a/package.json b/package.json index 596dccb3..af26af6b 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "setup": "(npm run link && npm run venv) && echo Setup complete - run 'npm start' to open Node-RED", "link": "bash bin/link.sh || bin/link.sh", "venv": "bash bin/pyvenv.sh || bin/pyvenv.sh", - "test": "mocha \"test/**/*_spec.js\" --timeout 15000", + "test": "mocha \"test/**/*_spec.js\" --timeout 10000", "clean": "rm -r venv/ & npm uninstall --prefix ~/.node-red/ node-red-contrib-quantum", "lint": "eslint --fix --ignore-path .gitignore .", "postinstall": "npm run venv" diff --git a/test/nodes/grovers_spec.js b/test/nodes/grovers_spec.js index 138bdc66..e993047d 100644 --- a/test/nodes/grovers_spec.js +++ b/test/nodes/grovers_spec.js @@ -1,9 +1,10 @@ const groversNode = require('../../nodes/quantum-algorithms/grovers/grovers.js'); const testUtil = require('../test-util'); const nodeTestHelper = testUtil.nodeTestHelper; +const {FlowBuilder} = require('../flow-builder'); +const errors = require('../../nodes/errors'); const assert = require('chai').assert; - describe('GroversNode', function() { beforeEach(function(done) { nodeTestHelper.startServer(done); @@ -19,34 +20,38 @@ describe('GroversNode', function() { }); it('default name outputs correctly', function(done) { - let flow = [{id: 'groversNode', type: 'grovers', wires: []}]; - nodeTestHelper.load(groversNode, flow, function() { + flow = new FlowBuilder(); + flow.add('grovers', 'groversNode', []); + + nodeTestHelper.load(flow.nodes, flow.flow, function() { let groversTestNode = nodeTestHelper.getNode('groversNode'); - groversTestNode.should.have.property('name', 'Grovers'); + groversTestNode.should.have.property('name', 'grovers'); done(); }); }); it('return success output on valid input', function(done) { - let flow = [{id: 'groversNode', type: 'grovers', wires: [['helperNode']]}, - {id: 'helperNode', type: 'helper'}]; + flow = new FlowBuilder(); + flow.add('grovers', 'n1', [['n2']]); + flow.addOutput('n2'); + + const givenInput = {payload: '111111'}; + const expectedOutput = {topMeasurement: '111111', iterationsNum: 6}; + testUtil.correctOutputReceived(flow, givenInput, expectedOutput, done); + }); + + it('should fail on invalid input', function(done) { + flow = new FlowBuilder(); + flow.add('grovers', 'groversNode', []); - nodeTestHelper.load(groversNode, flow, function() { + nodeTestHelper.load(flow.nodes, flow.flow, function() { let groversTestNode = nodeTestHelper.getNode('groversNode'); - let helperNode = nodeTestHelper.getNode('helperNode'); - - helperNode.on('input', function(msg) { - const expectedPayload = {topMeasurement: '111111', iterationsNum: 6}; - try { - assert.strictEqual(msg.payload.topMeasurement, expectedPayload.topMeasurement); - assert.strictEqual(msg.payload.iterationsNum, expectedPayload.iterationsNum); - done(); - } catch (err) { - done(err); - } + groversTestNode.on('call:error', (call)=> { + const actualError = call.firstArg; + assert.strictEqual(actualError.message, errors.NOT_BIT_STRING); + done(); }); - - groversTestNode.receive({payload: '111111'}); + groversTestNode.receive({payload: '111112'}); }); }); }); diff --git a/test/nodes/measure_spec.js b/test/nodes/measure_spec.js index 4f64d8db..e9918ea3 100644 --- a/test/nodes/measure_spec.js +++ b/test/nodes/measure_spec.js @@ -30,4 +30,20 @@ describe('MeasureNode', function() { testUtil.commandExecuted(flow, command, done); }); + + it('should return correct output', function(done) { + let flow = new FlowBuilder(); + + flow.add('quantum-circuit', 'n0', [['n1']], + {structure: 'qubits', outputs: '1', qbitsreg: '1', cbitsreg: '1'}); + flow.add('not-gate', 'n1', [['n2']]); + flow.add('measure', 'n2', [['n3']], {selectedBit: '0'}); + flow.add('local-simulator', 'n3', [['n4']], {shots: 1}); + flow.addOutput('n4'); + + const givenInput = 'dummy input'; + const expectedOutput = {1: 1}; + + testUtil.correctOutputReceived(flow, givenInput, expectedOutput, done); + }); }); diff --git a/test/nodes/shors_spec.js b/test/nodes/shors_spec.js index da4e841a..9e0a2e74 100644 --- a/test/nodes/shors_spec.js +++ b/test/nodes/shors_spec.js @@ -15,11 +15,11 @@ describe('ShorsNode', function() { nodeTestHelper.stopServer(done); }); - xit('load node', function(done) { + it('load node', function(done) { testUtil.isLoaded(shorsNode, 'shors', done); }); - xit('default name outputs correctly', function(done) { + it('default name outputs correctly', function(done) { flow = new FlowBuilder(); flow.add('shors', 'shorsNode', [[]]); @@ -29,6 +29,7 @@ describe('ShorsNode', function() { done(); }); }); + it('return success output on valid input', function(done) { flow = new FlowBuilder(); flow.add('shors', 'shorsNode', [['helperNode']]); @@ -50,13 +51,13 @@ describe('ShorsNode', function() { inputNode.receive({payload: 15}); }); - }); + }).timeout(25000); - it('return error for input less than 3', function(done){ + it('return error for input less than 3', function(done) { flow = new FlowBuilder(); flow.add('shors', 'shorsNode', []); - nodeTestHelper.load(flow.nodes, flow.flow, function(){ + nodeTestHelper.load(flow.nodes, flow.flow, function() { let shorsTestNode = nodeTestHelper.getNode('shorsNode'); shorsTestNode.on('call:error', (call)=> { const actualError = call.firstArg; @@ -64,14 +65,14 @@ describe('ShorsNode', function() { done(); }); shorsTestNode.receive({payload: 1}); - }) + }); }); - it('return error for even input', function(done){ + it('return error for even input', function(done) { flow = new FlowBuilder(); flow.add('shors', 'shorsNode', []); - nodeTestHelper.load(flow.nodes, flow.flow, function(){ + nodeTestHelper.load(flow.nodes, flow.flow, function() { let shorsTestNode = nodeTestHelper.getNode('shorsNode'); shorsTestNode.on('call:error', (call)=> { const actualError = call.firstArg; @@ -79,14 +80,14 @@ describe('ShorsNode', function() { done(); }); shorsTestNode.receive({payload: 4}); - }) + }); }); - it('return error for non-integer input', function(done){ + it('return error for non-integer input', function(done) { flow = new FlowBuilder(); flow.add('shors', 'shorsNode', []); - nodeTestHelper.load(flow.nodes, flow.flow, function(){ + nodeTestHelper.load(flow.nodes, flow.flow, function() { let shorsTestNode = nodeTestHelper.getNode('shorsNode'); shorsTestNode.on('call:error', (call)=> { const actualError = call.firstArg; @@ -94,6 +95,6 @@ describe('ShorsNode', function() { done(); }); shorsTestNode.receive({payload: 'a'}); - }) + }); }); }); diff --git a/test/test-util.js b/test/test-util.js index 164ad21e..e2872a2d 100644 --- a/test/test-util.js +++ b/test/test-util.js @@ -42,8 +42,27 @@ function commandExecuted(flowBuilder, command, done) { }); } +function correctOutputReceived(flow, givenInput, expectedOutput, done) { + nodeTestHelper.load(flow.nodes, flow.flow, function() { + const inputNode = nodeTestHelper.getNode(flow.inputId); + const outputNode = nodeTestHelper.getNode(flow.outputId); + outputNode.on('input', function(msg) { + try { + assert.deepEqual(msg.payload, expectedOutput); + done(); + } catch (err) { + done(err); + } finally { + shell.stop(); + } + }); + inputNode.receive(givenInput); + }); +} + module.exports = { nodeTestHelper, isLoaded, commandExecuted, + correctOutputReceived, };