   id: 'server-test-view'
   template: require 'templates/server-test-view'
   status: 'Off'
-  
+
   events:
     'click .show-stack-button': 'onClickShowStackButton'
     'click #toggle': 'onToggleTesting'

      success: _.bind(@listenForChanges, @)
      error: _.bind(@onServerTestingSetupFailed, @)
    })
    
  onServerTestingSetupFailed: ->
    @setStatus('Server Down, Polling...')
    setTimeout(@setupServerTesting, 3000)
    
  teardownServerTesting: ->
    @setTesting(false)
    @setStatus('Restarting Server')
    $.ajax('/server-test/teardown', {
      type: 'POST'
      success: _.bind(@onTeardownDone, @)
      error: FrimFram.onAjaxError
    })
    
  onTeardownDone: ->
    @setStatus('Off')
      
  setStatus: (@status) ->
    @$el.find('#status').text(@status)

  onToggleTesting: ->
    if @testing then @teardownServerTesting() else @setupServerTesting()
    
  setTesting: (@testing) ->
    toggle = @$el.find('#toggle')
    toggle.removeClass('btn-danger btn-success')
    toggle.addClass(if @testing then 'btn-success' else 'btn-danger')
    toggle.text(if @testing then 'Turn Off Testing' else 'Turn On Testing')
    
  #- Report digestion. Tests were parsed from XML with odd names. Make the reports sensical.
    
  onReportsLoaded: (result) ->
    @setStatus('Standing By')
    @stacks = []
    @rootDir = result.rootDir
    
    if result.consoleError
      @consoleError = @digestStack(result.consoleError)
      
    if result.reports
      suites = (obj.testsuites.testsuite for obj in result.reports)
      suites = _.flatten(suites)
      @suites = (@digestSuite(suite) for suite in suites)
      
    @render()

  digestStack: (stack) ->
    short = []
    lastLineWasRemoved = false
    
    # shorten the stack by ...'ing all jasmine-node lines, and "removing" the rootDir.
    
    lineIsUseless = (line) ->
      return _.any([
          'node_modules/jasmine-node'

    if test.failure
      result.failures = (@digestFailure(failure) for failure in test.failure)
    return result

  digestFailure: (failure) ->
    return {
    stack: @digestStack(failure._)
    type: failure.$.type # always expect? Is something broken?
    message: failure.$.message
    }


  #- other

  onClickShowStackButton: (e) ->
