@zhin.js/core 1.0.0
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/README.md +159 -0
- package/dist/adapter.d.ts +22 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +67 -0
- package/dist/adapter.js.map +1 -0
- package/dist/app.d.ts +69 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +307 -0
- package/dist/app.js.map +1 -0
- package/dist/bot.d.ts +9 -0
- package/dist/bot.d.ts.map +1 -0
- package/dist/bot.js +2 -0
- package/dist/bot.js.map +1 -0
- package/dist/config.d.ts +24 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +242 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +3 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +3 -0
- package/dist/logger.js.map +1 -0
- package/dist/plugin.d.ts +41 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +95 -0
- package/dist/plugin.js.map +1 -0
- package/dist/types-generator.d.ts +6 -0
- package/dist/types-generator.d.ts.map +1 -0
- package/dist/types-generator.js +69 -0
- package/dist/types-generator.js.map +1 -0
- package/dist/types.d.ts +69 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +29 -0
- package/src/adapter.ts +69 -0
- package/src/app.ts +339 -0
- package/src/bot.ts +9 -0
- package/src/config.ts +276 -0
- package/src/index.ts +14 -0
- package/src/logger.ts +3 -0
- package/src/plugin.ts +122 -0
- package/src/types-generator.ts +74 -0
- package/src/types.ts +74 -0
- package/tests/adapter.test.ts +187 -0
- package/tests/app.test.ts +207 -0
- package/tests/bot.test.ts +132 -0
- package/tests/config.test.ts +328 -0
- package/tests/logger.test.ts +170 -0
- package/tests/plugin.test.ts +226 -0
- package/tests/test-utils.ts +59 -0
- package/tests/types.test.ts +162 -0
- package/tsconfig.json +25 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
loadConfig,
|
|
4
|
+
saveConfig,
|
|
5
|
+
createDefaultConfig,
|
|
6
|
+
defineConfig,
|
|
7
|
+
ConfigFormat,
|
|
8
|
+
ConfigOptions
|
|
9
|
+
} from '../src/config'
|
|
10
|
+
import fs from 'node:fs'
|
|
11
|
+
import path from 'node:path'
|
|
12
|
+
import { AppConfig } from '../src/types'
|
|
13
|
+
|
|
14
|
+
describe('配置系统测试', () => {
|
|
15
|
+
const testDir = path.join(process.cwd(), 'test-config')
|
|
16
|
+
const originalEnv = process.env
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
// 创建测试目录
|
|
20
|
+
if (!fs.existsSync(testDir)) {
|
|
21
|
+
fs.mkdirSync(testDir)
|
|
22
|
+
}
|
|
23
|
+
// 重置环境变量
|
|
24
|
+
process.env = { ...originalEnv }
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
// 清理测试目录
|
|
29
|
+
if (fs.existsSync(testDir)) {
|
|
30
|
+
fs.rmSync(testDir, { recursive: true, force: true })
|
|
31
|
+
}
|
|
32
|
+
// 恢复环境变量
|
|
33
|
+
process.env = originalEnv
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
describe('配置文件加载测试', () => {
|
|
37
|
+
it('应该正确加载JSON配置文件', async () => {
|
|
38
|
+
const config: AppConfig = {
|
|
39
|
+
bots: [
|
|
40
|
+
{ name: '测试机器人', context: 'test' }
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
const configPath = path.join(testDir, 'zhin.config.json')
|
|
44
|
+
fs.writeFileSync(configPath, JSON.stringify(config))
|
|
45
|
+
|
|
46
|
+
const [loadedPath, loadedConfig] = await loadConfig({ configPath })
|
|
47
|
+
expect(loadedPath).toBe(configPath)
|
|
48
|
+
expect(loadedConfig).toEqual(config)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('应该正确加载YAML配置文件', async () => {
|
|
52
|
+
const config = `
|
|
53
|
+
bots:
|
|
54
|
+
- name: 测试机器人
|
|
55
|
+
context: test
|
|
56
|
+
`
|
|
57
|
+
const configPath = path.join(testDir, 'zhin.config.yaml')
|
|
58
|
+
fs.writeFileSync(configPath, config)
|
|
59
|
+
|
|
60
|
+
const [loadedPath, loadedConfig] = await loadConfig({ configPath })
|
|
61
|
+
expect(loadedPath).toBe(configPath)
|
|
62
|
+
expect(loadedConfig).toEqual({
|
|
63
|
+
bots: [
|
|
64
|
+
{ name: '测试机器人', context: 'test' }
|
|
65
|
+
]
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('应该正确加载环境变量', async () => {
|
|
70
|
+
process.env.BOT_NAME = '环境变量机器人'
|
|
71
|
+
const config = `
|
|
72
|
+
bots:
|
|
73
|
+
- name: \${BOT_NAME}
|
|
74
|
+
context: test
|
|
75
|
+
`
|
|
76
|
+
const configPath = path.join(testDir, 'zhin.config.yaml')
|
|
77
|
+
fs.writeFileSync(configPath, config)
|
|
78
|
+
|
|
79
|
+
const [, loadedConfig] = await loadConfig({ configPath })
|
|
80
|
+
expect(loadedConfig.bots[0].name).toBe('环境变量机器人')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('应该使用环境变量默认值', async () => {
|
|
84
|
+
const config = `
|
|
85
|
+
bots:
|
|
86
|
+
- name: \${BOT_NAME:-默认机器人}
|
|
87
|
+
context: test
|
|
88
|
+
`
|
|
89
|
+
const configPath = path.join(testDir, 'zhin.config.yaml')
|
|
90
|
+
fs.writeFileSync(configPath, config)
|
|
91
|
+
|
|
92
|
+
const [, loadedConfig] = await loadConfig({ configPath })
|
|
93
|
+
expect(loadedConfig.bots[0].name).toBe('默认机器人')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('应该正确加载JavaScript配置文件', async () => {
|
|
97
|
+
const config = `
|
|
98
|
+
module.exports = {
|
|
99
|
+
bots: [
|
|
100
|
+
{ name: '测试机器人', context: 'test' }
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
`
|
|
104
|
+
const configPath = path.join(testDir, 'zhin.config.js')
|
|
105
|
+
fs.writeFileSync(configPath, config)
|
|
106
|
+
|
|
107
|
+
const [loadedPath, loadedConfig] = await loadConfig({ configPath })
|
|
108
|
+
expect(loadedPath).toBe(configPath)
|
|
109
|
+
expect(loadedConfig).toEqual({
|
|
110
|
+
bots: [
|
|
111
|
+
{ name: '测试机器人', context: 'test' }
|
|
112
|
+
]
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('应该正确加载TypeScript配置文件', async () => {
|
|
117
|
+
// 创建配置文件
|
|
118
|
+
const configPath = path.join(testDir, 'zhin.config.ts')
|
|
119
|
+
fs.writeFileSync(configPath, `
|
|
120
|
+
export default {
|
|
121
|
+
bots: [
|
|
122
|
+
{ name: '测试机器人', context: 'test' }
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
`)
|
|
126
|
+
|
|
127
|
+
const [loadedPath, loadedConfig] = await loadConfig({ configPath })
|
|
128
|
+
expect(loadedPath).toBe(configPath)
|
|
129
|
+
expect(loadedConfig).toEqual({
|
|
130
|
+
bots: [
|
|
131
|
+
{ name: '测试机器人', context: 'test' }
|
|
132
|
+
]
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
describe('配置文件保存测试', () => {
|
|
138
|
+
it('应该正确保存JSON配置文件', () => {
|
|
139
|
+
const config: AppConfig = {
|
|
140
|
+
bots: [
|
|
141
|
+
{ name: '测试机器人', context: 'test' }
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
const filePath = path.join(testDir, 'config.json')
|
|
145
|
+
saveConfig(config, filePath)
|
|
146
|
+
|
|
147
|
+
const savedContent = fs.readFileSync(filePath, 'utf-8')
|
|
148
|
+
expect(JSON.parse(savedContent)).toEqual(config)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('应该正确保存YAML配置文件', () => {
|
|
152
|
+
const config: AppConfig = {
|
|
153
|
+
bots: [
|
|
154
|
+
{ name: '测试机器人', context: 'test' }
|
|
155
|
+
]
|
|
156
|
+
}
|
|
157
|
+
const filePath = path.join(testDir, 'config.yaml')
|
|
158
|
+
saveConfig(config, filePath)
|
|
159
|
+
|
|
160
|
+
const savedContent = fs.readFileSync(filePath, 'utf-8')
|
|
161
|
+
expect(savedContent).toContain('name: 测试机器人')
|
|
162
|
+
expect(savedContent).toContain('context: test')
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('应该拒绝保存不支持的格式', () => {
|
|
166
|
+
const config: AppConfig = {
|
|
167
|
+
bots: [
|
|
168
|
+
{ name: '测试机器人', context: 'test' }
|
|
169
|
+
]
|
|
170
|
+
}
|
|
171
|
+
const filePath = path.join(testDir, 'config.toml')
|
|
172
|
+
expect(() => saveConfig(config, filePath)).toThrow('暂不支持保存 TOML 格式的配置文件')
|
|
173
|
+
})
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
describe('配置验证测试', () => {
|
|
177
|
+
it('应该验证必需的配置字段', async () => {
|
|
178
|
+
const invalidConfig = {
|
|
179
|
+
plugins: []
|
|
180
|
+
}
|
|
181
|
+
const configPath = path.join(testDir, 'zhin.config.json')
|
|
182
|
+
fs.writeFileSync(configPath, JSON.stringify(invalidConfig))
|
|
183
|
+
|
|
184
|
+
await expect(loadConfig({ configPath })).rejects.toThrow('配置文件必须包含 bots 数组')
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('应该验证机器人配置', async () => {
|
|
188
|
+
const invalidConfig = {
|
|
189
|
+
bots: [
|
|
190
|
+
{ context: 'test' }
|
|
191
|
+
]
|
|
192
|
+
}
|
|
193
|
+
const configPath = path.join(testDir, 'zhin.config.json')
|
|
194
|
+
fs.writeFileSync(configPath, JSON.stringify(invalidConfig))
|
|
195
|
+
|
|
196
|
+
await expect(loadConfig({ configPath })).rejects.toThrow('机器人 0 缺少 name 字段')
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
it('应该验证机器人上下文', async () => {
|
|
200
|
+
const invalidConfig = {
|
|
201
|
+
bots: [
|
|
202
|
+
{ name: '测试机器人' }
|
|
203
|
+
]
|
|
204
|
+
}
|
|
205
|
+
const configPath = path.join(testDir, 'zhin.config.json')
|
|
206
|
+
fs.writeFileSync(configPath, JSON.stringify(invalidConfig))
|
|
207
|
+
|
|
208
|
+
await expect(loadConfig({ configPath })).rejects.toThrow('机器人 测试机器人 缺少 context 字段')
|
|
209
|
+
})
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
describe('默认配置测试', () => {
|
|
213
|
+
it('应该创建默认配置', () => {
|
|
214
|
+
const config = createDefaultConfig()
|
|
215
|
+
expect(config.bots).toHaveLength(1)
|
|
216
|
+
expect(config.bots[0].name).toBe('onebot11')
|
|
217
|
+
expect(config.plugin_dirs).toEqual(['./src/plugins', 'node_modules'])
|
|
218
|
+
expect(config.plugins).toEqual([])
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('应该支持环境变量替换', () => {
|
|
222
|
+
process.env.ONEBOT_URL = 'ws://example.com'
|
|
223
|
+
const config = createDefaultConfig()
|
|
224
|
+
expect(config.bots[0].url).toBe('${ONEBOT_URL:-ws://localhost:8080}')
|
|
225
|
+
})
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
describe('defineConfig辅助函数测试', () => {
|
|
229
|
+
it('应该正确定义配置', () => {
|
|
230
|
+
const config = defineConfig({
|
|
231
|
+
bots: [
|
|
232
|
+
{ name: '测试机器人', context: 'test' }
|
|
233
|
+
]
|
|
234
|
+
})
|
|
235
|
+
expect(config).toEqual({
|
|
236
|
+
bots: [
|
|
237
|
+
{ name: '测试机器人', context: 'test' }
|
|
238
|
+
]
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it('应该支持函数配置', () => {
|
|
243
|
+
const config = defineConfig((env) => ({
|
|
244
|
+
bots: [
|
|
245
|
+
{ name: env.BOT_NAME || '测试机器人', context: 'test' }
|
|
246
|
+
]
|
|
247
|
+
}))
|
|
248
|
+
expect(typeof config).toBe('function')
|
|
249
|
+
})
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
describe('配置文件查找测试', () => {
|
|
253
|
+
it('应该按优先级查找配置文件', async () => {
|
|
254
|
+
// 创建多个配置文件
|
|
255
|
+
fs.writeFileSync(
|
|
256
|
+
path.join(testDir, 'config.json'),
|
|
257
|
+
JSON.stringify({ bots: [{ name: 'json', context: 'test' }] })
|
|
258
|
+
)
|
|
259
|
+
fs.writeFileSync(
|
|
260
|
+
path.join(testDir, 'zhin.config.yaml'),
|
|
261
|
+
'bots:\n - name: yaml\n context: test'
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
const [configPath, loadedConfig] = await loadConfig({ configPath: path.join(testDir, 'zhin.config.yaml') })
|
|
265
|
+
expect(configPath).toContain('zhin.config.yaml')
|
|
266
|
+
expect(loadedConfig.bots[0].name).toBe('yaml')
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
it('当没有配置文件时应该抛出错误', async () => {
|
|
270
|
+
await expect(loadConfig({ configPath: path.join(testDir, 'non-existent.json') }))
|
|
271
|
+
.rejects.toThrow('配置文件不存在')
|
|
272
|
+
})
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
describe('环境变量加载测试', () => {
|
|
276
|
+
it('应该加载自定义环境文件', async () => {
|
|
277
|
+
// 创建环境文件
|
|
278
|
+
const envPath = path.join(testDir, '.env.test')
|
|
279
|
+
fs.writeFileSync(envPath, 'BOT_NAME=测试环境机器人')
|
|
280
|
+
|
|
281
|
+
const config = `
|
|
282
|
+
bots:
|
|
283
|
+
- name: \${BOT_NAME}
|
|
284
|
+
context: test
|
|
285
|
+
`
|
|
286
|
+
const configPath = path.join(testDir, 'zhin.config.yaml')
|
|
287
|
+
fs.writeFileSync(configPath, config)
|
|
288
|
+
|
|
289
|
+
const options: ConfigOptions = {
|
|
290
|
+
configPath,
|
|
291
|
+
envPath
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const [, loadedConfig] = await loadConfig(options)
|
|
295
|
+
expect(loadedConfig.bots[0].name).toBe('测试环境机器人')
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
it('应该正确处理环境变量覆盖', async () => {
|
|
299
|
+
process.env.BOT_NAME = '原始机器人'
|
|
300
|
+
const envPath = path.join(testDir, '.env')
|
|
301
|
+
fs.writeFileSync(envPath, 'BOT_NAME=新机器人')
|
|
302
|
+
|
|
303
|
+
const config = `
|
|
304
|
+
bots:
|
|
305
|
+
- name: \${BOT_NAME}
|
|
306
|
+
context: test
|
|
307
|
+
`
|
|
308
|
+
const configPath = path.join(testDir, 'zhin.config.yaml')
|
|
309
|
+
fs.writeFileSync(configPath, config)
|
|
310
|
+
|
|
311
|
+
// 不允许覆盖
|
|
312
|
+
const [, loadedConfig1] = await loadConfig({
|
|
313
|
+
configPath,
|
|
314
|
+
envPath,
|
|
315
|
+
envOverride: false
|
|
316
|
+
})
|
|
317
|
+
expect(loadedConfig1.bots[0].name).toBe('原始机器人')
|
|
318
|
+
|
|
319
|
+
// 允许覆盖
|
|
320
|
+
const [, loadedConfig2] = await loadConfig({
|
|
321
|
+
configPath,
|
|
322
|
+
envPath,
|
|
323
|
+
envOverride: true
|
|
324
|
+
})
|
|
325
|
+
expect(loadedConfig2.bots[0].name).toBe('新机器人')
|
|
326
|
+
})
|
|
327
|
+
})
|
|
328
|
+
})
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
2
|
+
import { ConsoleLogger } from '@zhin.js/hmr'
|
|
3
|
+
|
|
4
|
+
describe('日志记录器测试', () => {
|
|
5
|
+
let logger: ConsoleLogger
|
|
6
|
+
let consoleLogSpy: any
|
|
7
|
+
let consoleInfoSpy: any
|
|
8
|
+
let consoleWarnSpy: any
|
|
9
|
+
let consoleErrorSpy: any
|
|
10
|
+
const originalNodeEnv = process.env.NODE_ENV
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
// 保存原始的console方法
|
|
14
|
+
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
15
|
+
consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {})
|
|
16
|
+
consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
17
|
+
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
// 恢复原始的console方法
|
|
22
|
+
consoleLogSpy.mockRestore()
|
|
23
|
+
consoleInfoSpy.mockRestore()
|
|
24
|
+
consoleWarnSpy.mockRestore()
|
|
25
|
+
consoleErrorSpy.mockRestore()
|
|
26
|
+
// 恢复原始的NODE_ENV
|
|
27
|
+
process.env.NODE_ENV = originalNodeEnv
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
describe('开发环境测试', () => {
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
process.env.NODE_ENV = 'development'
|
|
33
|
+
logger = new ConsoleLogger('[测试]')
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('应该正确记录调试信息', () => {
|
|
37
|
+
logger.debug('调试消息', { data: 'test' })
|
|
38
|
+
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
39
|
+
'[DEBUG] [测试]: 调试消息',
|
|
40
|
+
{ data: 'test' }
|
|
41
|
+
)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('应该正确记录普通信息', () => {
|
|
45
|
+
logger.info('普通消息', { data: 'test' })
|
|
46
|
+
expect(consoleInfoSpy).toHaveBeenCalledWith(
|
|
47
|
+
'[INFO] [测试]: 普通消息',
|
|
48
|
+
{ data: 'test' }
|
|
49
|
+
)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('应该正确记录警告信息', () => {
|
|
53
|
+
logger.warn('警告消息', { data: 'test' })
|
|
54
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
55
|
+
'[WARN] [测试]: 警告消息',
|
|
56
|
+
{ data: 'test' }
|
|
57
|
+
)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('应该正确记录错误信息', () => {
|
|
61
|
+
logger.error('错误消息', { data: 'test' })
|
|
62
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
63
|
+
'[ERROR] [测试]: 错误消息',
|
|
64
|
+
{ data: 'test' }
|
|
65
|
+
)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('生产环境测试', () => {
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
process.env.NODE_ENV = 'production'
|
|
72
|
+
logger = new ConsoleLogger('[测试]')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('不应该记录调试信息', () => {
|
|
76
|
+
logger.debug('调试消息', { data: 'test' })
|
|
77
|
+
expect(consoleLogSpy).not.toHaveBeenCalled()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('应该正确记录普通信息', () => {
|
|
81
|
+
logger.info('普通消息', { data: 'test' })
|
|
82
|
+
expect(consoleInfoSpy).toHaveBeenCalledWith(
|
|
83
|
+
'[INFO] [测试]: 普通消息',
|
|
84
|
+
{ data: 'test' }
|
|
85
|
+
)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('应该正确记录警告信息', () => {
|
|
89
|
+
logger.warn('警告消息', { data: 'test' })
|
|
90
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
91
|
+
'[WARN] [测试]: 警告消息',
|
|
92
|
+
{ data: 'test' }
|
|
93
|
+
)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('应该正确记录错误信息', () => {
|
|
97
|
+
logger.error('错误消息', { data: 'test' })
|
|
98
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
99
|
+
'[ERROR] [测试]: 错误消息',
|
|
100
|
+
{ data: 'test' }
|
|
101
|
+
)
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
describe('自定义调试模式测试', () => {
|
|
106
|
+
it('启用调试模式时应该记录调试信息', () => {
|
|
107
|
+
const debugLogger = new ConsoleLogger('[测试]', true)
|
|
108
|
+
debugLogger.debug('调试消息', { data: 'test' })
|
|
109
|
+
expect(consoleLogSpy).toHaveBeenCalledWith(
|
|
110
|
+
'[DEBUG] [测试]: 调试消息',
|
|
111
|
+
{ data: 'test' }
|
|
112
|
+
)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('禁用调试模式时不应该记录调试信息', () => {
|
|
116
|
+
const debugLogger = new ConsoleLogger('[测试]', false)
|
|
117
|
+
debugLogger.debug('调试消息', { data: 'test' })
|
|
118
|
+
expect(consoleLogSpy).not.toHaveBeenCalled()
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
describe('多参数测试', () => {
|
|
123
|
+
beforeEach(() => {
|
|
124
|
+
logger = new ConsoleLogger('[测试]', true)
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('应该正确处理多个参数', () => {
|
|
128
|
+
logger.info('消息', 123, { a: 1 }, [1, 2, 3])
|
|
129
|
+
expect(consoleInfoSpy).toHaveBeenCalledWith(
|
|
130
|
+
'[INFO] [测试]: 消息',
|
|
131
|
+
123,
|
|
132
|
+
{ a: 1 },
|
|
133
|
+
[1, 2, 3]
|
|
134
|
+
)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('应该正确处理没有额外参数的情况', () => {
|
|
138
|
+
logger.info('消息')
|
|
139
|
+
expect(consoleInfoSpy).toHaveBeenCalledWith(
|
|
140
|
+
'[INFO] [测试]: 消息'
|
|
141
|
+
)
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
describe('前缀格式测试', () => {
|
|
146
|
+
it('应该正确处理简单前缀', () => {
|
|
147
|
+
const simpleLogger = new ConsoleLogger('测试')
|
|
148
|
+
simpleLogger.info('消息')
|
|
149
|
+
expect(consoleInfoSpy).toHaveBeenCalledWith(
|
|
150
|
+
'[INFO] 测试: 消息'
|
|
151
|
+
)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
it('应该正确处理带方括号的前缀', () => {
|
|
155
|
+
const bracketLogger = new ConsoleLogger('[测试]')
|
|
156
|
+
bracketLogger.info('消息')
|
|
157
|
+
expect(consoleInfoSpy).toHaveBeenCalledWith(
|
|
158
|
+
'[INFO] [测试]: 消息'
|
|
159
|
+
)
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('应该正确处理多层级前缀', () => {
|
|
163
|
+
const nestedLogger = new ConsoleLogger('[模块/子模块]')
|
|
164
|
+
nestedLogger.info('消息')
|
|
165
|
+
expect(consoleInfoSpy).toHaveBeenCalledWith(
|
|
166
|
+
'[INFO] [模块/子模块]: 消息'
|
|
167
|
+
)
|
|
168
|
+
})
|
|
169
|
+
})
|
|
170
|
+
})
|