1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | import { test } from 'tstest'
|
23 |
|
24 | import { PuppetMock } from 'wechaty-puppet-mock'
|
25 |
|
26 | import { WechatyBuilder } from './wechaty-builder.js'
|
27 |
|
28 | test('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 |
|
43 | test('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 |
|
66 | test('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 | })
|