<commit_before>import { test } from 'ava'

import {
  PuppetWeb
  , log
} from '../../'

// const co = require('co')
// const test = require('tape')

const Browser = PuppetWeb.Browser
const Bridge  = PuppetWeb.Bridge
const PORT = 58788

// const log = require('../../src/npmlog-env')

test('Bridge retry-promise testing', async t => {
  // co(function* () {
    const EXPECTED_RESOLVE = 'Okey'
    const EXPECTED_REJECT  = 'NotTheTime'
    function delayedFactory(timeout) {
      const startTime = Date.now()
      return function() {
        const nowTime = Date.now()
        if (nowTime - startTime > timeout) {
          return Promise.resolve(EXPECTED_RESOLVE)
        }
        return Promise.reject(EXPECTED_REJECT)
      }
    }

    const retryPromise = require('retry-promise').default

    var delay50 = delayedFactory(50)
    await retryPromise({max:1, backoff: 10}, function() {
      return delay50()
    })
    .then(r => {
      t.fail('should not resolved retry-promise here')
    })
    .catch(e => {
      t.is(e, EXPECTED_REJECT, `retry-promise got ${EXPECTED_REJECT} when wait not enough`)
    })

    var anotherDelay50 = delayedFactory(50)
    await retryPromise({max:6, backoff: 10}, function() {
      return anotherDelay50()
    })
    .then(r => {
      t.is(r, EXPECTED_RESOLVE, `retryPromise got "${EXPECTED_RESOLVE}" when wait enough`)
    })
    .catch(e => {
      t.fail(`should not be rejected(with ${e}) when there is enough wait`)
    })

  // })
  // .catch(e => { // REJECTED
  //   t.fail(e)
  // })
  // .then(r => {  // FINALLY
  //   t.end()
  // })
  // .catch(e => { // EXCEPTION
  //   t.fail(e)
  // })
})

test('Bridge smoking test', async t => {
  const browser = new Browser({port: PORT})<commit_msg>use sinon spy to test promise<commit_after>import { test } from 'ava'

import {
  PuppetWeb
  , log
} from '../../'

import { spy } from 'sinon'

const Browser = PuppetWeb.Browser
const Bridge  = PuppetWeb.Bridge

test('Bridge retry-promise testing', async t => {
  // co(function* () {
    const EXPECTED_RESOLVE = 'Okey'
    const EXPECTED_REJECT  = 'NotTheTime'

    function delayedFactory(timeout) {
      const startTime = Date.now()
      return function() {
        const nowTime = Date.now()
        if (nowTime - startTime > timeout) {
          return Promise.resolve(EXPECTED_RESOLVE)
        }
        return Promise.reject(EXPECTED_REJECT)
      }
    }

    const thenSpy = spy()

    const retryPromise = require('retry-promise').default

    var delay500 = delayedFactory(500)
    await retryPromise({max:1, backoff: 1}, function() {
      return delay500()
    })
    .then(r => {
      thenSpy(r)
      // t.fail('should not resolved retry-promise here')
    })
    .catch(e => {
      thenSpy(e)
      console.log(e)
      // t.is(e, EXPECTED_REJECT, `retry-promise got ${EXPECTED_REJECT} when wait not enough`)
    })
    t.true(thenSpy.withArgs(EXPECTED_REJECT).calledOnce, 'should got EXPECTED_REJECT when wait not enough')

    thenSpy.reset()
    var anotherDelay50 = delayedFactory(50)
    await retryPromise({max:6, backoff: 10}, function() {
      return anotherDelay50()
    })
    .then(r => {
      thenSpy(r)
      // t.is(r, EXPECTED_RESOLVE, `retryPromise got "${EXPECTED_RESOLVE}" when wait enough`)
    })
    .catch(e => {
      thenSpy(e)
      // t.fail(`should not be rejected(with ${e}) when there is enough wait`)
    })
    t.true(thenSpy.withArgs(EXPECTED_RESOLVE).calledOnce, 'should got EXPECTED_RESOLVE when wait enough')

  // })
  // .catch(e => { // REJECTED
  //   t.fail(e)
  // })
  // .then(r => {  // FINALLY
  //   t.end()
  // })
  // .catch(e => { // EXCEPTION
  //   t.fail(e)
  // })
})

test('Bridge smoking test', async t => {
  const PORT = 58788

  const browser = new Browser({port: PORT})
{'fraction_non_alphanumeric': 0.15352260778128285, 'fraction_numerical': 0.012618296529968454, 'mean_word_length': 2.5036832412523022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6177153', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1297, 'n_words': 291}