@zhin.js/core 1.0.6 → 1.0.8

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/lib/adapter.d.ts +8 -6
  3. package/lib/adapter.d.ts.map +1 -1
  4. package/lib/adapter.js +13 -7
  5. package/lib/adapter.js.map +1 -1
  6. package/lib/app.d.ts +72 -14
  7. package/lib/app.d.ts.map +1 -1
  8. package/lib/app.js +241 -83
  9. package/lib/app.js.map +1 -1
  10. package/lib/bot.d.ts +10 -8
  11. package/lib/bot.d.ts.map +1 -1
  12. package/lib/config.d.ts +44 -14
  13. package/lib/config.d.ts.map +1 -1
  14. package/lib/config.js +275 -208
  15. package/lib/config.js.map +1 -1
  16. package/lib/index.d.ts +1 -1
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +1 -1
  19. package/lib/index.js.map +1 -1
  20. package/lib/log-transport.js +1 -1
  21. package/lib/log-transport.js.map +1 -1
  22. package/lib/models/system-log.d.ts +2 -2
  23. package/lib/models/system-log.d.ts.map +1 -1
  24. package/lib/models/system-log.js +1 -1
  25. package/lib/models/system-log.js.map +1 -1
  26. package/lib/models/user.d.ts +2 -2
  27. package/lib/models/user.d.ts.map +1 -1
  28. package/lib/models/user.js +1 -1
  29. package/lib/models/user.js.map +1 -1
  30. package/lib/plugin.d.ts +7 -3
  31. package/lib/plugin.d.ts.map +1 -1
  32. package/lib/plugin.js +16 -5
  33. package/lib/plugin.js.map +1 -1
  34. package/lib/prompt.d.ts +1 -1
  35. package/lib/prompt.d.ts.map +1 -1
  36. package/lib/prompt.js +9 -7
  37. package/lib/prompt.js.map +1 -1
  38. package/lib/types.d.ts +6 -5
  39. package/lib/types.d.ts.map +1 -1
  40. package/package.json +4 -4
  41. package/src/adapter.ts +18 -11
  42. package/src/app.ts +358 -105
  43. package/src/bot.ts +27 -25
  44. package/src/config.ts +352 -230
  45. package/src/index.ts +1 -1
  46. package/src/log-transport.ts +1 -1
  47. package/src/models/system-log.ts +2 -2
  48. package/src/models/user.ts +2 -2
  49. package/src/plugin.ts +19 -6
  50. package/src/prompt.ts +10 -9
  51. package/src/types.ts +8 -5
  52. package/tests/adapter.test.ts +5 -200
  53. package/tests/app.test.ts +208 -181
  54. package/tests/command.test.ts +2 -2
  55. package/tests/config.test.ts +5 -326
  56. package/tests/cron.test.ts +277 -0
  57. package/tests/jsx.test.ts +300 -0
  58. package/tests/permissions.test.ts +358 -0
  59. package/tests/plugin.test.ts +40 -177
  60. package/tests/prompt.test.ts +223 -0
  61. package/tests/schema.test.ts +248 -0
  62. package/lib/schema.d.ts +0 -83
  63. package/lib/schema.d.ts.map +0 -1
  64. package/lib/schema.js +0 -245
  65. package/lib/schema.js.map +0 -1
  66. package/src/schema.ts +0 -273
@@ -46,122 +46,29 @@ describe('Plugin系统测试', () => {
46
46
  describe('中间件系统测试', () => {
47
47
  it('应该正确添加中间件', () => {
48
48
  const middleware = vi.fn(async (message, next) => {
49
- await next()
50
- })
49
+ await next();
50
+ });
51
51
 
52
- const initialCount = plugin.middlewares.length
53
- const unsubscribe = plugin.addMiddleware(middleware)
54
- expect(plugin.middlewares).toContain(middleware)
55
- expect(plugin.middlewares.length).toBe(initialCount + 1) // 增加1个中间件
56
- expect(typeof unsubscribe).toBe('function')
52
+ const initialCount = plugin.middlewares.length;
53
+ const unsubscribe = plugin.addMiddleware(middleware);
54
+ expect(plugin.middlewares).toContain(middleware);
55
+ expect(plugin.middlewares.length).toBe(initialCount + 1); // 增加1个中间件
56
+ expect(typeof unsubscribe).toBe('function');
57
57
 
58
58
  // 测试移除中间件
59
- unsubscribe()
60
- expect(plugin.middlewares).not.toContain(middleware)
61
- })
62
-
63
- it('应该按顺序执行中间件', async () => {
64
- const executionOrder: number[] = []
65
-
66
- const middleware1 = vi.fn(async (message, next) => {
67
- executionOrder.push(1)
68
- await next()
69
- executionOrder.push(4)
70
- })
71
-
72
- const middleware2 = vi.fn(async (message, next) => {
73
- executionOrder.push(2)
74
- await next()
75
- executionOrder.push(3)
76
- })
77
-
78
- plugin.addMiddleware(middleware1)
79
- plugin.addMiddleware(middleware2)
80
-
81
- // 模拟消息对象
82
- const mockMessage: Message = {
83
- $id: '1',
84
- $adapter: 'test',
85
- $bot: 'test-bot',
86
- $content: [{ type: 'text', data: { text: 'test' } }],
87
- $sender: { id: 'user1', name: 'Test User' },
88
- $reply: vi.fn(),
89
- $channel: { id: 'test-channel', type: 'private' },
90
- $timestamp: Date.now(),
91
- $raw: 'test message'
92
- }
93
-
94
- // 触发消息处理 - 通过 App 的 receiveMessage 方法
95
- await app.receiveMessage(mockMessage)
96
-
97
- // 由于有默认的命令中间件,执行顺序可能会不同
98
- // 但我们的中间件应该被调用
99
- expect(middleware1).toHaveBeenCalledWith(mockMessage, expect.any(Function))
100
- expect(middleware2).toHaveBeenCalledWith(mockMessage, expect.any(Function))
101
- // 至少应该执行了我们的中间件的前半部分
102
- expect(executionOrder).toContain(1)
103
- expect(executionOrder).toContain(2)
104
- })
105
-
106
- it('应该正确处理中间件异常', async () => {
107
- const errorMiddleware = vi.fn(async () => {
108
- throw new Error('中间件测试错误')
109
- })
110
-
111
- plugin.addMiddleware(errorMiddleware)
112
-
113
- const mockMessage: Message = {
114
- $id: '1',
115
- $adapter: 'test',
116
- $bot: 'test-bot',
117
- $content: [{ type: 'text', data: { text: 'test' } }],
118
- $sender: { id: 'user1', name: 'Test User' },
119
- $reply: vi.fn(),
120
- $channel: { id: 'test-channel', type: 'private' },
121
- $timestamp: Date.now(),
122
- $raw: 'test message'
123
- }
124
-
125
- // 中间件异常应该被抛出
126
- await expect(app.receiveMessage(mockMessage)).rejects.toThrow('中间件测试错误')
127
-
128
- // 验证中间件被调用
129
- expect(errorMiddleware).toHaveBeenCalled()
130
- })
59
+ unsubscribe();
60
+ expect(plugin.middlewares).not.toContain(middleware);
61
+ });
131
62
  })
132
63
 
133
64
  describe('命令系统测试', () => {
134
65
  it('应该正确添加命令', () => {
135
- const mockCommand = new MessageCommand('test')
136
- plugin.addCommand(mockCommand)
66
+ const mockCommand = new MessageCommand('test');
67
+ plugin.addCommand(mockCommand);
137
68
 
138
- expect(plugin.commands).toContain(mockCommand)
139
- expect(plugin.commands.length).toBe(1)
140
- })
141
-
142
- it('应该通过默认中间件处理命令', async () => {
143
- const mockCommand = new MessageCommand('test')
144
- const handleSpy = vi.spyOn(mockCommand, 'handle').mockResolvedValue(undefined)
145
-
146
- plugin.addCommand(mockCommand)
147
-
148
- const mockMessage: Message = {
149
- $id: '1',
150
- $adapter: 'test',
151
- $bot: 'test-bot',
152
- $content: [{ type: 'text', data: { text: 'test' } }],
153
- $sender: { id: 'user1', name: 'Test User' },
154
- $reply: vi.fn(),
155
- $channel: { id: 'test-channel', type: 'private' },
156
- $timestamp: Date.now(),
157
- $raw: 'test message'
158
- }
159
-
160
- await app.receiveMessage(mockMessage)
161
-
162
- // 验证命令被调用(handle 现在需要传入 app 作为第二个参数)
163
- expect(handleSpy).toHaveBeenCalledWith(mockMessage, expect.any(App))
164
- })
69
+ expect(plugin.commands).toContain(mockCommand);
70
+ expect(plugin.commands.length).toBe(1);
71
+ });
165
72
  })
166
73
 
167
74
  describe('函数式组件系统测试', () => {
@@ -226,22 +133,34 @@ describe('Plugin系统测试', () => {
226
133
 
227
134
  describe('消息发送测试', () => {
228
135
  it('应该正确发送消息', async () => {
229
- const appSendSpy = vi.spyOn(app, 'sendMessage').mockResolvedValue()
230
-
231
- const sendOptions = { content: 'Hello World' }
232
- await plugin.sendMessage(sendOptions)
136
+ const appSendSpy = vi.spyOn(app, 'sendMessage').mockResolvedValue('message-id');
233
137
 
234
- expect(appSendSpy).toHaveBeenCalledWith(sendOptions)
235
- })
138
+ const sendOptions = {
139
+ content: 'Hello World',
140
+ context: 'test-context',
141
+ bot: 'test-bot',
142
+ id: 'test-id',
143
+ type: 'text' // 确保类型符合 MessageType
144
+ };
145
+ await plugin.sendMessage(sendOptions);
236
146
 
237
- it('应该正确处理发送消息失败', async () => {
238
- const sendError = new Error('发送失败')
239
- vi.spyOn(app, 'sendMessage').mockRejectedValue(sendError)
147
+ expect(appSendSpy).toHaveBeenCalledWith(sendOptions);
148
+ });
240
149
 
241
- const sendOptions = { content: 'Hello World' }
242
-
243
- await expect(plugin.sendMessage(sendOptions)).rejects.toThrow(MessageError)
244
- })
150
+ it('应该正确处理发送消息失败', async () => {
151
+ const sendError = new Error('发送失败');
152
+ vi.spyOn(app, 'sendMessage').mockRejectedValue(sendError);
153
+
154
+ const sendOptions = {
155
+ content: 'Hello World',
156
+ context: 'test-context',
157
+ bot: 'test-bot',
158
+ id: 'test-id',
159
+ type: 'text' // 确保类型符合 MessageType
160
+ };
161
+
162
+ await expect(plugin.sendMessage(sendOptions)).rejects.toThrow(MessageError);
163
+ });
245
164
  })
246
165
 
247
166
  describe('Prompt系统测试', () => {
@@ -288,31 +207,6 @@ describe('Plugin系统测试', () => {
288
207
  })
289
208
  })
290
209
 
291
- describe('错误处理测试', () => {
292
- it('应该正确处理消息处理异常', async () => {
293
- const errorMiddleware = vi.fn().mockRejectedValue(new Error('处理失败'))
294
- plugin.addMiddleware(errorMiddleware)
295
-
296
- const mockMessage: Message = {
297
- $id: 'error-msg',
298
- $adapter: 'test',
299
- $bot: 'test-bot',
300
- $content: [{ type: 'text', data: { text: 'test' } }],
301
- $sender: { id: 'user1', name: 'Test User' },
302
- $reply: vi.fn(),
303
- $channel: { id: 'error-channel', type: 'private' },
304
- $timestamp: Date.now(),
305
- $raw: 'error message'
306
- }
307
-
308
- // 中间件异常应该被抛出
309
- await expect(app.receiveMessage(mockMessage)).rejects.toThrow('处理失败')
310
-
311
- // 验证错误中间件被调用
312
- expect(errorMiddleware).toHaveBeenCalled()
313
- })
314
- })
315
-
316
210
  describe('资源清理测试', () => {
317
211
  it('应该正确销毁插件', () => {
318
212
  const middleware = vi.fn()
@@ -337,35 +231,4 @@ describe('Plugin系统测试', () => {
337
231
  })
338
232
  })
339
233
 
340
- describe('集成测试', () => {
341
- it('应该完整处理消息流程', async () => {
342
- // 设置测试环境
343
- const middlewareExecuted = vi.fn()
344
-
345
- // 添加测试中间件
346
- plugin.addMiddleware(async (message, next) => {
347
- middlewareExecuted(message.$content)
348
- await next()
349
- })
350
-
351
- // 模拟消息
352
- const mockMessage: Message = {
353
- $id: 'integration-test',
354
- $adapter: 'test',
355
- $bot: 'test-bot',
356
- $content: [{ type: 'text', data: { text: 'hello' } }],
357
- $sender: { id: 'user1', name: 'Test User' },
358
- $reply: vi.fn(),
359
- $channel: { id: 'test-channel', type: 'private' },
360
- $timestamp: Date.now(),
361
- $raw: 'hello'
362
- }
363
-
364
- // 触发消息处理
365
- await app.receiveMessage(mockMessage)
366
-
367
- // 验证中间件被调用
368
- expect(middlewareExecuted).toHaveBeenCalled()
369
- })
370
- })
371
234
  })
@@ -0,0 +1,223 @@
1
+ import { describe, it, expect, beforeEach, vi } from 'vitest'
2
+ import { Prompt } from '../src/prompt'
3
+ import { Plugin } from '../src/plugin'
4
+ import { App } from '../src/app'
5
+ import { MessageBase, MessageChannel } from '../src/message'
6
+ import { Schema } from '@zhin.js/core'
7
+
8
+ describe('Prompt交互系统测试', () => {
9
+ let app: App
10
+ let plugin: Plugin
11
+ let mockMessage: MessageBase
12
+ let prompt: Prompt<never> // Prompt类型参数约束为never,使用never
13
+
14
+ beforeEach(() => {
15
+ app = new App({
16
+ log_level: 1,
17
+ plugin_dirs: [],
18
+ plugins: [],
19
+ bots: [],
20
+ debug: false
21
+ })
22
+
23
+ plugin = app.createDependency('test-plugin', '/mock/test.ts')
24
+
25
+ // 创建模拟消息
26
+ const mockChannel: MessageChannel = {
27
+ id: 'channel123',
28
+ type: 'group'
29
+ }
30
+
31
+ mockMessage = {
32
+ $id: 'msg123',
33
+ $adapter: 'test',
34
+ $bot: 'bot123',
35
+ $content: [],
36
+ $sender: {
37
+ id: 'user123',
38
+ name: 'testuser'
39
+ },
40
+ $reply: vi.fn().mockResolvedValue('reply123'),
41
+ $channel: mockChannel,
42
+ $timestamp: Date.now(),
43
+ $raw: 'test message'
44
+ }
45
+
46
+ prompt = new Prompt(plugin, mockMessage as any)
47
+ })
48
+
49
+ describe('Prompt实例化', () => {
50
+ it('应该正确创建Prompt实例', () => {
51
+ expect(prompt).toBeInstanceOf(Prompt)
52
+ })
53
+
54
+ it('应该具有各种输入方法', () => {
55
+ expect(typeof prompt.text).toBe('function')
56
+ expect(typeof prompt.number).toBe('function')
57
+ expect(typeof prompt.confirm).toBe('function')
58
+ expect(typeof prompt.list).toBe('function')
59
+ expect(typeof prompt.pick).toBe('function')
60
+ })
61
+ })
62
+
63
+ describe('基础输入类型', () => {
64
+ it('应该创建文本输入prompt', async () => {
65
+ // 由于Prompt的实际实现依赖于消息监听,这里主要测试接口存在
66
+ expect(typeof prompt.text).toBe('function')
67
+ })
68
+
69
+ it('应该创建数字输入prompt', async () => {
70
+ expect(typeof prompt.number).toBe('function')
71
+ })
72
+
73
+ it('应该创建确认输入prompt', async () => {
74
+ expect(typeof prompt.confirm).toBe('function')
75
+ })
76
+
77
+ it('应该创建列表输入prompt', async () => {
78
+ expect(typeof prompt.list).toBe('function')
79
+ })
80
+
81
+ it('应该创建选项选择prompt', async () => {
82
+ expect(typeof prompt.pick).toBe('function')
83
+ })
84
+ })
85
+
86
+ describe('Schema集成', () => {
87
+ it('应该处理字符串Schema', () => {
88
+ const schema = Schema.string('请输入文本')
89
+ expect(typeof prompt.getValueWithSchema).toBe('function')
90
+
91
+ // 测试Schema类型识别
92
+ expect(schema.meta.type).toBe('string')
93
+ })
94
+
95
+ it('应该处理数字Schema', () => {
96
+ const schema = Schema.number('请输入数字')
97
+ expect(schema.meta.type).toBe('number')
98
+ })
99
+
100
+ it('应该处理布尔Schema', () => {
101
+ const schema = Schema.boolean('请确认')
102
+ expect(schema.meta.type).toBe('boolean')
103
+ })
104
+
105
+ it('应该处理选项Schema', () => {
106
+ const schema = Schema.string('请选择')
107
+ schema.meta.options = Schema.formatOptionList(['选项1', '选项2', '选项3'])
108
+
109
+ expect(schema.meta.options).toHaveLength(3)
110
+ })
111
+
112
+ it('应该处理对象Schema', () => {
113
+ const schema = Schema.object({
114
+ name: Schema.string('姓名'),
115
+ age: Schema.number('年龄')
116
+ })
117
+
118
+ expect(schema.meta.type).toBe('object')
119
+ expect(typeof prompt.getValueWithSchemas).toBe('function')
120
+ })
121
+ })
122
+
123
+ describe('高级功能', () => {
124
+ it('应该支持常量值返回', async () => {
125
+ const result = await prompt.const('固定值')
126
+ expect(result).toBe('固定值')
127
+ })
128
+
129
+ it('应该处理超时情况', () => {
130
+ // 测试prompt方法接受超时参数
131
+ expect(typeof prompt.text).toBe('function')
132
+ // text方法应该支持timeout参数
133
+ })
134
+
135
+ it('应该支持默认值', () => {
136
+ const schema = Schema.string('测试').default('默认值')
137
+ expect(schema.meta.default).toBe('默认值')
138
+ })
139
+ })
140
+
141
+ describe('配置选项', () => {
142
+ it('应该支持自定义超时时间', () => {
143
+ const customTimeout = 10000
144
+ // text方法接受timeout参数,测试方法存在
145
+ expect(typeof prompt.text).toBe('function')
146
+ })
147
+
148
+ it('应该支持格式化函数', () => {
149
+ const formatFn = (input: string) => input.toUpperCase()
150
+ // 测试格式化函数的概念存在
151
+ expect(typeof formatFn).toBe('function')
152
+ expect(formatFn('test')).toBe('TEST')
153
+ })
154
+ })
155
+
156
+ describe('错误处理', () => {
157
+ it('应该处理无效输入', () => {
158
+ // 测试错误处理机制的存在
159
+ expect(() => {
160
+ const schema = Schema.string('测试')
161
+ schema.meta.type = 'invalid' as any
162
+ }).not.toThrow() // 只是设置,不会立即验证
163
+ })
164
+
165
+ it('应该处理超时错误', () => {
166
+ // 测试超时机制通过方法参数支持
167
+ expect(typeof prompt.text).toBe('function')
168
+ expect(typeof prompt.middleware).toBe('function')
169
+ })
170
+ })
171
+
172
+ describe('类型推断', () => {
173
+ it('应该正确推断字符串类型', () => {
174
+ const schema = Schema.string('文本输入')
175
+ expect(schema.meta.type).toBe('string')
176
+ })
177
+
178
+ it('应该正确推断数字类型', () => {
179
+ const schema = Schema.number('数字输入')
180
+ expect(schema.meta.type).toBe('number')
181
+ })
182
+
183
+ it('应该正确推断布尔类型', () => {
184
+ const schema = Schema.boolean('布尔输入')
185
+ expect(schema.meta.type).toBe('boolean')
186
+ })
187
+
188
+ it('应该正确推断常量类型', () => {
189
+ const schema = Schema.const('CONSTANT', '常量')
190
+ expect(schema.meta.type).toBe('const')
191
+ expect(schema.meta.default).toBe('CONSTANT')
192
+ })
193
+ })
194
+
195
+ describe('复杂交互场景', () => {
196
+ it('应该支持条件性提示', () => {
197
+ // 测试条件逻辑
198
+ const condition = true
199
+ const schema = condition
200
+ ? Schema.string('条件为真时的提示')
201
+ : Schema.number('条件为假时的提示')
202
+
203
+ expect(schema.meta.type).toBe('string')
204
+ })
205
+
206
+ it('应该支持级联提示', () => {
207
+ const schemas = {
208
+ first: Schema.string('第一个问题'),
209
+ second: Schema.string('第二个问题'),
210
+ third: Schema.number('第三个问题')
211
+ }
212
+
213
+ expect(Object.keys(schemas)).toHaveLength(3)
214
+ expect(typeof prompt.getValueWithSchemas).toBe('function')
215
+ })
216
+
217
+ it('应该支持验证规则', () => {
218
+ const schema = Schema.number('年龄').min(0).max(150)
219
+ expect(schema.meta.min).toBe(0)
220
+ expect(schema.meta.max).toBe(150)
221
+ })
222
+ })
223
+ })