Skip to content

Commit

Permalink
Merge pull request #76 from LuxoriaSoft/feat/lda/improvements
Browse files Browse the repository at this point in the history
feat: Some improvements on Architecture and Core functions
  • Loading branch information
QuentinCraft authored Dec 8, 2024
2 parents b7c7d12 + 5132654 commit 91ab364
Show file tree
Hide file tree
Showing 25 changed files with 395 additions and 203 deletions.
8 changes: 4 additions & 4 deletions Luxoria.App/Luxoria.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public static IHostBuilder CreateHostBuilder(Startup startup)
/// <param name="args">Details about the launch request and process.</param>
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
_logger.Log("Application is starting...");
await _logger.LogAsync("Application is starting...");

// Show splash screen
var splashScreen = new SplashScreen();
splashScreen.Activate();

_logger.Log("Modules loaded. Closing slasph screen...");
await _logger.LogAsync("Modules loaded. Closing slasph screen...");
await Task.Delay(500);

// Load modules asynchronously and update the splash screen with the module names
Expand Down Expand Up @@ -140,7 +140,7 @@ private async Task LoadModulesFromFolderAsync(string moduleFolder, ModuleLoader

if (moduleFiles.Length == 0)
{
_logger.Log($"No module DLL files found in: {moduleFolder}", LOG_SECTION, LogLevel.Warning);
await _logger.LogAsync($"No module DLL files found in: {moduleFolder}", LOG_SECTION, LogLevel.Warning);
return;
}

Expand All @@ -163,7 +163,7 @@ private async Task LoadModuleAsync(string moduleFile, string moduleName, ModuleL
{
// Update the splash screen to indicate the module being loaded
await UpdateSplashScreenAsync(splashScreen, $"Loading {moduleName}...");
_logger.Log($"Trying to load: {moduleName}");
await _logger.LogAsync($"Trying to load: {moduleName}");

try
{
Expand Down
2 changes: 1 addition & 1 deletion Luxoria.App/Luxoria.App/Dialogs/OpenCollectionDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task ShowAsync(IEventBus eventBus, ILoggerService loggerServ
{
string selectedFolderPath = openCollectionControl.SelectedFolderPath;
string collectionName = openCollectionControl.CollectionName;
loggerService.Log($"Selected folder path: {selectedFolderPath}");
await loggerService.LogAsync($"Selected folder path: {selectedFolderPath}");

await ImportationDialog.ShowAsync(eventBus, loggerService, collectionName, selectedFolderPath, xamlRoot);
}
Expand Down
50 changes: 0 additions & 50 deletions Luxoria.App/Luxoria.Core.Tests/ModuleServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,6 @@ public ModuleServiceTests()
_moduleService = new ModuleService(_mockEventBus.Object, _mockLogger.Object);
}

[Fact]
public void Constructor_WithNullEventBus_ShouldThrowArgumentNullException()
{
// Arrange
IEventBus nullEventBus = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => new ModuleService(nullEventBus, _mockLogger.Object));
}

[Fact]
public void Constructor_WithNullLogger_ShouldThrowArgumentNullException()
{
// Arrange
ILoggerService nullLogger = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => new ModuleService(_mockEventBus.Object, nullLogger));
}

[Fact]
public void AddModule_WithValidModule_ShouldAddModuleToList()
{
Expand All @@ -51,16 +31,6 @@ public void AddModule_WithValidModule_ShouldAddModuleToList()
Assert.Contains(mockModule.Object, _moduleService.GetModules());
}

[Fact]
public void AddModule_NullModule_ShouldThrowArgumentNullException()
{
// Arrange
IModule nullModule = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => _moduleService.AddModule(nullModule));
}

[Fact]
public void RemoveModule_WithExistingModule_ShouldRemoveModuleFromList()
{
Expand All @@ -86,16 +56,6 @@ public void RemoveModule_WithNonexistentModule_ShouldNotThrow()
Assert.NotNull(mockModule);
}

[Fact]
public void RemoveModule_NullModule_ShouldThrowArgumentNullException()
{
// Arrange
IModule nullModule = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => _moduleService.RemoveModule(nullModule));
}

[Fact]
public void GetModules_WhenModulesExist_ShouldReturnListOfModules()
{
Expand Down Expand Up @@ -145,15 +105,5 @@ public void InitializeModules_WithNoModules_ShouldNotThrow()
_moduleService.InitializeModules(mockContext.Object); // Should not throw
Assert.NotNull(mockContext);
}

[Fact]
public void InitializeModules_NullContext_ShouldThrowArgumentNullException()
{
// Arrange
IModuleContext nullContext = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => _moduleService.InitializeModules(nullContext));
}
}
}
5 changes: 4 additions & 1 deletion Luxoria.App/Luxoria.Core.Tests/StartupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public void ConfigureServices_RegistersRequiredServices()

// Mock ILoggerService
var mockLogger = new Mock<ILoggerService>();
mockLogger.Setup(x => x.Log(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<LogLevel>()));
// Specify explicit setup to avoid optional arguments
mockLogger.Setup(x => x.Log(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<LogLevel>(), "", "", 0));

// Register mock in the service collection
serviceCollection.AddSingleton(mockLogger.Object);

var startup = new Startup();
Expand Down
8 changes: 5 additions & 3 deletions Luxoria.App/Luxoria.Core/Logics/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Luxoria.Core.Interfaces;
using Luxoria.Core.Interfaces;
using Luxoria.Core.Services;
using Luxoria.Modules;
using Luxoria.Modules.Interfaces;
using Luxoria.SDK.Interfaces;
using Luxoria.SDK.LogTargets;
using Luxoria.SDK.Models;
using Luxoria.SDK.Services;
using Luxoria.SDK.Services.Targets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand All @@ -18,7 +20,7 @@ public class Startup
/// </summary>
public void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
ILoggerService logger = new LoggerService();
LoggerService logger = new(LogLevel.Debug, new DebugLogTarget(), new FileLogTarget("log.txt"));
logger.Log("Configuring services...", LOG_SECTION, LogLevel.Info);
// Register services here

Expand All @@ -36,7 +38,7 @@ public void ConfigureServices(HostBuilderContext context, IServiceCollection ser

// Register Logger Service
logger.Log("Registering Logger Service...", LOG_SECTION, LogLevel.Info);
services.AddSingleton(logger);
services.AddSingleton(logger as ILoggerService);
logger.Log("Logger Service registered successfully !", LOG_SECTION, LogLevel.Info);

logger.Log("Services registered successfully !", LOG_SECTION, LogLevel.Info);
Expand Down
55 changes: 13 additions & 42 deletions Luxoria.App/Luxoria.Modules.Tests/BusTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Luxoria.Modules.Models.Events;
using Moq;
using System.Threading.Tasks;

namespace Luxoria.Modules.Tests
{
Expand All @@ -14,58 +15,38 @@ public EventBusTests()
}

[Fact]
public void Subscribe_WithValidHandler_ShouldAddSubscriber()
public async Task Subscribe_WithValidHandler_ShouldAddSubscriber()
{
// Arrange
var mockHandler = new Mock<Action<LogEvent>>();

// Act
_eventBus.Subscribe(mockHandler.Object);
_eventBus.Publish(new LogEvent("Test message"));
await _eventBus.Publish(new LogEvent("Test message"));

// Assert
mockHandler.Verify(handler => handler(It.IsAny<LogEvent>()), Times.Once,
"The handler should have been called once when a message is published, but it was not.");
}

[Fact]
public void Subscribe_NullHandler_ShouldThrowArgumentNullException()
{
// Arrange
Action<LogEvent> nullHandler = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => _eventBus.Subscribe(nullHandler));
}

[Fact]
public void SubscribeAsync_NullHandler_ShouldThrowArgumentNullException()
{
// Arrange
Func<LogEvent, Task> nullHandler = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => _eventBus.Subscribe(nullHandler));
}

[Fact]
public void Unsubscribe_WithValidHandler_ShouldRemoveSubscriber()
public async Task Unsubscribe_WithValidHandler_ShouldRemoveSubscriber()
{
// Arrange
var mockHandler = new Mock<Action<LogEvent>>();
_eventBus.Subscribe(mockHandler.Object);

// Act
_eventBus.Unsubscribe(mockHandler.Object);
_eventBus.Publish(new LogEvent("Test message"));
await _eventBus.Publish(new LogEvent("Test message"));

// Assert
mockHandler.Verify(handler => handler(It.IsAny<LogEvent>()), Times.Never,
"The handler should not be called after being unsubscribed, but it was.");
}

[Fact]
public void Unsubscribe_WithValidAsyncHandler_ShouldRemoveAsyncSubscriber()
public async Task Unsubscribe_WithValidAsyncHandler_ShouldRemoveAsyncSubscriber()
{
// Arrange
var mockAsyncHandler = new Mock<Func<LogEvent, Task>>();
Expand All @@ -74,15 +55,15 @@ public void Unsubscribe_WithValidAsyncHandler_ShouldRemoveAsyncSubscriber()

// Act
_eventBus.Unsubscribe(mockAsyncHandler.Object);
_eventBus.Publish(new LogEvent("Test message"));
await _eventBus.Publish(new LogEvent("Test message"));

// Assert
mockAsyncHandler.Verify(handler => handler(It.IsAny<LogEvent>()), Times.Never,
"The async handler should not be called after being unsubscribed, but it was.");
}

[Fact]
public void Unsubscribe_WithMultipleSubscribers_ShouldOnlyRemoveSpecifiedSubscriber()
public async Task Unsubscribe_WithMultipleSubscribers_ShouldOnlyRemoveSpecifiedSubscriber()
{
// Arrange
var mockHandler1 = new Mock<Action<LogEvent>>();
Expand All @@ -92,7 +73,7 @@ public void Unsubscribe_WithMultipleSubscribers_ShouldOnlyRemoveSpecifiedSubscri

// Act
_eventBus.Unsubscribe(mockHandler1.Object);
_eventBus.Publish(new LogEvent("Test message"));
await _eventBus.Publish(new LogEvent("Test message"));

// Assert
mockHandler1.Verify(handler => handler(It.IsAny<LogEvent>()), Times.Never,
Expand All @@ -116,7 +97,7 @@ public void Unsubscribe_SameHandlerTwice_ShouldNotThrow()
}

[Fact]
public void UnsubscribeAsync_WithMultipleIdenticalSubscribers_ShouldRemoveOneInstanceAtATime()
public async Task UnsubscribeAsync_WithMultipleIdenticalSubscribers_ShouldRemoveOneInstanceAtATime()
{
// Arrange
var mockAsyncHandler = new Mock<Func<LogEvent, Task>>();
Expand All @@ -126,7 +107,7 @@ public void UnsubscribeAsync_WithMultipleIdenticalSubscribers_ShouldRemoveOneIns

// Act
_eventBus.Unsubscribe(mockAsyncHandler.Object);
_eventBus.Publish(new LogEvent("Test message"));
await _eventBus.Publish(new LogEvent("Test message"));

// Assert
mockAsyncHandler.Verify(handler => handler(It.IsAny<LogEvent>()), Times.Once,
Expand Down Expand Up @@ -173,7 +154,7 @@ public void Unsubscribe_WithValidAsyncHandler_ShouldNotThrowWhenNotSubscribed()
}

[Fact]
public void Publish_WithMultipleSubscribers_ShouldNotifyAllSubscribers()
public async Task Publish_WithMultipleSubscribers_ShouldNotifyAllSubscribers()
{
// Arrange
var mockHandler1 = new Mock<Action<LogEvent>>();
Expand All @@ -182,7 +163,7 @@ public void Publish_WithMultipleSubscribers_ShouldNotifyAllSubscribers()
_eventBus.Subscribe(mockHandler2.Object);

// Act
_eventBus.Publish(new LogEvent("Test message"));
await _eventBus.Publish(new LogEvent("Test message"));

// Assert
mockHandler1.Verify(handler => handler(It.IsAny<LogEvent>()), Times.Once,
Expand Down Expand Up @@ -219,16 +200,6 @@ public async Task Publish_WithNoSubscribers_ShouldNotThrow()
Assert.NotNull(_eventBus);
}

[Fact]
public async Task Publish_NullEvent_ShouldThrowArgumentNullException()
{
// Arrange
LogEvent nullEvent = null;

// Act & Assert
await Assert.ThrowsAsync<ArgumentNullException>(() => _eventBus.Publish(nullEvent));
}

[Fact]
public void LogEvent_Constructor_WithMessage_ShouldSetMessageProperty()
{
Expand Down
20 changes: 0 additions & 20 deletions Luxoria.App/Luxoria.Modules.Tests/ModuleContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ public void UpdateImage_ShouldSetCurrentImage()
Assert.Equal(newImage, result);
}

[Fact]
public void UpdateImage_WithNull_ShouldThrowArgumentNullException()
{
// Arrange
ImageData nullImage = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => _moduleContext.UpdateImage(nullImage));
}

[Fact]
public void LogMessage_WithValidMessage_ShouldNotThrow()
{
Expand All @@ -63,15 +53,5 @@ public void LogMessage_WithValidMessage_ShouldNotThrow()
var exception = Record.Exception(() => _moduleContext.LogMessage(logMessage));
Assert.Null(exception); // Ensure that no exception is thrown
}

[Fact]
public void LogMessage_WithNullMessage_ShouldThrowArgumentNullException()
{
// Arrange
string logMessage = null;

// Act & Assert
Assert.Throws<ArgumentNullException>(() => _moduleContext.LogMessage(logMessage));
}
}
}
10 changes: 5 additions & 5 deletions Luxoria.App/Luxoria.Modules/EventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class EventBus : IEventBus
/// Publishes an event to all subscribed handlers.
/// Supports both synchronous and asynchronous execution.
/// </summary>
public async Task Publish<TEvent>(TEvent @event)
public async Task Publish<TEvent>(TEvent @event) where TEvent : IEvent
{
if (EqualityComparer<TEvent>.Default.Equals(@event, default(TEvent)))
{
Expand Down Expand Up @@ -48,7 +48,7 @@ public async Task Publish<TEvent>(TEvent @event)
/// <summary>
/// Subscribes a synchronous handler to the specified event type.
/// </summary>
public void Subscribe<TEvent>(Action<TEvent> handler)
public void Subscribe<TEvent>(Action<TEvent> handler) where TEvent : IEvent
{
if (handler == null)
{
Expand All @@ -67,7 +67,7 @@ public void Subscribe<TEvent>(Action<TEvent> handler)
/// <summary>
/// Subscribes an asynchronous handler to the specified event type.
/// </summary>
public void Subscribe<TEvent>(Func<TEvent, Task> asyncHandler)
public void Subscribe<TEvent>(Func<TEvent, Task> asyncHandler) where TEvent : IEvent
{
if (asyncHandler == null)
{
Expand All @@ -86,7 +86,7 @@ public void Subscribe<TEvent>(Func<TEvent, Task> asyncHandler)
/// <summary>
/// Unsubscribes a synchronous handler from the specified event type.
/// </summary>
public void Unsubscribe<TEvent>(Action<TEvent> handler)
public void Unsubscribe<TEvent>(Action<TEvent> handler) where TEvent : IEvent
{
if (handler == null)
{
Expand All @@ -102,7 +102,7 @@ public void Unsubscribe<TEvent>(Action<TEvent> handler)
/// <summary>
/// Unsubscribes an asynchronous handler from the specified event type.
/// </summary>
public void Unsubscribe<TEvent>(Func<TEvent, Task> asyncHandler)
public void Unsubscribe<TEvent>(Func<TEvent, Task> asyncHandler) where TEvent : IEvent
{
if (asyncHandler == null)
{
Expand Down
Loading

0 comments on commit 91ab364

Please sign in to comment.