2025-07-27 23:33:05 -05:00
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
using StreamShorts.Library.Media;
|
|
|
|
|
|
2025-07-24 11:22:15 -05:00
|
|
|
namespace StreamShorts.Library.Tests.Unit.Media.Audio;
|
|
|
|
|
|
2025-08-20 15:00:32 -05:00
|
|
|
public class AudioExtractorTests
|
2025-07-24 11:22:15 -05:00
|
|
|
{
|
2025-07-29 18:00:09 -05:00
|
|
|
private readonly Mock<IVideoService> _mockFfmpegService = new();
|
2025-07-27 23:33:05 -05:00
|
|
|
private readonly AudioExtractor _sut;
|
|
|
|
|
|
|
|
|
|
public AudioExtractorTests()
|
|
|
|
|
{
|
|
|
|
|
_sut = new(_mockFfmpegService.Object);
|
|
|
|
|
}
|
2025-07-24 11:22:15 -05:00
|
|
|
|
2025-07-29 18:00:09 -05:00
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WhenCalledWithNullFfmpegService_ItShouldThrow()
|
|
|
|
|
{
|
|
|
|
|
var action = () => new AudioExtractor(null!);
|
|
|
|
|
|
|
|
|
|
action.Should().Throw<ArgumentNullException>();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 11:22:15 -05:00
|
|
|
[Fact]
|
|
|
|
|
public async Task ExtractMp3FromMp4Async_WhenVideoIsNull_ItShouldThrow()
|
|
|
|
|
{
|
2025-10-10 16:23:02 -05:00
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(null!, new MemoryStream());
|
2025-07-24 11:22:15 -05:00
|
|
|
|
|
|
|
|
await action.Should().ThrowAsync<ArgumentNullException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ExtractMp3FromMp4Async_WhenVideoIsNotReadable_ItShouldThrow()
|
|
|
|
|
{
|
|
|
|
|
var mockStream = new Mock<Stream>();
|
|
|
|
|
mockStream.Setup(s => s.CanRead).Returns(false);
|
|
|
|
|
|
2025-10-10 16:23:02 -05:00
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
2025-07-24 11:22:15 -05:00
|
|
|
|
|
|
|
|
await action.Should().ThrowAsync<ArgumentException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ExtractMp3FromMp4Async_WhenVideoIsNotSeekable_ItShouldThrow()
|
|
|
|
|
{
|
|
|
|
|
var mockStream = new Mock<Stream>();
|
2025-07-28 17:59:27 -05:00
|
|
|
mockStream.Setup(s => s.CanRead).Returns(true);
|
2025-07-24 11:22:15 -05:00
|
|
|
mockStream.Setup(s => s.CanSeek).Returns(false);
|
|
|
|
|
|
2025-10-10 16:23:02 -05:00
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
2025-07-24 11:22:15 -05:00
|
|
|
|
|
|
|
|
await action.Should().ThrowAsync<ArgumentException>();
|
|
|
|
|
}
|
2025-07-27 23:33:05 -05:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ExtractMp3FromMp4Async_WhenFFMpegServiceThrows_ItShouldThrow()
|
|
|
|
|
{
|
|
|
|
|
var mockStream = new Mock<Stream>();
|
|
|
|
|
mockStream.Setup(static s => s.CanRead).Returns(true);
|
|
|
|
|
mockStream.Setup(static s => s.CanSeek).Returns(true);
|
|
|
|
|
|
|
|
|
|
_mockFfmpegService.
|
|
|
|
|
Setup(
|
|
|
|
|
static m => m.ExtractAudioFromVideoAsync(
|
|
|
|
|
It.IsAny<Stream>(),
|
|
|
|
|
It.IsAny<Stream>()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.ThrowsAsync(new Exception());
|
|
|
|
|
|
2025-10-10 16:23:02 -05:00
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
2025-07-27 23:33:05 -05:00
|
|
|
|
|
|
|
|
await action.Should().ThrowAsync<FailedAudioExtractionException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ExtractMp3FromMp4Async_WhenFFMpegServiceFailsExtraction_ItShouldThrow()
|
|
|
|
|
{
|
|
|
|
|
var mockStream = new Mock<Stream>();
|
|
|
|
|
mockStream.Setup(static s => s.CanRead).Returns(true);
|
|
|
|
|
mockStream.Setup(static s => s.CanSeek).Returns(true);
|
|
|
|
|
|
|
|
|
|
_mockFfmpegService.
|
|
|
|
|
Setup(
|
|
|
|
|
static m => m.ExtractAudioFromVideoAsync(
|
|
|
|
|
It.IsAny<Stream>(),
|
|
|
|
|
It.IsAny<Stream>()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.ReturnsAsync(false);
|
|
|
|
|
|
2025-10-10 16:23:02 -05:00
|
|
|
var action = async () => await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
2025-07-27 23:33:05 -05:00
|
|
|
|
|
|
|
|
await action.Should().ThrowAsync<FailedAudioExtractionException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ExtractMp3FromMp4Async_WhenFFMpegServiceSucceedsAtExtractingAudio_ItShouldReturnStream()
|
|
|
|
|
{
|
|
|
|
|
var mockStream = new Mock<Stream>();
|
|
|
|
|
mockStream.Setup(static s => s.CanRead).Returns(true);
|
|
|
|
|
mockStream.Setup(static s => s.CanSeek).Returns(true);
|
|
|
|
|
|
|
|
|
|
_mockFfmpegService.
|
|
|
|
|
Setup(
|
|
|
|
|
static m => m.ExtractAudioFromVideoAsync(
|
|
|
|
|
It.IsAny<Stream>(),
|
|
|
|
|
It.IsAny<Stream>()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.ReturnsAsync(true);
|
|
|
|
|
|
2025-10-10 16:23:02 -05:00
|
|
|
var result = await _sut.ExtractMp3FromMp4Async(mockStream.Object, new MemoryStream());
|
2025-07-27 23:33:05 -05:00
|
|
|
|
|
|
|
|
result.Should().BeAssignableTo<Stream>();
|
|
|
|
|
result.Should().BeOfType<MemoryStream>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ExtractMp3FromMp4Async_WhenCalled_ItShouldNotMutatePositionOfPassedStream()
|
|
|
|
|
{
|
|
|
|
|
var text = "hello world";
|
|
|
|
|
var textByte = Encoding.UTF8.GetBytes(text);
|
|
|
|
|
var stream = new MemoryStream(textByte);
|
|
|
|
|
|
|
|
|
|
var positionToRead = 5;
|
|
|
|
|
var buffer = new byte[5];
|
|
|
|
|
await stream.ReadAsync(buffer.AsMemory(0, positionToRead), TestContext.Current.CancellationToken);
|
|
|
|
|
|
|
|
|
|
_mockFfmpegService.
|
|
|
|
|
Setup(
|
|
|
|
|
static m => m.ExtractAudioFromVideoAsync(
|
|
|
|
|
It.IsAny<Stream>(),
|
|
|
|
|
It.IsAny<Stream>()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.ReturnsAsync(true);
|
|
|
|
|
|
2025-10-10 16:23:02 -05:00
|
|
|
await _sut.ExtractMp3FromMp4Async(stream, new MemoryStream());
|
2025-07-27 23:33:05 -05:00
|
|
|
|
|
|
|
|
stream.Position.Should().Be(positionToRead);
|
|
|
|
|
}
|
2025-07-24 11:22:15 -05:00
|
|
|
}
|