@zhin.js/core 1.0.15 → 1.0.17
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/ASYNC-JSX-SUPPORT.md +173 -0
- package/CHANGELOG.md +14 -0
- package/lib/command.d.ts +12 -1
- package/lib/command.d.ts.map +1 -1
- package/lib/command.js +31 -0
- package/lib/command.js.map +1 -1
- package/lib/component.d.ts +1 -1
- package/lib/component.d.ts.map +1 -1
- package/lib/component.js +8 -2
- package/lib/component.js.map +1 -1
- package/lib/cron.d.ts +3 -12
- package/lib/cron.d.ts.map +1 -1
- package/lib/cron.js +31 -64
- package/lib/cron.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/jsx.d.ts +6 -5
- package/lib/jsx.d.ts.map +1 -1
- package/lib/jsx.js +51 -14
- package/lib/jsx.js.map +1 -1
- package/lib/message.d.ts +1 -1
- package/lib/message.d.ts.map +1 -1
- package/lib/plugin.d.ts +1 -1
- package/lib/plugin.d.ts.map +1 -1
- package/lib/plugin.js +13 -1
- package/lib/plugin.js.map +1 -1
- package/package.json +6 -6
- package/src/command.ts +38 -1
- package/src/component.ts +9 -5
- package/src/cron.ts +33 -70
- package/src/index.ts +1 -1
- package/src/jsx.ts +61 -18
- package/src/message.ts +1 -1
- package/src/plugin.ts +20 -2
- package/tests/command.test.ts +77 -0
- package/tests/component-new.test.ts +3 -1
- package/tests/jsx.test.ts +25 -6
package/src/plugin.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { PermissionItem,PermissionChecker } from './permissions.js';
|
|
|
9
9
|
import {Message} from './message.js'
|
|
10
10
|
import { Schema } from '@zhin.js/hmr';
|
|
11
11
|
import {Dependency, Logger,} from "@zhin.js/hmr";
|
|
12
|
-
import {App} from "./app";
|
|
12
|
+
import {App} from "./app.js";
|
|
13
13
|
import {MessageCommand} from "./command.js";
|
|
14
14
|
import {Component, renderComponents} from "./component.js";
|
|
15
15
|
import { PluginError, MessageError, errorManager } from './errors.js';
|
|
@@ -194,7 +194,25 @@ export class Plugin extends Dependency<Plugin> {
|
|
|
194
194
|
for (const middleware of this.middlewares) {
|
|
195
195
|
this.dispatch('middleware.remove', middleware)
|
|
196
196
|
}
|
|
197
|
-
this.middlewares =
|
|
197
|
+
this.middlewares.length = 0; // 清空数组但保持引用
|
|
198
|
+
|
|
199
|
+
// 清理 commands
|
|
200
|
+
this.commands.length = 0;
|
|
201
|
+
|
|
202
|
+
// 清理权限
|
|
203
|
+
this.permissions.length = 0;
|
|
204
|
+
|
|
205
|
+
// 清理组件 Map
|
|
206
|
+
this.components.clear();
|
|
207
|
+
|
|
208
|
+
// 清理定义 Map
|
|
209
|
+
this.definitions.clear();
|
|
210
|
+
|
|
211
|
+
// 清理 crons(在 dispose 事件中已处理,这里确保清空)
|
|
212
|
+
this.crons.length = 0;
|
|
213
|
+
|
|
214
|
+
// 清空 logger 引用
|
|
215
|
+
this.#logger = undefined;
|
|
198
216
|
|
|
199
217
|
// 调用父类的dispose方法
|
|
200
218
|
super.dispose()
|
package/tests/command.test.ts
CHANGED
|
@@ -611,6 +611,83 @@ describe('Command系统测试', () => {
|
|
|
611
611
|
})
|
|
612
612
|
})
|
|
613
613
|
|
|
614
|
+
describe('帮助系统测试', () => {
|
|
615
|
+
it('应该正确设置和获取描述信息', () => {
|
|
616
|
+
const command = new MessageCommand('help')
|
|
617
|
+
.desc('这是命令描述', '可以有多行描述')
|
|
618
|
+
|
|
619
|
+
expect(command.helpInfo.desc).toEqual(['这是命令描述', '可以有多行描述'])
|
|
620
|
+
})
|
|
621
|
+
|
|
622
|
+
it('应该正确设置和获取用法信息', () => {
|
|
623
|
+
const command = new MessageCommand('help')
|
|
624
|
+
.usage('help', 'help <command>')
|
|
625
|
+
|
|
626
|
+
expect(command.helpInfo.usage).toEqual(['help', 'help <command>'])
|
|
627
|
+
})
|
|
628
|
+
|
|
629
|
+
it('应该正确设置和获取示例信息', () => {
|
|
630
|
+
const command = new MessageCommand('help')
|
|
631
|
+
.examples('help', 'help echo', 'help admin')
|
|
632
|
+
|
|
633
|
+
expect(command.helpInfo.examples).toEqual(['help', 'help echo', 'help admin'])
|
|
634
|
+
})
|
|
635
|
+
|
|
636
|
+
it('应该支持链式调用设置帮助信息', () => {
|
|
637
|
+
const command = new MessageCommand('test')
|
|
638
|
+
.desc('测试命令', '用于测试功能')
|
|
639
|
+
.usage('test', 'test <arg>')
|
|
640
|
+
.examples('test', 'test hello')
|
|
641
|
+
.action(() => 'Test response')
|
|
642
|
+
|
|
643
|
+
expect(command.helpInfo.pattern).toBe('test')
|
|
644
|
+
expect(command.helpInfo.desc).toEqual(['测试命令', '用于测试功能'])
|
|
645
|
+
expect(command.helpInfo.usage).toEqual(['test', 'test <arg>'])
|
|
646
|
+
expect(command.helpInfo.examples).toEqual(['test', 'test hello'])
|
|
647
|
+
})
|
|
648
|
+
|
|
649
|
+
it('应该正确生成帮助文本', () => {
|
|
650
|
+
const command = new MessageCommand('greet')
|
|
651
|
+
.desc('打招呼命令')
|
|
652
|
+
.usage('greet <name>')
|
|
653
|
+
.examples('greet Alice')
|
|
654
|
+
|
|
655
|
+
const help = command.help
|
|
656
|
+
|
|
657
|
+
expect(help).toContain('greet')
|
|
658
|
+
expect(help).toContain('打招呼命令')
|
|
659
|
+
expect(help).toContain('greet <name>')
|
|
660
|
+
expect(help).toContain('greet Alice')
|
|
661
|
+
})
|
|
662
|
+
|
|
663
|
+
it('应该处理没有帮助信息的情况', () => {
|
|
664
|
+
const command = new MessageCommand('simple')
|
|
665
|
+
|
|
666
|
+
expect(command.helpInfo.desc).toEqual([])
|
|
667
|
+
expect(command.helpInfo.usage).toEqual([])
|
|
668
|
+
expect(command.helpInfo.examples).toEqual([])
|
|
669
|
+
expect(command.help).toBe('simple')
|
|
670
|
+
})
|
|
671
|
+
|
|
672
|
+
it('应该正确返回 helpInfo 对象结构', () => {
|
|
673
|
+
const command = new MessageCommand('info')
|
|
674
|
+
.desc('信息命令')
|
|
675
|
+
.usage('info')
|
|
676
|
+
.examples('info')
|
|
677
|
+
|
|
678
|
+
const helpInfo = command.helpInfo
|
|
679
|
+
|
|
680
|
+
expect(helpInfo).toHaveProperty('pattern')
|
|
681
|
+
expect(helpInfo).toHaveProperty('desc')
|
|
682
|
+
expect(helpInfo).toHaveProperty('usage')
|
|
683
|
+
expect(helpInfo).toHaveProperty('examples')
|
|
684
|
+
expect(typeof helpInfo.pattern).toBe('string')
|
|
685
|
+
expect(Array.isArray(helpInfo.desc)).toBe(true)
|
|
686
|
+
expect(Array.isArray(helpInfo.usage)).toBe(true)
|
|
687
|
+
expect(Array.isArray(helpInfo.examples)).toBe(true)
|
|
688
|
+
})
|
|
689
|
+
})
|
|
690
|
+
|
|
614
691
|
describe('权限系统测试', () => {
|
|
615
692
|
it('应该正确处理权限检查失败', async () => {
|
|
616
693
|
const command = new MessageCommand('admin')
|
|
@@ -342,7 +342,9 @@ describe('函数式组件系统测试', () => {
|
|
|
342
342
|
bot: 'test'
|
|
343
343
|
}
|
|
344
344
|
|
|
345
|
-
await
|
|
345
|
+
const result = await renderComponents(componentMap, options)
|
|
346
|
+
// 错误会被捕获并转换为错误消息
|
|
347
|
+
expect(result.content).toMatch(/\[error Error: Test error\]/)
|
|
346
348
|
})
|
|
347
349
|
})
|
|
348
350
|
})
|
package/tests/jsx.test.ts
CHANGED
|
@@ -218,15 +218,15 @@ describe('JSX消息组件系统测试', () => {
|
|
|
218
218
|
})
|
|
219
219
|
|
|
220
220
|
describe('错误处理', () => {
|
|
221
|
-
it('
|
|
221
|
+
it('应该捕获组件执行错误并返回错误消息', async () => {
|
|
222
222
|
const ErrorComponent: Component<{}> = async () => {
|
|
223
223
|
throw new Error('Component error')
|
|
224
224
|
}
|
|
225
225
|
|
|
226
226
|
const element = jsx(ErrorComponent, {})
|
|
227
227
|
|
|
228
|
-
await
|
|
229
|
-
|
|
228
|
+
const result = await renderJSX(element as MessageComponent<any>, mockContext)
|
|
229
|
+
expect(result).toBe('❌ 组件渲染失败: Component error')
|
|
230
230
|
})
|
|
231
231
|
|
|
232
232
|
it('应该处理无效的元素类型', async () => {
|
|
@@ -235,9 +235,28 @@ describe('JSX消息组件系统测试', () => {
|
|
|
235
235
|
data: { children: 'test' }
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
238
|
+
const result = await renderJSX(invalidElement as MessageComponent<any>, mockContext)
|
|
239
|
+
expect(result).toMatch(/❌ 组件渲染失败/)
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it('应该处理异步组件返回 Promise 的情况', async () => {
|
|
243
|
+
const AsyncPromiseComponent: Component<{ text: string }> = async (props) => {
|
|
244
|
+
// 返回一个 Promise(会被自动 await)
|
|
245
|
+
return Promise.resolve(`Async: ${props.text}`)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const element = jsx(AsyncPromiseComponent, { text: 'test' })
|
|
249
|
+
const result = await renderJSX(element as MessageComponent<any>, mockContext)
|
|
250
|
+
|
|
251
|
+
expect(result).toBe('Async: test')
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
it('应该处理子组件为 Promise 的情况', async () => {
|
|
255
|
+
const AsyncChild = Promise.resolve('Async Child Content')
|
|
256
|
+
const element = jsx('div', { children: [AsyncChild] })
|
|
257
|
+
|
|
258
|
+
const result = await renderJSX(element as MessageComponent<any>, mockContext)
|
|
259
|
+
expect(result).toBe('Async Child Content')
|
|
241
260
|
})
|
|
242
261
|
})
|
|
243
262
|
|