|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using Moq; |
| 4 | +using RemoteLogViewer.Composition.Stores.Settings; |
| 5 | +using RemoteLogViewer.Core.Services; |
| 6 | +using RemoteLogViewer.Core.Services.Viewer; |
| 7 | +using RemoteLogViewer.Core.Stores.Settings; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace RemoteLogViewer.Core.Tests.Services.Viewer; |
| 11 | + |
| 12 | +public class HighlightServiceTests { |
| 13 | + [Fact] |
| 14 | + public void CreateStyledLine_ShouldHighlightCorrectly() { |
| 15 | + var serviceProvider = CreateServiceProvider(); |
| 16 | + var settingsStore = serviceProvider.GetRequiredService<SettingsStoreModel>(); |
| 17 | + var highlightService = serviceProvider.GetRequiredService<HighlightService>(); |
| 18 | + |
| 19 | + var highlightSettings = settingsStore.SettingsModel.HighlightSettings; |
| 20 | + var rule1 = highlightSettings.AddRule(); |
| 21 | + var cond1 = rule1.AddCondition(); |
| 22 | + cond1.Pattern.Value = "test"; |
| 23 | + cond1.PatternType.Value = HighlightPatternType.Exact; |
| 24 | + cond1.HighlightOnlyMatch.Value = true; |
| 25 | + |
| 26 | + highlightService.CreateCss(".wrapper"); |
| 27 | + |
| 28 | + var result = highlightService.CreateStyledLine("this is a test line"); |
| 29 | + |
| 30 | + Assert.Contains("<span class=\"c0\">test</span>", result); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void CreateStyledLine_OverlappingStyles_ShouldHandleCorrectly() { |
| 35 | + var serviceProvider = CreateServiceProvider(); |
| 36 | + var settingsStore = serviceProvider.GetRequiredService<SettingsStoreModel>(); |
| 37 | + var highlightService = serviceProvider.GetRequiredService<HighlightService>(); |
| 38 | + |
| 39 | + var highlightSettings = settingsStore.SettingsModel.HighlightSettings; |
| 40 | + |
| 41 | + // Priority 0 (Lower number, higher priority) |
| 42 | + var rule0 = highlightSettings.AddRule(); |
| 43 | + var cond0 = rule0.AddCondition(); |
| 44 | + cond0.Pattern.Value = "abcde"; |
| 45 | + cond0.PatternType.Value = HighlightPatternType.Exact; |
| 46 | + cond0.HighlightOnlyMatch.Value = true; |
| 47 | + |
| 48 | + // Priority 1 |
| 49 | + var rule1 = highlightSettings.AddRule(); |
| 50 | + var cond1 = rule1.AddCondition(); |
| 51 | + cond1.Pattern.Value = "bcd"; |
| 52 | + cond1.PatternType.Value = HighlightPatternType.Exact; |
| 53 | + cond1.HighlightOnlyMatch.Value = true; |
| 54 | + |
| 55 | + highlightService.CreateCss(".wrapper"); |
| 56 | + |
| 57 | + var result = highlightService.CreateStyledLine("abcde"); |
| 58 | + |
| 59 | + // Expected behavior depends on implementation details of nesting, |
| 60 | + // but both classes should be present in some form. |
| 61 | + Assert.Contains("c0", result); |
| 62 | + Assert.Contains("c1", result); |
| 63 | + } |
| 64 | + |
| 65 | + private static IServiceProvider CreateServiceProvider() { |
| 66 | + var services = new ServiceCollection(); |
| 67 | + services.AddSingleton(Mock.Of<ILogger<SettingsStoreModel>>()); |
| 68 | + services.AddSingleton(Mock.Of<ILogger<WorkspaceService>>()); |
| 69 | + services.AddSingleton<WorkspaceService>(); |
| 70 | + services.AddSingleton<SettingsStoreModel>(); |
| 71 | + services.AddTransient<HighlightService>(); |
| 72 | + services.AddScoped<HighlightConditionModel>(sp => new HighlightConditionModel(sp)); |
| 73 | + services.AddSingleton<SettingsModel>(); |
| 74 | + services.AddSingleton<HighlightSettingsModel>(); |
| 75 | + services.AddSingleton<TextViewerSettingsModel>(); |
| 76 | + services.AddSingleton<AdvancedSettingsModel>(); |
| 77 | + services.AddScoped<HighlightRuleModel>(); |
| 78 | + return services.BuildServiceProvider(); |
| 79 | + } |
| 80 | +} |
0 commit comments