'use strict'

const ActiveRules = require('../src/ActiveRules.js')
const AR = new ActiveRules()
const sinon = require('sinon')

function engineFactory(rules) {
  return new AR.Engine(rules);
}

async function factSenior (params, engine) {
  return 65
}

async function factChild (params, engine) {
  return 10
}

async function factAdult (params, engine) {
  return 30
}

describe('Engine: "all" conditions', () => {
  let engine

  describe('supports a single "all" condition', () => {
    let event = {
      type: 'ageTrigger',
      params: {
        demographic: 'under50'
      }
    }
    let conditions = {
      all: [{
        'fact': 'age',
        'operator': 'lessThan',
        'value': 50
      }]
    }
    let eventSpy = sinon.spy()
    beforeEach(() => {
      //eventSpy.reset()
      let rule = factories.rule({ conditions, event })
      engine = engineFactory()
      engine.addRule(rule)
      engine.on('success', eventSpy)
    })

    it('emits when the condition is met', async () => {
      engine.addFact('age', factChild)
      await engine.run()
      expect(eventSpy).to.have.been.calledWith(event)
    })

    it('does not emit when the condition fails', () => {
      let eventSpy = sinon.spy()
      engine.addFact('age', factSenior)
      engine.run()
      expect(eventSpy).to.not.have.been.calledWith(event)
    })
  })

  describe('supports "any" with multiple conditions', () => {
    let conditions = {
      all: [{
        'fact': 'age',
        'operator': 'lessThan',
        'value': 50
      }, {
        'fact': 'age',
        'operator': 'greaterThan',
        'value': 21
      }]
    }
    let event = {
      type: 'ageTrigger',
      params: {
        demographic: 'adult'
      }
    }
    let eventSpy = sinon.spy()
    beforeEach(() => {
      //eventSpy.reset()
      let rule = factories.rule({ conditions, event })
      engine = engineFactory()
      engine.addRule(rule)
      engine.on('success', eventSpy)
    })

    it('emits an event when every condition is met', async () => {
      engine.addFact('age', factAdult)
      await engine.run()
      expect(eventSpy).to.have.been.calledWith(event)
    })

    describe('a condition fails', () => {
      it('does not emit when the first condition fails', async () => {
        let eventSpy = sinon.spy()
        engine.addFact('age', factChild)
        await engine.run()
        expect(eventSpy).to.not.have.been.calledWith(event)
      })

      it('does not emit when the second condition', async () => {
        let eventSpy = sinon.spy()
        engine.addFact('age', factSenior)
        await engine.run()
        expect(eventSpy).to.not.have.been.calledWith(event)
      })
    })
  })
})

@@ -764,34 +764,32 @@
 h(() =%3E %7B%0A      
-//
 eventSpy.reset()
@@ -778,32 +778,39 @@
   eventSpy.reset
+History
 ()%0A      let rul
@@ -1197,41 +1197,8 @@
 %3E %7B%0A
-      let eventSpy = sinon.spy()%0A
     
@@ -1768,10 +1768,8 @@
     
-//
 even
@@ -1778,16 +1778,23 @@
 py.reset
+History
 ()%0A     

use reset history