using qdr.app.studiou.orders2printpack.EmailStorage; using System.IO.Abstractions.TestingHelpers; namespace Test { [TestFixture] public class EmailItemTests { private MockFileSystem _mockFileSystem; private string _basePath; private MetadataItem _testMetadata; [SetUp] public void SetUp() { _mockFileSystem = new MockFileSystem(); _basePath = @"C:\TestBase"; _testMetadata = new MetadataItem { DirectoryName = "test_user_at_example_dot_com", EmailAddress = "test.user@example.com", State = EmailStateEnum.Ready, Subject = "Test Subject", Body = "Test Body", FileNameAttachment = "test_attachment.zip", SizeAttachment = 1024, InternalFilesCount = 3, Orders = "1001,1002,1003" }; // Create base directory _mockFileSystem.AddDirectory(_basePath); } #region Constructor Tests [Test] public void Constructor_WithValidParameters_ShouldInitializeCorrectly() { // Act var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Assert Assert.That(emailItem.Metadata, Is.EqualTo(_testMetadata)); Assert.That(emailItem.DirectoryPath, Is.EqualTo(Path.Combine(_basePath, _testMetadata.DirectoryName))); } [Test] public void Constructor_WithNullMetadata_ShouldThrowArgumentNullException() { // Act & Assert var ex = Assert.Throws(() => new EmailItem(null, _basePath, _mockFileSystem)); Assert.That(ex.ParamName, Is.EqualTo("metadata")); } [Test] public void Constructor_WithNullBasePath_ShouldThrowArgumentNullException() { // Act & Assert var ex = Assert.Throws(() => new EmailItem(_testMetadata, null, _mockFileSystem)); Assert.That(ex.ParamName, Is.EqualTo("basePath")); } #endregion #region Property Tests [Test] public void DirectoryPath_ShouldCombineBasePathWithDirectoryName() { // Arrange var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); var expectedPath = Path.Combine(_basePath, _testMetadata.DirectoryName); // Act & Assert Assert.That(emailItem.DirectoryPath, Is.EqualTo(expectedPath)); } [Test] public void DirectoryExists_WhenDirectoryExists_ShouldReturnTrue() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.DirectoryExists, Is.True); } [Test] public void DirectoryExists_WhenDirectoryDoesNotExist_ShouldReturnFalse() { // Arrange var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.DirectoryExists, Is.False); } [Test] public void JpgFiles_WhenDirectoryExists_ShouldReturnJpgFiles() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image2.jpg"), new MockFileData("image2")); _mockFileSystem.AddFile(Path.Combine(directoryPath, "document.txt"), new MockFileData("text")); var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var jpgFiles = emailItem.JpgFiles.ToList(); // Assert Assert.That(jpgFiles.Count, Is.EqualTo(2)); Assert.That(jpgFiles.Any(f => f.EndsWith("image1.jpg")), Is.True); Assert.That(jpgFiles.Any(f => f.EndsWith("image2.jpg")), Is.True); Assert.That(jpgFiles.Any(f => f.EndsWith("document.txt")), Is.False); } [Test] public void JpgFiles_WhenDirectoryDoesNotExist_ShouldReturnEmptyEnumerable() { // Arrange var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var jpgFiles = emailItem.JpgFiles.ToList(); // Assert Assert.That(jpgFiles, Is.Empty); } [Test] public void FileCount_WhenDirectoryHasJpgFiles_ShouldReturnCorrectCount() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image2.jpg"), new MockFileData("image2")); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image3.jpg"), new MockFileData("image3")); var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.FileCount, Is.EqualTo(3)); } [Test] public void FileCount_WhenDirectoryDoesNotExist_ShouldReturnZero() { // Arrange var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.FileCount, Is.EqualTo(0)); } [Test] public void TotalFileSize_WhenDirectoryHasJpgFiles_ShouldReturnCorrectTotalSize() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData(new byte[100])); // 100 bytes _mockFileSystem.AddFile(Path.Combine(directoryPath, "image2.jpg"), new MockFileData(new byte[200])); // 200 bytes _mockFileSystem.AddFile(Path.Combine(directoryPath, "image3.jpg"), new MockFileData(new byte[300])); // 300 bytes var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.TotalFileSize, Is.EqualTo(600)); } [Test] public void TotalFileSize_WhenDirectoryDoesNotExist_ShouldReturnZero() { // Arrange var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.TotalFileSize, Is.EqualTo(0)); } [Test] public void IsReadyForProcessing_WhenStateIsReadyAndDirectoryExistsWithFiles_ShouldReturnTrue() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _testMetadata.State = EmailStateEnum.Ready; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.IsReadyForProcessing, Is.True); } [Test] public void IsReadyForProcessing_WhenStateIsNotReady_ShouldReturnFalse() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _testMetadata.State = EmailStateEnum.Processing; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.IsReadyForProcessing, Is.False); } [Test] public void IsReadyForProcessing_WhenDirectoryDoesNotExist_ShouldReturnFalse() { // Arrange _testMetadata.State = EmailStateEnum.Ready; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.IsReadyForProcessing, Is.False); } [Test] public void IsReadyForProcessing_WhenDirectoryExistsButNoFiles_ShouldReturnFalse() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _testMetadata.State = EmailStateEnum.Ready; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.IsReadyForProcessing, Is.False); } [Test] public void CanBeReset_WhenStateIsNotProcessingOrUploading_ShouldReturnTrue() { // Arrange var testCases = new[] { EmailStateEnum.Ready, EmailStateEnum.Sending, EmailStateEnum.Sent, EmailStateEnum.Error }; foreach (var state in testCases) { _testMetadata.State = state; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.CanBeReset, Is.True, $"Failed for state: {state}"); } } [Test] public void CanBeReset_WhenStateIsProcessingOrUploading_ShouldReturnFalse() { // Arrange var testCases = new[] { EmailStateEnum.Processing, EmailStateEnum.Uploading }; foreach (var state in testCases) { _testMetadata.State = state; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.CanBeReset, Is.False, $"Failed for state: {state}"); } } [Test] public void NormalizedRecipient_ShouldReplaceSpecialCharacters() { // Arrange _testMetadata.EmailAddress = "test.user@example.com"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.NormalizedRecipient, Is.EqualTo("test_dot_user_at_example_dot_com")); } [Test] public void NormalizedRecipient_WithComplexEmail_ShouldHandleMultipleSpecialCharacters() { // Arrange _testMetadata.EmailAddress = "test.user.name@sub.domain.example.com"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.NormalizedRecipient, Is.EqualTo("test_dot_user_dot_name_at_sub_dot_domain_dot_example_dot_com")); } #endregion #region Method Tests [Test] public void GenerateZipFileName_WithDefaultTimestamp_ShouldIncludeCurrentTime() { // Arrange _testMetadata.EmailAddress = "test.user@example.com"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var fileName = emailItem.GenerateZipFileName(); // Assert Assert.That(fileName, Does.StartWith("studioufoto_test_dot_user_at_example_dot_com_")); Assert.That(fileName, Does.EndWith(".zip")); // Check that it contains a timestamp pattern (YYYYMMDD_HHMMSS) var timestampPattern = @"\d{8}_\d{6}"; Assert.That(fileName, Does.Match($@"studioufoto_test_dot_user_at_example_dot_com_{timestampPattern}\.zip")); } [Test] public void GenerateZipFileName_WithSpecificTimestamp_ShouldUseProvidedTimestamp() { // Arrange _testMetadata.EmailAddress = "test.user@example.com"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); var timestamp = new DateTime(2024, 5, 30, 14, 30, 45); // Act var fileName = emailItem.GenerateZipFileName(timestamp); // Assert Assert.That(fileName, Is.EqualTo("studioufoto_test_dot_user_at_example_dot_com_20240530_143045.zip")); } [Test] public void Validate_WithAllValidData_ShouldReturnValid() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _testMetadata.EmailAddress = "test.user@example.com"; _testMetadata.Subject = "Valid Subject"; _testMetadata.Body = "Valid Body"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var (isValid, issues) = emailItem.Validate(); // Assert Assert.That(isValid, Is.True); Assert.That(issues, Is.Empty); } [Test] public void Validate_WithMissingEmailAddress_ShouldReturnInvalid() { // Arrange _testMetadata.EmailAddress = ""; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var (isValid, issues) = emailItem.Validate(); // Assert Assert.That(isValid, Is.False); Assert.That(issues, Contains.Item("Email address is required")); } [Test] public void Validate_WithNullEmailAddress_ShouldReturnInvalid() { // Arrange _testMetadata.EmailAddress = null; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var (isValid, issues) = emailItem.Validate(); // Assert Assert.That(isValid, Is.False); Assert.That(issues, Contains.Item("Email address is required")); } [Test] public void Validate_WithNonExistentDirectory_ShouldReturnInvalid() { // Arrange _testMetadata.EmailAddress = "test.user@example.com"; _testMetadata.Subject = "Valid Subject"; _testMetadata.Body = "Valid Body"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var (isValid, issues) = emailItem.Validate(); // Assert Assert.That(isValid, Is.False); Assert.That(issues, Contains.Item($"Directory does not exist: {emailItem.DirectoryPath}")); } [Test] public void Validate_WithDirectoryButNoJpgFiles_ShouldReturnInvalid() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _testMetadata.EmailAddress = "test.user@example.com"; _testMetadata.Subject = "Valid Subject"; _testMetadata.Body = "Valid Body"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var (isValid, issues) = emailItem.Validate(); // Assert Assert.That(isValid, Is.False); Assert.That(issues, Contains.Item("No JPG files found in directory")); } [Test] public void Validate_WithMissingSubject_ShouldReturnInvalid() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _testMetadata.EmailAddress = "test.user@example.com"; _testMetadata.Subject = ""; _testMetadata.Body = "Valid Body"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var (isValid, issues) = emailItem.Validate(); // Assert Assert.That(isValid, Is.False); Assert.That(issues, Contains.Item("Email subject is required")); } [Test] public void Validate_WithMissingBody_ShouldReturnInvalid() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _testMetadata.EmailAddress = "test.user@example.com"; _testMetadata.Subject = "Valid Subject"; _testMetadata.Body = ""; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var (isValid, issues) = emailItem.Validate(); // Assert Assert.That(isValid, Is.False); Assert.That(issues, Contains.Item("Email body is required")); } [Test] public void Validate_WithMultipleIssues_ShouldReturnAllIssues() { // Arrange _testMetadata.EmailAddress = ""; _testMetadata.Subject = ""; _testMetadata.Body = ""; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var (isValid, issues) = emailItem.Validate(); // Assert Assert.That(isValid, Is.False); Assert.That(issues.Count, Is.EqualTo(4)); // Email, Directory, Subject, Body Assert.That(issues, Contains.Item("Email address is required")); Assert.That(issues, Contains.Item($"Directory does not exist: {emailItem.DirectoryPath}")); Assert.That(issues, Contains.Item("Email subject is required")); Assert.That(issues, Contains.Item("Email body is required")); } [Test] public void GetSummary_ShouldReturnFormattedSummary() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image2.jpg"), new MockFileData("image2")); _testMetadata.EmailAddress = "test.user@example.com"; _testMetadata.State = EmailStateEnum.Ready; _testMetadata.Orders = "1001,1002"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var summary = emailItem.GetSummary(); // Assert Assert.That(summary, Is.EqualTo("test.user@example.com - 2 files, Ready, Orders: 1001,1002")); } [Test] public void GetSummary_WithNoFiles_ShouldShowZeroFiles() { // Arrange _testMetadata.EmailAddress = "test.user@example.com"; _testMetadata.State = EmailStateEnum.Error; _testMetadata.Orders = "1001"; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var summary = emailItem.GetSummary(); // Assert Assert.That(summary, Is.EqualTo("test.user@example.com - 0 files, Error, Orders: 1001")); } #endregion #region Edge Cases and Boundary Tests [Test] public void NormalizedRecipient_WithEmptyEmail_ShouldReturnEmpty() { // Arrange _testMetadata.EmailAddress = ""; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act & Assert Assert.That(emailItem.NormalizedRecipient, Is.EqualTo("")); } [Test] public void GenerateZipFileName_WithEmptyEmail_ShouldStillGenerateFileName() { // Arrange _testMetadata.EmailAddress = ""; var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var fileName = emailItem.GenerateZipFileName(); // Assert Assert.That(fileName, Does.StartWith("studioufoto__")); Assert.That(fileName, Does.EndWith(".zip")); } [Test] public void JpgFiles_WithMixedCaseExtensions_ShouldReturnJpgFilesOnly() { // Arrange var directoryPath = Path.Combine(_basePath, _testMetadata.DirectoryName); _mockFileSystem.AddDirectory(directoryPath); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image1.jpg"), new MockFileData("image1")); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image2.JPG"), new MockFileData("image2")); _mockFileSystem.AddFile(Path.Combine(directoryPath, "image3.jpeg"), new MockFileData("image3")); _mockFileSystem.AddFile(Path.Combine(directoryPath, "document.txt"), new MockFileData("text")); var emailItem = new EmailItem(_testMetadata, _basePath, _mockFileSystem); // Act var jpgFiles = emailItem.JpgFiles.ToList(); // Assert // Note: MockFileSystem's GetFiles with "*.jpg" pattern is case-sensitive // This test verifies the current behavior - adjust if case-insensitive matching is needed Assert.That(jpgFiles.Count, Is.EqualTo(2)); // Only lowercase .jpg Assert.That(jpgFiles.Any(f => f.EndsWith("image1.jpg")), Is.True); } #endregion } }