diff --git a/src/ChannelForge/Private/Invoke-ChannelForgeGuidedSetupAcceptance.ps1 b/src/ChannelForge/Private/Invoke-ChannelForgeGuidedSetupAcceptance.ps1 index 5564693..f34c1a1 100644 --- a/src/ChannelForge/Private/Invoke-ChannelForgeGuidedSetupAcceptance.ps1 +++ b/src/ChannelForge/Private/Invoke-ChannelForgeGuidedSetupAcceptance.ps1 @@ -247,6 +247,14 @@ function Get-ChannelForgeGuidedSetupAcceptanceResponse { $enrollmentMessage = 'Lineup accepted, but saved sources need repair before refresh.' } + $auditStatus = 'NOT_RECORDED' + try { + $auditRecord = New-ChannelForgeGuidedSetupAuditRecord -Session $session -Accepted $accepted -Enrollment $enrollment -RequestSchemaVersion ([int]$request.SchemaVersion) + Write-ChannelForgeGuidedSetupAuditRecord -RepositoryRoot $RepositoryRoot -Record $auditRecord | Out-Null + $auditStatus = 'RECORDED' + } + catch { $enrollmentMessage += ' The acceptance record could not be saved.' } + $acceptedSession = [ordered]@{} foreach ($property in @($session.PSObject.Properties)) { $acceptedSession[$property.Name] = $property.Value } $acceptedSession.Status = 'Accepted' @@ -257,6 +265,7 @@ function Get-ChannelForgeGuidedSetupAcceptanceResponse { Version = if ([string]$session.Version -ceq 'guided-setup/review/v2') { 'guided-setup/acceptance/v2' } else { 'guided-setup/acceptance/v1' } Status = 'ACCEPTED' EnrollmentStatus = $enrollmentStatus + Audit = $auditStatus Message = $enrollmentMessage Proposal = [ordered]@{ ChannelCount = $entryIds.Count diff --git a/tests/unit/GuidedSetupAudit.Tests.ps1 b/tests/unit/GuidedSetupAudit.Tests.ps1 index 3beec91..eab317f 100644 --- a/tests/unit/GuidedSetupAudit.Tests.ps1 +++ b/tests/unit/GuidedSetupAudit.Tests.ps1 @@ -45,6 +45,8 @@ BeforeAll { $publish = [pscustomobject]@{ GenerationManifest = $snapshot.Manifest.Object; AcceptedState = $snapshot.State.Object; AcceptedOutputManifest = $snapshot.Output.Object; DecisionManifest = $snapshot.Decision.Object } New-ChannelForgeGuidedSetupAuditRecord -Session $session -Accepted $publish -Enrollment (Read-ChannelForgeSourceEnrollmentRecord -RepositoryRoot $Root) -RequestSchemaVersion 2 } @($Root, $proposalId) + # Acceptance already wrote a record; clear it so each test starts from a known state. + Remove-Item -LiteralPath (Join-Path $Root 'state\guided-setup-audit') -Recurse -Force return $record } diff --git a/tests/unit/WebServer.Tests.ps1 b/tests/unit/WebServer.Tests.ps1 index 8f4ba11..5ea9fd5 100644 --- a/tests/unit/WebServer.Tests.ps1 +++ b/tests/unit/WebServer.Tests.ps1 @@ -1661,4 +1661,108 @@ Describe 'ChannelForge web server foundation' { $accepted.StatusCode | Should -Be 422 Test-Path -LiteralPath (Join-Path $root 'state\accepted-lineup.json') | Should -BeFalse } + + It 'records one private audit record per acceptance and never blocks acceptance when it cannot be saved' { + $guideXml = { param($Id) "$Id$Id" } + $playlists = @( + [pscustomobject]@{ Key = 'lbl-news'; Text = "#EXTM3U`n#EXTINF:-1 tvg-id=`"news`",News`nhttps://example.invalid/news`n" } + [pscustomobject]@{ Key = 'lbl-sports'; Text = "#EXTM3U`n#EXTINF:-1 tvg-id=`"sports`",Sports`nhttps://example.invalid/sports`n" } + ) + $guides = @( + [pscustomobject]@{ Key = 'guide-news'; Text = (& $guideXml 'news') } + [pscustomobject]@{ Key = 'guide-sports'; Text = (& $guideXml 'sports') } + [pscustomobject]@{ Key = 'guide-spare'; Text = (& $guideXml 'spare') } + ) + $bindings = @( + [pscustomobject]@{ Guide = 'guide-news'; Playlists = @('lbl-news'); All = $false } + [pscustomobject]@{ Guide = 'guide-sports'; Playlists = @('lbl-sports'); All = $false } + [pscustomobject]@{ Guide = 'guide-spare'; Playlists = @(); All = $false; ExplicitlyUnbound = $true } + ) + $analyze = { + param($Root) + $body = New-TestMultiSourceProposalBody -Playlists $playlists -Guides $guides -Bindings $bindings + $response = Get-TestWebResponse -Method POST -Path '/api/guided-setup/proposal' -RepositoryRoot $Root -BodyBytes $body -ContentType 'application/json' -ContentLength $body.Length + $response.StatusCode | Should -Be 200 + $response.Body | ConvertFrom-Json + } + $accept = { + param($Root, $ProposalId, [bool]$Acknowledged = $true) + $body = [Text.Encoding]::UTF8.GetBytes((@{ schemaVersion = 2; proposalId = $ProposalId; acknowledged = $Acknowledged } | ConvertTo-Json -Compress)) + Get-TestWebResponse -Method POST -Path '/api/guided-setup/accept' -RepositoryRoot $Root -BodyBytes $body -ContentType 'application/json' -ContentLength $body.Length + } + $readAudit = { + param($Root) + & (Get-Module ChannelForge) { + param($Root) + $snapshot = Get-ChannelForgeWebCurrentAcceptedSnapshot -RepositoryRoot $Root + Read-ChannelForgeGuidedSetupAuditRecord -RepositoryRoot $Root -GenerationManifestHash ([string]$snapshot.Manifest.Object.GenerationManifestHash) -Snapshot $snapshot + } $Root + } + + $root = Join-Path $TestDrive 'v2-audit' + Initialize-TestCandidateData -Root $root + $stateRoot = Join-Path $root 'state' + $before = if (Test-Path -LiteralPath $stateRoot) { Get-TestTreeSnapshot -Root $stateRoot } else { @() } + $first = & $analyze $root + $after = if (Test-Path -LiteralPath $stateRoot) { Get-TestTreeSnapshot -Root $stateRoot } else { @() } + $first.Proposal.ExactGuideMatchCount | Should -Be 2 + $first.Proposal.AmbiguityCount | Should -Be 0 + $first.Proposal.UnmatchedPlaylistCount | Should -Be 0 + $first.Proposal.UnboundGuideCount | Should -Be 1 + $first.Safety.AcceptedStateMutation | Should -Be 'none' + ($after | ConvertTo-Json -Depth 4) | Should -Be ($before | ConvertTo-Json -Depth 4) + Test-Path -LiteralPath (Join-Path $stateRoot 'guided-setup-audit') | Should -BeFalse + + $accepted = & $accept $root $first.Proposal.ProposalId + $accepted.StatusCode | Should -Be 200 + $acceptedPayload = $accepted.Body | ConvertFrom-Json + $acceptedPayload.Status | Should -Be 'ACCEPTED' + $acceptedPayload.Audit | Should -Be 'RECORDED' + $files = @(Get-ChildItem -LiteralPath (Join-Path $stateRoot 'guided-setup-audit') -File) + $files.Count | Should -Be 1 + $audit = & $readAudit $root + $audit.Status | Should -Be 'AUDIT_OK' + @($audit.Record.Playlists).Count | Should -Be 2 + @($audit.Record.Guides).Count | Should -Be 3 + @($audit.Record.Bindings | Where-Object { -not $_.ExplicitlyUnbound }).Count | Should -Be 2 + @($audit.Record.Bindings | Where-Object ExplicitlyUnbound).Count | Should -Be 1 + $audit.Record.Outcome.ExactGuideMatchCount | Should -Be 2 + $audit.Record.Outcome.UnboundGuideCount | Should -Be 1 + $audit.Record.Outcome.UnmatchedPlaylistCount | Should -Be 0 + $text = [IO.File]::ReadAllText($files[0].FullName) + foreach ($secret in @('example.invalid', 'EXTM3U', '