@zhin.js/core 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/lib/app.d.ts +5 -2
- package/lib/app.d.ts.map +1 -1
- package/lib/app.js +33 -3
- package/lib/app.js.map +1 -1
- package/lib/command.d.ts +2 -2
- package/lib/command.d.ts.map +1 -1
- package/lib/command.js +4 -4
- package/lib/command.js.map +1 -1
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +2 -0
- package/lib/config.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/plugin.d.ts +1 -4
- package/lib/plugin.d.ts.map +1 -1
- package/lib/plugin.js +6 -25
- package/lib/plugin.js.map +1 -1
- package/lib/prompt.d.ts.map +1 -1
- package/lib/prompt.js +1 -1
- package/lib/prompt.js.map +1 -1
- package/lib/types.d.ts +5 -1
- package/lib/types.d.ts.map +1 -1
- package/lib/utils.d.ts +8 -1
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +29 -0
- package/lib/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/app.ts +36 -6
- package/src/command.ts +5 -5
- package/src/config.ts +2 -0
- package/src/index.ts +1 -0
- package/src/plugin.ts +7 -34
- package/src/prompt.ts +3 -3
- package/src/types.ts +5 -0
- package/src/utils.ts +27 -1
- package/tests/adapter.test.ts +3 -2
- package/tests/app.test.ts +7 -6
- package/tests/command.test.ts +71 -69
- package/tests/plugin.test.ts +18 -14
package/tests/command.test.ts
CHANGED
|
@@ -34,54 +34,56 @@ vi.mock('segment-matcher', () => {
|
|
|
34
34
|
return { SegmentMatcher, MatchResult }
|
|
35
35
|
})
|
|
36
36
|
|
|
37
|
-
// Mock
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
37
|
+
// Mock App with permissions
|
|
38
|
+
const mockApp = {
|
|
39
|
+
permissions: {
|
|
40
|
+
get: vi.fn((permission: string) => {
|
|
41
|
+
// Mock permit checker
|
|
42
|
+
return {
|
|
43
|
+
check: vi.fn(async (perm: string, message: any) => {
|
|
44
|
+
if (permission === 'adapter(discord)') {
|
|
45
|
+
return message.$adapter === 'discord'
|
|
46
|
+
}
|
|
47
|
+
if (permission === 'adapter(telegram)') {
|
|
48
|
+
return message.$adapter === 'telegram'
|
|
49
|
+
}
|
|
50
|
+
if (permission === 'adapter(email)') {
|
|
51
|
+
return message.$adapter === 'email'
|
|
52
|
+
}
|
|
53
|
+
if (permission === 'adapter(test)') {
|
|
54
|
+
return message.$adapter === 'test'
|
|
55
|
+
}
|
|
56
|
+
return true
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
}
|
|
60
61
|
} as any
|
|
61
62
|
|
|
62
|
-
// 为多个权限测试创建特殊的 mock
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
63
|
+
// 为多个权限测试创建特殊的 mock app
|
|
64
|
+
const multiPermitMockApp = {
|
|
65
|
+
permissions: {
|
|
66
|
+
get: vi.fn((permission: string) => {
|
|
67
|
+
return {
|
|
68
|
+
check: vi.fn(async (perm: string, message: any) => {
|
|
69
|
+
// 对于多个权限,只要有一个匹配就返回 true
|
|
70
|
+
if (permission === 'adapter(discord)' && message.$adapter === 'discord') {
|
|
71
|
+
return true
|
|
72
|
+
}
|
|
73
|
+
if (permission === 'adapter(telegram)' && message.$adapter === 'telegram') {
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
if (permission === 'adapter(email)' && message.$adapter === 'email') {
|
|
77
|
+
return true
|
|
78
|
+
}
|
|
79
|
+
if (permission === 'adapter(test)' && message.$adapter === 'test') {
|
|
80
|
+
return true
|
|
81
|
+
}
|
|
82
|
+
return false
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
}
|
|
85
87
|
} as any
|
|
86
88
|
|
|
87
89
|
describe('Command系统测试', () => {
|
|
@@ -135,8 +137,8 @@ describe('Command系统测试', () => {
|
|
|
135
137
|
$raw: 'hello world'
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
const discordResult = await command.handle(discordMessage,
|
|
139
|
-
const telegramResult = await command.handle(telegramMessage,
|
|
140
|
+
const discordResult = await command.handle(discordMessage, mockApp)
|
|
141
|
+
const telegramResult = await command.handle(telegramMessage, mockApp)
|
|
140
142
|
|
|
141
143
|
expect(discordResult).toBe('Hello from Discord!')
|
|
142
144
|
expect(telegramResult).toBeUndefined()
|
|
@@ -183,9 +185,9 @@ describe('Command系统测试', () => {
|
|
|
183
185
|
$raw: 'hello'
|
|
184
186
|
}
|
|
185
187
|
|
|
186
|
-
const discordResult = await command.handle(discordMessage,
|
|
187
|
-
const telegramResult = await command.handle(telegramMessage,
|
|
188
|
-
const emailResult = await command.handle(emailMessage,
|
|
188
|
+
const discordResult = await command.handle(discordMessage, mockApp)
|
|
189
|
+
const telegramResult = await command.handle(telegramMessage, mockApp)
|
|
190
|
+
const emailResult = await command.handle(emailMessage, mockApp)
|
|
189
191
|
|
|
190
192
|
expect(discordResult).toBe('Hello!')
|
|
191
193
|
expect(telegramResult).toBeUndefined()
|
|
@@ -212,7 +214,7 @@ describe('Command系统测试', () => {
|
|
|
212
214
|
$raw: 'test message'
|
|
213
215
|
}
|
|
214
216
|
|
|
215
|
-
const result = await command.handle(message,
|
|
217
|
+
const result = await command.handle(message, mockApp)
|
|
216
218
|
|
|
217
219
|
expect(actionSpy).toHaveBeenCalledWith(message, expect.any(Object))
|
|
218
220
|
expect(result).toBe('Action executed!')
|
|
@@ -240,7 +242,7 @@ describe('Command系统测试', () => {
|
|
|
240
242
|
$raw: 'test'
|
|
241
243
|
}
|
|
242
244
|
|
|
243
|
-
const result = await command.handle(message,
|
|
245
|
+
const result = await command.handle(message, mockApp)
|
|
244
246
|
|
|
245
247
|
expect(action1).toHaveBeenCalled()
|
|
246
248
|
expect(action2).toHaveBeenCalled()
|
|
@@ -266,7 +268,7 @@ describe('Command系统测试', () => {
|
|
|
266
268
|
$raw: 'async test'
|
|
267
269
|
}
|
|
268
270
|
|
|
269
|
-
const result = await command.handle(message,
|
|
271
|
+
const result = await command.handle(message, mockApp)
|
|
270
272
|
|
|
271
273
|
expect(asyncAction).toHaveBeenCalled()
|
|
272
274
|
expect(result).toBe('Async result')
|
|
@@ -290,7 +292,7 @@ describe('Command系统测试', () => {
|
|
|
290
292
|
$raw: 'echo hello world'
|
|
291
293
|
}
|
|
292
294
|
|
|
293
|
-
const result = await command.handle(message,
|
|
295
|
+
const result = await command.handle(message, mockApp)
|
|
294
296
|
|
|
295
297
|
expect(actionSpy).toHaveBeenCalledWith(
|
|
296
298
|
message,
|
|
@@ -320,7 +322,7 @@ describe('Command系统测试', () => {
|
|
|
320
322
|
$raw: 'goodbye'
|
|
321
323
|
}
|
|
322
324
|
|
|
323
|
-
const result = await command.handle(message,
|
|
325
|
+
const result = await command.handle(message, mockApp)
|
|
324
326
|
expect(result).toBeUndefined()
|
|
325
327
|
})
|
|
326
328
|
|
|
@@ -340,7 +342,7 @@ describe('Command系统测试', () => {
|
|
|
340
342
|
$raw: ''
|
|
341
343
|
}
|
|
342
344
|
|
|
343
|
-
const result = await command.handle(message,
|
|
345
|
+
const result = await command.handle(message, mockApp)
|
|
344
346
|
expect(result).toBeUndefined()
|
|
345
347
|
})
|
|
346
348
|
|
|
@@ -362,7 +364,7 @@ describe('Command系统测试', () => {
|
|
|
362
364
|
$raw: '[图片]'
|
|
363
365
|
}
|
|
364
366
|
|
|
365
|
-
const result = await command.handle(message,
|
|
367
|
+
const result = await command.handle(message, mockApp)
|
|
366
368
|
expect(result).toBeUndefined()
|
|
367
369
|
})
|
|
368
370
|
})
|
|
@@ -412,9 +414,9 @@ describe('Command系统测试', () => {
|
|
|
412
414
|
$raw: 'hello world'
|
|
413
415
|
}
|
|
414
416
|
|
|
415
|
-
const validResult = await command.handle(validMessage,
|
|
416
|
-
const wrongAdapterResult = await command.handle(wrongAdapterMessage,
|
|
417
|
-
const nonMatchingResult = await command.handle(nonMatchingMessage,
|
|
417
|
+
const validResult = await command.handle(validMessage, mockApp)
|
|
418
|
+
const wrongAdapterResult = await command.handle(wrongAdapterMessage, mockApp)
|
|
419
|
+
const nonMatchingResult = await command.handle(nonMatchingMessage, mockApp)
|
|
418
420
|
|
|
419
421
|
expect(validResult).toBe('Admin command executed')
|
|
420
422
|
expect(wrongAdapterResult).toBeUndefined()
|
|
@@ -443,7 +445,7 @@ describe('Command系统测试', () => {
|
|
|
443
445
|
$raw: 'error test'
|
|
444
446
|
}
|
|
445
447
|
|
|
446
|
-
await expect(command.handle(message,
|
|
448
|
+
await expect(command.handle(message, mockApp)).rejects.toThrow('Action failed')
|
|
447
449
|
})
|
|
448
450
|
|
|
449
451
|
it('应该正确处理动作中的异步错误', async () => {
|
|
@@ -464,7 +466,7 @@ describe('Command系统测试', () => {
|
|
|
464
466
|
$raw: 'async-error test'
|
|
465
467
|
}
|
|
466
468
|
|
|
467
|
-
await expect(command.handle(message,
|
|
469
|
+
await expect(command.handle(message, mockApp)).rejects.toThrow('Async action failed')
|
|
468
470
|
})
|
|
469
471
|
})
|
|
470
472
|
|
|
@@ -504,7 +506,7 @@ describe('Command系统测试', () => {
|
|
|
504
506
|
|
|
505
507
|
const startTime = Date.now()
|
|
506
508
|
const results = await Promise.all(
|
|
507
|
-
messages.map(message => command.handle(message,
|
|
509
|
+
messages.map(message => command.handle(message, mockApp))
|
|
508
510
|
)
|
|
509
511
|
const endTime = Date.now()
|
|
510
512
|
|
|
@@ -539,7 +541,7 @@ describe('Command系统测试', () => {
|
|
|
539
541
|
$raw: 'say hello world from bot'
|
|
540
542
|
}
|
|
541
543
|
|
|
542
|
-
const result = await command.handle(message,
|
|
544
|
+
const result = await command.handle(message, mockApp)
|
|
543
545
|
|
|
544
546
|
expect(actionSpy).toHaveBeenCalledWith(
|
|
545
547
|
message,
|
|
@@ -587,8 +589,8 @@ describe('Command系统测试', () => {
|
|
|
587
589
|
$raw: 'multi test'
|
|
588
590
|
}
|
|
589
591
|
|
|
590
|
-
const privateResult = await command.handle(privateMessage,
|
|
591
|
-
const groupResult = await command.handle(groupMessage,
|
|
592
|
+
const privateResult = await command.handle(privateMessage, mockApp)
|
|
593
|
+
const groupResult = await command.handle(groupMessage, mockApp)
|
|
592
594
|
|
|
593
595
|
expect(privateResult).toBe('私人消息响应')
|
|
594
596
|
expect(groupResult).toBe('群组消息响应')
|
|
@@ -613,7 +615,7 @@ describe('Command系统测试', () => {
|
|
|
613
615
|
$raw: 'admin test'
|
|
614
616
|
}
|
|
615
617
|
|
|
616
|
-
const result = await command.handle(message,
|
|
618
|
+
const result = await command.handle(message, mockApp)
|
|
617
619
|
expect(result).toBeUndefined()
|
|
618
620
|
})
|
|
619
621
|
|
|
@@ -634,7 +636,7 @@ describe('Command系统测试', () => {
|
|
|
634
636
|
$raw: 'admin test'
|
|
635
637
|
}
|
|
636
638
|
|
|
637
|
-
const result = await command.handle(message,
|
|
639
|
+
const result = await command.handle(message, mockApp)
|
|
638
640
|
expect(result).toBe('Admin command')
|
|
639
641
|
})
|
|
640
642
|
})
|
package/tests/plugin.test.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { MessageCommand } from '../src/command'
|
|
|
5
5
|
import { Component, defineComponent, ComponentContext } from '../src/component'
|
|
6
6
|
import { Message } from '../src/message'
|
|
7
7
|
import { PluginError, MessageError } from '../src/errors'
|
|
8
|
+
import * as path from 'path'
|
|
8
9
|
|
|
9
10
|
describe('Plugin系统测试', () => {
|
|
10
11
|
let app: App
|
|
@@ -12,7 +13,8 @@ describe('Plugin系统测试', () => {
|
|
|
12
13
|
|
|
13
14
|
beforeEach(() => {
|
|
14
15
|
app = new App()
|
|
15
|
-
|
|
16
|
+
// 使用 mock 文件路径,不依赖真实文件
|
|
17
|
+
plugin = app.createDependency('test-plugin', '/mock/test-plugin.ts')
|
|
16
18
|
})
|
|
17
19
|
|
|
18
20
|
afterEach(async () => {
|
|
@@ -23,13 +25,13 @@ describe('Plugin系统测试', () => {
|
|
|
23
25
|
it('应该正确初始化Plugin实例', () => {
|
|
24
26
|
expect(plugin).toBeInstanceOf(Plugin)
|
|
25
27
|
expect(plugin.name).toBe('test-plugin')
|
|
26
|
-
expect(plugin.filename).
|
|
28
|
+
expect(plugin.filename).toContain('test-plugin.ts')
|
|
27
29
|
expect(plugin.app).toBe(app)
|
|
28
30
|
expect(plugin.commands).toEqual([])
|
|
29
31
|
expect(plugin.components).toBeInstanceOf(Map)
|
|
30
32
|
expect(plugin.components.size).toBe(0)
|
|
31
|
-
// Plugin
|
|
32
|
-
expect(plugin.middlewares.length).
|
|
33
|
+
// Plugin没有默认的中间件
|
|
34
|
+
expect(plugin.middlewares.length).toBe(0)
|
|
33
35
|
})
|
|
34
36
|
|
|
35
37
|
it('应该正确获取logger实例', () => {
|
|
@@ -89,8 +91,8 @@ describe('Plugin系统测试', () => {
|
|
|
89
91
|
$raw: 'test message'
|
|
90
92
|
}
|
|
91
93
|
|
|
92
|
-
// 触发消息处理
|
|
93
|
-
await
|
|
94
|
+
// 触发消息处理 - 通过 App 的 receiveMessage 方法
|
|
95
|
+
await app.receiveMessage(mockMessage)
|
|
94
96
|
|
|
95
97
|
// 由于有默认的命令中间件,执行顺序可能会不同
|
|
96
98
|
// 但我们的中间件应该被调用
|
|
@@ -120,9 +122,10 @@ describe('Plugin系统测试', () => {
|
|
|
120
122
|
$raw: 'test message'
|
|
121
123
|
}
|
|
122
124
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
125
|
+
// 中间件异常应该被抛出
|
|
126
|
+
await expect(app.receiveMessage(mockMessage)).rejects.toThrow('中间件测试错误')
|
|
127
|
+
|
|
128
|
+
// 验证中间件被调用
|
|
126
129
|
expect(errorMiddleware).toHaveBeenCalled()
|
|
127
130
|
})
|
|
128
131
|
})
|
|
@@ -154,10 +157,10 @@ describe('Plugin系统测试', () => {
|
|
|
154
157
|
$raw: 'test message'
|
|
155
158
|
}
|
|
156
159
|
|
|
157
|
-
await
|
|
160
|
+
await app.receiveMessage(mockMessage)
|
|
158
161
|
|
|
159
|
-
// 验证命令被调用(handle 现在需要传入
|
|
160
|
-
expect(handleSpy).toHaveBeenCalledWith(mockMessage, expect.any(
|
|
162
|
+
// 验证命令被调用(handle 现在需要传入 app 作为第二个参数)
|
|
163
|
+
expect(handleSpy).toHaveBeenCalledWith(mockMessage, expect.any(App))
|
|
161
164
|
})
|
|
162
165
|
})
|
|
163
166
|
|
|
@@ -302,7 +305,8 @@ describe('Plugin系统测试', () => {
|
|
|
302
305
|
$raw: 'error message'
|
|
303
306
|
}
|
|
304
307
|
|
|
305
|
-
|
|
308
|
+
// 中间件异常应该被抛出
|
|
309
|
+
await expect(app.receiveMessage(mockMessage)).rejects.toThrow('处理失败')
|
|
306
310
|
|
|
307
311
|
// 验证错误中间件被调用
|
|
308
312
|
expect(errorMiddleware).toHaveBeenCalled()
|
|
@@ -358,7 +362,7 @@ describe('Plugin系统测试', () => {
|
|
|
358
362
|
}
|
|
359
363
|
|
|
360
364
|
// 触发消息处理
|
|
361
|
-
await
|
|
365
|
+
await app.receiveMessage(mockMessage)
|
|
362
366
|
|
|
363
367
|
// 验证中间件被调用
|
|
364
368
|
expect(middlewareExecuted).toHaveBeenCalled()
|