 { Vows, assert, brains, Browser } = require("./helpers")
 
 
-Vows.describe("Scripts").addBatch(
+Vows.describe("Scripts").addBatch
 
   "basic":
     topic: ->

        "should evaluate in context and return value": (title)->
          assert.equal title, "The Living"


  "evaluating":
    "context":

      #assert.equal browser.document.title, "OneTwoThreeFour"


.addBatch
  
  "failing":
    "incomplete":
      topic: ->
        brains.get "/script/incomplete", (req, res)->
          res.send "<script>1+</script>"
        browser = new Browser
        browser.wants "http://localhost:3003/script/incomplete", @callback
      "should propagate error to window": (browser)->
        assert.equal browser.error.message, "Unexpected end of input"

    "error":
      topic: ->
        brains.get "/script/error", (req, res)->
          res.send "<script>(function(foo) { foo.bar })()</script>"
        browser = new Browser
        browser.wants "http://localhost:3003/script/error", @callback
      "should propagate error to window": (browser)->
        assert.equal browser.error.message, "Cannot read property 'bar' of undefined"


.addBatch

  "loading":
    "with entities":
      topic: ->
        brains.get "/script/split", (req, res)->
          res.send """
          <html>
            <script>foo = 1 < 2 ? 1 : 2; '&'; document.title = foo</script>
          </html>
          """
        browser = new Browser
        browser.wants "http://localhost:3003/script/split", @callback
      "should run full script": (browser)->
        assert.equal browser.text("title"), "1"

    ###
    # NOTE: htmlparser can't deal with CDATA sections
    "with CDATA":
      topic: ->
        brains.get "/script/cdata", (req, res)-> res.send """
          <html>
            <script>foo = 2; <![CDATA[ document.title ]]> = foo</script>
          </html>
          """
        browser = new Browser
        browser.wants "http://localhost:3003/script/cdata", @callback
      "should run full script": (browser)-> assert.equal browser.text("title"), "2"
    ###

    # NOTE: htmlparser can't deal with document.write.
    ###
    "using document.write":
      topic: ->
        brains.get "/script/write", (req, res)-> res.send """
          <html>
            <head>
              <script>document.write(unescape(\'%3Cscript src="/jquery.js"%3E%3C/script%3E\'));</script>
            </head>
            <body>
              <script>
                $(function() { document.title = "Script document.write" });
              </script>
            </body>
          </html>
          """
        browser = new Browser
        browser.wants "http://localhost:3003/script/write", @callback
      "should run script": (browser)-> assert.equal browser.document.title, "Script document.write"
    ###

    "using appendChild":
      topic: ->
        brains.get "/script/append", (req, res)->
          res.send """
          <html>
            <head>
              <script>
                var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true;
                s.src = '/jquery.js';
                (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);
              </script>
            </head>
            <body>
              <script>
                $(function() { document.title = "Script appendChild" });
              </script>
            </body>
          </html>
          """
        browser = new Browser
        browser.wants "http://localhost:3003/script/append", @callback
      "should run script": (browser)->
        assert.equal browser.document.title, "Script appendChild"

  "scripts disabled":
    topic: ->
      brains.get "/script/no-scripts", (req, res)->
        res.send """
        <html>
          <head>
            <title>Zero</title>
            <script src="/script/no-scripts.js"></script>
          </head>
          <body>
            <script>
            document.title = document.title + "Two";</script>
          </body>
        </html>
        """
      brains.get "/script/no-scripts.js", (req, res)->
        res.send "document.title = document.title + 'One'"
      browser = new Browser(runScripts: false)
      browser.wants "http://localhost:3003/script/order", @callback
    "should not run scripts": (browser)->
      assert.equal browser.document.title, "Zero"


  ###
.addBatch

  "new Image":
    Browser.wants "http://localhost:3003/script/living"
      "should construct an img tag": (browser)-> assert.equal domToHtml(browser.evaluate("new Image")), "<img>\r\n"
      "should construct an img tag with width and height": (browser)->
        assert.equal domToHtml(browser.evaluate("new Image(1, 1)")), "<img width=\"1\" height=\"1\">\r\n"
  ###

      
.export(module)
