using Titano.Core.Import;
using Titano.Testing;
namespace Titano.Core.Tests.Import;
[Category("import")]
public sealed class SessionSplitterTests
{
private static readonly DateTime Start = new(2026, 9, 28, 18, 0, 0);
private static readonly TimeSpan Gap = TimeSpan.FromMinutes(45);
/// Capture times from a compact description: runs of frames 10 s apart, separated by pauses in minutes.
private static List Times(params (int Frames, int PauseBeforeMinutes)[] runs)
{
var times = new List();
var t = Start;
foreach (var (frames, pause) in runs)
{
t = t.AddMinutes(pause);
for (var i = 0; i < frames; i++)
{
times.Add(t);
t = t.AddSeconds(10);
}
}
return times;
}
private static string Describe(IReadOnlyList ranges) => string.Join(",", ranges.Select(r => r.Count));
[Theory]
[Case(new[] { 30 }, new[] { 0 }, "30", Name = "one session")]
[Case(new[] { 30, 40 }, new[] { 0, 60 }, "30,40", Name = "pause longer than the gap splits")]
[Case(new[] { 30, 40 }, new[] { 0, 44 }, "70", Name = "pause shorter than the gap does not")]
[Case(new[] { 3, 30 }, new[] { 0, 90 }, "33", Name = "leading test shots join the next session")]
[Case(new[] { 30, 3 }, new[] { 0, 90 }, "33", Name = "trailing test shots join the previous session")]
[Case(new[] { 30, 2, 30 }, new[] { 0, 90, 90 }, "32,30", Name = "small middle group joins the previous session")]
[Case(new[] { 2, 3 }, new[] { 0, 90 }, "5", Name = "only small groups make one session")]
public void AutomaticSessions(int[] frames, int[] pauses, string expected)
{
var times = Times(frames.Zip(pauses).ToArray());
Assert.Equal(expected, Describe(SessionSplitter.Split(times, Gap, 8)));
}
[Test]
public void APauseOfExactlyTheGapDoesNotSplit()
{
var times = new List { Start };
for (var i = 1; i < 10; i++) times.Add(Start.AddSeconds(i));
for (var i = 0; i < 10; i++) times.Add(times[9] + Gap + TimeSpan.FromSeconds(i));
Assert.Equal("20", Describe(SessionSplitter.Split(times, Gap, 8)));
}
[Test]
public void UserSplitsAndMergesOverrideTheRules()
{
var times = Times((30, 0), (40, 60));
// Split before frame 25 (a 5-frame group the user wants on its own) and merge the automatic break at 30.
var ranges = SessionSplitter.Split(times, Gap, 8, i => i == 25 ? true : i == 30 ? false : null);
Assert.Equal("25,45", Describe(ranges));
Assert.Equal(25, ranges[1].Start);
}
[Test]
public void AForcedSmallSessionIsNotMergedAway()
{
var times = Times((30, 0));
Assert.Equal("28,2", Describe(SessionSplitter.Split(times, Gap, 8, i => i == 28 ? true : null)));
}
[Test]
public void NoFramesNoSessions() => Assert.Empty(SessionSplitter.Split([], Gap, 8));
}