# generate-test-report.ps1
# 使用模拟数据测试 html-report-generator skill 的输出效果
# Usage: .\workflows\generate-test-report.ps1 [-MockDataPath ]
param(
[string]$MockDataPath = (Join-Path $PSScriptRoot "output\mock-report-data.json")
)
$templatePath = Join-Path $PSScriptRoot "templates\report_template.html"
$outputPath = Join-Path $PSScriptRoot "output\test-report-home-fragrance.html"
# ============================================================
# 验证文件存在
# ============================================================
if (-not (Test-Path $templatePath)) {
Write-Error "Template not found: $templatePath"
exit 1
}
if (-not (Test-Path $MockDataPath)) {
Write-Error "Mock data not found: $MockDataPath"
exit 1
}
$outputDir = Split-Path $outputPath
if (-not (Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
}
# ============================================================
# STEP 1: 读取 Mock 数据 JSON(UTF-8)
# ============================================================
$mockJson = [System.IO.File]::ReadAllText($MockDataPath, [System.Text.Encoding]::UTF8)
$mockObj = $mockJson | ConvertFrom-Json
$templateVars = $mockObj.templateVars
$reportDataObj = $mockObj.reportData
$reportDataStr = ($reportDataObj | ConvertTo-Json -Depth 20 -Compress:$false)
# ============================================================
# STEP 2: 读取 HTML 模板
# ============================================================
$html = [System.IO.File]::ReadAllText($templatePath, [System.Text.Encoding]::UTF8)
# ============================================================
# STEP 3: 替换 {{placeholder}} 变量
# ============================================================
$templateVars.PSObject.Properties | ForEach-Object {
$html = $html.Replace("{{$($_.Name)}}", $_.Value)
}
# ============================================================
# STEP 4: 注入 REPORT_DATA JSON
# ============================================================
$html = $html.Replace('/*{{REPORT_DATA_JSON}}*/', $reportDataStr)
# ============================================================
# STEP 5: 输出 HTML 文件(UTF-8 无 BOM)
# ============================================================
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($outputPath, $html, $utf8NoBom)
Write-Host "========================================"
Write-Host " HTML Report Test Generated"
Write-Host "========================================"
Write-Host ""
Write-Host "Output: $outputPath" -ForegroundColor Green
Write-Host "Open in browser to preview the 9-slide report."
Write-Host ""
Write-Host "Template vars filled:" -ForegroundColor Cyan
$templateVars.PSObject.Properties | ForEach-Object {
Write-Host " {{$($_.Name)}} = $($_.Value)"
}
Write-Host ""
Write-Host "REPORT_DATA fields injected:" -ForegroundColor Cyan
$reportDataObj.PSObject.Properties.Name | ForEach-Object {
Write-Host " $_"
}