@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.
@@ -34,54 +34,56 @@ vi.mock('segment-matcher', () => {
34
34
  return { SegmentMatcher, MatchResult }
35
35
  })
36
36
 
37
- // Mock Plugin
38
- const mockPlugin = {
39
- name: 'test-plugin',
40
- getPermit: 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
- })
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 plugin
63
- const multiPermitMockPlugin = {
64
- name: 'test-plugin',
65
- getPermit: vi.fn((permission: string) => {
66
- return {
67
- check: vi.fn(async (perm: string, message: any) => {
68
- // 对于多个权限,只要有一个匹配就返回 true
69
- if (permission === 'adapter(discord)' && message.$adapter === 'discord') {
70
- return true
71
- }
72
- if (permission === 'adapter(telegram)' && message.$adapter === 'telegram') {
73
- return true
74
- }
75
- if (permission === 'adapter(email)' && message.$adapter === 'email') {
76
- return true
77
- }
78
- if (permission === 'adapter(test)' && message.$adapter === 'test') {
79
- return true
80
- }
81
- return false
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, mockPlugin)
139
- const telegramResult = await command.handle(telegramMessage, mockPlugin)
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, mockPlugin)
187
- const telegramResult = await command.handle(telegramMessage, mockPlugin)
188
- const emailResult = await command.handle(emailMessage, mockPlugin)
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, mockPlugin)
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, mockPlugin)
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, mockPlugin)
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, mockPlugin)
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, mockPlugin)
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, mockPlugin)
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, mockPlugin)
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, mockPlugin)
416
- const wrongAdapterResult = await command.handle(wrongAdapterMessage, mockPlugin)
417
- const nonMatchingResult = await command.handle(nonMatchingMessage, mockPlugin)
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, mockPlugin)).rejects.toThrow('Action failed')
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, mockPlugin)).rejects.toThrow('Async action failed')
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, mockPlugin))
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, mockPlugin)
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, mockPlugin)
591
- const groupResult = await command.handle(groupMessage, mockPlugin)
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, mockPlugin)
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, mockPlugin)
639
+ const result = await command.handle(message, mockApp)
638
640
  expect(result).toBe('Admin command')
639
641
  })
640
642
  })
@@ -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
- plugin = app.createDependency('test-plugin', 'test-plugin.ts')
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).toBe('test-plugin.ts')
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).toBeGreaterThan(0)
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 plugin.emit('message.receive', mockMessage)
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
- await plugin.emit('message.receive', mockMessage)
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 plugin.emit('message.receive', mockMessage)
160
+ await app.receiveMessage(mockMessage)
158
161
 
159
- // 验证命令被调用(handle 现在需要传入 plugin 作为第二个参数)
160
- expect(handleSpy).toHaveBeenCalledWith(mockMessage, expect.any(Plugin))
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
- await plugin.emit('message.receive', mockMessage)
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 plugin.emit('message.receive', mockMessage)
365
+ await app.receiveMessage(mockMessage)
362
366
 
363
367
  // 验证中间件被调用
364
368
  expect(middlewareExecuted).toHaveBeenCalled()