using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using ytLive.Services;
namespace ytLive.Tests;
///
/// TASK 32·2 — the reconnecting sign is driven by what YouTube says, not by a timer (spec revised
/// 2026-11-05: the creator pointed out that the cutoff is not a constant, it depends on variables
/// "some that are dark to us"). So: the chat-ended marker is a prompt to check, the broadcast status
/// decides, and a completion ends recovery honestly.
///
public class ChatEndedSignalTests
{
[Fact]
public void ChatEndedEvent_IsNotAChatRow_ButItIsASignal()
{
const string json = """
{
"items": [
{"id":"e1","snippet":{"type":"chatEndedEvent","publishedAt":"2026-11-05T10:00:00.000Z"}}
]
}
""";
// It never becomes a message…
Assert.Empty(YouTubeChatService.ParsePage(json).Messages);
}
[Fact]
public void ThePollingService_RaisesChatEnded_ForThatMarker()
{
var service = new YouTubeChatService(new YouTubeAuthService("id", "secret"));
var raised = 0;
service.ChatEnded += () => raised++;
service.NotifyChatEnded();
Assert.Equal(1, raised);
}
[Fact]
public async Task BroadcastComplete_EndsRecovery_InsteadOfPretendingToBeLive()
{
var reported = new List();
var recovery = new StreamRecovery(new StreamRetryPolicy(new[] { TimeSpan.Zero }), new StreamRecoverySteps
{
RestartAudio = _ => Task.CompletedTask,
StartPush = _ => Task.FromResult(true),
CheckBroadcast = _ => Task.FromResult(BroadcastLiveness.Complete),
Report = reported.Add,
Log = _ => { },
});
var result = await recovery.RunAsync(CancellationToken.None);
Assert.Equal(RecoveryOutcome.BroadcastComplete, result.Outcome);
Assert.Contains(reported, text => text.Contains("complete") && text.Contains("cannot be resumed"));
}
}