param() $ErrorActionPreference = "Stop" 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 ([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 @( "C:\redacted\importal\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\redacted-report.docx", "C:\redacted\importal\bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\redacted-sheet.xlsx", "C:\redacted\other\ignored.txt" ) | Set-Content -LiteralPath $archiveList -Encoding UTF8 $archiveOutput = & powershell -NoProfile -ExecutionPolicy Bypass -File $script -ArchiveListPath $archiveList -SkipDb if ($LASTEXITCODE -ne 0) { throw "archive-list verify-file-cache-mapping.ps1 failed with exit code $LASTEXITCODE" } $archiveJson = $archiveOutput | ConvertFrom-Json 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) { Remove-Item -LiteralPath $tempDir -Recurse -Force } }