提交 818c513a 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

export default can lead to problems, get rid of them #40

上级 8d67d6f5
......@@ -30,7 +30,7 @@ Wechaty is a Bot Framework for Wechat **Personal** Account that helps you easy c
The shortest wechat bot code in the world: 6 lines JavaScript
```javascript
const Wechaty = require('wechaty')
const { Wechaty } = require('wechaty')
Wechaty.instance() // Singleton
.on('scan', (url, code) => console.log(`Scan QrCode to login: ${code}\n${url}`))
......
#!/usr/bin/env node
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import {
IoClient
......
#!/usr/bin/env node
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import * as os from 'os'
......
#!/usr/bin/env node
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { Wechaty } from '../'
......
......@@ -13,9 +13,9 @@ import {
, log
} from '../../'
import onMessage from './on-message'
import onFriend from './on-friend'
import onRoomJoin from './on-room-join'
import { onMessage } from './on-message'
import { onFriend } from './on-friend'
import { onRoomJoin } from './on-room-join'
const welcome = `
=============== Powered by Wechaty ===============
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import {
Contact
, FriendRequest
, Room
} from '../../'
export default async function onFriend(contact: Contact, request?: FriendRequest): Promise<void> {
export async function onFriend(contact: Contact, request?: FriendRequest): Promise<void> {
try {
if (!request) {
console.log('New friend ' + contact.name() + ' relationship confirmed!')
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import {
Message
, Room
} from '../../'
export default async function onMessage(message: Message): Promise<void> {
export async function onMessage(message: Message): Promise<void> {
try {
const room = message.room()
const sender = message.from()
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import {
Contact
, Room
, Sayable
} from '../../'
export default async function onRoomJoin(
export async function onRoomJoin(
this: Sayable
, room: Room
, inviteeList: Contact[]
......
import {
Config
, Sayable
} from './src/config'
import Contact from './src/contact'
import FriendRequest from './src/friend-request'
import IoClient from './src/io-client'
import Message from './src/message'
import Puppet from './src/puppet'
import PuppetWeb from './src/puppet-web/'
import Room from './src/room'
import UtilLib from './src/util-lib'
import Wechaty from './src/wechaty'
import log from './src/brolog-env'
, log
} from './src/config'
import { Contact } from './src/contact'
import { FriendRequest } from './src/friend-request'
import { IoClient } from './src/io-client'
import { Message } from './src/message'
import { Puppet } from './src/puppet'
import { PuppetWeb } from './src/puppet-web/'
import { Room } from './src/room'
import { UtilLib } from './src/util-lib'
import { Wechaty } from './src/wechaty'
const VERSION = require('./package.json').version
export default Wechaty
export {
Config
, Contact
......@@ -28,24 +26,7 @@ export {
, Room
, Sayable
, UtilLib
, Wechaty
, log // for convenionce use npmlog with environment variable LEVEL
, VERSION
}
Object.assign(Wechaty, {
Config
, Contact
, FriendRequest
, IoClient
, Message
, Puppet
, PuppetWeb
, Room
, UtilLib
, Wechaty
, default: Wechaty
, log // for convenionce use npmlog with environment variable LEVEL
, VERSION
})
exports = Wechaty
}
......@@ -28,5 +28,3 @@ if (levelRegex.test(level)) {
}
export { Brolog }
export default log
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import Config from './config'
import Puppet from './puppet'
test('Config list vars', t => {
// t.truthy(Config.default , 'should export default')
// t.truthy(Config.Config , 'should export Config')
import { Config } from './config'
import { Puppet } from './puppet'
test('important variables', t => {
t.true('head' in Config, 'should exist `head` in Config')
t.true('puppet' in Config, 'should exist `puppet` in Config')
t.true('apihost' in Config, 'should exist `apihost` in Config')
......@@ -23,7 +27,7 @@ test('Config list vars', t => {
t.truthy(Config.CMD_CHROMIUM , 'should export CMD_CHROMIUM')
})
test('Config methods', t => {
test('validApiHost()', t => {
const OK_APIHOSTS = [
'api.wechaty.io'
, 'wechaty.io:8080'
......@@ -45,19 +49,17 @@ test('Config methods', t => {
})
test('Config puppetInstance', t => {
test('puppetInstance()', t => {
t.throws(() => {
Config.puppetInstance()
}, Error, 'should throw when not initialized')
const EXPECTED = {userId: 'test'}
const puppet = <Puppet>EXPECTED
Config.puppetInstance(puppet)
const EXPECTED = <Puppet>{userId: 'test'}
const mockPuppet = EXPECTED
Config.puppetInstance(puppet)
Config.puppetInstance(mockPuppet)
let instance = Config.puppetInstance()
t.deepEqual(instance, <Puppet>EXPECTED, 'should equal with initialized data')
t.deepEqual(instance, EXPECTED, 'should equal with initialized data')
Config.puppetInstance(null)
t.throws(() => {
......@@ -66,7 +68,7 @@ test('Config puppetInstance', t => {
})
test('Config docker mode', t => {
test('isDocker', t => {
t.true('isDocker' in Config, 'should identify docker env by `isDocker`')
if ('C9_PORT' in process.env) {
......
......@@ -6,8 +6,8 @@
import { execSync } from 'child_process'
import * as fs from 'fs'
import Puppet from './puppet'
import log from './brolog-env'
import { Puppet } from './puppet'
import { log } from './brolog-env'
export type PuppetName = 'web' | 'android' | 'ios'
export type HeadName = 'chrome' | 'phantomjs' | 'firefox'
......@@ -192,4 +192,3 @@ export interface Sleepable {
}
export { log }
export default Config
......@@ -10,10 +10,10 @@ import {
Config
, Sayable
} from './config'
import Message from './message'
import UtilLib from './util-lib'
import Wechaty from './wechaty'
import log from './brolog-env'
import { Message } from './message'
import { UtilLib } from './util-lib'
import { Wechaty } from './wechaty'
import { log } from './brolog-env'
type ContactObj = {
address: string
......@@ -290,5 +290,3 @@ export class Contact implements Sayable {
// return []
// }
export default Contact
/**
* Wechat for Bot. Connecting ChatBots
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Interface for puppet
*
......@@ -10,8 +10,8 @@
*
*/
import Config from './config'
import Contact from './contact'
import { Config } from './config'
import { Contact } from './contact'
export abstract class FriendRequest {
......@@ -29,5 +29,3 @@ export abstract class FriendRequest {
public abstract accept(): Promise<void>
}
export default FriendRequest
......@@ -16,11 +16,11 @@
*/
// import Brolog from 'brolog'
import Config from './config'
import Io from './io'
import StateMonitor from './state-monitor'
import Wechaty from './wechaty'
import brolog from './brolog-env'
import { Config } from './config'
import { Io } from './io'
import { StateMonitor } from './state-monitor'
import { Wechaty } from './wechaty'
import { Brolog } from './brolog-env'
export class IoClient {
......@@ -31,7 +31,7 @@ export class IoClient {
constructor(
private token: string = Config.token || Config.DEFAULT_TOKEN
, private log: any = brolog
, private log: any = new Brolog()
) {
if (!log) {
const e = new Error('constructor() log(npmlog/brolog) must be set')
......@@ -299,5 +299,3 @@ export class IoClient {
}
}
}
export default IoClient
......@@ -14,11 +14,11 @@ import * as WebSocket from 'ws'
import {
Config
// WechatyEventName
, log
} from './config'
import StateMonitor from './state-monitor'
import Wechaty from './wechaty'
import log from './brolog-env'
import { StateMonitor } from './state-monitor'
import { Wechaty } from './wechaty'
export type IoSetting = {
wechaty: Wechaty
......@@ -448,5 +448,3 @@ export class Io {
}
}
export default Io
......@@ -6,14 +6,12 @@
* https://github.com/zixia/wechaty
*
*/
import Config from './config'
import Message from './message'
import UtilLib from './util-lib'
import { Config, log } from './config'
import { Message } from './message'
import { UtilLib } from './util-lib'
import log from './brolog-env'
import PuppetWeb from './puppet-web/puppet-web'
import PuppetWebBridge from './puppet-web/bridge'
import { PuppetWeb } from './puppet-web/puppet-web'
import { Bridge as PuppetWebBridge } from './puppet-web/bridge'
export class MediaMessage extends Message {
private bridge: PuppetWebBridge
......@@ -76,5 +74,3 @@ export class MediaMessage extends Message {
})
}
}
export default MediaMessage
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
Config
, log
} from './config'
import Message from './message'
import PuppetWeb from './puppet-web/'
import { Message } from './message'
import { PuppetWeb } from './puppet-web/'
const MOCK_USER_ID = 'TEST-USER-ID'
......
/**
*
* Wechaty: Wechat for Bot. Connecting ChatBots
* Wechaty: * * Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
......@@ -10,12 +10,12 @@ import {
Config
, RecommendInfo
, Sayable
, log
} from './config'
import Contact from './contact'
import Room from './room'
import UtilLib from './util-lib'
import log from './brolog-env'
import { Contact } from './contact'
import { Room } from './room'
import { UtilLib } from './util-lib'
export type MessageRawObj = {
MsgId: string
......@@ -352,7 +352,6 @@ export class Message implements Sayable {
Message.initType()
export default Message
export * from './message-media'
/*
......
......@@ -12,9 +12,9 @@
/* tslint:disable:no-var-requires */
const retryPromise = require('retry-promise').default
import log from '../brolog-env'
import { log } from '../brolog-env'
import PuppetWeb from './puppet-web'
import { PuppetWeb } from './puppet-web'
export class Bridge {
......@@ -382,8 +382,6 @@ export class Bridge {
}
}
export default Bridge
/* tslint:disable:jsdoc-format */
/* tslint:disable:max-line-length */
/**
......
/**
* Wechat for Bot. Connecting ChatBots
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* BrowserCookie
*
......@@ -12,9 +12,9 @@
import * as fs from 'fs'
const arrify = require('arrify')
import log from '../brolog-env'
import { log } from '../config'
import BrowserDriver from './browser-driver'
import { BrowserDriver } from './browser-driver'
/**
* The reason that driverCookie type defined here
......@@ -135,7 +135,7 @@ export class BrowserCookie {
public async load(): Promise<void> {
if (!this.storeFile) {
log.silly('PuppetWebBrowserCookie', 'load() skipped because no session store file')
log.silly('PuppetWebBrowserCookie', 'load() skipped: no session store file')
return
// } else if (this.browser.dead()) {
// throw new Error('loadSession() - browser dead')
......@@ -146,14 +146,14 @@ export class BrowserCookie {
try {
fs.statSync(storeFile).isFile()
} catch (e) {
log.silly('PuppetWebBrowserCookie', 'load() skipped because session store file not exist')
log.silly('PuppetWebBrowserCookie', 'load() skipped: session store file not exist')
return
}
await new Promise((resolve, reject) => {
fs.readFile(storeFile, (err, jsonStr) => {
if (err) {
log.verbose('PuppetWebBrowserCookie', 'load(%s) skipped because error code: %s', this.storeFile, err.code)
log.verbose('PuppetWebBrowserCookie', 'load(%s) skipped: error code: %s', this.storeFile, err.code)
reject()
return
}
......@@ -218,5 +218,3 @@ export class BrowserCookie {
}
}
export default BrowserCookie
/**
* Wechat for Bot. Connecting ChatBots
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* BrowserDriver
*
......@@ -16,8 +16,8 @@ import {
import {
Config
, HeadName
, log
} from '../config'
import log from '../brolog-env'
export class BrowserDriver {
private driver: WebDriver
......@@ -294,5 +294,3 @@ export class BrowserDriver {
public navigate() { return this.driver.navigate() }
public quit() { return this.driver.quit() }
}
export default BrowserDriver
/**
* Wechat for Bot. Connecting ChatBots
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Interface for puppet
*
......@@ -15,15 +15,15 @@ const retryPromise = require('retry-promise').default // https://github.com/ola
import {
Config
, HeadName
} from '../config'
import StateMonitor from '../state-monitor'
import log from '../brolog-env'
} from '../config'
import { StateMonitor } from '../state-monitor'
import { log } from '../brolog-env'
import {
CookieType
, BrowserCookie
} from './browser-cookie'
import BrowserDriver from './browser-driver'
} from './browser-cookie'
import { BrowserDriver } from './browser-driver'
export type BrowserSetting = {
head: HeadName
......@@ -371,5 +371,3 @@ export class Browser extends EventEmitter {
public readCookie() { return this.cookie.read() }
public cleanCookie() { return this.cookie.clean() }
}
export default Browser
......@@ -18,19 +18,19 @@
import {
WatchdogFood
, ScanInfo
} from '../config'
import Contact from '../contact'
, log
} from '../config'
import { Contact } from '../contact'
import {
Message
, MediaMessage
} from '../message'
import log from '../brolog-env'
} from '../message'
import Firer from './firer'
import PuppetWeb from './puppet-web'
import { Firer } from './firer'
import { PuppetWeb } from './puppet-web'
/* tslint:disable:variable-name */
export const PuppetWebEvent = {
export const Event = {
onBrowserDead
, onServerLogin
......@@ -398,5 +398,3 @@ async function onServerMessage(this: PuppetWeb, data): Promise<void> {
return
}
export default PuppetWebEvent
/**
*
* Wechaty: Wechat for Bot. Connecting ChatBots
* Wechaty: * * Wechaty - Wechat for Bot. Connecting ChatBots
*
* Class PuppetWeb Firer
*
......@@ -12,14 +12,13 @@
*/
import { test } from 'ava'
import Firer from './firer'
import { Firer as Firer} from './firer'
test('Firer smoking test', t => {
t.true(true, 'should be true')
})
test('Firer.parseFriendConfirm', t => {
test('parseFriendConfirm()', t => {
const contentList = [
[
'You have added 李卓桓 as your WeChat contact. Start chatting!'
......@@ -41,7 +40,7 @@ test('Firer.parseFriendConfirm', t => {
t.false(result, 'should be falsy for other msg')
})
test('Firer.parseRoomJoin', t => {
test('parseRoomJoin()', t => {
const contentList: [string, string, string[]][] = [
[
`You've invited "李卓桓" to the group chat`
......@@ -83,7 +82,7 @@ test('Firer.parseRoomJoin', t => {
}, Error, 'should throws if message is not expected')
})
test('Firer.parseRoomLeave', t => {
test('parseRoomLeave()', t => {
const contentList = [
[
`You removed "Bruce LEE" from the group chat`
......@@ -107,7 +106,7 @@ test('Firer.parseRoomLeave', t => {
}, Error, 'should throw if message is not expected')
})
test('Firer.parseRoomTopic', t => {
test('parseRoomTopic()', t => {
const contentList = [
[
`"李卓桓.PreAngel" changed the group name to "ding"`
......
/**
*
* Wechaty: Wechat for Bot. Connecting ChatBots
* Wechaty: * * Wechaty - Wechat for Bot. Connecting ChatBots
*
* Class PuppetWeb Firer
*
......@@ -20,18 +20,17 @@ const retryPromise = require('retry-promise').default
import {
// RecommendInfo
} from '../config'
import Contact from '../contact'
log
} from '../config'
import { Contact } from '../contact'
import {
Message
} from '../message'
import log from '../brolog-env'
} from '../message'
import FriendRequest from './friend-request'
import { FriendRequest } from './friend-request'
/* tslint:disable:variable-name */
export const PuppetWebFirer = {
export const Firer = {
checkFriendConfirm
, checkFriendRequest
......@@ -376,4 +375,3 @@ async function checkRoomTopic(m: Message): Promise<void> {
}
}
export default PuppetWebFirer
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import Config from '../config'
import Contact from '../contact'
import Message from '../message'
import Puppet from '../puppet'
import PuppetWebFriendRequest from './friend-request'
import { Config } from '../config'
import { Contact } from '../contact'
import { Message } from '../message'
import { Puppet } from '../puppet'
import { FriendRequest as PuppetWebFriendRequest } from './friend-request'
Config.puppetInstance({
userId: 'xxx'
......
/**
* Wechat for Bot. Connecting ChatBots
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Interface for puppet
*
......@@ -19,13 +19,13 @@
/* tslint:disable:no-var-requires */
const retryPromise = require('retry-promise').default
import Contact from '../contact'
import { Contact } from '../contact'
import {
Config
, RecommendInfo
} from '../config'
import FriendRequest from '../friend-request'
import log from '../brolog-env'
, log
} from '../config'
import { FriendRequest } from '../friend-request'
class PuppetWebFriendRequest extends FriendRequest {
......@@ -38,7 +38,7 @@ class PuppetWebFriendRequest extends FriendRequest {
super()
}
public receive(info: RecommendInfo): PuppetWebFriendRequest {
public receive(info: RecommendInfo): void {
log.verbose('PuppetWebFriendRequest', 'receive(%s)', info)
if (!info || !info.UserName) {
......@@ -62,7 +62,7 @@ class PuppetWebFriendRequest extends FriendRequest {
this.type = 'receive'
return this
return
}
public confirm(contact: Contact): void {
......@@ -132,4 +132,4 @@ class PuppetWebFriendRequest extends FriendRequest {
}
export default PuppetWebFriendRequest
export { PuppetWebFriendRequest as FriendRequest }
......@@ -8,13 +8,10 @@
* https://github.com/zixia/wechaty
*
*/
export { Bridge } from './bridge'
export { Browser } from './browser'
export { PuppetWebEvent as Event } from './event'
export { Server } from './server'
export { Watchdog } from './watchdog'
import PuppetWeb from './puppet-web'
export { PuppetWeb }
export default PuppetWeb
export { Bridge } from './bridge'
export { Browser } from './browser'
export { Event } from './event'
export { FriendRequest } from './friend-request'
export { PuppetWeb } from './puppet-web'
export { Server } from './server'
export { Watchdog } from './watchdog'
......@@ -18,27 +18,27 @@ import {
, HeadName
, ScanInfo
, WatchdogFood
} from '../config'
, log
} from '../config'
import Contact from '../contact'
import { Contact } from '../contact'
// import FriendRequest from '../friend-request'
import Message from '../message'
import Puppet from '../puppet'
import Room from '../room'
import UtilLib from '../util-lib'
import log from '../brolog-env'
import Bridge from './bridge'
import Browser from './browser'
import Event from './event'
import Server from './server'
import Watchdog from './watchdog'
import { Message } from '../message'
import { Puppet } from '../puppet'
import { Room } from '../room'
import { UtilLib } from '../util-lib'
import { Bridge } from './bridge'
import { Browser } from './browser'
import { Event } from './event'
import { Server } from './server'
import { Watchdog } from './watchdog'
export type PuppetWebSetting = {
head?: HeadName
profile?: string
}
const DEFAULT_PUPPET_PORT = 18788 // // W(87) X(88), ascii char code ;-]
const DEFAULT_PUPPET_PORT = 18788 // W(87) X(88), ascii char code ;-]
export class PuppetWeb extends Puppet {
......@@ -87,6 +87,12 @@ export class PuppetWeb extends Puppet {
await this.initBridge()
log.verbose('PuppetWeb', 'initBridge() done')
/**
* state must set to `live`
* before feed Watchdog
*/
this.state.current('live')
const food: WatchdogFood = {
data: 'inited'
, timeout: 120000 // 2 mins for first login
......@@ -94,7 +100,6 @@ export class PuppetWeb extends Puppet {
this.emit('watchdog', food)
log.verbose('PuppetWeb', 'init() done')
this.state.current('live')
return
} catch (e) {
......@@ -112,16 +117,19 @@ export class PuppetWeb extends Puppet {
// this.state.target('dead')
if (this.state.current() === 'dead' && this.state.inprocess()) {
// log.warn('PuppetWeb', 'quit() is called but readyState is `disconnecting`?')
log.warn('PuppetWeb', 'quit() is called but state.current() is `dead` and inprocess() ?')
throw new Error('do not call quit again when quiting')
}
// POISON must feed before state set to `dead` & `inprocess`
this.emit('watchdog', {
data: 'PuppetWeb.quit()',
type: 'POISON'
})
/**
* must feed POISON to Watchdog
* before state set to `dead` & `inprocess`
*/
const food: WatchdogFood = {
data: 'PuppetWeb.quit()'
, type: 'POISON'
}
this.emit('watchdog', food)
this.state.current('dead', false)
......@@ -158,7 +166,7 @@ export class PuppetWeb extends Puppet {
throw e
}
log.verbose('PuppetWeb', 'quit() done')
log.silly('PuppetWeb', 'quit() done')
this.state.current('dead')
}
......@@ -508,5 +516,3 @@ export class PuppetWeb extends Puppet {
})
}
}
export default PuppetWeb
/**
* Wechat for Bot. Connecting ChatBots
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Web Server for puppet
*
......@@ -16,7 +16,7 @@ import * as bodyParser from 'body-parser'
import * as express from 'express'
import { EventEmitter } from 'events'
import log from '../brolog-env'
import { log } from '../config'
export class Server extends EventEmitter {
private express: express.Application
......@@ -171,5 +171,3 @@ export class Server extends EventEmitter {
return
}
}
export default Server
......@@ -69,9 +69,5 @@ g+Xdc4Ag/St5eqgrp95KOlVeepSlb35LAD1Cc91LddTXCYS7+dc4ndQYpgrLU0ru
Sw==
-----END CERTIFICATE-----
`
// module.exports = {
// cert: cert
// , key: key
// }
export { cert, key }
......@@ -20,8 +20,9 @@ import {
, WatchdogFoodName
, log
} from '../config'
import PuppetWeb from './puppet-web'
import Event from './event'
import { PuppetWeb } from './puppet-web'
import { Event } from './event'
/* tslint:disable:variable-name */
export const Watchdog = {
......@@ -62,7 +63,7 @@ function onFeed(this: PuppetWeb, food: WatchdogFood) {
// return
// }
if (this.state.target() === 'dead' || this.state.inprocess()) {
log.info('PuppetWebWatchdog', 'onFeed() is disabled because target state is `dead` or state is inprocess')
log.warn('PuppetWebWatchdog', 'onFeed() is disabled because target state is `dead` or state is inprocess')
return
}
......@@ -191,5 +192,3 @@ function monitorScan(this: PuppetWeb, type: WatchdogFoodName) {
this.lastScanEventTime = Date.now()
}
}
export default Watchdog
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import { PuppetWeb } from '../'
......
/**
* Wechat for Bot. Connecting ChatBots
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Interface for Puppet
*
......@@ -15,11 +15,10 @@ import { EventEmitter } from 'events'
import {
Sayable
} from './config'
import Contact from './contact'
import Message from './message'
import StateMonitor from './state-monitor'
import Room from './room'
// import log from './brolog-env'
import { Contact } from './contact'
import { Message } from './message'
import { StateMonitor } from './state-monitor'
import { Room } from './room'
// type ContactGetterFunc = {
// (id: string): Promise<any>
......@@ -83,5 +82,3 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public abstract contactFind(filterFunc: string): Promise<Contact[]>
public abstract contactRemark(contact: Contact, remark: string): Promise<boolean>
}
export default Puppet
......@@ -14,12 +14,11 @@ const arrify = require('arrify')
import {
Config
, Sayable
, log
} from './config'
import Contact from './contact'
import Message from './message'
import UtilLib from './util-lib'
import log from './brolog-env'
import { Contact } from './contact'
import { Message } from './message'
import { UtilLib } from './util-lib'
type RoomObj = {
id: string
......@@ -436,5 +435,3 @@ export class Room extends EventEmitter implements Sayable {
}
}
export default Room
......@@ -12,7 +12,7 @@
*/
import test from 'ava'
import StateMonitor from './state-monitor'
import { StateMonitor } from './state-monitor'
test('StateMonitor target/current & stable', t => {
const CLIENT_NAME = 'StateMonitorTest'
......
......@@ -34,14 +34,14 @@ export class StateMonitor <A, B>{
*/
public target(newState?: A|B): A|B {
if (newState) {
log.verbose('StateMonitor', 'state.target(%s) <- %s for %s'
log.verbose('StateMonitor', '%s:target(%s) <- %s'
, this._client
, newState
, this._target
, this._client
)
this._target = newState
} else {
log.silly('StateMonitor', 'state.target() is %s of %s', this._target, this._client)
log.silly('StateMonitor', '%s:target() %s', this._client, this._target)
}
return this._target
}
......@@ -52,10 +52,10 @@ export class StateMonitor <A, B>{
*/
public current(newState?: A|B, stable = true): A|B {
if (newState) {
log.verbose('StateMonitor', 'state.current(%s,%s) <- (%s,%s) for %s'
log.verbose('StateMonitor', '%s:current(%s,%s) <- (%s,%s)'
, this._client
, newState, stable
, this._current, this._stable
, this._client
)
/**
......@@ -64,9 +64,9 @@ export class StateMonitor <A, B>{
if (this._current === newState && this._stable === stable
&& stable === false
) {
log.warn('StateMonitor', 'state.current(%s,%s) called but there are already in the same state for %s'
, newState, stable
log.warn('StateMonitor', '%s:current(%s,%s) called but there are already in the same state'
, this._client
, newState, stable
)
const e = new Error('current unchange')
log.verbose('StateMonitor', e.stack)
......@@ -75,7 +75,7 @@ export class StateMonitor <A, B>{
this._current = newState
this._stable = stable
} else {
log.silly('StateMonitor', 'state.current() is %s of %s', this._current, this._client)
log.silly('StateMonitor', '%s:current() %s', this._client, this._current)
}
return this._current
}
......@@ -84,7 +84,7 @@ export class StateMonitor <A, B>{
* does the current state be stable(not inprocess)?
*/
public stable() {
log.silly('StateMonitor', 'state.stable() is %s of %s', this._stable, this._client)
log.silly('StateMonitor', '%s:stable() is %s', this._client, this._stable)
return this._stable
}
......@@ -92,7 +92,7 @@ export class StateMonitor <A, B>{
* does the current state be inprocess(not stable)?
*/
public inprocess() {
log.silly('StateMonitor', 'state.inprocess() is %s of %s', !this._stable, this._client)
log.silly('StateMonitor', '%s:inprocess() %s', this._client, !this._stable)
return !this._stable
}
......@@ -103,5 +103,3 @@ export class StateMonitor <A, B>{
return this._client
}
}
export default StateMonitor
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
UtilLib
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import * as http from 'http'
import log from './brolog-env'
import { log } from './config'
/**
* bug compatible with:
......@@ -8,7 +15,7 @@ import log from './brolog-env'
*/
// import * as ws from 'ws'
class UtilLib {
export class UtilLib {
public static stripHtml(html?: string): string {
if (!html) {
return ''
......@@ -196,5 +203,3 @@ class UtilLib {
}
}
}
export default UtilLib
......@@ -18,18 +18,17 @@ import {
, HeadName
, PuppetName
, Sayable
, log
} from './config'
import Contact from './contact'
import FriendRequest from './friend-request'
import Message from './message'
import Puppet from './puppet'
import PuppetWeb from './puppet-web/'
import Room from './room'
import StateMonitor from './state-monitor'
import UtilLib from './util-lib'
import log from './brolog-env'
import { Contact } from './contact'
import { FriendRequest } from './friend-request'
import { Message } from './message'
import { Puppet } from './puppet'
import { PuppetWeb } from './puppet-web/'
import { Room } from './room'
import { StateMonitor } from './state-monitor'
import { UtilLib } from './util-lib'
export type PuppetSetting = {
head?: HeadName
......@@ -359,5 +358,3 @@ export class Wechaty extends EventEmitter implements Sayable {
})
}
}
export default Wechaty
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
Contact
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import * as fs from 'fs'
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
test('Electron smoke testing', async t => {
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
// import { log } from '../'
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
Bridge
, Browser
, PuppetWeb
// , log
} from '../../src/puppet-web/'
import { spy } from 'sinon'
test('Bridge retry-promise test', 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)
test('retryPromise()', async t => {
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
let 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)
// 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()
let 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')
}
const thenSpy = spy()
const retryPromise = require('retry-promise').default
let delay500 = delayedFactory(500)
await retryPromise({ max: 1, backoff: 1 }, function() {
return delay500()
}).catch(e => {
thenSpy(e)
})
t.true(thenSpy.withArgs(EXPECTED_REJECT).calledOnce, 'should got EXPECTED_REJECT when wait not enough')
thenSpy.reset()
let anotherDelay50 = delayedFactory(50)
await retryPromise({ max: 6, backoff: 10 }, function() {
return anotherDelay50()
})
.then(r => {
thenSpy(r)
})
t.true(thenSpy.withArgs(EXPECTED_RESOLVE).calledOnce, 'should got EXPECTED_RESOLVE when wait enough')
})
test('Bridge smoking test', async t => {
test('WechatyBro.ding()', async t => {
const PORT = 58788
const browser = new Browser()
t.truthy(browser, 'should instanciated a browser')
const mockPuppet = {browser: browser}
const b = new Bridge(mockPuppet as PuppetWeb, PORT)
t.truthy(b, 'should instanciated a bridge with mocked puppet')
const bridge = new Bridge(mockPuppet as PuppetWeb, PORT)
t.truthy(bridge, 'should instanciated a bridge with mocked puppet')
await browser.init()
t.pass('should instanciated a browser')
......@@ -75,20 +69,20 @@ test('Bridge smoking test', async t => {
await browser.open()
t.pass('should open success')
await b.inject()
await bridge.inject()
t.pass('should injected WechatyBro')
const retDing = await b.execute('return WechatyBro.ding()')
const retDing = await bridge.execute('return WechatyBro.ding()')
t.is(retDing, 'dong', 'should got dong after execute WechatyBro.ding()')
// @deprecated
// const retReady = await b.execute('return WechatyBro.isReady()')
// t.is(typeof retReady, 'boolean', 'should got a boolean return after execute WechatyBro.isReady()')
const retCode = await b.proxyWechaty('isLogin')
const retCode = await bridge.proxyWechaty('isLogin')
t.is(typeof retCode, 'boolean', 'should got a boolean after call proxyWechaty(isLogin)')
await b.quit()
await bridge.quit()
t.pass('b.quit()')
await browser.quit()
t.pass('browser.quit()')
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import * as fs from 'fs'
import { test } from 'ava'
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
......@@ -19,11 +26,16 @@ test('Puppet Web Event smoking test', async t => {
let pw = new PuppetWeb({profile: PROFILE})
t.truthy(pw, 'should instantiated a PuppetWeb')
await pw.init()
t.pass('should be inited')
try {
await pw.init()
t.pass('should be inited')
await Event.onBrowserDead.call(pw, 'event unit test')
t.pass('should finish onBrowserDead event process')
await Event.onBrowserDead.call(pw, 'event unit test')
t.pass('should finish onBrowserDead event process')
await pw.quit()
await pw.quit()
} catch (e) {
t.fail('exception: ' + e.message)
throw e
}
})
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
......@@ -13,7 +20,7 @@ import {
// import { spy } from 'sinon'
test.skip('@deprecated Puppet Web Self Message Identification', t => {
test('@deprecated Puppet Web Self Message Identification', t => {
const p = new PuppetWeb()
t.truthy(p, 'should instantiated a PuppetWeb')
......@@ -29,7 +36,7 @@ test.skip('@deprecated Puppet Web Self Message Identification', t => {
* static variable `Contact.puppet` will be changed
* when `PuppteWeb.init()` and `PuppteWeb.quit()`
*/
test.serial('PuppetWeb login/logout events', async t => {
test.serial('login/logout events', async t => {
let pw = new PuppetWeb()
t.truthy(pw, 'should instantiated a PuppetWeb')
......@@ -56,7 +63,7 @@ test.serial('PuppetWeb login/logout events', async t => {
await pw.quit()
})
test.serial('PuppetWeb server/browser communication', async t => {
test.only('server/browser socketio ding', async t => {
let pw = new PuppetWeb()
t.truthy(pw, 'should instantiated a PuppetWeb')
......@@ -71,13 +78,15 @@ test.serial('PuppetWeb server/browser communication', async t => {
const ret = await dingSocket(pw.server)
t.is(ret, EXPECTED_DING_DATA, 'should got EXPECTED_DING_DATA after resolved dingSocket()')
} catch (e) {
t.fail(e.message)
t.fail(e && e.message || e || 'unknown exception???')
}
await pw.quit()
return
/////////////////////////////////////////////////////////////////////////////
function dingSocket(server: Server) {
const maxTime = 60000 // 60s
const waitTime = 3000
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import * as https from 'https'
import * as sinon from 'sinon'
import {
// PuppetWeb
// , Message
UtilLib
, log
} from '../../'
} from '../../'
import {
// PuppetWeb
Server
} from '../../src/puppet-web/'
} from '../../src/puppet-web/'
test('PuppetWebServer basic tests', async t => {
test('create & close', async t => {
const port = await UtilLib.getPort(18788)
const s = new Server(port)
t.is(typeof s, 'object', 'PuppetWebServer instance created')
......@@ -49,19 +53,25 @@ test('PuppetWebServer basic tests', async t => {
await s.quit()
})
test('PuppetWebServer smoke testing', async t => {
test('http ding', async t => {
const port = await UtilLib.getPort(18788)
const server = new Server(port)
t.truthy(server, 'new server instance')
await server.init()
t.pass('server:' + port + ' inited')
try {
await server.init()
t.pass('server:' + port + ' inited')
const retHttps = await dingHttps()
t.is(retHttps , 'dong', 'ding https got dong')
const retHttps = await dingHttps()
t.is(retHttps , 'dong', 'ding https got dong')
await server.quit()
await server.quit()
} catch (e) {
t.fail('smoke testing exception: ' + e.message)
throw e
}
return // The following is help functions only
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
/* tslint:disable:no-var-requires */
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
Room
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import * as WebDriver from 'selenium-webdriver'
import * as express from 'express'
import * as http from 'http'
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
// import {
......@@ -6,8 +13,8 @@ import { test } from 'ava'
// } from 'selenium-webdriver'
import {
Bridge as PuppetWebBridge
, Browser as PuppetWebBrowser
Bridge
, Browser
, PuppetWeb
} from '../src/puppet-web/'
......@@ -19,38 +26,38 @@ import {
* there will have race conditions for the conflict of `getBrowserPids()`
*/
test.serial('WebDriver process create & quit test', async t => {
const b = new PuppetWebBrowser()
t.truthy(b, 'should instanciate a browser')
const browser = new Browser()
t.truthy(browser, 'should instanciate a browser')
await b.init()
await browser.init()
t.pass('should be inited successful')
await b.open()
await browser.open()
t.pass('should open successful')
let pids = await b.getBrowserPids()
let pids = await browser.getBrowserPids()
t.truthy(pids.length > 0, 'should exist browser process after b.open()')
await b.quit()
await browser.quit()
t.pass('quited')
pids = await b.getBrowserPids()
pids = await browser.getBrowserPids()
t.is(pids.length, 0, 'no driver process after quit')
})
test.serial('WebDriver smoke testing', async t => {
const wb = new PuppetWebBrowser()
t.truthy(wb, 'Browser instnace')
const browser = new Browser()
t.truthy(browser, 'Browser instnace')
const mockPuppet = <PuppetWeb>{browser: wb}
const bridge = new PuppetWebBridge(mockPuppet, 8788)
const mockPuppet = <PuppetWeb>{browser: browser}
const bridge = new Bridge(mockPuppet, 8788)
t.truthy(bridge, 'Bridge instnace')
let driver // for help function `execute`
const m = (await wb.getBrowserPids()).length
const m = (await browser.getBrowserPids()).length
t.is(m, 0, 'should has no browser process before get()')
driver = await wb.driver.init()
driver = await browser.driver.init()
t.truthy(driver, 'should init driver success')
const injectio = bridge.getInjectio()
......@@ -59,7 +66,7 @@ test.serial('WebDriver smoke testing', async t => {
await driver.get('https://wx.qq.com/')
t.pass('should open wx.qq.com')
const n = (await wb.getBrowserPids()).length
const n = (await browser.getBrowserPids()).length
t.truthy(n > 0, 'should exist browser process after get()')
const retAdd = await driverExecute('return 1+1')
......@@ -69,7 +76,7 @@ test.serial('WebDriver smoke testing', async t => {
t.truthy(retInject, 'should return a object contains status of inject operation')
t.is(retInject.code, 200, 'should got code 200 for a success wechaty inject')
await wb.quit()
await browser.quit()
return
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册