UNPKG

2.94 kBPlain TextView Raw
1#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2/**
3 * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
4 *
5 * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
6 * Wechaty Contributors <https://github.com/wechaty>.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22import { test } from 'tstest'
23
24import { PuppetMock } from 'wechaty-puppet-mock'
25
26import { WechatyBuilder } from './wechaty-builder.js'
27
28test('WechatyBuilder class', async t => {
29 const wechaty1 = WechatyBuilder.build()
30 const wechaty2 = WechatyBuilder.build()
31 t.not(wechaty1, wechaty2, 'should build two different Wechaty instance')
32
33 const singleton1 = WechatyBuilder.singleton()
34 const singleton2 = WechatyBuilder.singleton()
35 t.equal(singleton1, singleton2, 'should get the same singleton instance')
36
37 const wechaty = WechatyBuilder.build({ puppet: 'wechaty-puppet-mock' })
38 await wechaty.start()
39 t.ok(PuppetMock.validInstance(wechaty.puppet), 'should set options.puppet to mock')
40 await wechaty.stop()
41})
42
43test('throw when set options twice', async t => {
44 class WechatyBuilderTest extends WechatyBuilder {
45
46 constructor () { super() }
47
48 static override new () { return new this() }
49
50 override options (...args: any[]) {
51 super.options(...args)
52 return this
53 }
54
55 }
56
57 const builder = WechatyBuilderTest.new()
58 t.doesNotThrow(() => builder.options({}), 'should not throw for the first time')
59 t.doesNotThrow(() => builder.options({}), 'should not throw as long as the `options` is an empty object')
60
61 const options = { name: 'bot' }
62 t.doesNotThrow(() => builder.options(options), 'should not throw for setting non-empty `options` for the first time')
63 t.throws(() => builder.options(options), 'should throw for setting non-empty `options` again')
64})
65
66test('WechatyBuilder class static', async t => {
67 const wechaty1 = WechatyBuilder.build()
68 const wechaty2 = WechatyBuilder.build()
69 t.not(wechaty1, wechaty2, 'should build two different Wechaty instance')
70
71 const singleton1 = WechatyBuilder.singleton()
72 const singleton2 = WechatyBuilder.singleton()
73 t.equal(singleton1, singleton2, 'should get the same singleton instance')
74
75 const wechaty = WechatyBuilder.build({ puppet: 'wechaty-puppet-mock' })
76 await wechaty.start()
77 t.ok(PuppetMock.validInstance(wechaty.puppet), 'should set options.puppet to mock')
78 await wechaty.stop()
79})