using AmiReel.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AmiReel.Tests.Services;

[TestClass]
public class FfmpegProgressParserTests
{
    [TestMethod]
    public void Parse_WithFullProgressLine_ExtractsAllFields()
    {
        // Arrange
        const string line = "frame= 1234 fps=49.8 q=-1.0 size=  102400kB time=00:00:24.68 bitrate=33987.6kbits/s speed=0.996x";

        // Act
        FfmpegOutputLine result = FfmpegProgressParser.Parse(line);

        // Assert
        Assert.AreEqual(new TimeSpan(0, 0, 0, 24, 680), result.Time);
        Assert.AreEqual(1234, result.Frame);
        Assert.AreEqual(49.8, result.FramesPerSecond);
        Assert.AreEqual(0.996, result.Speed);
    }

    [TestMethod]
    public void Parse_WithNoRecognizedFields_ReturnsAllNulls()
    {
        // Arrange
        const string line = "Metadata:";

        // Act
        FfmpegOutputLine result = FfmpegProgressParser.Parse(line);

        // Assert
        Assert.IsNull(result.Time);
        Assert.IsNull(result.Frame);
        Assert.IsNull(result.FramesPerSecond);
        Assert.IsNull(result.Speed);
        Assert.AreEqual(line, result.Line);
    }

    [TestMethod]
    public void Parse_WithOnlyTimeField_ExtractsTimeAndLeavesFrameFieldsNull()
    {
        // Arrange: has a "time=" field but no matching frame=/fps=/speed=x group
        const string line = "size=    1024kB time=00:01:02.03 bitrate= 100.0kbits/s";

        // Act
        FfmpegOutputLine result = FfmpegProgressParser.Parse(line);

        // Assert
        Assert.AreEqual(new TimeSpan(0, 0, 1, 2, 30), result.Time);
        Assert.IsNull(result.Frame);
        Assert.IsNull(result.FramesPerSecond);
        Assert.IsNull(result.Speed);
    }

    [TestMethod]
    public void Parse_PreservesOriginalLineText()
    {
        // Arrange
        const string line = "some arbitrary ffmpeg output";

        // Act
        FfmpegOutputLine result = FfmpegProgressParser.Parse(line);

        // Assert
        Assert.AreEqual(line, result.Line);
    }
}
