require '../spec_helper'

describe "Freakset Acceptance", ->

  beforeEach ->
    @freakset = new Freakset()
    @set = null


  it "should build a parallel Freakset on the Fly", ->

    @freakset.group "group:parallel", { parallel: yes }, (innerFreakset) ->

      innerFreakset.step "step1", (set) ->
        set.step "flyingStep1", (set) ->
          _.delay ( => 
            set.store['steps'].push("step1")
            set.commit()
          ), 1000

      innerFreakset.step "step2", (set) ->
        set.store['steps'].push("step2")
        _.delay ( => set.commit() ), 2000


    @set = @freakset.compile { steps: [] }

    # Just wait until compiling startet..
    waitsFor -> !@set.isRunning()

    runs ->
      expect(@set.store['steps']).toContain("step1")
      expect(@set.store['steps']).toContain("step2")


  it "should build a Freakset on the Fly with Steps", ->

    @freakset.step "step1", (set) ->
      set.store['steps'].push("step1")
      set.commit()

    @freakset.step "build:onTheFly", (set) ->
      set.step "step2", (set) ->
        _.delay ( =>
          set.store['steps'].push("step2")
          set.commit()
        ), 100

      set.commit()


    @freakset.step "step3", (set) ->
      set.store['steps'].push("step3")
      set.commit()

    @set = @freakset.compile { steps: [] }


    waitsFor -> !@set.isRunning()

    runs ->
      expect(@set.store['steps'][0]).toEqual("step1")
      expect(@set.store['steps'][1]).toEqual("step2")
      expect(@set.store['steps'][2]).toEqual("step3")



  it "should build a Freakset on the Fly with a Group", ->

    @freakset.step "step1", (set) ->
      set.store['steps'].push("step1")
      set.commit()

    @freakset.step "build:onTheFly", (set) ->

      # The Group and its inner Actors are added on the fly to
      # the currently running Freakset. The Actors are automatically 
      # put to the top of the current stack to be executed in the right order.
      set.group "group:builder", (group) ->

        group.step "step2", (set) ->
          _.delay ( =>
            set.store['steps'].push("step2")
            set.commit()
          ), 100

        group.commit()


    @freakset.step "step3", (set) ->
      set.store['steps'].push("step3")
      set.commit()

    @set = @freakset.compile { steps: [] }


    waitsFor -> !@set.isRunning()

    runs ->
      expect(@set.store['steps'][0]).toEqual("step1")
      expect(@set.store['steps'][1]).toEqual("step2")
      expect(@set.store['steps'][2]).toEqual("step3")
