<commit_before>soda = require("soda")
describe "networkInteractions", ->
  it "should give a player an id", ->
    browser1 = soda.createClient(
      host: "localhost"
      port: 4444
      url: "http://localhost:8080"
      browser: "firefox"
    )

    browser2 = soda.createClient(
      host: "localhost"
      port: 4444
      url: "http://localhost:8080"
      browser: "firefox"
    )

    id1 = undefined
    id2 = undefined
    finishedFirst = false
    finishedSecond = false
    
    runs(->
      browser1.session (err) ->
        browser1.open "/", (err, body) ->
          browser1.waitForPageToLoad 5000, (err, body) ->
            console.log err
            evalResult = undefined
            browser1.getEval "var tetris = 'JAMES'; storedVars['evalResult'] = tetris;", (err, body) ->
              console.log "<<<<<<<<<<<<<INSIDE getEval>>>>>>>>>>>>>>>>"
              console.log err
              console.log body
              console.log "<<<<<<<<<<<<<>>>>>>>>>>>>>>>>"
              browser1.testComplete (err, body) ->
                console.log "done"
                finishedFirst = true
                finishedSecond = true

    )

  
    waitsFor(->
      return finishedFirst == true
    , "First browser never completed", 20000);

    runs(->
      #browser2.chain.session().open("/").waitForPageToLoad(5000).end (err) ->
      #  browser2.testComplete ->
      #    console.log "done 2"
      #    finishedSecond = true
      #    throw err  if err
    )

    waitsFor( ->
      return finishedSecond == true
    , "Second browser never completed", 20000);

    runs(->
      console.log browserTitle1
      console.log browserTitle2
      expect(browserTitle1).toEqual(browserTitle2)
    )<commit_msg>FINALLY got unit tests working<commit_after>soda = require("soda")
describe "networkInteractions", ->
  it "should give each player a unique id", ->

    ###
    HOW THIS WORKS
    1. Define the browser settings and set them up
    2. Do the stuff we want to do using the browsers
    3. In the mean time, wait until the browsers are finished doing what we want
    4. Use the stuff we got from the browsers to do the actual jasmine test.

    ###

    #Define what URL will be in each browser etc.
    browserSettings = 
      host: "localhost"
      port: 4444
      url: "http://localhost:8080"
      browser: "firefox"

    # Define two browser windows.
    browser1 = soda.createClient(browserSettings)
    browser2 = soda.createClient(browserSettings)

    # These are the ids we are going to get from the browser windows
    id1 = undefined
    id2 = undefined

    # A busy-wait lock that gets released when browsers close
    browsersClosed = false
    
    # Open browser 1
    browser1.session (err) ->
      browser1.open "/", (err, body) ->
        # Wait for the page (dom) to load. If more than 5000 milliseconds pass and the page hasn't loaded, exit.
        browser1.waitForPageToLoad 5000, (err, body) ->
          # Page is now loaded. Now, repeatedly check if Game.myPlayerId is defined. 20000 is the timeout
          browser1.waitForCondition "typeof window.Game.myPlayerId !== 'undefined'", 20000, (err, body) ->
            # Game.myPlayerId is finally defined. Now, get it's result and store it in body (the param call back)
            browser1.getEval "storedVars['evalResult'] = window.Game.myPlayerId;", (err, body) ->
              id1 = body
              #OK, we're done with browser 1 but don't want to close it yet because we need to open
              #a new browser window to check what tha player id for *it* is.
              #Open a new browser
              browser2.session (err) ->
                browser2.open "/", (err, body) ->
                  # Wait for the page to load
                  browser2.waitForPageToLoad 5000, (err, body) ->
                    # Wait up to 20000 milliseconds ultil the myPlayerId is defined.
                    browser2.waitForCondition "typeof window.Game.myPlayerId !== 'undefined'", 20000, (err, body) ->
                      # Get Game.myPlayerId
                      browser2.getEval "storedVars['evalResult'] = window.Game.myPlayerId;", (err, body) ->
                        id2 = body
                        #Great, we now have the player id of browser 1 and browser two. Close browser 2.
                        browser2.testComplete (err, body) ->
                          # Browser 2 is now closed. Now close browser 1.
                          browser1.testComplete (err, body) ->
                            #Browser 1 *and* browser 2 are now close. We can now release the lock.
                            browsersClosed = true
  

    # Busy wait until the browsers close. Uses finishedTest to test this.
    waitsFor(->
      return browsersClosed == true
    , "Browser test never completed", 100000);

    # We need a 'runs' here to say "only run the runs code AFTER the waiting is done"
    runs(->
      # Finally, do the unit test (this is normal jasmine stuff)
      expect(id1).not.toEqual(id2)
    )
{'fraction_non_alphanumeric': 0.10265556876734047, 'fraction_numerical': 0.02536662703131193, 'mean_word_length': 1.995252225519288, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1992852', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1360, 'n_words': 487}