 # @param [Function] ready the action when the condition is fulfilled
 # @param [Number] timeout the max amount of time to wait
 #
-waitFor = (condition, ready, timeout = 3000) ->
+waitFor = (condition, ready, timeout = 5000) ->
   start = new Date().getTime()
   wait = ->
     if new Date().getTime() - start > timeout
-      console.log JSON.stringify({ error: "Timeout requesting Jasmine test runner!" })
+      console.log JSON.stringify({ error: 'Timeout requesting Jasmine test runner!' })
       phantom.exit(1)
     else
       if condition()

# This file is the script that runs within PhantomJS and requests the Jasmine specs,
# waits until they are ready, extracts the result form the dom and outputs a JSON
# structure that is the parsed by Guard::Jasmine.
#
# This scripts needs the TrivialReporter to report the results.
#
# This file is inspired by the Jasmine runner that comes with the PhantomJS examples:
# https://github.com/ariya/phantomjs/blob/master/examples/run-jasmine.coffee, by https://github.com/Roejames12
#
# This file is licensed under the BSD license.

# Wait until the test condition is true or a timeout occurs.
#

specsReady = ->
  page.evaluate -> if document.body.querySelector('.finished-at') then true else false

# Check arguments of the script.
#
if phantom.args.length isnt 1
  console.log JSON.stringify({ error: 'Wrong usage of PhantomJS script!' })
  phantom.exit()
else
  url = phantom.args[0]

# Create the web page.
#
page = require('webpage').create()

# Capture console.log output to wrap the output
# from the specs into a JSON message.
#
page.onConsoleMessage = (msg, line, source) ->
  console.log JSON.stringify({ log: msg, line: line, source: source })

# The console reporter sends its response as alert.
#
page.onAlert = (msg) -> console.log msg

# Initialize the page before the JavaScript is run.
#
page.onInitialized = ->
  page.evaluate ->

    # Jasmine Reporter that logs results through an alert.
    #
    class ConsoleReporter

      reportSpecResults: (spec) ->
        @report { spec: spec.description, failed: spec.results().failedCount, skiped: spec.results().skipped }

      reportSuiteResults: (suite) ->
        @report { suite: suite.getFullName(), failed: suite.results().failedCount, skiped: suite.results().skipped  }

      reportRunnerResults: (runner) ->
        @report { finish: runner.description, total: runner.results().totalCount, failed: runner.results().failedCount }

      reportRunnerStarting: (runner) ->
      reportSpecStarting: (spec) ->
      log: (str) ->

      report: (response) ->
        alert JSON.stringify(response)

    # Attach the console reporter when the document is ready.
    #
    window.onload = ->
      jasmine.getEnv().addReporter(new ConsoleReporter())

# Open web page and run the Jasmine test runner
#
page.open url, (status) ->
  if status isnt 'success'
    console.log JSON.stringify({ error: "Unable to access Jasmine specs at #{ url }" })
    phantom.exit()
  else
    waitFor specsReady, -> phantom.exit()

