<commit_before>sauceTunnel = require('sauce-tunnel')
st = require('st')
http = require('http')
wd = require('wd')
assert = require('assert')
chalk = require('chalk')

sauceUser = process.env.SAUCE_USERNAME || 'mathdown'
# I hope storing this in a public test is OK given that an Open Sauce account
# is free anyway.  Can always revoke if needed...
sauceKey = process.env.SAUCE_ACCESS_KEY || '23056294-abe8-4fe9-8140-df9a59c45c7d'

sauceConnectOptions = {
  username: sauceUser
  accessKey: sauceKey
  verbose: true
  logger: console.log
}

browser = wd.remote('ondemand.saucelabs.com', 80, sauceUser, sauceKey)

browser.on 'status', (info) ->
  console.log(chalk.cyan(info))
browser.on 'command', (meth, path) ->
  console.log(' > %s: %s', chalk.yellow(meth), path)
#browser.on 'http', (meth, path, data) ->
#  console.log(' > %s %s %s', chalk.magenta(meth), path, chalk.grey(data))

desired = {
  browserName: 'internet explorer'
  version: '8'
  platform: 'Windows XP'
  name: 'smoke test'
}

# TODO: Cleanup even if there were errors.  Use promises for sanity?

test = (cb) ->
  # TODO: use current source (via Sauce Connect?)
  browser.get 'http://localhost:8000/?doc=_mathdown_test_smoke', (err) ->
    assert.ifError(err)
    browser.waitFor wd.asserters.jsCondition('document.title.match(/smoke test/)', 2000), (err, value) ->
      assert.ifError(err)
      browser.waitForElementByCss '.MathJax_Display', 15000, (err, el) ->
        assert.ifError(err)
        el.text (err, text) ->
          assert.ifError(err)
          if not text.match(/^\s*α\s*$/)
            assert.fail(text, '/^\s*α\s*$/', 'math text is wrong', ' match ')
          console.log(chalk.green('ALL PASSED'))
          cb()

server = http.createServer(st({
  path: process.cwd()
  index: 'index.html'
}))
server.listen(8000)
console.log('Server up, e.g. http://localhost:8000/?doc=_mathdown_test_smoke')

tunnel = new sauceTunnel(sauceUser, sauceKey, undefined, true, ['--verbose'])
tunnel.start (status) ->
  assert(status, 'tunnel creation failed')
  console.log('tunnel created')
  browser.init desired, (err) ->
    assert.ifError(err)
    test ->
      browser.quit()
      server.close()
      tunnel.stop ->
        console.log(chalk.green('Cleaned up.'))<commit_msg>Log incoming requests to static file server.<commit_after>sauceTunnel = require('sauce-tunnel')
st = require('st')
http = require('http')
wd = require('wd')
assert = require('assert')
chalk = require('chalk')

sauceUser = process.env.SAUCE_USERNAME || 'mathdown'
# I hope storing this in a public test is OK given that an Open Sauce account
# is free anyway.  Can always revoke if needed...
sauceKey = process.env.SAUCE_ACCESS_KEY || '23056294-abe8-4fe9-8140-df9a59c45c7d'

sauceConnectOptions = {
  username: sauceUser
  accessKey: sauceKey
  verbose: true
  logger: console.log
}

browser = wd.remote('ondemand.saucelabs.com', 80, sauceUser, sauceKey)

browser.on 'status', (info) ->
  console.log(chalk.cyan(info))
browser.on 'command', (meth, path) ->
  console.log(' > %s: %s', chalk.yellow(meth), path)
#browser.on 'http', (meth, path, data) ->
#  console.log(' > %s %s %s', chalk.magenta(meth), path, chalk.grey(data))

desired = {
  browserName: 'internet explorer'
  version: '8'
  platform: 'Windows XP'
  name: 'smoke test'
}

# TODO: Cleanup even if there were errors.  Use promises for sanity?

test = (cb) ->
  # TODO: use current source (via Sauce Connect?)
  browser.get 'http://localhost:8000/?doc=_mathdown_test_smoke', (err) ->
    assert.ifError(err)
    browser.waitFor wd.asserters.jsCondition('document.title.match(/smoke test/)', 2000), (err, value) ->
      assert.ifError(err)
      browser.waitForElementByCss '.MathJax_Display', 15000, (err, el) ->
        assert.ifError(err)
        el.text (err, text) ->
          assert.ifError(err)
          if not text.match(/^\s*α\s*$/)
            assert.fail(text, '/^\s*α\s*$/', 'math text is wrong', ' match ')
          console.log(chalk.green('ALL PASSED'))
          cb()

server = http.createServer(st({
  path: process.cwd()
  index: 'index.html'
}))
server.on 'request', (req, res) ->
  console.log(' < %s %s', chalk.green(req.method), req.url)
server.listen(8000)
console.log('Server up, e.g. http://localhost:8000/?doc=_mathdown_test_smoke')

tunnel = new sauceTunnel(sauceUser, sauceKey, undefined, true, ['--verbose'])
tunnel.start (status) ->
  assert(status, 'tunnel creation failed')
  console.log('tunnel created')
  browser.init desired, (err) ->
    assert.ifError(err)
    test ->
      browser.quit()
      server.close()
      tunnel.stop ->
        console.log(chalk.green('Cleaned up.'))
{'fraction_non_alphanumeric': 0.16451056489866323, 'fraction_numerical': 0.019404915912031046, 'mean_word_length': 3.6623115577889447, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 32, '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': '7345668', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1625, 'n_words': 380}