From b6c19e4ad43bb99a3e8ebe5a3185f177205ec945 Mon Sep 17 00:00:00 2001 From: Jumpstile Date: Sat, 14 Nov 2026 16:56:27 -0500 Subject: [PATCH] feat: agree event times across channels and report conflicts (#175, event slice 2) Report only. Groups programmes of rated event channels by title, compares starts as instants, keeps each channel's own time, and flags conflicts in the build summary. Nothing in the guide or playlist output changes. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01NzHAckAcs78Lu3rv8Gqgrf --- scripts/Build-Lineup.ps1 | 3 + .../Get-ChannelForgeEventChannelCandidate.ps1 | 21 +++++-- .../Get-ChannelForgeEventTimeAgreement.ps1 | 52 ++++++++++++++++++ .../Get-ChannelForgeEventTimeSummaryLines.ps1 | 29 ++++++++++ tests/unit/EventTimeAgreement.Tests.ps1 | 55 +++++++++++++++++++ 5 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 src/ChannelForge/Private/Get-ChannelForgeEventTimeAgreement.ps1 create mode 100644 src/ChannelForge/Private/Get-ChannelForgeEventTimeSummaryLines.ps1 create mode 100644 tests/unit/EventTimeAgreement.Tests.ps1 diff --git a/scripts/Build-Lineup.ps1 b/scripts/Build-Lineup.ps1 index fbc2e2c..2ba4a09 100644 --- a/scripts/Build-Lineup.ps1 +++ b/scripts/Build-Lineup.ps1 @@ -1145,6 +1145,9 @@ if ($candidatePublished) { $eventFound = @(Invoke-ChannelForgePrivateCandidateFunction -Name 'Get-ChannelForgeEventChannelCandidate' -Arguments @{ Channel = $eventInput }) $eventLines = Invoke-ChannelForgePrivateCandidateFunction -Name 'Get-ChannelForgeEventChannelSummaryLines' -Arguments @{ Candidate = $eventFound } foreach ($eventLine in $eventLines) { $md += (Get-SafeReportText $eventLine) } + $eventAgreement = Invoke-ChannelForgePrivateCandidateFunction -Name 'Get-ChannelForgeEventTimeAgreement' -Arguments @{ Candidate = $eventFound; Channel = $eventInput } + $eventTimeLines = Invoke-ChannelForgePrivateCandidateFunction -Name 'Get-ChannelForgeEventTimeSummaryLines' -Arguments @{ Agreement = $eventAgreement } + foreach ($eventTimeLine in $eventTimeLines) { $md += (Get-SafeReportText $eventTimeLine) } } catch { Write-Warning 'The event channel check could not run, so the event channel section is left out of the summary.' diff --git a/src/ChannelForge/Private/Get-ChannelForgeEventChannelCandidate.ps1 b/src/ChannelForge/Private/Get-ChannelForgeEventChannelCandidate.ps1 index 6ef8a49..fa48470 100644 --- a/src/ChannelForge/Private/Get-ChannelForgeEventChannelCandidate.ps1 +++ b/src/ChannelForge/Private/Get-ChannelForgeEventChannelCandidate.ps1 @@ -7,14 +7,11 @@ function Get-ChannelForgeEventChannelCandidate { [CmdletBinding()] param([Parameter(Mandatory)][AllowEmptyCollection()][object[]]$Channel) - # Placeholder titles, compared after lowercasing and dropping punctuation. Extend here. - $fillers = @('next event coming soon', 'sign off', 'channel no longer available', 'to be announced', 'tba', 'tbd', - 'no event', 'no events', 'no live event', 'event not started', 'coming soon', 'event offline', - 'pay per view', 'ppv', 'event', 'events') + $fillers = Get-ChannelForgeEventFillerTitle $ppvWords = @([pscustomobject]@{ Key = 'ppv'; Aliases = @('ppv', 'pay per view') }) $eventWords = @([pscustomobject]@{ Key = 'event'; Aliases = @('ppv', 'pay per view', 'event', 'events', 'ufc', 'wwe', 'aew', 'boxing', 'fight', 'wrestling', 'live event') }) $sportsKeys = @(Get-ChannelForgeOneGuideCategoryRegistry | Where-Object { $_.Group -ceq 'sports' } | ForEach-Object { [string]$_.Key }) - $normalize = { param([string]$Text) ((($Text.ToLowerInvariant() -replace '[^\p{L}\p{N}]+', ' ')).Trim()) } + $normalize = { param([string]$Text) ConvertTo-ChannelForgeEventTitleKey -Text $Text } foreach ($item in $Channel) { $name = & $normalize ([string]$item.DisplayName) @@ -64,6 +61,20 @@ function Get-ChannelForgeEventChannelCandidate { } } +function Get-ChannelForgeEventFillerTitle { + # Placeholder titles, compared after ConvertTo-ChannelForgeEventTitleKey. Extend here. + @('next event coming soon', 'sign off', 'channel no longer available', 'to be announced', 'tba', 'tbd', + 'no event', 'no events', 'no live event', 'event not started', 'coming soon', 'event offline', + 'pay per view', 'ppv', 'event', 'events') +} + +function ConvertTo-ChannelForgeEventTitleKey { + # Lowercase, punctuation to single spaces. The one title comparison rule for event channels. + [CmdletBinding()] + param([string]$Text) + ((($Text.ToLowerInvariant() -replace '[^\p{L}\p{N}]+', ' ')).Trim()) +} + function Get-ChannelForgeEventChannelInput { # Joins the candidate channels, their category and their provider programmes into the # shape Get-ChannelForgeEventChannelCandidate reads. Programmes join on the exact tvg-id. diff --git a/src/ChannelForge/Private/Get-ChannelForgeEventTimeAgreement.ps1 b/src/ChannelForge/Private/Get-ChannelForgeEventTimeAgreement.ps1 new file mode 100644 index 0000000..ca44236 --- /dev/null +++ b/src/ChannelForge/Private/Get-ChannelForgeEventTimeAgreement.ps1 @@ -0,0 +1,52 @@ +function Get-ChannelForgeEventTimeAgreement { + # event-channel/v2, slice 2: report only. For channels the detector rated (any level), reads + # each channel's own programmes and compares the same event across channels. Same normalized + # title with starts inside the window is one event. Starts are compared as instants. Each + # channel keeps its own start; a difference is flagged, never resolved. Pure: nothing is + # stored or hashed. A programme with no parseable start, or a placeholder title, is skipped + # and counted. Channel is the detector input (Key, DisplayName, Programmes with Title, Start). + [CmdletBinding()] + param( + [AllowEmptyCollection()][object[]]$Candidate = @(), + [AllowEmptyCollection()][object[]]$Channel = @(), + [int]$WindowMinutes = 15 + ) + + $fillers = Get-ChannelForgeEventFillerTitle + $rated = @{} + foreach ($row in $Candidate) { $rated[[string]$row.Key] = $true } + $entries = [System.Collections.Generic.List[object]]::new() + $skipped = 0 + foreach ($item in $Channel) { + if (-not $rated.ContainsKey([string]$item.Key)) { continue } + $name = ConvertTo-ChannelForgeEventTitleKey -Text ([string]$item.DisplayName) + foreach ($programme in @($item.Programmes)) { + $titleKey = ConvertTo-ChannelForgeEventTitleKey -Text ([string]$programme.Title) + $start = [datetimeoffset]::MinValue + $hasStart = $programme.Start -is [datetimeoffset] -or [datetimeoffset]::TryParse([string]$programme.Start, [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$start) + if ($programme.Start -is [datetimeoffset]) { $start = $programme.Start } + if (-not $hasStart -or $start -eq [datetimeoffset]::MinValue -or -not $titleKey -or $fillers -contains $titleKey -or $titleKey -ceq $name) { $skipped++; continue } + $entries.Add([pscustomobject]@{ TitleKey = $titleKey; Title = ([string]$programme.Title).Trim(); ChannelKey = [string]$item.Key; DisplayName = [string]$item.DisplayName; Start = $start }) + } + } + + $events = [System.Collections.Generic.List[object]]::new() + $window = [timespan]::FromMinutes($WindowMinutes) + foreach ($group in @($entries | Group-Object TitleKey | Sort-Object Name)) { + $cluster = [System.Collections.Generic.List[object]]::new() + $flush = { + if ($cluster.Count -eq 0) { return } + $starts = @($cluster | Sort-Object { $_.Start.UtcDateTime }, ChannelKey | ForEach-Object { [pscustomobject][ordered]@{ ChannelKey = $_.ChannelKey; DisplayName = $_.DisplayName; Start = $_.Start } }) + $channelKeys = @($starts | ForEach-Object { $_.ChannelKey } | Sort-Object -Unique) + $distinct = @($starts | ForEach-Object { $_.Start.UtcDateTime } | Sort-Object -Unique) + $status = if ($channelKeys.Count -lt 2) { 'single-source' } elseif ($distinct.Count -gt 1) { 'conflict' } else { 'agreed' } + $events.Add([pscustomobject][ordered]@{ Title = $cluster[0].Title; Status = $status; ChannelKeys = $channelKeys; Starts = $starts }) + } + foreach ($entry in @($group.Group | Sort-Object { $_.Start.UtcDateTime }, ChannelKey)) { + if ($cluster.Count -gt 0 -and ($entry.Start - $cluster[0].Start) -gt $window) { & $flush; $cluster.Clear() } + $cluster.Add($entry) + } + & $flush + } + [pscustomobject][ordered]@{ Events = @($events | Sort-Object { $_.Starts[0].Start.UtcDateTime }, Title); SkippedCount = $skipped } +} diff --git a/src/ChannelForge/Private/Get-ChannelForgeEventTimeSummaryLines.ps1 b/src/ChannelForge/Private/Get-ChannelForgeEventTimeSummaryLines.ps1 new file mode 100644 index 0000000..9e64c45 --- /dev/null +++ b/src/ChannelForge/Private/Get-ChannelForgeEventTimeSummaryLines.ps1 @@ -0,0 +1,29 @@ +function Get-ChannelForgeEventTimeSummaryLines { + <# + .SYNOPSIS + Turns the event time agreement into the few lines shown in the build summary (event-channel/v2, report only). + #> + [CmdletBinding()] + param([Parameter(Mandatory)][object]$Agreement, [int]$Cap = 5) + + $events = @($Agreement.Events) + if ($events.Count -eq 0 -and $Agreement.SkippedCount -eq 0) { return @() } + $zone = try { [System.TimeZoneInfo]::FindSystemTimeZoneById('Eastern Standard Time') } catch { [System.TimeZoneInfo]::FindSystemTimeZoneById('America/New_York') } + $eastern = { + param([datetimeoffset]$Instant) + $local = [System.TimeZoneInfo]::ConvertTime($Instant, $zone) + $label = if ($zone.IsDaylightSavingTime($local)) { 'EDT' } else { 'EST' } + '{0} {1}' -f $local.ToString('MMM d h:mm tt', [System.Globalization.CultureInfo]::InvariantCulture), $label + } + $agreed = @($events | Where-Object { $_.Status -ceq 'agreed' }) + $conflict = @($events | Where-Object { $_.Status -ceq 'conflict' }) + $single = @($events | Where-Object { $_.Status -ceq 'single-source' }) + $lines = @('', '## Event Times', + "$($events.Count) events: $($agreed.Count) agreed, $($conflict.Count) conflict, $($single.Count) single-source. $($Agreement.SkippedCount) entries skipped (no usable start time or placeholder title). Nothing was changed.") + foreach ($event in @($conflict | Select-Object -First $Cap)) { + $times = @($event.Starts | ForEach-Object { "$($_.DisplayName) $(& $eastern $_.Start)" }) -join '; ' + $lines += "- Conflict: $($event.Title): $times" + } + if ($conflict.Count -gt $Cap) { $lines += "- and $($conflict.Count - $Cap) more conflicts" } + return $lines +} diff --git a/tests/unit/EventTimeAgreement.Tests.ps1 b/tests/unit/EventTimeAgreement.Tests.ps1 new file mode 100644 index 0000000..a829553 --- /dev/null +++ b/tests/unit/EventTimeAgreement.Tests.ps1 @@ -0,0 +1,55 @@ +BeforeAll { + $script:RepoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) + Import-Module (Join-Path $script:RepoRoot 'src\ChannelForge\ChannelForge.psd1') -Global -Force + + function New-Programme { param([string]$Title, $Start) [pscustomobject]@{ Title = $Title; Start = $Start; Stop = $null } } + function New-Carrier { + param([string]$Name, [object[]]$Programmes) + [pscustomobject]@{ Key = ('{0:x64}' -f $Name.Length); DisplayName = $Name; GroupTitle = 'T'; Category = $null; Programmes = $Programmes } + } + function Get-Agreement { + param([object[]]$Channel) + $candidate = @($Channel | ForEach-Object { [pscustomobject]@{ Key = $_.Key } }) + InModuleScope ChannelForge -Parameters @{ C = $candidate; Ch = $Channel } { + $result = Get-ChannelForgeEventTimeAgreement -Candidate $C -Channel $Ch + [pscustomobject]@{ Result = $result; Lines = @(Get-ChannelForgeEventTimeSummaryLines -Agreement $result) } + } + } +} + +Describe 'Event time agreement' { + It 'marks the same event at the same instant on two channels as agreed, even with different offsets' { + $one = New-Carrier 'PPV 1' @(New-Programme 'Big Fight Night' ([datetimeoffset]'2026-11-15T01:00:00Z')) + $two = New-Carrier 'PPV 22' @(New-Programme 'big fight night!' ([datetimeoffset]'2026-11-14T20:00:00-05:00')) + $out = Get-Agreement -Channel @($one, $two) + $out.Result.Events.Count | Should -Be 1 + $out.Result.Events[0].Status | Should -Be 'agreed' + $out.Result.Events[0].ChannelKeys.Count | Should -Be 2 + } + + It 'flags a conflict, keeps each channel own start, and reports both in Eastern time' { + $one = New-Carrier 'PPV 1' @(New-Programme 'Big Fight Night' ([datetimeoffset]'2026-11-15T01:00:00Z')) + $two = New-Carrier 'PPV 22' @(New-Programme 'Big Fight Night' ([datetimeoffset]'2026-11-15T01:10:00Z')) + $out = Get-Agreement -Channel @($one, $two) + $event = $out.Result.Events[0] + $event.Status | Should -Be 'conflict' + @($event.Starts | ForEach-Object { $_.Start.UtcDateTime.ToString('HH:mm') }) | Should -Be @('01:00', '01:10') + $out.Lines | Should -Contain '- Conflict: Big Fight Night: PPV 1 Nov 14 8:00 PM EST; PPV 22 Nov 14 8:10 PM EST' + } + + It 'marks an event on one channel as single-source' { + $out = Get-Agreement -Channel @(New-Carrier 'PPV 1' @(New-Programme 'Solo Show' ([datetimeoffset]'2026-11-15T01:00:00Z'))) + $out.Result.Events[0].Status | Should -Be 'single-source' + } + + It 'skips and counts a programme with no start time and a placeholder title' { + $one = New-Carrier 'PPV 1' @( + (New-Programme 'No Time Show' $null), + (New-Programme 'No Event' ([datetimeoffset]'2026-11-15T01:00:00Z')), + (New-Programme 'Real Show' ([datetimeoffset]'2026-11-15T03:00:00Z'))) + $out = Get-Agreement -Channel @($one) + $out.Result.Events.Count | Should -Be 1 + $out.Result.SkippedCount | Should -Be 2 + $out.Lines | Should -Contain '1 events: 0 agreed, 0 conflict, 1 single-source. 2 entries skipped (no usable start time or placeholder title). Nothing was changed.' + } +}