<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
}

# http://docs.travis-ci.com/user/ci-environment/#Environment-variables
# http://docs.drone.io/env.html  (Jenkins compatible)
# https://www.codeship.io/documentation/continuous-integration/set-environment-variables/
env = process.env
build = env.BUILD_ID || env.TRAVIS_BUILD_ID || env.CI_BUILD_NUMBER || env.JOB_ID
buildUrl = env.BUILD_URL || env.CI_BUILD_URL || build
commit = env.GIT_COMMIT || env.TRAVIS_COMMIT || env.CI_COMMIT_ID || env.COMMIT
tunnelId = build
tags = []
tags.push('travis') if env.TRAVIS
tags.push('drone') if env.DRONE
tags.push('shippable') if env.USER is 'shippable'
tags.push(env.CI_NAME) if env.CI_NAME

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'
  build: "#{buildUrl} commit #{commit}"
  tags: tags
  # Most my tests timeout a lot due crashing without cleanup (see below);
  # this will waste less Sauce resources than default 90s.
  'idle-timeout': 30
}

# TODO: Cleanup even if there were errors.  Use promises for sanity?
#  Use a test runner with guaranteed pre/post methods?

test = (cb) ->
  # Kludge: set to failed first, change to passed if we get to the end without crashing.
  browser.sauceJobStatus false, ->
    browser.get 'http://localhost:8000/?doc=_mathdown_test_smoke', (err) ->
      assert.ifError(err)
      browser.waitFor wd.asserters.jsCondition('document.title.match(/smoke test/)'), 10000, (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'))
            browser.sauceJobStatus(true)
            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, tunnelId, true, ['--verbose'])
console.log('Creating tunnel...')
tunnel.start (status) ->
  assert(status, 'tunnel creation failed')
  console.log('tunnel created')
  desired['tunnel-identifier'] = tunnel.identifier
  console.log(desired)
  browser.init desired, (err) ->
    assert.ifError(err)
    test ->
      browser.quit()
      server.close()
      tunnel.stop ->
        console.log(chalk.green('Cleaned up.'))<commit_msg>test: use port 8001 instead of 8000 so it works on my machine. This is really dumb - it started failing for me with EADDRINUSE a long time ago, turns out I've installed webfsd which runs on port 8000. The right way would be to pick a free port, but Sauce Connect only forwards some specific ports.<commit_after>sauceTunnel = require('sauce-tunnel')
st = require('st')
http = require('http')
wd = require('wd')
assert = require('assert')
chalk = require('chalk')

# 'mathdown' is a sub-account I created.
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
}

# http://docs.travis-ci.com/user/ci-environment/#Environment-variables
# http://docs.drone.io/env.html  (Jenkins compatible)
# https://www.codeship.io/documentation/continuous-integration/set-environment-variables/
env = process.env
build = env.BUILD_ID || env.TRAVIS_BUILD_ID || env.CI_BUILD_NUMBER || env.JOB_ID
buildUrl = env.BUILD_URL || env.CI_BUILD_URL || build
commit = env.GIT_COMMIT || env.TRAVIS_COMMIT || env.CI_COMMIT_ID || env.COMMIT
tunnelId = build
tags = []
tags.push('travis') if env.TRAVIS
tags.push('drone') if env.DRONE
tags.push('shippable') if env.USER is 'shippable'
tags.push(env.CI_NAME) if env.CI_NAME

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'
  build: "#{buildUrl} commit #{commit}"
  tags: tags
  # Most my tests timeout a lot due crashing without cleanup (see below);
  # this will waste less Sauce resources than default 90s.
  'idle-timeout': 30
}

# TODO: Cleanup even if there were errors.  Use promises for sanity?
#  Use a test runner with guaranteed pre/post methods?

test = (url, cb) ->
  # Kludge: set to failed first, change to passed if we get to the end without crashing.
  browser.sauceJobStatus false, ->
    browser.get url + '?doc=_mathdown_test_smoke', (err) ->
      assert.ifError(err)
      browser.waitFor wd.asserters.jsCondition('document.title.match(/smoke test/)'), 10000, (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'))
            browser.sauceJobStatus(true)
            cb()

# https://docs.saucelabs.com/reference/sauce-connect/#can-i-access-applications-on-localhost-
# lists ports we can use.
port = 8001
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(port)
console.log('Server up, e.g. http://localhost:' + port + '/?doc=_mathdown_test_smoke')

tunnel = new sauceTunnel(sauceUser, sauceKey, tunnelId, true, ['--verbose'])
console.log('Creating tunnel...')
tunnel.start (status) ->
  assert(status, 'tunnel creation failed')
  console.log('tunnel created')
  desired['tunnel-identifier'] = tunnel.identifier
  console.log(desired)
  browser.init desired, (err) ->
    assert.ifError(err)
    test 'http://localhost:' + port, ->
      browser.quit()
      server.close()
      tunnel.stop ->
        console.log(chalk.green('Tunnel stopped, cleaned up.'))
{'fraction_non_alphanumeric': 0.14914392723381487, 'fraction_numerical': 0.013911182450508293, 'mean_word_length': 3.747301587301587, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, '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': '7274489', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2540, 'n_words': 644}