diff --git a/src/ChannelForge/Private/ConvertTo-ChannelForgeGuideEvidenceFromPlaylist.ps1 b/src/ChannelForge/Private/ConvertTo-ChannelForgeGuideEvidenceFromPlaylist.ps1 new file mode 100644 index 0000000..04f5302 --- /dev/null +++ b/src/ChannelForge/Private/ConvertTo-ChannelForgeGuideEvidenceFromPlaylist.ps1 @@ -0,0 +1,59 @@ +function ConvertTo-ChannelForgeGuideEvidenceFromPlaylist { + <# + .SYNOPSIS + Turns playlist channels plus one guide's programme rows into GuideEvidenceRecord objects for Get-ChannelForgeGuideReadiness. Pure, read only. + + .DESCRIPTION + One record per channel, built with New-ChannelForgeGuideEvidence. Nothing is invented from programme text. + - Channel has programmes: XMLTV evidence. StartUtc/EndUtc is the span the guide covers for that channel. + The guide is Stale when that span ended before BuildTimeUtc, else Current. State is SafeCandidate: + one source, never cross-checked. Source relationship stays Unknown. + - Channel has no programmes (or no usable tvg-id): ProviderM3UMetadata evidence, Unresolved, reasons + EvidenceMissing and NoEventData. It is never dropped. + - Title is the channel display name, so each channel is one readiness group. Programme titles are not used. + Channels are sorted first, so the result does not depend on input order. + Programme rows: ChannelId or RawChannelId, Start, End (as read by Read-ChannelForgeXmltvSourceFile). + #> + [CmdletBinding()] + param( + [AllowEmptyCollection()][object[]]$Channel = @(), + [AllowEmptyCollection()][object[]]$Programme = @(), + [Parameter(Mandatory)][datetime]$BuildTimeUtc, + [string]$SourceId = 'guide' + ) + + $format = "yyyy-MM-dd'T'HH:mm:ss'Z'" + $culture = [System.Globalization.CultureInfo]::InvariantCulture + $build = $BuildTimeUtc.ToUniversalTime() + + $span = @{} + foreach ($p in @($Programme)) { + $key = if ([string]::IsNullOrEmpty($p.RawChannelId)) { [string]$p.ChannelId } else { [string]$p.RawChannelId } + if ([string]::IsNullOrEmpty($key) -or $p.End -le $p.Start) { continue } + if (-not $span.ContainsKey($key)) { $span[$key] = @{ Start = $p.Start; End = $p.End; Count = 0 } } + if ($p.Start -lt $span[$key].Start) { $span[$key].Start = $p.Start } + if ($p.End -gt $span[$key].End) { $span[$key].End = $p.End } + $span[$key].Count++ + } + + $ordered = @($Channel | Where-Object { $null -ne $_ } | Sort-Object { [string]$_.TvgId }, { [string]$_.DisplayName }) + foreach ($c in $ordered) { + $tvgId = [string]$c.TvgId + $usable = $tvgId -cmatch '^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$' + $name = [string]$c.DisplayName + $common = @{ SourceId = $SourceId; DisplayName = $name; Group = [string]$c.Group } + if ($usable) { $common.ChannelReference = $tvgId; $common.Title = $name } + + if ($usable -and $span.ContainsKey($tvgId)) { + $s = $span[$tvgId] + New-ChannelForgeGuideEvidence -EvidenceType XMLTV @common ` + -StartUtc $s.Start.UtcDateTime.ToString($format, $culture) -EndUtc $s.End.UtcDateTime.ToString($format, $culture) ` + -ConfidenceState SafeCandidate -ConfidenceScore 70 ` + -FreshnessState $(if ($s.End.UtcDateTime -lt $build) { 'Stale' } else { 'Current' }) + } + else { + $codes = if ($span.ContainsKey($tvgId)) { @('InvalidGuideId') } else { @('EvidenceMissing', 'NoEventData') } + New-ChannelForgeGuideEvidence -EvidenceType ProviderM3UMetadata @common -ReasonCodes $codes + } + } +} diff --git a/tests/unit/GuideEvidenceFromPlaylist.Tests.ps1 b/tests/unit/GuideEvidenceFromPlaylist.Tests.ps1 new file mode 100644 index 0000000..f452d2d --- /dev/null +++ b/tests/unit/GuideEvidenceFromPlaylist.Tests.ps1 @@ -0,0 +1,53 @@ +BeforeAll { + $script:RepoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) + Import-Module (Join-Path $script:RepoRoot 'src\ChannelForge\ChannelForge.psd1') -Force + + $script:Build = [datetime]::Parse('2026-10-01T12:00:00Z').ToUniversalTime() + function New-Chan([string]$Id, [string]$Name) { [pscustomobject]@{ TvgId = $Id; DisplayName = $Name; Group = 'News' } } + function New-Prog([string]$Id, [string]$StartText, [int]$Hours = 2) { + $start = [datetimeoffset]::Parse($StartText) + New-ChannelForgeProgramme -ChannelId $Id -Start $start -End $start.AddHours($Hours) -Title 'Show' -SourceId 'g' + } + function Get-Ready($Channel, $Programme) { + InModuleScope ChannelForge -Parameters @{ C = $Channel; P = $Programme; B = $script:Build } { + $evidence = @(ConvertTo-ChannelForgeGuideEvidenceFromPlaylist -Channel $C -Programme $P -BuildTimeUtc $B -SourceId 'g') + [pscustomobject]@{ Evidence = $evidence; Readiness = (Get-ChannelForgeGuideReadiness -Evidence $evidence) } + } + } + function Get-Record($Result, [string]$Id) { @($Result.Readiness.Records | Where-Object { $_.ChannelReference -ceq $Id })[0] } +} + +Describe 'Guide evidence from a playlist and one guide' { + It 'gives readiness a result for a current guide, one record per channel' { + $r = Get-Ready @((New-Chan 'a' 'Alpha'), (New-Chan 'b' 'Beta')) @((New-Prog 'a' '2026-10-01T10:00:00Z'), (New-Prog 'b' '2026-10-01T11:00:00Z')) + $r.Evidence.Count | Should -Be 2 + $r.Evidence[0].GetType().Name | Should -Be 'GuideEvidenceRecord' + @($r.Readiness.Records).Count | Should -Be 2 + (Get-Record $r 'a').ReadinessState | Should -Be 'SafeCandidate' + $r.Readiness.StateCounts.StaleSource | Should -Be 0 + } + + It 'reports a channel without programmes as missing, and keeps it' { + $r = Get-Ready @((New-Chan 'a' 'Alpha'), (New-Chan 'none' 'Nothing'), (New-Chan '' 'No id')) @((New-Prog 'a' '2026-10-01T10:00:00Z')) + @($r.Readiness.Records).Count | Should -Be 3 + $missing = Get-Record $r 'none' + $missing.ReadinessState | Should -Be 'Unresolved' + $missing.ReasonCodes | Should -Contain 'NoEventData' + $r.Readiness.StateCounts.Unresolved | Should -Be 2 + } + + It 'shows a guide that ended before the build as stale' { + $r = Get-Ready @((New-Chan 'a' 'Alpha'), (New-Chan 'b' 'Beta')) @((New-Prog 'a' '2026-09-20T10:00:00Z'), (New-Prog 'b' '2026-10-01T11:00:00Z')) + (Get-Record $r 'a').ReadinessState | Should -Be 'StaleSource' + (Get-Record $r 'b').ReadinessState | Should -Be 'SafeCandidate' + $r.Readiness.StateCounts.StaleSource | Should -Be 1 + } + + It 'gives the same result whatever the channel order' { + $channels = @((New-Chan 'c' 'Gamma'), (New-Chan 'a' 'Alpha'), (New-Chan 'x' 'Gone'), (New-Chan '' 'No id'), (New-Chan 'b' 'Beta')) + $programmes = @((New-Prog 'a' '2026-10-01T10:00:00Z'), (New-Prog 'b' '2026-09-01T10:00:00Z'), (New-Prog 'c' '2026-10-01T10:00:00Z')) + $one = Get-Ready $channels $programmes + $two = Get-Ready @($channels[4..0]) @($programmes[2..0]) + ($two.Readiness | ConvertTo-Json -Depth 8) | Should -Be ($one.Readiness | ConvertTo-Json -Depth 8) + } +}