86 lines
6.1 KiB
PowerShell
86 lines
6.1 KiB
PowerShell
$ErrorActionPreference = 'Stop'
|
|
$projectRoot = Split-Path -Parent $PSScriptRoot
|
|
$indexer = Join-Path $PSScriptRoot 'index-manga.ps1'
|
|
$tempBase = [IO.Path]::GetFullPath([IO.Path]::GetTempPath())
|
|
$testRoot = Join-Path $tempBase ('mangabom-tests-' + [Guid]::NewGuid().ToString('N'))
|
|
function Assert-True($Condition, $Message) { if (-not $Condition) { throw $Message } }
|
|
function Load-Library { Get-Content -LiteralPath (Join-Path $testRoot 'library.json') -Raw -Encoding UTF8 | ConvertFrom-Json }
|
|
function Reindex { & $indexer -ContentRoot $testRoot 6>$null 3>$null }
|
|
try {
|
|
$chapterRoot = Join-Path $testRoot 'SAMPLE_MANGA\chapter-001'
|
|
[IO.Directory]::CreateDirectory($chapterRoot) | Out-Null
|
|
[IO.File]::WriteAllText((Join-Path $testRoot 'SAMPLE_MANGA\series.json'), '{"id":"sample","title":"Тестовая манга"}', [Text.UTF8Encoding]::new($false))
|
|
Reindex
|
|
$library = Load-Library
|
|
Assert-True ($library.series.Count -eq 1) 'Single series must remain an array'
|
|
Assert-True (-not $library.series[0].chapters[0].ready) 'Empty chapter must not be ready'
|
|
Assert-True ($library.series[0].chapters[0].pages.Count -eq 0) 'Empty pages must remain an array'
|
|
$png = [Convert]::FromBase64String('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aWZsAAAAASUVORK5CYII=')
|
|
foreach ($number in 1..15) { [IO.File]::WriteAllBytes((Join-Path $chapterRoot ('{0:00}.png' -f $number)), $png) }
|
|
Reindex
|
|
Assert-True (-not (Load-Library).series[0].chapters[0].ready) '15 pages must not be published'
|
|
[IO.File]::WriteAllBytes((Join-Path $chapterRoot '16.PNG'), $png)
|
|
[IO.File]::Move((Join-Path $chapterRoot '10.png'), (Join-Path $chapterRoot '010.png'))
|
|
Reindex
|
|
$chapter = (Load-Library).series[0].chapters[0]
|
|
Assert-True ($chapter.ready -and $chapter.pages.Count -eq 16) '16 pages must be published'
|
|
Assert-True ($chapter.pages[1].src.EndsWith('/02.png') -and $chapter.pages[9].src.EndsWith('/010.png')) 'Pages must be in numeric order'
|
|
Assert-True ($library.series[0].id -eq 'sample' -and $library.series[0].folder -eq 'SAMPLE_MANGA') 'Folder renames must preserve the series identity'
|
|
$stableManifest = [IO.File]::ReadAllText((Join-Path $testRoot 'library.json'))
|
|
Reindex
|
|
Assert-True ([IO.File]::ReadAllText((Join-Path $testRoot 'library.json')) -eq $stableManifest) 'Unchanged content must not rewrite the manifest'
|
|
[IO.File]::WriteAllBytes((Join-Path $chapterRoot '1.jpg'), $png)
|
|
Reindex
|
|
Assert-True (-not (Load-Library).series[0].chapters[0].ready) 'Duplicate page must block publication'
|
|
[IO.File]::Delete((Join-Path $chapterRoot '1.jpg'))
|
|
[IO.File]::WriteAllBytes((Join-Path $chapterRoot 'page-17.png'), $png)
|
|
Reindex
|
|
Assert-True (-not (Load-Library).series[0].chapters[0].ready) 'Bad image name must block publication'
|
|
[IO.File]::Delete((Join-Path $chapterRoot 'page-17.png'))
|
|
[IO.File]::WriteAllBytes((Join-Path $chapterRoot '17.png'), $png)
|
|
Reindex
|
|
Assert-True (-not (Load-Library).series[0].chapters[0].ready) 'Extra page must block publication'
|
|
[IO.File]::Delete((Join-Path $chapterRoot '17.png'))
|
|
[IO.File]::WriteAllBytes((Join-Path $chapterRoot '16.PNG'), [byte[]]@())
|
|
Reindex
|
|
Assert-True (-not (Load-Library).series[0].chapters[0].ready) 'Empty image must block publication'
|
|
[IO.File]::WriteAllBytes((Join-Path $chapterRoot '16.PNG'), $png)
|
|
$secondChapter = Join-Path $testRoot 'SAMPLE_MANGA\chapter-002'
|
|
[IO.Directory]::CreateDirectory($secondChapter) | Out-Null
|
|
[IO.File]::WriteAllText((Join-Path $secondChapter 'chapter.json'), '{"title":"Особая глава"}', [Text.UTF8Encoding]::new($false))
|
|
Reindex
|
|
$library = Load-Library
|
|
Assert-True ($library.series[0].chapters.Count -eq 2 -and $library.series[0].chapters[1].title -eq 'Особая глава') 'New chapter and custom title must be indexed'
|
|
$before = [IO.File]::ReadAllText((Join-Path $testRoot 'library.json'))
|
|
[IO.Directory]::CreateDirectory((Join-Path $testRoot 'SAMPLE_MANGA\chapter-1')) | Out-Null
|
|
$failed = $false
|
|
try { Reindex } catch { $failed = $true }
|
|
Assert-True $failed 'Duplicate chapter number must fail'
|
|
Assert-True ([IO.File]::ReadAllText((Join-Path $testRoot 'library.json')) -eq $before) 'Failed indexing must preserve the previous manifest'
|
|
Write-Host 'PASS: content indexing, numeric sorting, 16-page validation, duplicates, missing/extra/empty pages, custom chapters and atomic manifest.'
|
|
} finally {
|
|
$resolvedTestRoot = [IO.Path]::GetFullPath($testRoot)
|
|
if (-not $resolvedTestRoot.StartsWith((Join-Path $tempBase 'mangabom-tests-'), [StringComparison]::OrdinalIgnoreCase)) { throw 'Unsafe test cleanup path' }
|
|
if (Test-Path -LiteralPath $resolvedTestRoot) { Remove-Item -LiteralPath $resolvedTestRoot -Recurse -Force }
|
|
}
|
|
$node = Get-Command node -ErrorAction SilentlyContinue
|
|
if ($node) { $nodePath = $node.Source } else {
|
|
$nodePath = Join-Path $env:LOCALAPPDATA 'Programs\Microsoft VS Code\Code.exe'
|
|
if (-not (Test-Path -LiteralPath $nodePath)) { throw 'Node.js or VS Code is required for JavaScript tests.' }
|
|
$env:ELECTRON_RUN_AS_NODE = '1'
|
|
if ($projectRoot.StartsWith('\\')) { $env:NODE_UNC_HOST_ALLOWLIST = ($projectRoot -split '\\')[2].ToLowerInvariant() }
|
|
}
|
|
& $nodePath (Join-Path $PSScriptRoot 'test-reader.cjs') 2>&1 | Out-Host
|
|
if ($LASTEXITCODE -ne 0) { throw 'JavaScript reader tests failed' }
|
|
$dist = Join-Path $projectRoot 'dist'
|
|
foreach ($page in (Get-ChildItem -LiteralPath $dist -Filter '*.html' -File)) {
|
|
$html = [IO.File]::ReadAllText($page.FullName)
|
|
if ($html.Contains('http-equiv="refresh"')) { continue }
|
|
Assert-True ($html.Contains('src="theme.js"' ) -and $html.Contains('href="theme.css"')) ('Missing theme: ' + $page.Name)
|
|
foreach ($match in [regex]::Matches($html, '(?:href|src)="([^"]+)"')) {
|
|
$target = ([Net.WebUtility]::HtmlDecode($match.Groups[1].Value) -split '[#?]')[0]
|
|
if (-not $target -or $target -match '^(https?:|data:|mailto:|tel:|/)') { continue }
|
|
Assert-True (Test-Path -LiteralPath (Join-Path $dist $target)) ('Missing local target: ' + $page.Name + ' -> ' + $target)
|
|
}
|
|
}
|
|
Write-Host 'PASS: all HTML references resolve; every page includes the shared theme.' |