aico-cli 0.3.16 → 0.3.20

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.
@@ -0,0 +1,476 @@
1
+ # AICO CLI Windows 兼容性测试套件
2
+ # 验证 PowerShell 脚本的跨平台执行效果
3
+
4
+ param(
5
+ [Parameter(Mandatory=$false)]
6
+ [ValidateSet("All", "Platform", "Notify", "Hooks", "TaskManager")]
7
+ [string]$TestScope = "All",
8
+
9
+ [switch]$VerboseOutput,
10
+ [switch]$NoColor
11
+ )
12
+
13
+ # 测试配置
14
+ $TEST_VERSION = "1.0.0"
15
+ $TEST_RESULTS = @()
16
+
17
+ # 颜色配置(可禁用)
18
+ $COLORS = if ($NoColor) {
19
+ @{
20
+ Red = @{ForegroundColor = "White"}
21
+ Green = @{ForegroundColor = "White"}
22
+ Yellow = @{ForegroundColor = "White"}
23
+ Blue = @{ForegroundColor = "White"}
24
+ Cyan = @{ForegroundColor = "White"}
25
+ }
26
+ } else {
27
+ @{
28
+ Red = @{ForegroundColor = "Red"}
29
+ Green = @{ForegroundColor = "Green"}
30
+ Yellow = @{ForegroundColor = "Yellow"}
31
+ Blue = @{ForegroundColor = "Blue"}
32
+ Cyan = @{ForegroundColor = "Cyan"}
33
+ }
34
+ }
35
+
36
+ function Write-TestHeader {
37
+ Write-Host @"
38
+ ╔═══════════════════════════════════════════════════════════════════════════════════════╗
39
+ ║ ║
40
+ ║ 🧪 AICO CLI Windows 兼容性测试套件 ║
41
+ ║ 测试版本: $TEST_VERSION ║
42
+ ║ 测试范围: $TestScope ║
43
+ ║ ║
44
+ ╚═══════════════════════════════════════════════════════════════════════════════════════╝
45
+ "@ @COLORS.Cyan
46
+ }
47
+
48
+ function Write-TestSection {
49
+ param([string]$SectionName)
50
+ Write-Host "`n📋 $SectionName 测试开始..." @COLORS.Blue
51
+ Write-Host ("=" * 80) @COLORS.Blue
52
+ }
53
+
54
+ function Write-TestResult {
55
+ param(
56
+ [string]$TestName,
57
+ [bool]$Success,
58
+ [string]$Details = "",
59
+ [timespan]$ExecutionTime
60
+ )
61
+
62
+ $statusIcon = if ($Success) { "✅" } else { "❌" }
63
+ $statusText = if ($Success) { "通过" } else { "失败" }
64
+ $statusColor = if ($Success) { $COLORS.Green } else { $COLORS.Red }
65
+
66
+ $result = @{
67
+ TestName = $TestName
68
+ Success = $Success
69
+ Details = $Details
70
+ ExecutionTime = $ExecutionTime
71
+ Timestamp = Get-Date
72
+ }
73
+
74
+ $TEST_RESULTS += $result
75
+
76
+ Write-Host "$statusIcon $TestName - $statusText (耗时: $($ExecutionTime.TotalMilliseconds)ms)" @statusColor
77
+
78
+ if ($VerboseOutput -and $Details) {
79
+ Write-Host " 详情: $Details" @COLORS.Yellow
80
+ }
81
+
82
+ if (!$Success -and !$VerboseOutput) {
83
+ Write-Host " ❗ 使用 -VerboseOutput 获取详细信息" @COLORS.Yellow
84
+ }
85
+ }
86
+
87
+ function Measure-ScriptExecution {
88
+ param([ScriptBlock]$ScriptBlock, [string]$ScriptName)
89
+
90
+ $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
91
+
92
+ try {
93
+ $result = & $ScriptBlock
94
+ $stopwatch.Stop()
95
+
96
+ return @{
97
+ Success = $true
98
+ Result = $result
99
+ ExecutionTime = $stopwatch.Elapsed
100
+ Error = $null
101
+ }
102
+ }
103
+ catch {
104
+ $stopwatch.Stop()
105
+
106
+ return @{
107
+ Success = $false
108
+ Result = $null
109
+ ExecutionTime = $stopwatch.Elapsed
110
+ Error = $_.Exception.Message
111
+ }
112
+ }
113
+ }
114
+
115
+ # 1. 平台兼容性测试
116
+ function Test-PlatformCompatibility {
117
+ Write-TestSection -SectionName "平台兼容性"
118
+
119
+ # PowerShell 版本检测
120
+ $psTest = Measure-ScriptExecution -ScriptBlock {
121
+ $psVersion = $PSVersionTable.PSVersion
122
+ if ($psVersion.Major -ge 5) {
123
+ return "PowerShell $($psVersion.ToString()) - 兼容"
124
+ } else {
125
+ throw "PowerShell $psVersion - 版本过低,需要 5.0+"
126
+ }
127
+ } -ScriptName "PowerShell版本检测"
128
+
129
+ Write-TestResult -TestName "PowerShell版本支持" -Success $psTest.Success `
130
+ -Details $psTest.Result -ExecutionTime $psTest.ExecutionTime
131
+
132
+ if ($VerboseOutput) {
133
+ Write-Host " PowerShell 详情:" $PSVersionTable.PSVersion @COLORS.Yellow
134
+ }
135
+
136
+ # 操作系统检测
137
+ $osTest = Measure-ScriptExecution -ScriptBlock {
138
+ $os = [System.Environment]::OSVersion
139
+ $isWindows = $os.Platform -eq "Win32NT"
140
+
141
+ if ($isWindows) {
142
+ $winVersion = Get-CimInstance -ClassName Win32_OperatingSystem |
143
+ Select-Object -ExpandProperty Caption
144
+ return "Windows 兼容 - $winVersion"
145
+ } else {
146
+ throw "非 Windows 环境: $($os.Platform)"
147
+ }
148
+ } -ScriptName "操作系统检测"
149
+
150
+ Write-TestResult -TestName "Windows环境检测" -Success $osTest.Success `
151
+ -Details $osTest.Result -ExecutionTime $osTest.ExecutionTime
152
+
153
+ # 架构检测
154
+ $archTest = Measure-ScriptExecution -ScriptBlock {
155
+ $arch = if ([System.Environment]::Is64BitOperatingSystem) { "64位" } else { "32位" }
156
+ $proc = if ([System.Environment]::Is64BitProcess) { "64位进程" } else { "32位进程" }
157
+ return "$arch 系统, $proc"
158
+ } -ScriptName "系统架构检测"
159
+
160
+ Write-TestResult -TestName "系统架构支持" -Success $archTest.Success `
161
+ -Details $archTest.Result -ExecutionTime $archTest.ExecutionTime
162
+
163
+ # 依赖项检测
164
+ $depTest = Measure-ScriptExecution -ScriptBlock {
165
+ $results = @{}
166
+
167
+ # 检测 WSL
168
+ $results.WSL = Test-Path "C:\Windows\System32\wsl.exe"
169
+
170
+ # 检测 Git Bash
171
+ $gitPaths = @(
172
+ "C:\Program Files\Git\bin\bash.exe",
173
+ "C:\Program Files (x86)\Git\bin\bash.exe"
174
+ )
175
+
176
+ $results.GitBash = $false
177
+ foreach ($path in $gitPaths) {
178
+ if (Test-Path $path) {
179
+ $results.GitBash = $path
180
+ break
181
+ }
182
+ }
183
+
184
+ # 检测 .NET Framework
185
+ $results.DotNetFrame = [bool](Get-ItemProperty "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" -Name Release -ErrorAction SilentlyContinue)
186
+
187
+ return "$($results.WSL) WSL, $($results.GitBash) GitBash, $($results.DotNetFrame) .NET Framework"
188
+ } -ScriptName "依赖项检测"
189
+
190
+ Write-TestResult -TestName "可选依赖项" -Success $depTest.Success `
191
+ -Details $depTest.Result -ExecutionTime $depTest.ExecutionTime
192
+ }
193
+
194
+ # 2. 主启动器测试
195
+ function Test-BootstrapLauncher {
196
+ Write-TestSection -SectionName "启动器功能"
197
+
198
+ $templatesPath = Split-Path -Parent $PSScriptRoot
199
+ $bootstrapPath = Join-Path $templatesPath "windows-bootstrap.ps1"
200
+
201
+ if (!(Test-Path $bootstrapPath)) {
202
+ Write-TestResult -TestName "启动器文件存在" -Success $false `
203
+ -Details "启动器文件不存在: $bootstrapPath"
204
+ return
205
+ }
206
+
207
+ # 基础功能测试
208
+ $basicTest = Measure-ScriptExecution -ScriptBlock {
209
+ & $bootstrapPath "test-connection"
210
+ } -ScriptName "连接性测试"
211
+
212
+ Write-TestResult -TestName "连接性测试" -Success ($basicTest.Success -and $basicTest.Result -eq $true) `
213
+ -Details $basicTest.Error -ExecutionTime $basicTest.ExecutionTime
214
+
215
+ # 帮助信息测试
216
+ $helpTest = Measure-ScriptExecution -ScriptBlock {
217
+ & $bootstrapPath "help"
218
+ } -ScriptName "帮助信息"
219
+
220
+ Write-TestResult -TestName "帮助信息显示" -Success $helpTest.Success `
221
+ -Details $helpTest.Error -ExecutionTime $helpTest.ExecutionTime
222
+
223
+ # 平台信息测试
224
+ $platformTest = Measure-ScriptExecution -ScriptBlock {
225
+ $result = & $bootstrapPath "platform-info"
226
+ return $result -ne $null
227
+ } -ScriptName "平台信息"
228
+
229
+ Write-TestResult -TestName "平台信息获取" -Success $platformTest.Success `
230
+ -Details $platformTest.Error -ExecutionTime $platformTest.ExecutionTime
231
+ }
232
+
233
+ # 3. 通知系统测试
234
+ function Test-NotificationSystem {
235
+ Write-TestSection -SectionName "通知系统"
236
+
237
+ $notificationScript = Join-Path (Split-Path -Parent $PSScriptRoot) "hooks\notify.ps1"
238
+
239
+ if (!(Test-Path $notificationScript)) {
240
+ Write-TestResult -TestName "通知脚本存在" -Success $false `
241
+ -Details "通知脚本不存在: $notificationScript"
242
+ return
243
+ }
244
+
245
+ # 音频播放能力测试(静默)
246
+ $audioTest = Measure-ScriptExecution -ScriptBlock {
247
+ # 检查音频播放能力而不实际播放
248
+ $hasMediaPlayer = [Type]::GetType("WMPlayer.OCX") -ne $null
249
+ $hasNetMedia = [Type]::GetType("System.Media.SoundPlayer") -ne $null
250
+
251
+ if ($hasMediaPlayer -or $hasNetMedia) {
252
+ return "具备音频播放能力 ($($hasMediaPlayer ? 'WMP' : ''), $($hasNetMedia ? '.NET' : ''))"
253
+ } else {
254
+ return "仅支持系统蜂鸣声"
255
+ }
256
+ } -ScriptName "音频播放能力"
257
+
258
+ Write-TestResult -TestName "音频系统集成" -Success $audioTest.Success `
259
+ -Details $audioTest.Result -ExecutionTime $audioTest.ExecutionTime
260
+
261
+ # 通知触发测试(使用参数避免实际播放)
262
+ $notifyTest = Measure-ScriptExecution -ScriptBlock {
263
+ # 使用检测模式避免实际播放
264
+ $env:AICO_NOTIFY_TEST_MODE = "true"
265
+ & $notificationScript "input"
266
+ $result1 = $?
267
+
268
+ & $notificationScript "complete"
269
+ $result2 = $?
270
+
271
+ return $result1 -and $result2
272
+ } -ScriptName "通知触发"
273
+
274
+ Write-TestResult -TestName "通知触发机制" -Success $notifyTest.Success `
275
+ -Details $notifyTest.Error -ExecutionTime $notifyTest.ExecutionTime
276
+ }
277
+
278
+ # 4. Hook 系统测试
279
+ function Test-HookSystem {
280
+ Write-TestSection -SectionName "Hook系统"
281
+
282
+ $hookUtils = Join-Path (Split-Path -Parent $PSScriptRoot) "hooks\requirement\hook-utils.ps1"
283
+
284
+ # Hook 工具库测试
285
+ $utilsTest = Measure-ScriptExecution -ScriptBlock {
286
+ if (Test-Path $hookUtils) {
287
+ Import-Module $hookUtils -Force
288
+
289
+ # 测试日志功能
290
+ Write-HookLog -Message "测试日志功能" -Level "INFO" -Source "Test-Suite" -ConsoleOnly
291
+
292
+ # 测试文件操作
293
+ $tempFile = Join-Path $env:TEMP "aico-test-$(Get-Random).txt"
294
+ $result = Invoke-SafeFileOperation -Operation "write" -FilePath $tempFile -Content "测试内容"
295
+
296
+ if ($result) {
297
+ $content = Invoke-SafeFileOperation -Operation "read" -FilePath $tempFile
298
+ $result = $content -eq "测试内容"
299
+ Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
300
+ }
301
+
302
+ return $result
303
+ } else {
304
+ throw "Hook工具库不存在: $hookUtils"
305
+ }
306
+ } -ScriptName "Hook工具库"
307
+
308
+ Write-TestResult -TestName "Hook通用工具" -Success $utilsTest.Success `
309
+ -Details $utilsTest.Error -ExecutionTime $utilsTest.ExecutionTime
310
+
311
+ # Hook 状态管理测试
312
+ $statusTest = Measure-ScriptExecution -ScriptBlock {
313
+ $testHookName = "test-hook-$(Get-Random)"
314
+
315
+ # 测试状态设置
316
+ $setResult = Set-HookStatus -HookName $testHookName -Status "success" -Details "测试"
317
+
318
+ # 测试状态读取
319
+ $readStatus = Get-HookStatus -HookName $testHookName
320
+
321
+ # 清理
322
+ Clear-HookTempData -HookName $testHookName
323
+
324
+ return $setResult -and ($readStatus -eq "success")
325
+ } -ScriptName "Hook状态管理"
326
+
327
+ Write-TestResult -TestName "Hook状态管理" -Success $statusTest.Success `
328
+ -Details $statusTest.Error -ExecutionTime $statusTest.ExecutionTime
329
+ }
330
+
331
+ # 5. 跨平台路径转换测试
332
+ function Test-CrossPlatformPaths {
333
+ Write-TestSection -SectionName "跨平台路径处理"
334
+
335
+ # 路径转换测试
336
+ $pathTest = Measure-ScriptExecution -ScriptBlock {
337
+ $unixPaths = @(
338
+ "/c/Users/test/file.txt",
339
+ "/mnt/c/project/src/main.js",
340
+ "/home/user/docs/readme.md"
341
+ )
342
+
343
+ $expectedWindows = @(
344
+ "C:\Users\test\file.txt",
345
+ "C:\project\src\main.js",
346
+ "C:\home\user\docs\readme.md" # Git Bash 风格
347
+ )
348
+
349
+ # 使用 platform-launcher 进行路径转换
350
+ $launcher = Join-Path (Split-Path -Parent $PSScriptRoot) "utils\platform-launcher.ps1"
351
+
352
+ if (Test-Path $launcher) {
353
+ # 测试平台信息获取(包含路径处理逻辑)
354
+ $platformInfo = & $launcher "platform-info" | ConvertFrom-Json
355
+ return $platformInfo.Platform -eq "windows"
356
+ } else {
357
+ throw "platform-launcher 不存在"
358
+ }
359
+ } -ScriptName "路径转换"
360
+
361
+ Write-TestResult -TestName "跨平台路径转换" -Success $pathTest.Success `
362
+ -Details $pathTest.Error -ExecutionTime $pathTest.ExecutionTime
363
+ }
364
+
365
+ # 6. 集成测试
366
+ function Test-Integration {
367
+ Write-TestSection -SectionName "集成测试"
368
+
369
+ # 完整工作流测试
370
+ $workflowTest = Measure-ScriptExecution -ScriptBlock {
371
+ $bootstrap = Join-Path (Split-Path -Parent $PSScriptRoot) "windows-bootstrap.ps1"
372
+
373
+ # 模拟完整需求处理工作流
374
+ Write-Host "🔄 模拟需求识别工作流..." @COLORS.Blue
375
+
376
+ # Step 1: 触发前置 Hook
377
+ $preHook = & $bootstrap "pre-requirement-identifier" "开发用户登录系统" 2>$null
378
+
379
+ # Step 2: 通知系统
380
+ $notify = & $bootstrap "notify" "input" 2>$null
381
+
382
+ # Step 3: 任务分配
383
+ $taskTest = $true # 模拟任务系统
384
+
385
+ return $preHook -and $notify -and $taskTest
386
+ } -ScriptName "集成工作流"
387
+
388
+ Write-TestResult -TestName "完整集成工作流" -Success $workflowTest.Success `
389
+ -Details $workflowTest.Error -ExecutionTime $workflowTest.ExecutionTime
390
+ }
391
+
392
+ # 主测试执行
393
+ function Invoke-AllTests {
394
+ Write-TestHeader
395
+
396
+ $startTime = Get-Date
397
+
398
+ # 根据测试范围执行不同测试
399
+ switch ($TestScope) {
400
+ "All" {
401
+ Test-PlatformCompatibility
402
+ Test-BootstrapLauncher
403
+ Test-NotificationSystem
404
+ Test-HookSystem
405
+ Test-CrossPlatformPaths
406
+ Test-Integration
407
+ }
408
+ "Platform" { Test-PlatformCompatibility }
409
+ "Notify" { Test-NotificationSystem }
410
+ "Hooks" { Test-HookSystem }
411
+ "TaskManager" { Test-CrossPlatformPaths }
412
+ }
413
+
414
+ $endTime = Get-Date
415
+ $totalTime = $endTime - $startTime
416
+
417
+ # 测试报告
418
+ Write-TestSection -SectionName "测试报告"
419
+
420
+ $passedTests = ($TEST_RESULTS | Where-Object { $_.Success }).Count
421
+ $totalTests = $TEST_RESULTS.Count
422
+ $passRate = [math]::Round(($passedTests / $totalTests) * 100, 2)
423
+
424
+ Write-Host "`n📊 测试统计:" @COLORS.Blue
425
+ Write-Host " ✅ 通过: $passedTests 项" @COLORS.Green
426
+ Write-Host " ❌ 失败: $($totalTests - $passedTests) 项" @COLORS.Red
427
+ Write-Host " 📈 通过率: $passRate%" @COLORS.Yellow
428
+ Write-Host " ⏱️ 总耗时: $($totalTime.TotalSeconds) 秒" @COLORS.Cyan
429
+
430
+ if ($passRate -eq 100) {
431
+ Write-Host "`n🎉 所有测试通过!Windows PowerShell 环境准备就绪!" @COLORS.Green
432
+ } elseif ($passRate -ge 80) {
433
+ Write-Host "`n⚠️ 大部分测试通过,部分功能可能需要配置" @COLORS.Yellow
434
+ } else {
435
+ Write-Host "`n❌ 测试未通过,需要修复兼容性问题" @COLORS.Red
436
+ }
437
+
438
+ # 详细失败信息
439
+ $failedTests = $TEST_RESULTS | Where-Object { !$_.Success }
440
+ if ($failedTests.Count -gt 0) {
441
+ Write-Host "`n🔍 失败详情:" @COLORS.Red
442
+ foreach ($test in $failedTests) {
443
+ Write-Host " - $($test.TestName): $($test.Details)" @COLORS.Red
444
+ }
445
+ }
446
+
447
+ # 生成测试报告文件
448
+ $report = @{
449
+ TestVersion = $TEST_VERSION
450
+ TestScope = $TestScope
451
+ StartTime = $startTime
452
+ EndTime = $endTime
453
+ TotalTests = $totalTests
454
+ PassedTests = $passedTests
455
+ FailedTests = $failedTests.Count
456
+ PassRate = $passRate
457
+ Results = $TEST_RESULTS
458
+ }
459
+
460
+ $reportPath = Join-Path $env:TEMP "aico-windows-compatibility-test.json"
461
+ $report | ConvertTo-Json -Depth 5 | Set-Content -Path $reportPath -Force
462
+
463
+ Write-Host "`n📄 详细报告已保存: $reportPath" @COLORS.Cyan
464
+
465
+ return $passRate -ge 80 # 80% 以上通过率视为基本合格
466
+ }
467
+
468
+ # 执行测试
469
+ try {
470
+ $overallSuccess = Invoke-AllTests
471
+ exit if ($overallSuccess) { 0 } else { 1 }
472
+ }
473
+ catch {
474
+ Write-Host "`n💥 测试执行异常: $($_.Exception.Message)" @COLORS.Red
475
+ exit 1
476
+ }