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

get rid of the global puppetInstance (#518)

上级 89fe5e04
......@@ -23,7 +23,7 @@ import * as test from 'blue-tape'
// const sinonTest = require('sinon-test')(sinon)
import { config } from './config'
import { Puppet } from './puppet'
// import { Puppet } from './puppet'
test('important variables', async t => {
t.true('puppet' in config, 'should exist `puppet` in Config')
......@@ -59,28 +59,28 @@ test('validApiHost()', async t => {
})
test('puppetInstance()', async t => {
// BUG Compitable with Win32 CI
// global instance infected across unit tests... :(
const bak = config.puppetInstance()
// test('puppetInstance()', async t => {
// // BUG Compitable with Win32 CI
// // global instance infected across unit tests... :(
// const bak = config.puppetInstance()
config.puppetInstance(null)
t.throws(() => {
config.puppetInstance()
}, Error, 'should throw when not initialized')
config.puppetInstance(bak)
// config.puppetInstance(null)
// t.throws(() => {
// config.puppetInstance()
// }, Error, 'should throw when not initialized')
// config.puppetInstance(bak)
const EXPECTED: Puppet = {userId: 'test'} as any
const mockPuppet = EXPECTED
// const EXPECTED: Puppet = {userId: 'test'} as any
// const mockPuppet = EXPECTED
config.puppetInstance(mockPuppet)
const instance = config.puppetInstance()
t.deepEqual(instance, EXPECTED, 'should equal with initialized data')
// config.puppetInstance(mockPuppet)
// const instance = config.puppetInstance()
// t.deepEqual(instance, EXPECTED, 'should equal with initialized data')
config.puppetInstance(null)
t.throws(() => {
config.puppetInstance()
}, Error, 'should throw after set to null')
// config.puppetInstance(null)
// t.throws(() => {
// config.puppetInstance()
// }, Error, 'should throw after set to null')
config.puppetInstance(bak)
})
// config.puppetInstance(bak)
// })
......@@ -24,7 +24,7 @@ import * as readPkgUp from 'read-pkg-up'
import * as Raven from 'raven'
import { log } from 'brolog'
import Puppet from './puppet'
// import Puppet from './puppet'
const pkg = readPkgUp.sync({ cwd: __dirname }).pkg
export const VERSION = pkg.version
......@@ -116,7 +116,7 @@ export class Config {
public httpPort = process.env['PORT'] || process.env['WECHATY_PORT'] || DEFAULT_SETTING.DEFAULT_PORT
public docker = !!(process.env['WECHATY_DOCKER'])
private _puppetInstance: Puppet | null = null
// private _puppetInstance: Puppet | null = null
constructor() {
log.verbose('Config', 'constructor()')
......@@ -126,29 +126,29 @@ export class Config {
/**
* 5. live setting
*/
public puppetInstance(): Puppet
public puppetInstance(empty: null): void
public puppetInstance(instance: Puppet): void
// public puppetInstance(): Puppet
// public puppetInstance(empty: null): void
// public puppetInstance(instance: Puppet): void
public puppetInstance(instance?: Puppet | null): Puppet | void {
// public puppetInstance(instance?: Puppet | null): Puppet | void {
if (typeof instance === 'undefined') {
if (!this._puppetInstance) {
throw new Error('no puppet instance')
}
return this._puppetInstance
// if (typeof instance === 'undefined') {
// if (!this._puppetInstance) {
// throw new Error('no puppet instance')
// }
// return this._puppetInstance
} else if (instance === null) {
log.verbose('Config', 'puppetInstance(null)')
this._puppetInstance = null
return
}
// } else if (instance === null) {
// log.verbose('Config', 'puppetInstance(null)')
// this._puppetInstance = null
// return
// }
log.verbose('Config', 'puppetInstance(%s)', instance.constructor.name)
this._puppetInstance = instance
return
// log.verbose('Config', 'puppetInstance(%s)', instance.constructor.name)
// this._puppetInstance = instance
// return
}
// }
public gitRevision(): string | null {
const dotGitPath = path.join(__dirname, '..', '.git') // only for ts-node, not for dist
......
......@@ -18,18 +18,20 @@
* @ignore
*/
import {
config,
// config,
Raven,
Sayable,
log,
} from './config'
} from './config'
import {
Message,
MediaMessage,
} from './message'
import Misc from './misc'
import PuppetWeb from './puppet-web'
import Wechaty from './wechaty'
} from './message'
import Misc from './misc'
import PuppetAccessory from './puppet-accessory'
import Wechaty from './wechaty'
import PuppetWeb from './puppet-web/'
export interface ContactObj {
address: string,
......@@ -104,7 +106,7 @@ const specialContactList: string[] = [
* `Contact` is `Sayable`,
* [Examples/Contact-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/contact-bot.ts}
*/
export class Contact implements Sayable {
export class Contact extends PuppetAccessory implements Sayable {
private static pool = new Map<string, Contact>()
public obj: ContactObj | null
......@@ -117,6 +119,7 @@ export class Contact implements Sayable {
constructor(
public readonly id: string,
) {
super()
log.silly('Contact', `constructor(${id})`)
if (typeof id !== 'string') {
......@@ -277,7 +280,7 @@ export class Contact implements Sayable {
/**
* must be string because we need inject variable value
* into code as variable name
* into code as variable namespecialContactList
*/
let filterFunction: string
......@@ -291,7 +294,7 @@ export class Contact implements Sayable {
}
try {
const contactList = await config.puppetInstance()
const contactList = await this.puppet // config.puppetInstance()
.contactFind(filterFunction)
await Promise.all(contactList.map(c => c.ready()))
......@@ -406,25 +409,25 @@ export class Contact implements Sayable {
return this.obj && this.obj.alias || null
}
return config.puppetInstance()
.contactAlias(this, newAlias)
.then(ret => {
if (ret) {
if (this.obj) {
this.obj.alias = newAlias
} else {
log.error('Contact', 'alias() without this.obj?')
}
return this.puppet // config.puppetInstance()
.contactAlias(this, newAlias)
.then(ret => {
if (ret) {
if (this.obj) {
this.obj.alias = newAlias
} else {
log.warn('Contact', 'alias(%s) fail', newAlias)
log.error('Contact', 'alias() without this.obj?')
}
return ret
})
.catch(e => {
log.error('Contact', 'alias(%s) rejected: %s', newAlias, e.message)
Raven.captureException(e)
return false // fail safe
})
} else {
log.warn('Contact', 'alias(%s) fail', newAlias)
}
return ret
})
.catch(e => {
log.error('Contact', 'alias(%s) rejected: %s', newAlias, e.message)
Raven.captureException(e)
return false // fail safe
})
}
/**
......@@ -542,9 +545,9 @@ export class Contact implements Sayable {
}
try {
const hostname = await (config.puppetInstance() as PuppetWeb).hostname()
const hostname = await (/* config.puppetInstance() */ this.puppet as PuppetWeb ).hostname()
const avatarUrl = `http://${hostname}${this.obj.avatar}&type=big` // add '&type=big' to get big image
const cookies = await (config.puppetInstance() as PuppetWeb).cookies()
const cookies = await (/* config.puppetInstance() */ this.puppet as PuppetWeb).cookies()
log.silly('Contact', 'avatar() url: %s', avatarUrl)
return Misc.urlStream(avatarUrl, cookies)
......@@ -599,9 +602,9 @@ export class Contact implements Sayable {
}
if (!contactGetter) {
log.silly('Contact', 'get contact via ' + config.puppetInstance().constructor.name)
contactGetter = config.puppetInstance()
.getContact.bind(config.puppetInstance())
log.silly('Contact', 'get contact via ' + /* config.puppetInstance() */ this.puppet.constructor.name)
contactGetter = /* config.puppetInstance() */ this.puppet
.getContact.bind(/* config.puppetInstance() */ this.puppet)
}
if (!contactGetter) {
throw new Error('no contatGetter')
......@@ -648,8 +651,8 @@ export class Contact implements Sayable {
* const isSelf = contact.self()
*/
public self(): boolean {
const userId = config.puppetInstance()
.userId
const userId = this.puppet // config.puppetInstance()
.userId
const selfId = this.id
......
......@@ -19,10 +19,11 @@
*/
import {
config,
// config,
log,
} from './config'
import Contact from './contact'
} from './config'
import Contact from './contact'
import PuppetAccessory from './puppet-accessory'
/**
* Send, receive friend request, and friend confirmation events.
......@@ -33,18 +34,19 @@ import Contact from './contact'
*
* [Examples/Friend-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/friend-bot.ts}
*/
export abstract class FriendRequest {
export abstract class FriendRequest extends PuppetAccessory {
public contact: Contact
public hello: string
public type: 'send' | 'receive' | 'confirm'
constructor() {
super()
log.verbose('FriendRequest', 'constructor()')
if (!config.puppetInstance()) {
throw new Error('no Config.puppetInstance() instanciated')
}
// if (!config.puppetInstance()) {
// throw new Error('no Config.puppetInstance() instanciated')
// }
}
public abstract send(contact: Contact, hello: string): Promise<boolean>
......
......@@ -23,12 +23,14 @@ import * as test from 'blue-tape'
// const sinonTest = require('sinon-test')(sinon)
import {
config,
// config,
log,
} from './config'
import Contact from './contact'
import Message from './message'
import Profile from './profile'
import PuppetWeb from './puppet-web/'
import Room from './room'
const MOCK_USER_ID = 'TEST-USER-ID'
......@@ -36,7 +38,8 @@ const puppet = new PuppetWeb({
profile: new Profile(),
})
puppet.userId = MOCK_USER_ID
config.puppetInstance(puppet)
// config.puppetInstance(puppet)
Message.puppet = puppet
test('constructor()', async t => {
/* tslint:disable:max-line-length */
......@@ -100,8 +103,12 @@ test('ready()', async t => {
})
}
config.puppetInstance()
.getContact = mockGetContact
// config.puppetInstance()
// .getContact = mockGetContact
Room.puppet = Contact.puppet = Message.puppet = {
...puppet,
getContact: mockGetContact,
} as any
const m = new Message(rawData)
......@@ -138,7 +145,8 @@ test('findAll()', async t => {
})
test('self()', async t => {
config.puppetInstance(puppet)
// config.puppetInstance(puppet)
Room.puppet = Contact.puppet = Message.puppet = puppet
const m = new Message()
m.from(MOCK_USER_ID)
......@@ -186,14 +194,17 @@ test('mentioned()', async t => {
})
}
let puppet1
try {
puppet1 = config.puppetInstance()
puppet1.getContact = mockContactGetter
} catch (err) {
puppet1 = { getContact: mockContactGetter }
config.puppetInstance(puppet1)
}
// let puppet1
// try {
// puppet1 = config.puppetInstance()
// puppet1.getContact = mockContactGetter
// } catch (err) {
// puppet1 = { getContact: mockContactGetter }
// config.puppetInstance(puppet1)
// }
Contact.puppet = Room.puppet = Message.puppet = {
getContact: mockContactGetter,
} as any
const msg11 = new Message(rawObj11)
const room11 = msg11.room()
if (room11) {
......
......@@ -25,15 +25,17 @@ import {
import * as mime from 'mime'
import {
config,
// config,
Raven,
Sayable,
log,
} from './config'
} from './config'
import Contact from './contact'
import Room from './room'
import Misc from './misc'
import PuppetAccessory from './puppet-accessory'
import Contact from './contact'
import Room from './room'
import Misc from './misc'
import PuppetWeb from './puppet-web/puppet-web'
import Bridge from './puppet-web/bridge'
......@@ -55,7 +57,7 @@ export type TypeName = 'attachment'
* `Message` is `Sayable`,
* [Examples/Ding-Dong-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/ding-dong-bot.ts}
*/
export class Message implements Sayable {
export class Message extends PuppetAccessory implements Sayable {
/**
* @private
*/
......@@ -87,6 +89,7 @@ export class Message implements Sayable {
* @private
*/
constructor(public rawObj?: MsgRawObj) {
super()
this._counter = Message.counter++
log.silly('Message', 'constructor() SN:%d', this._counter)
......@@ -243,8 +246,8 @@ export class Message implements Sayable {
}
}
return config.puppetInstance()
.send(m)
return this.puppet // config.puppetInstance()
.send(m)
}
/**
......@@ -403,7 +406,7 @@ export class Message implements Sayable {
* }
*/
public self(): boolean {
const userId = config.puppetInstance()
const userId = this.puppet // config.puppetInstance()
.userId
const fromId = this.obj.from
......@@ -690,7 +693,7 @@ export class MediaMessage extends Message {
}
// FIXME: decoupling needed
this.bridge = (config.puppetInstance() as PuppetWeb)
this.bridge = (/* config.puppetInstance() */ this.puppet as PuppetWeb)
.bridge
}
......@@ -886,7 +889,7 @@ export class MediaMessage extends Message {
try {
await this.ready()
// FIXME: decoupling needed
const cookies = await (config.puppetInstance() as PuppetWeb).cookies()
const cookies = await (/* config.puppetInstance() */ this.puppet as PuppetWeb).cookies()
if (!this.obj.url) {
throw new Error('no url')
}
......@@ -983,7 +986,7 @@ export class MediaMessage extends Message {
*/
public async forward(to: Room|Contact): Promise<boolean> {
try {
const ret = await config.puppetInstance().forward(this, to)
const ret = await /* config.puppetInstance() */ this.puppet.forward(this, to)
return ret
} catch (e) {
log.error('Message', 'forward(%s) exception: %s', to, e)
......
......@@ -29,7 +29,7 @@ const EXPECTED_PUPPET2 = {p: 2} as any as Puppet
test('PuppetAccessory smoke testing', async t => {
class FixtureClass extends PuppetAccessory {}
t.ok(FixtureClass.puppet === undefined, 'should be undefined initialy for static puppet')
t.throws(() => FixtureClass.puppet, 'should throw if read static puppet before initialize')
const c = new FixtureClass()
t.throws(() => c.puppet, 'should throw if read instance puppet before initialization')
......
import { EventEmitter } from 'events'
import { log } from './config'
import { Puppet } from './puppet'
export abstract class PuppetAccessory {
public static puppet?: Puppet
export abstract class PuppetAccessory extends EventEmitter {
/**
* 1. Static puppet property
*/
private static _puppet?: Puppet
public static set puppet(puppet: Puppet) {
log.verbose('PuppetAssessory', 'static set puppet()')
this._puppet = puppet
}
public static get puppet(): Puppet {
log.silly('PuppetAssessory', 'static get puppet()')
if (this._puppet) {
log.silly('PuppetAssessory', 'static get puppet() from instance properties')
return this._puppet
}
throw new Error('static puppet not found')
}
/**
* 2. Instance puppet property
*/
private _puppet?: Puppet
public set puppet(puppet: Puppet) {
log.verbose('PuppetAssessory', 'set puppet()')
this._puppet = puppet
}
public get puppet() {
public get puppet(): Puppet {
log.silly('PuppetAssessory', 'get puppet()')
if (this._puppet) {
......@@ -19,13 +40,7 @@ export abstract class PuppetAccessory {
return this._puppet
}
const staticPuppet = (this.constructor as any).puppet
if (staticPuppet) {
log.silly('PuppetAssessory', 'get puppet() from static properties')
return staticPuppet
}
throw new Error('puppet not found')
return (this.constructor as any).puppet
}
}
......
......@@ -177,7 +177,7 @@ function onLogout(this: PuppetWeb, data) {
async function onMessage(
this: PuppetWeb,
obj: MsgRawObj,
obj: MsgRawObj,
): Promise<void> {
let m = new Message(obj)
......
......@@ -22,15 +22,18 @@ import * as test from 'blue-tape'
// import * as sinon from 'sinon'
// const sinonTest = require('sinon-test')(sinon)
import config from '../config'
// import config from '../config'
import Contact from '../contact'
import Message from '../message'
import Puppet from '../puppet'
import PuppetWebFriendRequest from './friend-request'
config.puppetInstance({
PuppetWebFriendRequest.puppet = {
userId: 'xxx',
} as any as Puppet)
} as any as Puppet
// config.puppetInstance({
// userId: 'xxx',
// } as any as Puppet)
test('PuppetWebFriendRequest.receive smoke testing', async t => {
/* tslint:disable:max-line-length */
......
......@@ -31,7 +31,7 @@ const retryPromise = require('retry-promise').default
import { Contact } from '../contact'
import {
config,
// config,
log,
} from '../config'
import FriendRequest from '../friend-request'
......@@ -114,7 +114,7 @@ export class PuppetWebFriendRequest extends FriendRequest {
this.hello = hello
}
return config.puppetInstance()
return this.puppet // config.puppetInstance()
.friendRequestSend(contact, hello)
}
......@@ -130,8 +130,8 @@ export class PuppetWebFriendRequest extends FriendRequest {
throw new Error('request is not a `receive` type. it is a ' + this.type + ' type')
}
const ret = await config.puppetInstance()
.friendRequestAccept(this.contact, this.ticket)
const ret = await this.puppet // config.puppetInstance()
.friendRequestAccept(this.contact, this.ticket)
const max = 20
const backoff = 300
......
......@@ -25,7 +25,7 @@ import {
Bridge,
Event,
PuppetWeb,
} from './index'
} from './'
test('PuppetWeb Module Exports', async t => {
t.ok(PuppetWeb , 'should export PuppetWeb')
......
......@@ -17,20 +17,19 @@
*
* @ignore
*/
import { EventEmitter } from 'events'
import {
config,
// config,
Raven,
Sayable,
log,
} from './config'
import Contact from './contact'
} from './config'
import Contact from './contact'
import {
Message,
MediaMessage,
} from './message'
import Misc from './misc'
} from './message'
import Misc from './misc'
import PuppetAccessory from './puppet-accessory'
interface RoomObj {
id: string,
......@@ -82,7 +81,7 @@ export interface MemberQueryFilter {
* `Room` is `Sayable`,
* [Examples/Room-Bot]{@link https://github.com/Chatie/wechaty/blob/master/examples/room-bot.ts}
*/
export class Room extends EventEmitter implements Sayable {
export class Room extends PuppetAccessory implements Sayable {
private static pool = new Map<string, Room>()
private dirtyObj: RoomObj | null // when refresh, use this to save dirty data for query
......@@ -141,8 +140,8 @@ export class Room extends EventEmitter implements Sayable {
}
if (!contactGetter) {
contactGetter = config.puppetInstance()
.getContact.bind(config.puppetInstance())
contactGetter = this.puppet // config.puppetInstance()
.getContact.bind(/* config.puppetInstance() */ this.puppet)
}
if (!contactGetter) {
throw new Error('no contactGetter')
......@@ -247,8 +246,8 @@ export class Room extends EventEmitter implements Sayable {
m.room(this)
return config.puppetInstance()
.send(m)
return this.puppet // config.puppetInstance()
.send(m)
}
public on(event: 'leave', listener: (this: Room, leaver: Contact) => void): this
......@@ -427,8 +426,8 @@ export class Room extends EventEmitter implements Sayable {
throw new Error('contact not found')
}
const n = config.puppetInstance()
.roomAdd(this, contact)
const n = this.puppet // config.puppetInstance()
.roomAdd(this, contact)
return n
}
......@@ -455,9 +454,9 @@ export class Room extends EventEmitter implements Sayable {
if (!contact) {
throw new Error('contact not found')
}
const n = await config.puppetInstance()
.roomDel(this, contact)
.then(_ => this.delLocal(contact))
const n = await this.puppet // config.puppetInstance()
.roomDel(this, contact)
.then(_ => this.delLocal(contact))
return n
}
......@@ -536,14 +535,14 @@ export class Room extends EventEmitter implements Sayable {
return Misc.plainText(this.obj ? this.obj.topic : '')
}
config.puppetInstance()
.roomTopic(this, newTopic)
.catch(e => {
log.warn('Room', 'topic(newTopic=%s) exception: %s',
newTopic, e && e.message || e,
)
Raven.captureException(e)
})
this.puppet // config.puppetInstance()
.roomTopic(this, newTopic)
.catch(e => {
log.warn('Room', 'topic(newTopic=%s) exception: %s',
newTopic, e && e.message || e,
)
Raven.captureException(e)
})
if (!this.obj) {
this.obj = <RoomObj>{}
......@@ -820,13 +819,13 @@ export class Room extends EventEmitter implements Sayable {
throw new Error('contactList not found')
}
return config.puppetInstance()
.roomCreate(contactList, topic)
.catch(e => {
log.error('Room', 'create() exception: %s', e && e.stack || e.message || e)
Raven.captureException(e)
throw e
})
return this.puppet // config.puppetInstance()
.roomCreate(contactList, topic)
.catch(e => {
log.error('Room', 'create() exception: %s', e && e.stack || e.message || e)
Raven.captureException(e)
throw e
})
}
/**
......@@ -862,13 +861,13 @@ export class Room extends EventEmitter implements Sayable {
throw new Error('unsupport topic type')
}
const roomList = await config.puppetInstance()
.roomFind(filterFunction)
.catch(e => {
log.verbose('Room', 'findAll() rejected: %s', e.message)
Raven.captureException(e)
return [] as Room[] // fail safe
})
const roomList = await this.puppet // config.puppetInstance()
.roomFind(filterFunction)
.catch(e => {
log.verbose('Room', 'findAll() rejected: %s', e.message)
Raven.captureException(e)
return [] as Room[] // fail safe
})
await Promise.all(roomList.map(room => room.ready()))
// for (let i = 0; i < roomList.length; i++) {
......@@ -919,7 +918,7 @@ export class Room extends EventEmitter implements Sayable {
public owner(): Contact | null {
const ownerUin = this.obj && this.obj.ownerUin
const user = config.puppetInstance()
const user = this.puppet // config.puppetInstance()
.user
if (user && user.get('uin') === ownerUin) {
......
......@@ -27,6 +27,7 @@ import {
hotImport,
} from 'hot-import'
import cloneClass from './clone-class'
import {
config,
log,
......@@ -36,16 +37,17 @@ import {
VERSION,
WechatyEvent,
} from './config'
import Contact from './contact'
import FriendRequest from './friend-request'
import {
Message,
MediaMessage,
} from './message'
import Profile from './profile'
import Puppet from './puppet'
import PuppetWeb from './puppet-web/'
import {
FriendRequest,
PuppetWeb,
} from './puppet-web/'
import Room from './room'
// import Misc from './misc'
......@@ -91,6 +93,15 @@ export class Wechaty extends EventEmitter implements Sayable {
*/
public cuid: string
// tslint:disable-next-line:variable-name
public Contact : typeof Contact
// tslint:disable-next-line:variable-name
public FriendRequest : typeof FriendRequest
// tslint:disable-next-line:variable-name
public Message : typeof Message
// tslint:disable-next-line:variable-name
public Room : typeof Room
/**
* get the singleton instance of Wechaty
*
......@@ -408,7 +419,17 @@ export class Wechaty extends EventEmitter implements Sayable {
}
// set puppet instance to Wechaty Static variable, for using by Contact/Room/Message/FriendRequest etc.
config.puppetInstance(puppet)
// config.puppetInstance(puppet)
this.Contact = cloneClass(Contact)
this.FriendRequest = cloneClass(FriendRequest)
this.Message = cloneClass(Message)
this.Room = cloneClass(Room)
this.Contact.puppet = puppet
this.FriendRequest.puppet = puppet
this.Message.puppet = puppet
this.Room.puppet = puppet
await puppet.start()
return puppet
......@@ -441,7 +462,11 @@ export class Wechaty extends EventEmitter implements Sayable {
const puppet = this.puppet
this.puppet = null
config.puppetInstance(null)
// config.puppetInstance(null)
// this.Contact.puppet = undefined
// this.FriendRequest.puppet = undefined
// this.Message.puppet = undefined
// this.Room.puppet = undefined
try {
await puppet.stop()
......
......@@ -22,14 +22,17 @@
import * as test from 'blue-tape'
// import * as sinon from 'sinon'
import config from '../src/config'
// import config from '../src/config'
import Contact from '../src/contact'
import Profile from '../src/profile'
import PuppetWeb from '../src/puppet-web'
config.puppetInstance(new PuppetWeb({
// config.puppetInstance(new PuppetWeb({
// profile: new Profile(),
// }))
Contact.puppet = new PuppetWeb({
profile: new Profile(),
}))
})
test('Contact smoke testing', async t => {
/* tslint:disable:variable-name */
......
......@@ -31,7 +31,7 @@ const sinonTest = require('sinon-test')(sinon, {
// log.level('silly')
import {
config,
// config,
Contact,
Profile,
} from '../../'
......@@ -60,7 +60,8 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
const pw = new PuppetWeb({ profile })
t.ok(pw, 'should instantiated a PuppetWeb')
config.puppetInstance(pw)
// config.puppetInstance(pw)
Contact.puppet = pw
await pw.start()
t.pass('should be inited')
......
......@@ -22,15 +22,19 @@
import * as test from 'blue-tape'
// import * as sinon from 'sinon'
import config from '../src/config'
// import config from '../src/config'
import Contact from '../src/contact'
import Message from '../src/message'
import Profile from '../src/profile'
import PuppetWeb from '../src/puppet-web'
import Room from '../src/room'
config.puppetInstance(new PuppetWeb({
// config.puppetInstance(new PuppetWeb({
// profile: new Profile(),
// }))
Room.puppet = new PuppetWeb({
profile: new Profile(),
}))
})
// Room.attach(new PuppetWeb())
// test('Room smoke testing', async t => {
......@@ -110,14 +114,15 @@ test('Room smoking test', async t => {
t.is(r.id, EXPECTED.id, 'should set id/UserName right')
let puppet
try {
puppet = config.puppetInstance()
puppet.getContact = mockContactGetter
} catch (err) {
puppet = { getContact: mockContactGetter }
config.puppetInstance(puppet)
}
// let puppet
// try {
// puppet = config.puppetInstance()
// puppet.getContact = mockContactGetter
// } catch (err) {
// puppet = { getContact: mockContactGetter }
// config.puppetInstance(puppet)
// }
Contact.puppet = Message.puppet = Room.puppet = { getContact: mockContactGetter } as any
await r.ready()
t.is(r.get('id') , EXPECTED.id, 'should set id/UserName')
......@@ -175,6 +180,10 @@ test('Room smoking test', async t => {
})
test('Room static method', async t => {
Room.puppet = new PuppetWeb({
profile: new Profile(),
})
try {
const result = await Room.find({ topic: 'xxx' })
t.is(result, null, `should return null if cannot find the room`)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册