@zhin.js/core 1.0.16 → 1.0.18

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 (118) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/REFACTORING_COMPLETE.md +178 -0
  3. package/REFACTORING_STATUS.md +263 -0
  4. package/lib/adapter.d.ts +44 -19
  5. package/lib/adapter.d.ts.map +1 -1
  6. package/lib/adapter.js +81 -50
  7. package/lib/adapter.js.map +1 -1
  8. package/lib/bot.d.ts +7 -12
  9. package/lib/bot.d.ts.map +1 -1
  10. package/lib/built/adapter-process.d.ts +36 -0
  11. package/lib/built/adapter-process.d.ts.map +1 -0
  12. package/lib/built/adapter-process.js +77 -0
  13. package/lib/built/adapter-process.js.map +1 -0
  14. package/lib/built/command.d.ts +46 -0
  15. package/lib/built/command.d.ts.map +1 -0
  16. package/lib/built/command.js +54 -0
  17. package/lib/built/command.js.map +1 -0
  18. package/lib/built/component.d.ts +42 -0
  19. package/lib/built/component.d.ts.map +1 -0
  20. package/lib/built/component.js +66 -0
  21. package/lib/built/component.js.map +1 -0
  22. package/lib/built/config.d.ts +31 -0
  23. package/lib/built/config.d.ts.map +1 -0
  24. package/lib/built/config.js +141 -0
  25. package/lib/built/config.js.map +1 -0
  26. package/lib/built/cron.d.ts +53 -0
  27. package/lib/built/cron.d.ts.map +1 -0
  28. package/lib/built/cron.js +79 -0
  29. package/lib/built/cron.js.map +1 -0
  30. package/lib/built/database.d.ts +17 -0
  31. package/lib/built/database.d.ts.map +1 -0
  32. package/lib/built/database.js +28 -0
  33. package/lib/built/database.js.map +1 -0
  34. package/lib/{permissions.d.ts → built/permission.d.ts} +5 -10
  35. package/lib/built/permission.d.ts.map +1 -0
  36. package/lib/{permissions.js → built/permission.js} +11 -10
  37. package/lib/built/permission.js.map +1 -0
  38. package/lib/command.d.ts +18 -7
  39. package/lib/command.d.ts.map +1 -1
  40. package/lib/command.js +36 -15
  41. package/lib/command.js.map +1 -1
  42. package/lib/component.d.ts +1 -1
  43. package/lib/component.d.ts.map +1 -1
  44. package/lib/component.js.map +1 -1
  45. package/lib/cron.d.ts +4 -12
  46. package/lib/cron.d.ts.map +1 -1
  47. package/lib/cron.js +33 -64
  48. package/lib/cron.js.map +1 -1
  49. package/lib/index.d.ts +11 -3
  50. package/lib/index.d.ts.map +1 -1
  51. package/lib/index.js +14 -4
  52. package/lib/index.js.map +1 -1
  53. package/lib/jsx-runtime.d.ts +2 -2
  54. package/lib/jsx.d.ts +2 -3
  55. package/lib/jsx.d.ts.map +1 -1
  56. package/lib/jsx.js.map +1 -1
  57. package/lib/message.d.ts +4 -7
  58. package/lib/message.d.ts.map +1 -1
  59. package/lib/message.js.map +1 -1
  60. package/lib/plugin.d.ts +164 -51
  61. package/lib/plugin.d.ts.map +1 -1
  62. package/lib/plugin.js +520 -137
  63. package/lib/plugin.js.map +1 -1
  64. package/lib/prompt.d.ts +1 -1
  65. package/lib/prompt.d.ts.map +1 -1
  66. package/lib/prompt.js +2 -1
  67. package/lib/prompt.js.map +1 -1
  68. package/lib/types.d.ts +33 -33
  69. package/lib/types.d.ts.map +1 -1
  70. package/lib/utils.d.ts +16 -1
  71. package/lib/utils.d.ts.map +1 -1
  72. package/lib/utils.js +166 -66
  73. package/lib/utils.js.map +1 -1
  74. package/package.json +17 -11
  75. package/src/adapter.ts +131 -80
  76. package/src/bot.ts +8 -13
  77. package/src/built/adapter-process.ts +77 -0
  78. package/src/built/command.ts +102 -0
  79. package/src/built/component.ts +111 -0
  80. package/src/built/config.ts +126 -0
  81. package/src/built/cron.ts +140 -0
  82. package/src/built/database.ts +38 -0
  83. package/src/{permissions.ts → built/permission.ts} +9 -12
  84. package/src/command.ts +48 -20
  85. package/src/component.ts +2 -3
  86. package/src/cron.ts +35 -70
  87. package/src/index.ts +15 -5
  88. package/src/jsx.ts +2 -3
  89. package/src/message.ts +3 -4
  90. package/src/plugin.ts +671 -184
  91. package/src/prompt.ts +4 -3
  92. package/src/types.ts +41 -35
  93. package/src/utils.ts +418 -296
  94. package/test/minimal-bot.ts +31 -0
  95. package/test/stress-test.ts +123 -0
  96. package/tests/command.test.ts +124 -44
  97. package/ASYNC-JSX-SUPPORT.md +0 -173
  98. package/lib/app.d.ts +0 -191
  99. package/lib/app.d.ts.map +0 -1
  100. package/lib/app.js +0 -604
  101. package/lib/app.js.map +0 -1
  102. package/lib/config.d.ts +0 -54
  103. package/lib/config.d.ts.map +0 -1
  104. package/lib/config.js +0 -308
  105. package/lib/config.js.map +0 -1
  106. package/lib/log-transport.d.ts +0 -37
  107. package/lib/log-transport.d.ts.map +0 -1
  108. package/lib/log-transport.js +0 -136
  109. package/lib/log-transport.js.map +0 -1
  110. package/lib/permissions.d.ts.map +0 -1
  111. package/lib/permissions.js.map +0 -1
  112. package/src/app.ts +0 -772
  113. package/src/config.ts +0 -397
  114. package/src/log-transport.ts +0 -163
  115. package/tests/app.test.ts +0 -265
  116. package/tests/permissions.test.ts +0 -358
  117. package/tests/plugin.test.ts +0 -234
  118. package/tests/prompt.test.ts +0 -223
@@ -1,234 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest'
2
- import { Plugin } from '../src/plugin'
3
- import { App } from '../src/app'
4
- import { MessageCommand } from '../src/command'
5
- import { Component, defineComponent, ComponentContext } from '../src/component'
6
- import { Message } from '../src/message'
7
- import { PluginError, MessageError } from '../src/errors'
8
- import * as path from 'path'
9
-
10
- describe('Plugin系统测试', () => {
11
- let app: App
12
- let plugin: Plugin
13
-
14
- beforeEach(() => {
15
- app = new App()
16
- // 使用 mock 文件路径,不依赖真实文件
17
- plugin = app.createDependency('test-plugin', '/mock/test-plugin.ts')
18
- })
19
-
20
- afterEach(async () => {
21
- await app.stop()
22
- })
23
-
24
- describe('基础功能测试', () => {
25
- it('应该正确初始化Plugin实例', () => {
26
- expect(plugin).toBeInstanceOf(Plugin)
27
- expect(plugin.name).toBe('test-plugin')
28
- expect(plugin.filename).toContain('test-plugin.ts')
29
- expect(plugin.app).toBe(app)
30
- expect(plugin.commands).toEqual([])
31
- expect(plugin.components).toBeInstanceOf(Map)
32
- expect(plugin.components.size).toBe(0)
33
- // Plugin没有默认的中间件
34
- expect(plugin.middlewares.length).toBe(0)
35
- })
36
-
37
- it('应该正确获取logger实例', () => {
38
- const logger = plugin.logger
39
- expect(logger).toBeDefined()
40
- expect(typeof logger.info).toBe('function')
41
- expect(typeof logger.warn).toBe('function')
42
- expect(typeof logger.error).toBe('function')
43
- })
44
- })
45
-
46
- describe('中间件系统测试', () => {
47
- it('应该正确添加中间件', () => {
48
- const middleware = vi.fn(async (message, next) => {
49
- await next();
50
- });
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');
57
-
58
- // 测试移除中间件
59
- unsubscribe();
60
- expect(plugin.middlewares).not.toContain(middleware);
61
- });
62
- })
63
-
64
- describe('命令系统测试', () => {
65
- it('应该正确添加命令', () => {
66
- const mockCommand = new MessageCommand('test');
67
- plugin.addCommand(mockCommand);
68
-
69
- expect(plugin.commands).toContain(mockCommand);
70
- expect(plugin.commands.length).toBe(1);
71
- });
72
- })
73
-
74
- describe('函数式组件系统测试', () => {
75
- it('应该正确添加函数式组件', () => {
76
- const mockComponent = defineComponent(async function TestComponent(props: { name: string }, context: ComponentContext) {
77
- return `Hello ${props.name}`
78
- }, 'test-component')
79
-
80
- plugin.addComponent(mockComponent)
81
-
82
- expect(plugin.components.has('test-component')).toBe(true)
83
- expect(plugin.components.get('test-component')).toBe(mockComponent)
84
- })
85
-
86
- it('应该正确处理组件渲染', async () => {
87
- const mockComponent = defineComponent(async function TestComponent(props: { text: string }, context: ComponentContext) {
88
- return `Rendered: ${props.text}`
89
- }, 'test-component')
90
-
91
- plugin.addComponent(mockComponent)
92
-
93
- // 模拟app.sendMessage
94
- const appSendSpy = vi.spyOn(app, 'sendMessage').mockResolvedValue()
95
-
96
- await plugin.sendMessage({ content: '<test-component text="Hello" />' })
97
-
98
- expect(appSendSpy).toHaveBeenCalled()
99
- })
100
-
101
- it('应该正确处理表达式属性', async () => {
102
- const mockComponent = defineComponent(async function TestComponent(props: { sum: number }, context: ComponentContext) {
103
- return `Sum: ${props.sum}`
104
- }, 'math-component')
105
-
106
- plugin.addComponent(mockComponent)
107
-
108
- const appSendSpy = vi.spyOn(app, 'sendMessage').mockResolvedValue()
109
-
110
- await plugin.sendMessage({ content: '<math-component sum={1+2+3} />' })
111
-
112
- expect(appSendSpy).toHaveBeenCalled()
113
- })
114
- })
115
-
116
- describe('钩子系统测试', () => {
117
- it('应该正确注册beforeSend钩子', () => {
118
- const initialCount = plugin.listenerCount('before-message.send')
119
- const handler = vi.fn((options) => options)
120
- plugin.beforeSend(handler)
121
-
122
- // 验证事件监听器已注册
123
- expect(plugin.listenerCount('before-message.send')).toBe(initialCount + 1)
124
- })
125
-
126
- it('应该正确注册通用before钩子', () => {
127
- const handler = vi.fn()
128
- plugin.before('test.event', handler)
129
-
130
- expect(plugin.listenerCount('before-test.event')).toBe(1)
131
- })
132
- })
133
-
134
- describe('消息发送测试', () => {
135
- it('应该正确发送消息', async () => {
136
- const appSendSpy = vi.spyOn(app, 'sendMessage').mockResolvedValue('message-id');
137
-
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);
146
-
147
- expect(appSendSpy).toHaveBeenCalledWith(sendOptions);
148
- });
149
-
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
- });
164
- })
165
-
166
- describe('Prompt系统测试', () => {
167
- it('应该创建Prompt实例', () => {
168
- const mockMessage: Message = {
169
- $id: '1',
170
- $adapter: 'test',
171
- $bot: 'test-bot',
172
- $content: [{ type: 'text', data: { text: 'test' } }],
173
- $sender: { id: 'user1', name: 'Test User' },
174
- $reply: vi.fn(),
175
- $channel: { id: 'test-channel', type: 'private' },
176
- $timestamp: Date.now(),
177
- $raw: 'test message'
178
- }
179
-
180
- const prompt = plugin.prompt(mockMessage)
181
- expect(prompt).toBeDefined()
182
- expect(prompt.constructor.name).toBe('Prompt')
183
- })
184
- })
185
-
186
- describe('事件系统测试', () => {
187
- it('应该正确分发中间件添加事件', () => {
188
- const eventSpy = vi.fn()
189
- plugin.on('middleware.add', eventSpy)
190
-
191
- const middleware = vi.fn()
192
- plugin.addMiddleware(middleware)
193
-
194
- // 可能事件名称不同或者没有触发,简化测试
195
- expect(plugin.middlewares).toContain(middleware)
196
- })
197
-
198
- it('应该正确分发命令添加事件', () => {
199
- const eventSpy = vi.fn()
200
- plugin.on('command.add', eventSpy)
201
-
202
- const command = new MessageCommand('test')
203
- plugin.addCommand(command)
204
-
205
- // 可能事件名称不同或者没有触发,简化测试
206
- expect(plugin.commands).toContain(command)
207
- })
208
- })
209
-
210
- describe('资源清理测试', () => {
211
- it('应该正确销毁插件', () => {
212
- const middleware = vi.fn()
213
- plugin.addMiddleware(middleware)
214
-
215
- expect(plugin.middlewares.length).toBeGreaterThan(0)
216
-
217
- plugin.dispose()
218
-
219
- expect(plugin.middlewares).toEqual([])
220
- })
221
- })
222
-
223
- describe('生命周期测试', () => {
224
- it('应该正确处理插件事件监听', () => {
225
- const eventSpy = vi.fn()
226
- plugin.on('test-event', eventSpy)
227
-
228
- plugin.emit('test-event', 'test-data')
229
-
230
- expect(eventSpy).toHaveBeenCalledWith('test-data')
231
- })
232
- })
233
-
234
- })
@@ -1,223 +0,0 @@
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
- })