<commit_before>#!/usr/bin/env ts-node
/**
 *   Wechaty - https://github.com/chatie/wechaty
 *
 *   @copyright 2016-2018 <NAME> <<EMAIL>>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
// tslint:disable:no-shadowed-variable
import * as test  from 'blue-tape'
import * as sinon from 'sinon'
const sinonTest   = require('sinon-test')(sinon, {
  useFakeTimers: {  // https://github.com/sinonjs/lolex
    shouldAdvanceTime : true,
    advanceTimeDelta  : 10,
  },
})

// import { log }    from '../../src/config'
// log.level('silly')

import Profile      from '../profile'
import Wechaty      from '../wechaty'

import {
  Contact,
}                   from '../contact'

import PuppetPuppeteer  from './puppet-puppeteer'
import Bridge           from './bridge'
import Event            from './event'

test('Puppet smoke testing', async t => {
  const profile = new Profile(Math.random().toString(36).substr(2, 5))
  const wechaty = new Wechaty()

  const p = new PuppetPuppeteer({
    profile,
    wechaty,
  })

  t.ok(p.state.off(), 'should be OFF state after instanciate')
  p.state.on('pending')
  t.ok(p.state.on(), 'should be ON state after set')
  t.ok(p.state.pending(), 'should be pending state after set')
})

test('login/logout events', sinonTest(async function (t: test.Test) {
  const sandbox = sinon.createSandbox()
  sandbox.stub(Contact, 'findAll')
        .onFirstCall().resolves([])
        .onSecondCall().resolves([1])
        .resolves([1, 2])

  sandbox.stub(Event, 'onScan') // block the scan event to prevent reset logined user

  sandbox.stub(Bridge.prototype,    'getUserName').resolves('mockedUserName')
  sandbox.stub(PuppetPuppeteer.prototype, 'contactPayload').resolves({
    NickName: 'mockedNickName',
    UserName: 'mockedUserName',
  })

  try {
    const profile = new Profile()
    const wechaty = new Wechaty()

    const pw = new PuppetPuppeteer({
      profile,
      wechaty,
    })
    t.ok(pw, 'should instantiated a PuppetPuppeteer')

    // FIXME: do not modify global instance
    Contact.puppet = pw

    await pw.start()
    t.pass('should be inited')
    t.is(pw.logonoff() , false  , 'should be not logined')

    const EXPECTED_CHIPER = 'loginFired'
    const loginPromise = new Promise(r => pw.once('login', _ => r(EXPECTED_CHIPER)))
    pw.bridge.emit('login', 'TestPuppetPuppeteer')
    t.is(await loginPromise, EXPECTED_CHIPER, 'should fired login event')
    t.is(pw.logonoff(), true  , 'should be logined')

    t.ok((pw.bridge.getUserName as any).called, 'bridge.getUserName should be called')
    t.ok((pw.contactPayload as any).called,         'pw.getContact should be called')

    t.ok((Contact.findAll as any).called,       'contactFind stub should be called')
    t.is((Contact.findAll as any).callCount, 4, 'should call stubContactFind 4 times')

    const logoutPromise = new Promise(resolve => pw.once('logout', _ => resolve('logoutFired')))
    pw.bridge.emit('logout')
    t.is(await logoutPromise, 'logoutFired', 'should fire logout event')
    t.is(pw.logonoff(), false, 'should be logouted')

    await pw.stop()
    profile.destroy()<commit_msg>fix init puppet in unit test<commit_after>#!/usr/bin/env ts-node
/**
 *   Wechaty - https://github.com/chatie/wechaty
 *
 *   @copyright 2016-2018 <NAME> <<EMAIL>>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */
// tslint:disable:no-shadowed-variable
import * as test  from 'blue-tape'
import * as sinon from 'sinon'
const sinonTest   = require('sinon-test')(sinon, {
  useFakeTimers: {  // https://github.com/sinonjs/lolex
    shouldAdvanceTime : true,
    advanceTimeDelta  : 10,
  },
})

// import { log }    from '../../src/config'
// log.level('silly')

import Profile      from '../profile'
import Wechaty      from '../wechaty'

// import {
//   Contact as GlobalContact,
// }                             from '../contact'

import PuppetPuppeteer  from './puppet-puppeteer'
import Bridge           from './bridge'
import Event            from './event'

test('Puppet smoke testing', async t => {
  const profile = new Profile(Math.random().toString(36).substr(2, 5))
  const wechaty = new Wechaty()

  const puppet = new PuppetPuppeteer({
    profile,
    wechaty,
  })
  ;
  (wechaty as any).initPuppetAccessory(puppet)

  t.ok(puppet.state.off(), 'should be OFF state after instanciate')
  puppet.state.on('pending')
  t.ok(puppet.state.on(), 'should be ON state after set')
  t.ok(puppet.state.pending(), 'should be pending state after set')
})

test('login/logout events', sinonTest(async function (t: test.Test) {
  const sandbox = sinon.createSandbox()
  try {
    const profile = new Profile()
    const wechaty = new Wechaty()

    const puppet = new PuppetPuppeteer({
      profile,
      wechaty,
    })
    ;
    (wechaty as any).initPuppetAccessory(puppet)

    t.ok(puppet, 'should instantiated a PuppetPuppeteer')

    sandbox.stub(wechaty.Contact, 'findAll')
      .onFirstCall().resolves([])
      .onSecondCall().resolves([1])
      .resolves([1, 2])

    sandbox.stub(Event, 'onScan') // block the scan event to prevent reset logined user

    sandbox.stub(Bridge.prototype,    'getUserName').resolves('mockedUserName')
    sandbox.stub(puppet, 'contactRawPayload').resolves({
      NickName: 'mockedNickName',
      UserName: 'mockedUserName',
    })

    await puppet.start()
    t.pass('should be inited')
    t.is(puppet.logonoff() , false  , 'should be not logined')

    const EXPECTED_CHIPER = 'loginFired'
    const loginPromise = new Promise(r => puppet.once('login', _ => r(EXPECTED_CHIPER)))
    puppet.bridge.emit('login', 'TestPuppetPuppeteer')
    t.is(await loginPromise, EXPECTED_CHIPER, 'should fired login event')
    t.is(puppet.logonoff(), true  , 'should be logined')

    t.ok((puppet.bridge.getUserName as any).called, 'bridge.getUserName should be called')
    t.ok((puppet.contactRawPayload as any).called,  'puppet.contactRawPayload should be called')

    t.ok((wechaty.Contact.findAll as any).called,       'contactFind stub should be called')
    t.is((wechaty.Contact.findAll as any).callCount, 4, 'should call stubContactFind 4 times')

    const logoutPromise = new Promise(resolve => puppet.once('logout', _ => resolve('logoutFired')))
    puppet.bridge.emit('logout')
    t.is(await logoutPromise, 'logoutFired', 'should fire logout event')
    t.is(puppet.logonoff(), false, 'should be logouted')

    await puppet.stop()
    profile.destroy()
{'fraction_non_alphanumeric': 0.13289702150394017, 'fraction_numerical': 0.006143982903699746, 'mean_word_length': 3.2715345122646893, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 4, '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': '7050284', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2449, 'n_words': 656}