@yivan-lab/pretty-please 1.3.1 → 1.5.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 +250 -620
- package/bin/pls.tsx +178 -40
- package/dist/bin/pls.js +149 -27
- package/dist/package.json +10 -2
- package/dist/src/__integration__/command-generation.test.d.ts +5 -0
- package/dist/src/__integration__/command-generation.test.js +508 -0
- package/dist/src/__integration__/error-recovery.test.d.ts +5 -0
- package/dist/src/__integration__/error-recovery.test.js +511 -0
- package/dist/src/__integration__/shell-hook-workflow.test.d.ts +5 -0
- package/dist/src/__integration__/shell-hook-workflow.test.js +375 -0
- package/dist/src/__tests__/alias.test.d.ts +5 -0
- package/dist/src/__tests__/alias.test.js +421 -0
- package/dist/src/__tests__/chat-history.test.d.ts +5 -0
- package/dist/src/__tests__/chat-history.test.js +372 -0
- package/dist/src/__tests__/config.test.d.ts +5 -0
- package/dist/src/__tests__/config.test.js +822 -0
- package/dist/src/__tests__/history.test.d.ts +5 -0
- package/dist/src/__tests__/history.test.js +439 -0
- package/dist/src/__tests__/remote-history.test.d.ts +5 -0
- package/dist/src/__tests__/remote-history.test.js +641 -0
- package/dist/src/__tests__/remote.test.d.ts +5 -0
- package/dist/src/__tests__/remote.test.js +689 -0
- package/dist/src/__tests__/shell-hook-install.test.d.ts +5 -0
- package/dist/src/__tests__/shell-hook-install.test.js +413 -0
- package/dist/src/__tests__/shell-hook-remote.test.d.ts +5 -0
- package/dist/src/__tests__/shell-hook-remote.test.js +507 -0
- package/dist/src/__tests__/shell-hook.test.d.ts +5 -0
- package/dist/src/__tests__/shell-hook.test.js +440 -0
- package/dist/src/__tests__/sysinfo.test.d.ts +5 -0
- package/dist/src/__tests__/sysinfo.test.js +572 -0
- package/dist/src/__tests__/system-history.test.d.ts +5 -0
- package/dist/src/__tests__/system-history.test.js +457 -0
- package/dist/src/components/Chat.js +9 -28
- package/dist/src/config.d.ts +2 -0
- package/dist/src/config.js +30 -2
- package/dist/src/mastra-chat.js +10 -6
- package/dist/src/multi-step.js +10 -8
- package/dist/src/project-context.d.ts +22 -0
- package/dist/src/project-context.js +168 -0
- package/dist/src/prompts.d.ts +4 -4
- package/dist/src/prompts.js +23 -6
- package/dist/src/shell-hook.d.ts +32 -0
- package/dist/src/shell-hook.js +226 -33
- package/dist/src/sysinfo.d.ts +38 -9
- package/dist/src/sysinfo.js +245 -21
- package/dist/src/system-history.d.ts +18 -0
- package/dist/src/system-history.js +151 -0
- package/dist/src/ui/__tests__/theme.test.d.ts +5 -0
- package/dist/src/ui/__tests__/theme.test.js +688 -0
- package/dist/src/upgrade.js +3 -0
- package/dist/src/user-preferences.d.ts +44 -0
- package/dist/src/user-preferences.js +147 -0
- package/dist/src/utils/__tests__/platform-capabilities.test.d.ts +5 -0
- package/dist/src/utils/__tests__/platform-capabilities.test.js +214 -0
- package/dist/src/utils/__tests__/platform-exec.test.d.ts +5 -0
- package/dist/src/utils/__tests__/platform-exec.test.js +212 -0
- package/dist/src/utils/__tests__/platform-shell.test.d.ts +5 -0
- package/dist/src/utils/__tests__/platform-shell.test.js +300 -0
- package/dist/src/utils/__tests__/platform.test.d.ts +5 -0
- package/dist/src/utils/__tests__/platform.test.js +137 -0
- package/dist/src/utils/platform.d.ts +88 -0
- package/dist/src/utils/platform.js +331 -0
- package/package.json +10 -2
- package/src/__integration__/command-generation.test.ts +602 -0
- package/src/__integration__/error-recovery.test.ts +620 -0
- package/src/__integration__/shell-hook-workflow.test.ts +457 -0
- package/src/__tests__/alias.test.ts +545 -0
- package/src/__tests__/chat-history.test.ts +462 -0
- package/src/__tests__/config.test.ts +1043 -0
- package/src/__tests__/history.test.ts +538 -0
- package/src/__tests__/remote-history.test.ts +791 -0
- package/src/__tests__/remote.test.ts +866 -0
- package/src/__tests__/shell-hook-install.test.ts +510 -0
- package/src/__tests__/shell-hook-remote.test.ts +679 -0
- package/src/__tests__/shell-hook.test.ts +564 -0
- package/src/__tests__/sysinfo.test.ts +718 -0
- package/src/__tests__/system-history.test.ts +608 -0
- package/src/components/Chat.tsx +10 -37
- package/src/config.ts +29 -2
- package/src/mastra-chat.ts +12 -5
- package/src/multi-step.ts +11 -5
- package/src/project-context.ts +191 -0
- package/src/prompts.ts +26 -5
- package/src/shell-hook.ts +254 -32
- package/src/sysinfo.ts +326 -25
- package/src/system-history.ts +170 -0
- package/src/ui/__tests__/theme.test.ts +869 -0
- package/src/upgrade.ts +5 -0
- package/src/user-preferences.ts +178 -0
- package/src/utils/__tests__/platform-capabilities.test.ts +265 -0
- package/src/utils/__tests__/platform-exec.test.ts +278 -0
- package/src/utils/__tests__/platform-shell.test.ts +353 -0
- package/src/utils/__tests__/platform.test.ts +170 -0
- package/src/utils/platform.ts +431 -0
|
@@ -0,0 +1,718 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 系统信息检测模块测试
|
|
3
|
+
* 测试命令检测、包管理器检测、缓存机制、系统信息集成等功能
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
7
|
+
import {
|
|
8
|
+
validSystemCache,
|
|
9
|
+
expiredSystemCache,
|
|
10
|
+
freshSystemCache,
|
|
11
|
+
windowsSystemCache,
|
|
12
|
+
linuxSystemCache,
|
|
13
|
+
corruptedSystemCacheJson,
|
|
14
|
+
} from '../../tests/fixtures/system-cache'
|
|
15
|
+
|
|
16
|
+
// Mock fs 模块
|
|
17
|
+
vi.mock('fs', () => ({
|
|
18
|
+
default: {
|
|
19
|
+
existsSync: vi.fn(),
|
|
20
|
+
readFileSync: vi.fn(),
|
|
21
|
+
writeFileSync: vi.fn(),
|
|
22
|
+
mkdirSync: vi.fn(),
|
|
23
|
+
},
|
|
24
|
+
}))
|
|
25
|
+
|
|
26
|
+
// Mock os 模块
|
|
27
|
+
vi.mock('os', () => ({
|
|
28
|
+
default: {
|
|
29
|
+
platform: vi.fn(() => 'darwin'),
|
|
30
|
+
arch: vi.fn(() => 'arm64'),
|
|
31
|
+
userInfo: vi.fn(() => ({ username: 'testuser' })),
|
|
32
|
+
},
|
|
33
|
+
}))
|
|
34
|
+
|
|
35
|
+
// Mock config 模块
|
|
36
|
+
vi.mock('../config.js', () => ({
|
|
37
|
+
getConfig: vi.fn(() => ({
|
|
38
|
+
systemCacheExpireDays: 7,
|
|
39
|
+
})),
|
|
40
|
+
CONFIG_DIR: '/home/user/.please',
|
|
41
|
+
}))
|
|
42
|
+
|
|
43
|
+
// Mock platform 模块
|
|
44
|
+
vi.mock('../utils/platform.js', () => ({
|
|
45
|
+
detectShell: vi.fn(() => 'zsh'),
|
|
46
|
+
getShellCapabilities: vi.fn(() => ({
|
|
47
|
+
displayName: 'Zsh',
|
|
48
|
+
supportsHistory: true,
|
|
49
|
+
supportsHook: true,
|
|
50
|
+
})),
|
|
51
|
+
commandExists: vi.fn(() => false),
|
|
52
|
+
batchCommandExists: vi.fn(() => []),
|
|
53
|
+
isWindows: vi.fn(() => false),
|
|
54
|
+
}))
|
|
55
|
+
|
|
56
|
+
// Mock project-context 模块
|
|
57
|
+
vi.mock('../project-context.js', () => ({
|
|
58
|
+
detectProjectContext: vi.fn(() => null),
|
|
59
|
+
formatProjectContext: vi.fn(() => ''),
|
|
60
|
+
}))
|
|
61
|
+
|
|
62
|
+
// Mock theme 模块
|
|
63
|
+
vi.mock('../ui/theme.js', () => ({
|
|
64
|
+
getCurrentTheme: vi.fn(() => ({
|
|
65
|
+
primary: '#007acc',
|
|
66
|
+
success: '#4caf50',
|
|
67
|
+
warning: '#ff9800',
|
|
68
|
+
text: {
|
|
69
|
+
muted: '#666666',
|
|
70
|
+
secondary: '#999999',
|
|
71
|
+
},
|
|
72
|
+
})),
|
|
73
|
+
}))
|
|
74
|
+
|
|
75
|
+
// Mock chalk
|
|
76
|
+
vi.mock('chalk', () => ({
|
|
77
|
+
default: {
|
|
78
|
+
bold: vi.fn((s: string) => s),
|
|
79
|
+
hex: vi.fn(() => (s: string) => s),
|
|
80
|
+
},
|
|
81
|
+
}))
|
|
82
|
+
|
|
83
|
+
import fs from 'fs'
|
|
84
|
+
import os from 'os'
|
|
85
|
+
import { getConfig, CONFIG_DIR } from '../config.js'
|
|
86
|
+
import {
|
|
87
|
+
detectShell,
|
|
88
|
+
getShellCapabilities,
|
|
89
|
+
commandExists,
|
|
90
|
+
batchCommandExists,
|
|
91
|
+
isWindows,
|
|
92
|
+
} from '../utils/platform.js'
|
|
93
|
+
import { detectProjectContext } from '../project-context.js'
|
|
94
|
+
|
|
95
|
+
// 获取 mock 函数引用
|
|
96
|
+
const mockFs = vi.mocked(fs)
|
|
97
|
+
const mockOs = vi.mocked(os)
|
|
98
|
+
const mockDetectShell = vi.mocked(detectShell)
|
|
99
|
+
const mockGetShellCapabilities = vi.mocked(getShellCapabilities)
|
|
100
|
+
const mockCommandExists = vi.mocked(commandExists)
|
|
101
|
+
const mockBatchCommandExists = vi.mocked(batchCommandExists)
|
|
102
|
+
const mockIsWindows = vi.mocked(isWindows)
|
|
103
|
+
const mockGetConfig = vi.mocked(getConfig)
|
|
104
|
+
const mockDetectProjectContext = vi.mocked(detectProjectContext)
|
|
105
|
+
|
|
106
|
+
beforeEach(() => {
|
|
107
|
+
vi.clearAllMocks()
|
|
108
|
+
|
|
109
|
+
// 默认配置
|
|
110
|
+
mockGetConfig.mockReturnValue({
|
|
111
|
+
systemCacheExpireDays: 7,
|
|
112
|
+
} as any)
|
|
113
|
+
|
|
114
|
+
// 默认 macOS 环境
|
|
115
|
+
mockOs.platform.mockReturnValue('darwin')
|
|
116
|
+
mockOs.arch.mockReturnValue('arm64')
|
|
117
|
+
mockOs.userInfo.mockReturnValue({ username: 'testuser' } as any)
|
|
118
|
+
|
|
119
|
+
mockDetectShell.mockReturnValue('zsh')
|
|
120
|
+
mockGetShellCapabilities.mockReturnValue({
|
|
121
|
+
displayName: 'Zsh',
|
|
122
|
+
supportsHistory: true,
|
|
123
|
+
supportsHook: true,
|
|
124
|
+
} as any)
|
|
125
|
+
|
|
126
|
+
mockIsWindows.mockReturnValue(false)
|
|
127
|
+
mockCommandExists.mockReturnValue(false)
|
|
128
|
+
mockBatchCommandExists.mockReturnValue([])
|
|
129
|
+
|
|
130
|
+
// 默认配置目录存在
|
|
131
|
+
mockFs.existsSync.mockReturnValue(true)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
afterEach(() => {
|
|
135
|
+
vi.restoreAllMocks()
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
// ============================================================================
|
|
139
|
+
// 包管理器检测测试
|
|
140
|
+
// ============================================================================
|
|
141
|
+
|
|
142
|
+
describe('包管理器检测', () => {
|
|
143
|
+
describe('Windows 包管理器', () => {
|
|
144
|
+
beforeEach(() => {
|
|
145
|
+
mockIsWindows.mockReturnValue(true)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('应该优先检测 winget', async () => {
|
|
149
|
+
mockCommandExists.mockImplementation((cmd: string) => cmd === 'winget')
|
|
150
|
+
|
|
151
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
152
|
+
const info = getStaticSystemInfo()
|
|
153
|
+
|
|
154
|
+
expect(info.systemPackageManager).toBe('winget')
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('winget 不存在时应该降级到 scoop', async () => {
|
|
158
|
+
mockCommandExists.mockImplementation((cmd: string) => cmd === 'scoop')
|
|
159
|
+
|
|
160
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
161
|
+
const info = getStaticSystemInfo()
|
|
162
|
+
|
|
163
|
+
expect(info.systemPackageManager).toBe('scoop')
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('scoop 不存在时应该降级到 choco', async () => {
|
|
167
|
+
mockCommandExists.mockImplementation((cmd: string) => cmd === 'choco')
|
|
168
|
+
|
|
169
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
170
|
+
const info = getStaticSystemInfo()
|
|
171
|
+
|
|
172
|
+
expect(info.systemPackageManager).toBe('choco')
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('都不存在时应该返回 unknown', async () => {
|
|
176
|
+
mockCommandExists.mockReturnValue(false)
|
|
177
|
+
|
|
178
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
179
|
+
const info = getStaticSystemInfo()
|
|
180
|
+
|
|
181
|
+
expect(info.systemPackageManager).toBe('unknown')
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
describe('macOS 包管理器', () => {
|
|
186
|
+
beforeEach(() => {
|
|
187
|
+
mockIsWindows.mockReturnValue(false)
|
|
188
|
+
mockOs.platform.mockReturnValue('darwin')
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('应该检测到 brew', async () => {
|
|
192
|
+
mockCommandExists.mockImplementation((cmd: string) => cmd === 'brew')
|
|
193
|
+
|
|
194
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
195
|
+
const info = getStaticSystemInfo()
|
|
196
|
+
|
|
197
|
+
expect(info.systemPackageManager).toBe('brew')
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
describe('Linux 包管理器', () => {
|
|
202
|
+
beforeEach(() => {
|
|
203
|
+
mockIsWindows.mockReturnValue(false)
|
|
204
|
+
mockOs.platform.mockReturnValue('linux')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('应该检测 apt-get', async () => {
|
|
208
|
+
mockCommandExists.mockImplementation((cmd: string) => cmd === 'apt-get')
|
|
209
|
+
|
|
210
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
211
|
+
const info = getStaticSystemInfo()
|
|
212
|
+
|
|
213
|
+
expect(info.systemPackageManager).toBe('apt')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('应该检测 dnf', async () => {
|
|
217
|
+
mockCommandExists.mockImplementation((cmd: string) => cmd === 'dnf')
|
|
218
|
+
|
|
219
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
220
|
+
const info = getStaticSystemInfo()
|
|
221
|
+
|
|
222
|
+
expect(info.systemPackageManager).toBe('dnf')
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('应该检测 pacman', async () => {
|
|
226
|
+
mockCommandExists.mockImplementation((cmd: string) => cmd === 'pacman')
|
|
227
|
+
|
|
228
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
229
|
+
const info = getStaticSystemInfo()
|
|
230
|
+
|
|
231
|
+
expect(info.systemPackageManager).toBe('pacman')
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('应该按优先级返回第一个可用的', async () => {
|
|
235
|
+
// apt-get 和 dnf 都存在,应该返回 apt(优先级更高)
|
|
236
|
+
mockCommandExists.mockImplementation((cmd: string) =>
|
|
237
|
+
cmd === 'apt-get' || cmd === 'dnf'
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
241
|
+
const info = getStaticSystemInfo()
|
|
242
|
+
|
|
243
|
+
// brew 在 apt-get 之前检测
|
|
244
|
+
expect(info.systemPackageManager).toBe('apt')
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
// ============================================================================
|
|
250
|
+
// 命令检测测试
|
|
251
|
+
// ============================================================================
|
|
252
|
+
|
|
253
|
+
describe('命令检测', () => {
|
|
254
|
+
it('应该使用 batchCommandExists 检测所有命令', async () => {
|
|
255
|
+
mockBatchCommandExists.mockReturnValue(['git', 'npm', 'docker'])
|
|
256
|
+
|
|
257
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
258
|
+
const info = getStaticSystemInfo()
|
|
259
|
+
|
|
260
|
+
expect(mockBatchCommandExists).toHaveBeenCalled()
|
|
261
|
+
expect(info.availableCommands).toEqual(['git', 'npm', 'docker'])
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
it('命令都不可用时应该返回空数组', async () => {
|
|
265
|
+
mockBatchCommandExists.mockReturnValue([])
|
|
266
|
+
|
|
267
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
268
|
+
const info = getStaticSystemInfo()
|
|
269
|
+
|
|
270
|
+
expect(info.availableCommands).toEqual([])
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it('应该返回可用命令列表', async () => {
|
|
274
|
+
mockBatchCommandExists.mockReturnValue(['eza', 'fd', 'rg', 'bat', 'jq'])
|
|
275
|
+
|
|
276
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
277
|
+
const info = getStaticSystemInfo()
|
|
278
|
+
|
|
279
|
+
expect(info.availableCommands).toContain('eza')
|
|
280
|
+
expect(info.availableCommands).toContain('fd')
|
|
281
|
+
expect(info.availableCommands).toContain('rg')
|
|
282
|
+
})
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
// ============================================================================
|
|
286
|
+
// 缓存机制测试
|
|
287
|
+
// ============================================================================
|
|
288
|
+
|
|
289
|
+
describe('缓存机制', () => {
|
|
290
|
+
describe('缓存写入', () => {
|
|
291
|
+
it('首次调用应该生成缓存文件', async () => {
|
|
292
|
+
// 缓存文件不存在
|
|
293
|
+
mockFs.existsSync.mockImplementation((path: any) => {
|
|
294
|
+
if (path === CONFIG_DIR) return true
|
|
295
|
+
return false // 缓存文件不存在
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
299
|
+
getStaticSystemInfo()
|
|
300
|
+
|
|
301
|
+
expect(mockFs.writeFileSync).toHaveBeenCalled()
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
it('缓存文件应该包含 version/cachedAt/expiresInDays', async () => {
|
|
305
|
+
mockFs.existsSync.mockImplementation((path: any) => {
|
|
306
|
+
if (path === CONFIG_DIR) return true
|
|
307
|
+
return false
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
let writtenContent: string = ''
|
|
311
|
+
mockFs.writeFileSync.mockImplementation((path: any, content: any) => {
|
|
312
|
+
writtenContent = content
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
316
|
+
getStaticSystemInfo()
|
|
317
|
+
|
|
318
|
+
const cache = JSON.parse(writtenContent)
|
|
319
|
+
expect(cache.version).toBe(1)
|
|
320
|
+
expect(cache.cachedAt).toBeDefined()
|
|
321
|
+
expect(cache.expiresInDays).toBe(7)
|
|
322
|
+
expect(cache.static).toBeDefined()
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
it('缓存文件应该是合法 JSON', async () => {
|
|
326
|
+
mockFs.existsSync.mockImplementation((path: any) => {
|
|
327
|
+
if (path === CONFIG_DIR) return true
|
|
328
|
+
return false
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
let writtenContent: string = ''
|
|
332
|
+
mockFs.writeFileSync.mockImplementation((path: any, content: any) => {
|
|
333
|
+
writtenContent = content
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
337
|
+
getStaticSystemInfo()
|
|
338
|
+
|
|
339
|
+
expect(() => JSON.parse(writtenContent)).not.toThrow()
|
|
340
|
+
})
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
describe('缓存读取', () => {
|
|
344
|
+
it('缓存未过期时应该返回缓存数据', async () => {
|
|
345
|
+
mockFs.existsSync.mockReturnValue(true)
|
|
346
|
+
mockFs.readFileSync.mockReturnValue(JSON.stringify({
|
|
347
|
+
version: 1,
|
|
348
|
+
cachedAt: new Date().toISOString(), // 刚刚创建
|
|
349
|
+
expiresInDays: 7,
|
|
350
|
+
static: {
|
|
351
|
+
os: 'darwin',
|
|
352
|
+
arch: 'arm64',
|
|
353
|
+
shell: 'Zsh',
|
|
354
|
+
user: 'cacheduser',
|
|
355
|
+
systemPackageManager: 'brew',
|
|
356
|
+
availableCommands: ['git', 'cached-cmd'],
|
|
357
|
+
},
|
|
358
|
+
}))
|
|
359
|
+
|
|
360
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
361
|
+
const info = getStaticSystemInfo()
|
|
362
|
+
|
|
363
|
+
// 应该返回缓存数据
|
|
364
|
+
expect(info.user).toBe('cacheduser')
|
|
365
|
+
expect(info.availableCommands).toContain('cached-cmd')
|
|
366
|
+
// 不应该写入新缓存
|
|
367
|
+
expect(mockFs.writeFileSync).not.toHaveBeenCalled()
|
|
368
|
+
})
|
|
369
|
+
|
|
370
|
+
it('缓存过期后应该重新检测', async () => {
|
|
371
|
+
const expiredDate = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000).toISOString()
|
|
372
|
+
mockFs.existsSync.mockReturnValue(true)
|
|
373
|
+
mockFs.readFileSync.mockReturnValue(JSON.stringify({
|
|
374
|
+
version: 1,
|
|
375
|
+
cachedAt: expiredDate,
|
|
376
|
+
expiresInDays: 7,
|
|
377
|
+
static: {
|
|
378
|
+
os: 'darwin',
|
|
379
|
+
arch: 'arm64',
|
|
380
|
+
shell: 'Zsh',
|
|
381
|
+
user: 'olduser',
|
|
382
|
+
systemPackageManager: 'brew',
|
|
383
|
+
availableCommands: [],
|
|
384
|
+
},
|
|
385
|
+
}))
|
|
386
|
+
|
|
387
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
388
|
+
const info = getStaticSystemInfo()
|
|
389
|
+
|
|
390
|
+
// 应该重新检测,返回当前 mock 的用户
|
|
391
|
+
expect(info.user).toBe('testuser')
|
|
392
|
+
// 应该写入新缓存
|
|
393
|
+
expect(mockFs.writeFileSync).toHaveBeenCalled()
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
it('缓存文件损坏时应该重新检测', async () => {
|
|
397
|
+
mockFs.existsSync.mockReturnValue(true)
|
|
398
|
+
mockFs.readFileSync.mockReturnValue(corruptedSystemCacheJson)
|
|
399
|
+
|
|
400
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
401
|
+
const info = getStaticSystemInfo()
|
|
402
|
+
|
|
403
|
+
// 应该重新检测
|
|
404
|
+
expect(info.user).toBe('testuser')
|
|
405
|
+
expect(mockFs.writeFileSync).toHaveBeenCalled()
|
|
406
|
+
})
|
|
407
|
+
|
|
408
|
+
it('systemCacheExpireDays 配置应该生效', async () => {
|
|
409
|
+
// 配置为 1 天过期
|
|
410
|
+
mockGetConfig.mockReturnValue({ systemCacheExpireDays: 1 } as any)
|
|
411
|
+
|
|
412
|
+
// 缓存 2 天前创建
|
|
413
|
+
const twoDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString()
|
|
414
|
+
mockFs.existsSync.mockReturnValue(true)
|
|
415
|
+
mockFs.readFileSync.mockReturnValue(JSON.stringify({
|
|
416
|
+
version: 1,
|
|
417
|
+
cachedAt: twoDaysAgo,
|
|
418
|
+
expiresInDays: 1,
|
|
419
|
+
static: {
|
|
420
|
+
os: 'darwin',
|
|
421
|
+
arch: 'arm64',
|
|
422
|
+
shell: 'Zsh',
|
|
423
|
+
user: 'olduser',
|
|
424
|
+
systemPackageManager: 'brew',
|
|
425
|
+
availableCommands: [],
|
|
426
|
+
},
|
|
427
|
+
}))
|
|
428
|
+
|
|
429
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
430
|
+
const info = getStaticSystemInfo()
|
|
431
|
+
|
|
432
|
+
// 应该过期并重新检测
|
|
433
|
+
expect(info.user).toBe('testuser')
|
|
434
|
+
})
|
|
435
|
+
})
|
|
436
|
+
|
|
437
|
+
describe('目录创建', () => {
|
|
438
|
+
it('CONFIG_DIR 不存在时应该自动创建', async () => {
|
|
439
|
+
mockFs.existsSync.mockImplementation((path: any) => {
|
|
440
|
+
if (path === CONFIG_DIR) return false // 目录不存在
|
|
441
|
+
return false
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
445
|
+
getStaticSystemInfo()
|
|
446
|
+
|
|
447
|
+
expect(mockFs.mkdirSync).toHaveBeenCalledWith(CONFIG_DIR, { recursive: true })
|
|
448
|
+
})
|
|
449
|
+
})
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
// ============================================================================
|
|
453
|
+
// 系统信息集成测试
|
|
454
|
+
// ============================================================================
|
|
455
|
+
|
|
456
|
+
describe('系统信息集成', () => {
|
|
457
|
+
describe('getStaticSystemInfo', () => {
|
|
458
|
+
beforeEach(() => {
|
|
459
|
+
mockFs.existsSync.mockImplementation((path: any) => {
|
|
460
|
+
if (path === CONFIG_DIR) return true
|
|
461
|
+
return false // 缓存文件不存在
|
|
462
|
+
})
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
it('应该返回正确的 OS 信息', async () => {
|
|
466
|
+
mockOs.platform.mockReturnValue('darwin')
|
|
467
|
+
|
|
468
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
469
|
+
const info = getStaticSystemInfo()
|
|
470
|
+
|
|
471
|
+
expect(info.os).toBe('darwin')
|
|
472
|
+
})
|
|
473
|
+
|
|
474
|
+
it('应该返回正确的架构信息', async () => {
|
|
475
|
+
mockOs.arch.mockReturnValue('arm64')
|
|
476
|
+
|
|
477
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
478
|
+
const info = getStaticSystemInfo()
|
|
479
|
+
|
|
480
|
+
expect(info.arch).toBe('arm64')
|
|
481
|
+
})
|
|
482
|
+
|
|
483
|
+
it('应该返回正确的 Shell 信息', async () => {
|
|
484
|
+
mockGetShellCapabilities.mockReturnValue({
|
|
485
|
+
displayName: 'Zsh',
|
|
486
|
+
} as any)
|
|
487
|
+
|
|
488
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
489
|
+
const info = getStaticSystemInfo()
|
|
490
|
+
|
|
491
|
+
expect(info.shell).toBe('Zsh')
|
|
492
|
+
})
|
|
493
|
+
|
|
494
|
+
it('应该返回正确的用户信息', async () => {
|
|
495
|
+
mockOs.userInfo.mockReturnValue({ username: 'myuser' } as any)
|
|
496
|
+
|
|
497
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
498
|
+
const info = getStaticSystemInfo()
|
|
499
|
+
|
|
500
|
+
expect(info.user).toBe('myuser')
|
|
501
|
+
})
|
|
502
|
+
})
|
|
503
|
+
|
|
504
|
+
describe('getDynamicSystemInfo', () => {
|
|
505
|
+
it('应该返回当前工作目录', async () => {
|
|
506
|
+
const originalCwd = process.cwd
|
|
507
|
+
process.cwd = vi.fn(() => '/test/project')
|
|
508
|
+
|
|
509
|
+
const { getDynamicSystemInfo } = await import('../sysinfo.js')
|
|
510
|
+
const info = await getDynamicSystemInfo()
|
|
511
|
+
|
|
512
|
+
expect(info.cwd).toBe('/test/project')
|
|
513
|
+
process.cwd = originalCwd
|
|
514
|
+
})
|
|
515
|
+
|
|
516
|
+
it('应该包含项目上下文', async () => {
|
|
517
|
+
mockDetectProjectContext.mockResolvedValue({
|
|
518
|
+
types: ['node'],
|
|
519
|
+
packageManager: 'pnpm',
|
|
520
|
+
root: '/test/project',
|
|
521
|
+
} as any)
|
|
522
|
+
|
|
523
|
+
const { getDynamicSystemInfo } = await import('../sysinfo.js')
|
|
524
|
+
const info = await getDynamicSystemInfo()
|
|
525
|
+
|
|
526
|
+
expect(info.project).not.toBeNull()
|
|
527
|
+
expect(info.project?.types).toContain('node')
|
|
528
|
+
})
|
|
529
|
+
|
|
530
|
+
it('项目上下文可以为 null', async () => {
|
|
531
|
+
mockDetectProjectContext.mockResolvedValue(null)
|
|
532
|
+
|
|
533
|
+
const { getDynamicSystemInfo } = await import('../sysinfo.js')
|
|
534
|
+
const info = await getDynamicSystemInfo()
|
|
535
|
+
|
|
536
|
+
expect(info.project).toBeNull()
|
|
537
|
+
})
|
|
538
|
+
})
|
|
539
|
+
|
|
540
|
+
describe('getSystemInfo', () => {
|
|
541
|
+
beforeEach(() => {
|
|
542
|
+
mockFs.existsSync.mockImplementation((path: any) => {
|
|
543
|
+
if (path === CONFIG_DIR) return true
|
|
544
|
+
return false
|
|
545
|
+
})
|
|
546
|
+
})
|
|
547
|
+
|
|
548
|
+
it('应该合并静态和动态信息', async () => {
|
|
549
|
+
mockDetectProjectContext.mockResolvedValue({
|
|
550
|
+
types: ['node'],
|
|
551
|
+
root: '/test',
|
|
552
|
+
} as any)
|
|
553
|
+
|
|
554
|
+
const { getSystemInfo } = await import('../sysinfo.js')
|
|
555
|
+
const info = await getSystemInfo()
|
|
556
|
+
|
|
557
|
+
// 静态信息
|
|
558
|
+
expect(info.os).toBeDefined()
|
|
559
|
+
expect(info.arch).toBeDefined()
|
|
560
|
+
expect(info.shell).toBeDefined()
|
|
561
|
+
|
|
562
|
+
// 动态信息
|
|
563
|
+
expect(info.cwd).toBeDefined()
|
|
564
|
+
expect(info.project).not.toBeNull()
|
|
565
|
+
})
|
|
566
|
+
})
|
|
567
|
+
})
|
|
568
|
+
|
|
569
|
+
// ============================================================================
|
|
570
|
+
// 格式化测试
|
|
571
|
+
// ============================================================================
|
|
572
|
+
|
|
573
|
+
describe('formatSystemInfo', () => {
|
|
574
|
+
it('应该包含基础信息', async () => {
|
|
575
|
+
const { formatSystemInfo } = await import('../sysinfo.js')
|
|
576
|
+
const formatted = formatSystemInfo({
|
|
577
|
+
os: 'darwin',
|
|
578
|
+
arch: 'arm64',
|
|
579
|
+
shell: 'Zsh',
|
|
580
|
+
user: 'testuser',
|
|
581
|
+
systemPackageManager: 'brew',
|
|
582
|
+
availableCommands: [],
|
|
583
|
+
cwd: '/test',
|
|
584
|
+
project: null,
|
|
585
|
+
})
|
|
586
|
+
|
|
587
|
+
expect(formatted).toContain('darwin')
|
|
588
|
+
expect(formatted).toContain('arm64')
|
|
589
|
+
expect(formatted).toContain('Zsh')
|
|
590
|
+
expect(formatted).toContain('testuser')
|
|
591
|
+
expect(formatted).toContain('brew')
|
|
592
|
+
})
|
|
593
|
+
|
|
594
|
+
it('应该正确分类命令', async () => {
|
|
595
|
+
const { formatSystemInfo } = await import('../sysinfo.js')
|
|
596
|
+
const formatted = formatSystemInfo({
|
|
597
|
+
os: 'darwin',
|
|
598
|
+
arch: 'arm64',
|
|
599
|
+
shell: 'Zsh',
|
|
600
|
+
user: 'testuser',
|
|
601
|
+
systemPackageManager: 'brew',
|
|
602
|
+
availableCommands: ['eza', 'fd', 'pnpm', 'docker'],
|
|
603
|
+
cwd: '/test',
|
|
604
|
+
project: null,
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
expect(formatted).toContain('现代工具')
|
|
608
|
+
expect(formatted).toContain('eza')
|
|
609
|
+
expect(formatted).toContain('包管理器')
|
|
610
|
+
expect(formatted).toContain('pnpm')
|
|
611
|
+
expect(formatted).toContain('容器工具')
|
|
612
|
+
expect(formatted).toContain('docker')
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
it('空命令列表应该不显示工具部分', async () => {
|
|
616
|
+
const { formatSystemInfo } = await import('../sysinfo.js')
|
|
617
|
+
const formatted = formatSystemInfo({
|
|
618
|
+
os: 'darwin',
|
|
619
|
+
arch: 'arm64',
|
|
620
|
+
shell: 'Zsh',
|
|
621
|
+
user: 'testuser',
|
|
622
|
+
systemPackageManager: 'brew',
|
|
623
|
+
availableCommands: [],
|
|
624
|
+
cwd: '/test',
|
|
625
|
+
project: null,
|
|
626
|
+
})
|
|
627
|
+
|
|
628
|
+
expect(formatted).not.toContain('现代工具')
|
|
629
|
+
expect(formatted).not.toContain('【用户终端可用工具】')
|
|
630
|
+
})
|
|
631
|
+
})
|
|
632
|
+
|
|
633
|
+
// ============================================================================
|
|
634
|
+
// refreshSystemCache 测试
|
|
635
|
+
// ============================================================================
|
|
636
|
+
|
|
637
|
+
describe('refreshSystemCache', () => {
|
|
638
|
+
it('应该强制写入新缓存', async () => {
|
|
639
|
+
mockFs.existsSync.mockReturnValue(true)
|
|
640
|
+
|
|
641
|
+
const { refreshSystemCache } = await import('../sysinfo.js')
|
|
642
|
+
refreshSystemCache()
|
|
643
|
+
|
|
644
|
+
expect(mockFs.writeFileSync).toHaveBeenCalled()
|
|
645
|
+
})
|
|
646
|
+
|
|
647
|
+
it('应该创建配置目录(如果不存在)', async () => {
|
|
648
|
+
mockFs.existsSync.mockReturnValue(false)
|
|
649
|
+
|
|
650
|
+
const { refreshSystemCache } = await import('../sysinfo.js')
|
|
651
|
+
refreshSystemCache()
|
|
652
|
+
|
|
653
|
+
expect(mockFs.mkdirSync).toHaveBeenCalledWith(CONFIG_DIR, { recursive: true })
|
|
654
|
+
})
|
|
655
|
+
|
|
656
|
+
it('新缓存应该包含当前时间戳', async () => {
|
|
657
|
+
mockFs.existsSync.mockReturnValue(true)
|
|
658
|
+
|
|
659
|
+
let writtenContent: string = ''
|
|
660
|
+
mockFs.writeFileSync.mockImplementation((path: any, content: any) => {
|
|
661
|
+
writtenContent = content
|
|
662
|
+
})
|
|
663
|
+
|
|
664
|
+
const beforeTime = new Date().toISOString()
|
|
665
|
+
const { refreshSystemCache } = await import('../sysinfo.js')
|
|
666
|
+
refreshSystemCache()
|
|
667
|
+
const afterTime = new Date().toISOString()
|
|
668
|
+
|
|
669
|
+
const cache = JSON.parse(writtenContent)
|
|
670
|
+
expect(cache.cachedAt >= beforeTime).toBe(true)
|
|
671
|
+
expect(cache.cachedAt <= afterTime).toBe(true)
|
|
672
|
+
})
|
|
673
|
+
})
|
|
674
|
+
|
|
675
|
+
// ============================================================================
|
|
676
|
+
// 边界情况测试
|
|
677
|
+
// ============================================================================
|
|
678
|
+
|
|
679
|
+
describe('边界情况', () => {
|
|
680
|
+
it('Windows 系统上应该正确检测', async () => {
|
|
681
|
+
mockIsWindows.mockReturnValue(true)
|
|
682
|
+
mockOs.platform.mockReturnValue('win32')
|
|
683
|
+
mockOs.arch.mockReturnValue('x64')
|
|
684
|
+
mockDetectShell.mockReturnValue('powershell7')
|
|
685
|
+
mockGetShellCapabilities.mockReturnValue({
|
|
686
|
+
displayName: 'PowerShell 7+',
|
|
687
|
+
} as any)
|
|
688
|
+
mockFs.existsSync.mockImplementation((path: any) => {
|
|
689
|
+
if (path === CONFIG_DIR) return true
|
|
690
|
+
return false
|
|
691
|
+
})
|
|
692
|
+
|
|
693
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
694
|
+
const info = getStaticSystemInfo()
|
|
695
|
+
|
|
696
|
+
expect(info.os).toBe('win32')
|
|
697
|
+
expect(info.shell).toBe('PowerShell 7+')
|
|
698
|
+
})
|
|
699
|
+
|
|
700
|
+
it('Linux 系统上应该正确检测', async () => {
|
|
701
|
+
mockOs.platform.mockReturnValue('linux')
|
|
702
|
+
mockOs.arch.mockReturnValue('x64')
|
|
703
|
+
mockDetectShell.mockReturnValue('bash')
|
|
704
|
+
mockGetShellCapabilities.mockReturnValue({
|
|
705
|
+
displayName: 'Bash',
|
|
706
|
+
} as any)
|
|
707
|
+
mockFs.existsSync.mockImplementation((path: any) => {
|
|
708
|
+
if (path === CONFIG_DIR) return true
|
|
709
|
+
return false
|
|
710
|
+
})
|
|
711
|
+
|
|
712
|
+
const { getStaticSystemInfo } = await import('../sysinfo.js')
|
|
713
|
+
const info = getStaticSystemInfo()
|
|
714
|
+
|
|
715
|
+
expect(info.os).toBe('linux')
|
|
716
|
+
expect(info.shell).toBe('Bash')
|
|
717
|
+
})
|
|
718
|
+
})
|