 
   ".prompt":
     topic: ->
-      browser = new zombie.Browser
+      brains.get "/prompt", (req, res)-> res.send """
+        <script>
+          window.first = prompt("age");
+          window.second = prompt("gender");
+          window.third = prompt("location");
+          window.fourth = prompt("weight");
+        </script>
+        """
+      browser = new Browser
       browser.onprompt "age", 31
       browser.onprompt (message, def)-> "unknown" if message == "gender"
       browser.onprompt "location", false

require "./helpers"
{ vows: vows, assert: assert, zombie: zombie, brains: brains } = require("vows")
jsdom = require("jsdom")

brains.get "/static", (req, res)-> res.send """
  <html>
    <head>
      <title>Whatever</title>
    </head>
    <body>Hello World</body>
  </html>
  """

brains.get "/alert", (req, res)-> res.send """
  <script>
    alert("Hi");
    alert("Me again");
  </script>
  """

brains.get "/confirm", (req, res)-> res.send """
  <script>
    window.first = confirm("continue?");
    window.second = confirm("more?");
    window.third = confirm("silent?");
  </script>
  """

brains.get "/prompt", (req, res)-> res.send """
  <script>
    window.first = prompt("age");
    window.second = prompt("gender");
    window.third = prompt("location");
    window.fourth = prompt("weight");
  </script>
  """

brains.get "/screen", (req, res)-> res.send """
  <script>
    var props = [];

    for (key in window.screen) {
      props.push(key + "=" + window.screen[key]);
    }

    document.title = props.join(", ");
  </script>
  """

vows.describe("Window").addBatch(
  ".title":
    zombie.wants "http://localhost:3003/static"
      "should return the document's title": (browser)-> assert.equal browser.window.title, "Whatever"
      "should set the document's title": (browser)->
        browser.window.title = "Overwritten"
        assert.equal browser.window.title, browser.document.title

  ".alert":
    topic: ->
      browser = new zombie.Browser
      browser.onalert (message)-> browser.window.first = true if message = "Me again"
      browser.wants "http://localhost:3003/alert", @callback
    "should record last alert show to user": (browser)-> assert.ok browser.prompted("Me again")


  ".confirm":
    topic: ->
      brains.get "/confirm", (req, res)-> res.send """
        <script>
          window.first = confirm("continue?");
          window.second = confirm("more?");
          window.third = confirm("silent?");
        </script>
        """
      browser = new Browser
      browser.onconfirm "continue?", true
      browser.onconfirm (prompt)-> true if prompt == "more?"
      browser.wants "http://localhost:3003/confirm", @callback
