// @flow
import sinon from 'sinon'
import test from 'tape'

//TODO: import { PerpetualMotionMachine, TimedMachineFactory } from './machine'
import { PerpetualMotionMachine } from './motion-machine'

test('PerpetualMotionMachine', assert => {
  const expectedCallCount = 5;
  const interval = 100;
  const requestAnimationFrame = (fn) => { setTimeout(fn, interval) };
  const cancelAnimationFrame = clearTimeout;
  const spy = sinon.spy();
  let numIterations = 0;

  const onComplete = () => {
    assert.equals(spy.callCount, expectedCallCount, 'the machine runs the expected number of iterations');
    assert.end();
  }

  const PerpetualMachine = PerpetualMotionMachine(requestAnimationFrame, cancelAnimationFrame);
  const machine = PerpetualMachine(onComplete);

  const job = () => {
    if (numIterations === expectedCallCount) {
      machine.stop();
    }
    numIterations++;
    spy();
  };

  machine
    .do(job)
    .run();
});

test('TimedMachineFactory', assert => {
  const duration = 500;
  const interval = 100;
  const expectedCallCount = Math.floor(duration / interval);
  const requestAnimationFrame = (fn) => { setTimeout(fn, interval) };
  const cancelAnimationFrame = clearTimeout;
  const job = sinon.spy();

  const onComplete = () => {
    assert.equals(job.callCount, expectedCallCount, 'the machine runs the expected number of iterations');
    assert.true(machine.isStopped(), 'the machine knows when it is stopped');
    assert.end();
  }

  const TimedMachine = TimedMachineFactory(requestAnimationFrame, cancelAnimationFrame);
  const machine = TimedMachine(duration, onComplete);

  machine
    .do(job)
    .run();
});

@@ -57,88 +57,8 @@
 e'%0A%0A
-//TODO: import %7B PerpetualMotionMachine, TimedMachineFactory %7D from './machine'%0A
 impo
@@ -84,16 +84,37 @@
 nMachine
+, TimedMachineUpgrade
  %7D from 
@@ -411,176 +411,14 @@
 nst 
-onComplete = () =%3E %7B%0A    assert.equals(spy.callCount, expectedCallCount, 'the machine runs the expected number of iterations');%0A    assert.end();%0A  %7D%0A%0A  const Perpetual
+motion
 Mach
@@ -495,56 +495,8 @@
 me);
-%0A  const machine = PerpetualMachine(onComplete);
 %0A%0A  
@@ -569,16 +569,22 @@
 %0A      m
+otionM
 achine.s
@@ -590,16 +590,231 @@
 stop();%0A
+      assert.true(motionMachine.isStopped(), 'the machine knows when it is stopped');%0A      assert.equals(spy.callCount, expectedCallCount, 'the machine runs the expected number of iterations');%0A      assert.end();%0A
     %7D%0A  
@@ -848,24 +848,30 @@
 );%0A  %7D;%0A%0A  m
+otionM
 achine%0A    .
@@ -913,23 +913,23 @@
 dMachine
-Factory
+Upgrade
 ', asser
@@ -970,34 +970,33 @@
 onst interval = 
-10
+9
 0;%0A  const expec
@@ -1198,135 +1198,191 @@
 nst 
-onComplete = () =%3E %7B%0A    assert.equals(job.callCount, expectedCallCount, 'the machine ru
+motionMachine = PerpetualMotionMachine(requestAnimationFrame, cancelAnimationFrame);%0A  co
 ns
+t
  t
-he expected number of iterations');
+imedMachine = TimedMachineUpgrade(motionMachine, duration);%0A%0A  const onComplete = () =%3E %7B
 %0A   
@@ -1394,17 +1394,22 @@
 rt.true(
-m
+timedM
 achine.i
@@ -1477,166 +1477,134 @@
 rt.e
-nd();%0A  %7D%0A%0A  const TimedMachine = TimedMachineFactory(requestAnimationFrame, cancelAnimationFrame);%0A  const machine = TimedMachine(duration, onComplete);%0A%0A  m
+quals(job.callCount, expectedCallCount, 'the machine runs the expected number of iterations');%0A    assert.end();%0A  %7D%0A%0A  timedM
 achi
@@ -1612,31 +1612,41 @@
 e%0A    .do(job)%0A    .run(
+onComplete
 );%0A%7D);%0A

Fix motion-machine tests