$ErrorActionPreference = "Continue" Write-Host "=== 测试 CLI 爬虫程序 ===" -ForegroundColor Cyan # 测试1: 启动程序并显示帮助 Write-Host "`n1. 测试帮助命令..." -ForegroundColor Yellow $helpOutput = echo "help" | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "帮助命令执行失败" -ForegroundColor Red Write-Host $helpOutput } else { Write-Host "帮助命令执行成功" -ForegroundColor Green Write-Host $helpOutput | Select-Object -First 15 } # 测试2: 测试 list 命令(空列表) Write-Host "`n2. 测试 list 命令(空列表)..." -ForegroundColor Yellow $listOutput = echo "list" | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "list 命令执行失败" -ForegroundColor Red Write-Host $listOutput } else { Write-Host "list 命令执行成功" -ForegroundColor Green } # 测试3: 测试 Juejin 策略 Write-Host "`n3. 测试 Juejin 策略..." -ForegroundColor Yellow $juejinOutput = @("crawl https://juejin.cn/", "list", "exit") | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "Juejin 策略测试失败" -ForegroundColor Red Write-Host $juejinOutput | Select-Object -Last 10 } else { $articleCount = ($juejinOutput | Select-String "Crawled" | ForEach-Object { $_.Line -replace "Crawled (\d+) articles\.", '$1' }) Write-Host "Juejin 策略测试成功 - 爬取到 $articleCount 篇文章" -ForegroundColor Green } # 测试4: 测试 HnuNews 策略 Write-Host "`n4. 测试 HnuNews 策略..." -ForegroundColor Yellow $hnuOutput = @("crawl https://news.hnu.edu.cn/", "list", "exit") | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "HnuNews 策略测试失败" -ForegroundColor Red Write-Host $hnuOutput | Select-Object -Last 10 } else { $articleCount = ($hnuOutput | Select-String "Crawled" | ForEach-Object { $_.Line -replace "Crawled (\d+) articles\.", '$1' }) Write-Host "HnuNews 策略测试成功 - 爬取到 $articleCount 篇文章" -ForegroundColor Green } # 测试5: 测试导出功能 Write-Host "`n5. 测试导出功能..." -ForegroundColor Yellow $exportOutput = @("crawl https://juejin.cn/", "export test_export.json", "exit") | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 if (-not (Test-Path "test_export.json")) { Write-Host "导出功能测试失败" -ForegroundColor Red Write-Host $exportOutput | Select-Object -Last 10 } else { $fileSize = (Get-Item "test_export.json").Length Write-Host "导出功能测试成功 - 文件大小: $fileSize 字节" -ForegroundColor Green Remove-Item "test_export.json" -Force } # 测试6: 测试导入功能 Write-Host "`n6. 测试导入功能..." -ForegroundColor Yellow @("crawl https://juejin.cn/", "export import_test.json", "exit") | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 | Out-Null $importOutput = @("import import_test.json", "list", "exit") | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 if ($LASTEXITCODE -ne 0) { Write-Host "导入功能测试失败" -ForegroundColor Red Write-Host $importOutput | Select-Object -Last 10 } else { Write-Host "导入功能测试成功" -ForegroundColor Green Remove-Item "import_test.json" -Force } # 测试7: 测试未知命令 Write-Host "`n7. 测试未知命令处理..." -ForegroundColor Yellow $unknownOutput = echo "unknown_command" | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 if ($unknownOutput -match "Unknown command") { Write-Host "未知命令处理测试成功" -ForegroundColor Green } else { Write-Host "未知命令处理测试失败" -ForegroundColor Red } # 测试8: 测试会话持久化(退出后重新启动) Write-Host "`n8. 测试会话持久化..." -ForegroundColor Yellow @("crawl https://juejin.cn/", "exit") | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 | Out-Null $restoreOutput = echo "list" | java -jar target\datacollect-cli-0.1.0-jar-with-dependencies.jar 2>&1 if ($restoreOutput -match "Loaded") { Write-Host "会话持久化测试成功" -ForegroundColor Green } else { Write-Host "会话持久化测试失败" -ForegroundColor Red Write-Host $restoreOutput | Select-Object -Last 5 } Write-Host "`n=== 测试完成 ===" -ForegroundColor Cyan