CaiWuXiTong/tests/scripts/frontend-cache-integrity.test.ps1

26 lines
1.7 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$source = Join-Path $PSScriptRoot '../../scripts/build-frontend.ps1'
$ast = [Management.Automation.Language.Parser]::ParseFile((Resolve-Path $source), [ref]$null, [ref]$null)
$function = $ast.Find({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Get-PathFingerprint' }, $true)
Invoke-Expression $function.Extent.Text
$check = $ast.Find({ param($node) $node -is [Management.Automation.Language.AssignmentStatementAst] -and $node.Left.Extent.Text -eq '$outputCacheValid' }, $true).Right.Extent.Text
$repoRoot = Join-Path ([IO.Path]::GetTempPath()) ('caiwu-cache-test-' + [guid]::NewGuid().ToString('N'))
$frontendOutputRoot = Join-Path $repoRoot '.caiwu-build/public'
$outputFingerprintPath = Join-Path $repoRoot 'output.sha'
New-Item -ItemType Directory -Path $frontendOutputRoot -Force | Out-Null
try {
$asset = Join-Path $frontendOutputRoot 'pages.js'
Set-Content -LiteralPath $asset -Value 'new calendar'
if (Invoke-Expression $check) { throw 'Missing integrity stamp was accepted.' }
Set-Content -LiteralPath $outputFingerprintPath -Value (Get-PathFingerprint @('.caiwu-build/public'))
if (-not (Invoke-Expression $check)) { throw 'Valid output was rejected.' }
Set-Content -LiteralPath $asset -Value 'old calendar'
if (Invoke-Expression $check) { throw 'Overwritten output was accepted.' }
Remove-Item -LiteralPath $asset
if (Invoke-Expression $check) { throw 'Missing output was accepted.' }
Write-Host 'PASS missing stamp, valid cache, overwritten and missing asset detection'
} finally {
if ((Split-Path $repoRoot -Leaf) -notlike 'caiwu-cache-test-*') { throw 'Unexpected test directory.' }
Remove-Item -LiteralPath $repoRoot -Recurse -Force
}