test: add file download resolver v2 diagnostic

This commit is contained in:
zhaoyilun
2026-07-11 00:20:15 +08:00
parent 7db022469c
commit c8b619c254
5 changed files with 310 additions and 22 deletions

View File

@@ -5,13 +5,22 @@ Set-StrictMode -Version Latest
$repo = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot "..")).Path
$script = Join-Path $repo "scripts\verify-file-cache-mapping.ps1"
$tempDir = Join-Path $repo ("runs\file-cache-mapping-test-" + [guid]::NewGuid().ToString("N"))
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("isphere-file-cache-mapping-test-" + [guid]::NewGuid().ToString("N"))
$archiveList = Join-Path $tempDir "archive-list.txt"
$cacheRoot = Join-Path $tempDir "cache-root"
$recordsPath = Join-Path $tempDir "fixture-records.json"
$summaryPath = Join-Path $tempDir "sanitized-summary.json"
function Assert-True([bool]$Condition, [string]$Message) {
if (-not $Condition) { throw $Message }
}
function Write-AsciiFile([string]$Path, [string]$Text) {
$parent = Split-Path -Parent $Path
New-Item -ItemType Directory -Force -Path $parent | Out-Null
[System.IO.File]::WriteAllBytes($Path, [System.Text.Encoding]::ASCII.GetBytes($Text))
}
try {
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
@(
@@ -20,21 +29,79 @@ try {
"C:\redacted\other\ignored.txt"
) | Set-Content -LiteralPath $archiveList -Encoding UTF8
$output = & powershell -NoProfile -ExecutionPolicy Bypass -File $script -ArchiveListPath $archiveList -SkipDb
$archiveOutput = & powershell -NoProfile -ExecutionPolicy Bypass -File $script -ArchiveListPath $archiveList -SkipDb
if ($LASTEXITCODE -ne 0) {
throw "verify-file-cache-mapping.ps1 failed with exit code $LASTEXITCODE"
throw "archive-list verify-file-cache-mapping.ps1 failed with exit code $LASTEXITCODE"
}
$json = $output | ConvertFrom-Json
$archiveJson = $archiveOutput | ConvertFrom-Json
Assert-True ($json.ok -eq $true) "ok should be true"
Assert-True ($json.cache_checked -eq $true) "cache should be checked"
Assert-True ($json.cache_candidate_count -eq 2) "cache candidate count should be 2"
Assert-True ($json.cache_extension_counts.".docx" -eq 1) "docx extension count should be 1"
Assert-True ($json.cache_extension_counts.".xlsx" -eq 1) "xlsx extension count should be 1"
Assert-True ($json.hash_dir_length_counts."32" -eq 2) "hash dir length count should be 2"
Assert-True ($json.raw_paths_printed -eq $false) "raw paths must not be printed"
Assert-True ($json.file_contents_read -eq $false) "file contents must not be read"
Assert-True ($json.file_paths_returned -eq $false) "file paths must not be returned"
Assert-True ($archiveJson.ok -eq $true) "archive ok should be true"
Assert-True ($archiveJson.cache_checked -eq $true) "archive cache should be checked"
Assert-True ($archiveJson.cache_candidate_count -eq 2) "archive cache candidate count should be 2"
Assert-True ($archiveJson.cache_extension_counts.".docx" -eq 1) "docx extension count should be 1"
Assert-True ($archiveJson.cache_extension_counts.".xlsx" -eq 1) "xlsx extension count should be 1"
Assert-True ($archiveJson.hash_dir_length_counts."32" -eq 2) "hash dir length count should be 2"
Assert-True ($archiveJson.raw_paths_printed -eq $false) "archive raw paths must not be printed"
Assert-True ($archiveJson.file_contents_read -eq $false) "archive file contents must not be read"
Assert-True ($archiveJson.file_paths_returned -eq $false) "archive file paths must not be returned"
$reportPath = Join-Path $cacheRoot "stable\report.docx"
$payloadPath = Join-Path $cacheRoot "source-src-abc-123\payload.bin"
$loosePath = Join-Path $cacheRoot "loose\loose.txt"
$noisePath = Join-Path $cacheRoot "noise\noise.tmp"
Write-AsciiFile $reportPath "hello"
Write-AsciiFile $payloadPath "payload"
Write-AsciiFile $loosePath "abc"
Write-AsciiFile $noisePath "noise"
(Get-Item -LiteralPath $reportPath).LastWriteTimeUtc = [datetime]"2026-07-10T04:00:10Z"
(Get-Item -LiteralPath $payloadPath).LastWriteTimeUtc = [datetime]"2026-07-10T05:00:00Z"
(Get-Item -LiteralPath $loosePath).LastWriteTimeUtc = [datetime]"2026-07-10T06:00:00Z"
(Get-Item -LiteralPath $noisePath).LastWriteTimeUtc = [datetime]"2026-07-10T07:00:00Z"
@(
[ordered]@{ file_name = "report.docx"; size_bytes = 5; source_id = "report-source"; created_at = "2026-07-10T04:00:00Z" },
[ordered]@{ file_name = "receipt.pdf"; size_bytes = 7; source_id = "src-abc-123"; created_at = "2026-07-10T04:10:00Z" },
[ordered]@{ file_name = "loose.txt"; size_bytes = 999; source_id = "loose-source"; created_at = "2026-07-10T04:20:00Z" }
) | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath $recordsPath -Encoding UTF8
$fixtureOutput = & powershell -NoProfile -ExecutionPolicy Bypass -File $script -SkipDb -CacheRoot $cacheRoot -FixtureRecordsPath $recordsPath -SanitizedOutputPath $summaryPath
if ($LASTEXITCODE -ne 0) {
throw "fixture verify-file-cache-mapping.ps1 failed with exit code $LASTEXITCODE"
}
$fixtureJson = $fixtureOutput | ConvertFrom-Json
Assert-True ($fixtureJson.ok -eq $true) "fixture ok should be true"
Assert-True ($fixtureJson.records_checked -eq 3) "records_checked should be 3"
Assert-True ($fixtureJson.cache_candidates_checked -eq 4) "cache_candidates_checked should be 4"
Assert-True ($fixtureJson.accepted_matches -eq 2) "accepted_matches should be 2"
Assert-True ($fixtureJson.ambiguous_matches -eq 0) "ambiguous_matches should be 0"
Assert-True ($fixtureJson.score_100_matches -eq 1) "score_100_matches should be 1"
Assert-True ($fixtureJson.score_80_matches -eq 1) "score_80_matches should be 1"
Assert-True ($fixtureJson.score_40_only_matches -eq 1) "score_40_only_matches should be 1"
Assert-True ($fixtureJson.used_fixture_records -eq $true) "used_fixture_records should be true"
Assert-True ($fixtureJson.mapping_validated -eq $true) "fixture mapping should be validated"
Assert-True ($fixtureJson.raw_paths_printed -eq $false) "fixture raw paths must not be printed"
Assert-True ($fixtureJson.file_contents_read -eq $false) "fixture file contents must not be read"
Assert-True (Test-Path -LiteralPath $summaryPath) "sanitized summary should be written"
$summaryText = Get-Content -LiteralPath $summaryPath -Raw
Assert-True (-not $summaryText.Contains($cacheRoot)) "sanitized summary must not contain raw cache root"
Assert-True (-not $summaryText.Contains("hello")) "sanitized summary must not contain file content"
Assert-True (-not $summaryText.Contains("payload")) "sanitized summary must not contain file content"
[ordered]@{
ok = $true
archive_candidate_count = $archiveJson.cache_candidate_count
records_checked = $fixtureJson.records_checked
cache_candidates_checked = $fixtureJson.cache_candidates_checked
accepted_matches = $fixtureJson.accepted_matches
ambiguous_matches = $fixtureJson.ambiguous_matches
score_100_matches = $fixtureJson.score_100_matches
score_80_matches = $fixtureJson.score_80_matches
score_40_only_matches = $fixtureJson.score_40_only_matches
raw_paths_printed = $fixtureJson.raw_paths_printed
file_contents_read = $fixtureJson.file_contents_read
} | ConvertTo-Json -Depth 4 -Compress
}
finally {
if (Test-Path -LiteralPath $tempDir) {