# Smoke test for the local Live Rush Data Collector installation.
[CmdletBinding()]
param(
  [string]$Url = "http://127.0.0.1:18766",
  [int]$TimeoutSeconds = 30
)
$ErrorActionPreference = "Stop"
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$installDir = Join-Path $env:LOCALAPPDATA "LiveRushDataCollector"
$envPath = Join-Path $installDir "data\email.env"
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
while ((Get-Date) -lt $deadline) {
  try {
    $status = Invoke-RestMethod -Uri ($Url.TrimEnd('/') + "/api/status") -TimeoutSec 2
    $preflight = Invoke-RestMethod -Uri ($Url.TrimEnd('/') + "/api/preflight") -TimeoutSec 2
    Write-Host "Live Rush 采集器已启动。"
    Write-Host ("当前状态：" + $status.realtime.state + " / " + $status.realtime.operator_message)
    foreach ($check in $preflight.checks) {
      Write-Host ("环境检查：" + $check.label + " -> " + $check.level + "，" + $check.message)
    }
    if (Test-Path $envPath) {
      $hasSigner = Select-String -Path $envPath -Pattern "^LIVE_RUSH_TIKTOK_SIGN_API_(URL|KEY)=" -Quiet
      if ($hasSigner) { Write-Host "签名通道配置文件已存在。" } else { Write-Host "签名通道未写入；主线可能仍使用默认公开入口。" }
    }
    exit 0
  } catch {
    Start-Sleep -Milliseconds 800
  }
}
throw "采集器在超时时间内没有响应：$Url"
