
import EventEmitter from 'events';
import { expect } from 'chai';
import { Node, Project } from 'xod-espruino/runtime';

describe('Runtime', () => {
  describe('Project node graph', () => {
    let nodes;
    let topology;

    beforeEach(() => {
      nodes = {};
      topology = [];
    });

    function createNode(id, params) {
      const node = nodes[id] = new Node(Object.assign({ nodes }, params));
      topology.push[id];
      return node;
    }

    function createProject() {
      return new Project(nodes, topology);
    }

    it('should transmit signal from publisher to subsriber', () => {
      const publishStream = new EventEmitter();
      createNode(1, {
        setup: (fire) => publishStream.on('sample', fire),
        pure: false,
        outLinks: {
          val: [{
            nodeID: 2,
            inputName: 'val',
          }],
        },
      });

      let calls = [];
      createNode(2, {
        evaluate: inputs => calls.push(inputs),
        pure: false,
        inputTypes: { val: Number },
        nodes,
      });

      new Project({ nodes, topology: [1, 2] });

      publishStream.emit('sample', { val: 42 });
      expect(calls).to.be.eql([
        { val: 42 }
      ]);
    });

    it('should transmit multiple signals from publisher to subsriber', () => {
      const publishStream = new EventEmitter();
      createNode(1, {
        setup: (fire) => publishStream.on('sample', fire),
        pure: false,
        outLinks: {
          val: [{
            nodeID: 2,
            inputName: 'val',
          }],
        },
      });

      let calls = [];
      createNode(2, {
        evaluate: inputs => calls.push(inputs),
        pure: false,
        inputTypes: { val: Number },
        nodes,
      });

      new Project({ nodes, topology: [1, 2] });

      publishStream.emit('sample', { val: 42 });
      publishStream.emit('sample', { val: 43 });
      publishStream.emit('sample', { val: 44 });
      expect(calls).to.be.eql([
        { val: 42 },
        { val: 43 },
        { val: 44 },
      ]);
    });
  });
});

@@ -202,26 +202,8 @@
 des;
-%0A    let topology;
 %0A%0A  
@@ -245,29 +245,8 @@
 %7B%7D;%0A
-      topology = %5B%5D;%0A
     
@@ -297,21 +297,8 @@
     
- const node =
  nod
@@ -358,202 +358,57 @@
     
-  topology.push%5Bid%5D;%0A      return node;%0A    %7D%0A%0A    function createProject() %7B%0A      return new Project(nodes, topology);%0A    %7D%0A%0A    it('should transmit signal from publisher to subsriber', () =%3E
+%7D%0A%0A    function createPublisherNode(id, outLinks)
  %7B%0A 
@@ -414,37 +414,26 @@
       const 
-publishStream
+ee
  = new Event
@@ -452,33 +452,34 @@
      createNode(
-1
+id
 , %7B%0A        setu
@@ -495,32 +495,22 @@
  =%3E 
+ee.on('
 publish
-Stream.on('sample
 ', f
@@ -556,115 +556,128 @@
 inks
-: %7B
 %0A      
-    val: %5B%7B%0A            nodeID: 2,%0A   
+%7D);%0A%0A      return ee;%0A
     
+%7D%0A%0A
     
- inputName: 'val',%0A          %7D%5D,%0A        %7D,
+function createSubscriberNode(id) %7B%0A      let mock = %7B%0A        calls: %5B%5D
 %0A      %7D
 );%0A%0A
@@ -676,34 +676,11 @@
    %7D
-)
 ;%0A%0A
-      let calls = %5B%5D;%0A
     
@@ -717,32 +717,37 @@
 uate: inputs =%3E 
+mock.
 calls.push(input
@@ -826,32 +826,286 @@
 des,%0A      %7D);%0A%0A
+      return mock;%0A    %7D%0A%0A    it('should transmit signal from publisher to subsriber', () =%3E %7B%0A      const publisher = createPublisherNode(1, %7B%0A        val: %5B%7B nodeID: 2, inputName: 'val' %7D%5D,%0A      %7D);%0A%0A      const subscriber = createSubscriberNode(2);%0A%0A
       new Projec
@@ -1150,35 +1150,32 @@
  publish
-Stream
+er
 .emit('
-sample
+publish
 ', %7B val
@@ -1188,32 +1188,43 @@
 );%0A      expect(
+subscriber.
 calls).to.be.eql
@@ -1367,161 +1367,38 @@
 lish
-Stream = new EventEmitter();%0A      createNode(1, %7B%0A        setup: (fire) =%3E publishStream.on('sample', fire),%0A        pure: false,%0A        outLinks:
+er = createPublisherNode(1,
  %7B%0A
-  
     
@@ -1412,270 +1412,99 @@
 : %5B%7B
-%0A            nodeID: 2,%0A            inputName: 'val',%0A          %7D%5D,%0A        %7D,%0A      %7D);%0A%0A      let calls = %5B%5D;%0A      createNode(2, %7B%0A        evaluate: inputs =%3E calls.push(inputs),%0A        pure: false,%0A        inputTypes: %7B val: Number %7D,%0A        nodes,%0A      %7D
+ nodeID: 2, inputName: 'val' %7D%5D,%0A      %7D);%0A%0A      const subscriber = createSubscriberNode(2
 );%0A%0A
@@ -1565,35 +1565,32 @@
  publish
-Stream
+er
 .emit('
-sample
+publish
 ', %7B val
@@ -1611,35 +1611,32 @@
  publish
-Stream
+er
 .emit('
-sample
+publish
 ', %7B val
@@ -1661,27 +1661,24 @@
 lish
-Stream
+er
 .emit('
-sample
+publish
 ', %7B
@@ -1703,16 +1703,27 @@
  expect(
+subscriber.
 calls).t

make runtime tests DRY