/
index.spec.ts
250 lines (199 loc) · 7.12 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { App } from 'koishi'
import mock from '@koishijs/plugin-mock'
import * as help from '@koishijs/plugin-help'
import commands from '@koishijs/plugin-commands'
import { expect } from 'chai'
const app = new App()
app.plugin(help)
app.plugin(mock)
const client = app.mock.client('123')
before(() => app.start())
after(() => app.stop())
afterEach(() => {
for (const command of app.$commander._commandList.slice()) {
if (command.name === 'help') continue
command.dispose()
}
app.registry.delete(commands)
})
describe('@koishijs/plugin-commands', () => {
describe('basic usage', () => {
it('dispose command', async () => {
const cmd = app.command('bar').action(() => 'test')
await client.shouldReply('bar', 'test')
await client.shouldNotReply('baz')
app.plugin(commands, {
bar: 'baz',
})
await client.shouldReply('bar', 'test')
await client.shouldReply('baz', 'test')
cmd.dispose()
await client.shouldNotReply('bar')
await client.shouldNotReply('baz')
})
it('dispose plugin', async () => {
const fork = app.plugin(commands, {
bar: 'baz',
})
await client.shouldNotReply('bar')
await client.shouldNotReply('baz')
app.command('bar').action(() => 'test')
await app.sleep(0)
await client.shouldReply('bar', 'test')
await client.shouldReply('baz', 'test')
fork.dispose()
await client.shouldReply('bar', 'test')
await client.shouldNotReply('baz')
})
it('edit command', async () => {
const fork = app.plugin(commands)
const cmd = app.command('bar').action(() => 'test')
await client.shouldNotReply('baz')
await client.shouldReply('command bar -a baz', '已更新指令配置。')
await client.shouldReply('baz', 'test')
expect(fork.config).to.deep.equal({
bar: {
aliases: {
baz: {},
},
},
})
await client.shouldReply('command bar -A baz', '已更新指令配置。')
await client.shouldNotReply('baz')
})
})
describe('teleport (config)', () => {
it('leaf to root', async () => {
const foo = app.command('foo')
const bar = app.command('foo/bar').action(() => 'test')
expect(foo.children).to.have.length(1)
const fork = app.plugin(commands, {
'bar': '/baz',
})
expect(foo.children).to.have.length(0)
await client.shouldReply('bar', 'test')
await client.shouldReply('baz', 'test')
fork.dispose()
await client.shouldReply('bar', 'test')
await client.shouldNotReply('baz')
expect(foo.children).to.have.length(1)
})
it('root to leaf', async () => {
const foo = app.command('foo')
const bar = app.command('bar').action(() => 'test')
expect(foo.children).to.have.length(0)
const fork = app.plugin(commands, {
bar: 'foo/baz',
})
expect(foo.children).to.have.length(1)
await client.shouldReply('bar', 'test')
await client.shouldReply('baz', 'test')
fork.dispose()
await client.shouldReply('bar', 'test')
await client.shouldNotReply('baz')
expect(foo.children).to.have.length(0)
})
it('leaf to leaf', async () => {
const bar = app.command('bar')
const foo = app.command('bar/foo').action(() => 'test')
expect(bar.children).to.have.length(1)
const fork = app.plugin(commands, {
foo: 'baz/foo',
})
expect(bar.children).to.have.length(1)
const baz = app.command('baz')
await app.sleep(0)
expect(bar.children).to.have.length(0)
expect(baz.children).to.have.length(1)
await client.shouldReply('foo', 'test')
baz.dispose()
expect(bar.children).to.have.length(1)
fork.dispose()
await client.shouldReply('foo', 'test')
expect(bar.children).to.have.length(1)
expect(baz.children).to.have.length(0)
})
})
describe('teleport (command)', () => {
it('leaf to root', async () => {
const foo = app.command('foo')
const bar = app.command('foo/bar').action(() => 'test')
expect(foo.children).to.have.length(1)
const fork = app.plugin(commands)
await client.shouldReply('command bar -P -a baz', '已更新指令配置。')
expect(foo.children).to.have.length(0)
await client.shouldReply('bar', 'test')
await client.shouldReply('baz', 'test')
fork.dispose()
await client.shouldReply('bar', 'test')
await client.shouldNotReply('baz')
expect(foo.children).to.have.length(1)
})
it('root to leaf', async () => {
const foo = app.command('foo')
const bar = app.command('bar').action(() => 'test')
expect(foo.children).to.have.length(0)
const fork = app.plugin(commands)
await client.shouldReply('command bar -p foo -a baz', '已更新指令配置。')
expect(foo.children).to.have.length(1)
await client.shouldReply('bar', 'test')
await client.shouldReply('baz', 'test')
fork.dispose()
await client.shouldReply('bar', 'test')
await client.shouldNotReply('baz')
expect(foo.children).to.have.length(0)
})
it('leaf to leaf', async () => {
const bar = app.command('bar')
const foo = app.command('bar/foo').action(() => 'test')
expect(bar.children).to.have.length(1)
const fork = app.plugin(commands)
await client.shouldReply('command foo -p baz', '已更新指令配置。')
expect(bar.children).to.have.length(1)
const baz = app.command('baz')
await app.sleep(0)
expect(bar.children).to.have.length(0)
expect(baz.children).to.have.length(1)
await client.shouldReply('foo', 'test')
baz.dispose()
expect(bar.children).to.have.length(1)
fork.dispose()
await client.shouldReply('foo', 'test')
expect(bar.children).to.have.length(1)
expect(baz.children).to.have.length(0)
})
})
describe('create', () => {
it('from config', async () => {
app.command('bar').action(() => 'test')
const fork = app.plugin(commands, {
foo: { create: true },
bar: 'foo/baz',
})
const foo = app.command('foo')
expect(foo.children).to.have.length(1)
await client.shouldReply('foo', /baz/)
await client.shouldReply('baz', 'test')
fork.dispose()
await client.shouldNotReply('foo')
await client.shouldNotReply('baz')
await client.shouldReply('bar', 'test')
})
it('from command', async () => {
app.command('bar').action(() => 'test')
const fork = app.plugin(commands)
await client.shouldReply('command bar -p foo -n baz', '已更新指令配置。')
await client.shouldReply('command foo', '指令不存在。')
await client.shouldReply('command foo -c', '已创建指令。')
const foo = app.command('foo')
await app.sleep(0)
expect(foo.children).to.have.length(1)
await client.shouldReply('foo', /baz/)
await client.shouldReply('baz', 'test')
fork.dispose()
await client.shouldNotReply('foo')
await client.shouldNotReply('baz')
await client.shouldReply('bar', 'test')
})
})
})