|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Text.Json; |
| 5 | +using System.Text.Json.Nodes; |
| 6 | +using Aspire.Dashboard.Components.Dialogs; |
| 7 | +using Aspire.Dashboard.Components.Resize; |
| 8 | +using Aspire.Dashboard.Components.Tests.Shared; |
| 9 | +using Aspire.Dashboard.Model; |
| 10 | +using Aspire.Dashboard.Model.GenAI; |
| 11 | +using Aspire.Dashboard.Otlp.Model; |
| 12 | +using Aspire.Dashboard.Otlp.Storage; |
| 13 | +using Bunit; |
| 14 | +using Microsoft.Extensions.DependencyInjection; |
| 15 | +using Microsoft.Extensions.Localization; |
| 16 | +using Microsoft.Extensions.Logging.Abstractions; |
| 17 | +using Microsoft.FluentUI.AspNetCore.Components; |
| 18 | +using Xunit; |
| 19 | +using static Aspire.Tests.Shared.Telemetry.TelemetryTestHelpers; |
| 20 | + |
| 21 | +namespace Aspire.Dashboard.Components.Tests.Controls; |
| 22 | + |
| 23 | +public class GenAIVisualizerDialogTests : DashboardTestContext |
| 24 | +{ |
| 25 | + private static readonly DateTime s_testTime = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public async Task Render_NoGenAIAttributes_Success() |
| 29 | + { |
| 30 | + var context = new OtlpContext { Logger = NullLogger.Instance, Options = new() }; |
| 31 | + var resource = new OtlpResource("app", "instance", uninstrumentedPeer: false, context); |
| 32 | + |
| 33 | + var trace = new OtlpTrace(new byte[] { 1, 2, 3 }, DateTime.MinValue); |
| 34 | + var scope = CreateOtlpScope(context); |
| 35 | + |
| 36 | + var cut = SetUpDialog(out var dialogService); |
| 37 | + await GenAIVisualizerDialog.OpenDialogAsync( |
| 38 | + viewportInformation: new ViewportInformation(IsDesktop: true, IsUltraLowHeight: false, IsUltraLowWidth: false), |
| 39 | + dialogService: dialogService, |
| 40 | + dialogsLoc: Services.GetRequiredService<IStringLocalizer<Aspire.Dashboard.Resources.Dialogs>>(), |
| 41 | + span: CreateOtlpSpan(resource, trace, scope, spanId: "abc", parentSpanId: null, startDate: s_testTime), |
| 42 | + selectedLogEntryId: null, |
| 43 | + telemetryRepository: Services.GetRequiredService<TelemetryRepository>(), |
| 44 | + resources: [], |
| 45 | + getContextGenAISpans: () => [] |
| 46 | + ); |
| 47 | + |
| 48 | + var instance = cut.FindComponent<GenAIVisualizerDialog>().Instance; |
| 49 | + |
| 50 | + Assert.Empty(instance.Content.Items); |
| 51 | + Assert.Equal("app", instance.Content.SourceName); |
| 52 | + Assert.Equal("unknown-peer", instance.Content.PeerName); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task Render_HasGenAIMessages_Success() |
| 57 | + { |
| 58 | + var context = new OtlpContext { Logger = NullLogger.Instance, Options = new() }; |
| 59 | + var resource = new OtlpResource("app", "instance", uninstrumentedPeer: false, context); |
| 60 | + |
| 61 | + var systemInstruction = JsonSerializer.Serialize(new List<MessagePart> |
| 62 | + { |
| 63 | + new TextPart { Content = "System!" } |
| 64 | + }, GenAIMessagesContext.Default.ListMessagePart); |
| 65 | + |
| 66 | + var inputMessages = JsonSerializer.Serialize(new List<ChatMessage> |
| 67 | + { |
| 68 | + new ChatMessage |
| 69 | + { |
| 70 | + Role = "user", |
| 71 | + Parts = [new TextPart { Content = "User!" }] |
| 72 | + }, |
| 73 | + new ChatMessage |
| 74 | + { |
| 75 | + Role = "assistant", |
| 76 | + Parts = [new ToolCallRequestPart { Name = "generate_names", Arguments = JsonNode.Parse(@"{""count"":2}") }] |
| 77 | + }, |
| 78 | + new ChatMessage |
| 79 | + { |
| 80 | + Role = "user", |
| 81 | + Parts = [new ToolCallResponsePart { Response = JsonNode.Parse(@"[""Jack"",""Jane""]") }] |
| 82 | + } |
| 83 | + }, GenAIMessagesContext.Default.ListChatMessage); |
| 84 | + |
| 85 | + var outputMessages = JsonSerializer.Serialize(new List<ChatMessage> |
| 86 | + { |
| 87 | + new ChatMessage |
| 88 | + { |
| 89 | + Role = "assistant", |
| 90 | + Parts = [new TextPart { Content = "Output!" }] |
| 91 | + } |
| 92 | + }, GenAIMessagesContext.Default.ListChatMessage); |
| 93 | + |
| 94 | + var trace = new OtlpTrace(new byte[] { 1, 2, 3 }, DateTime.MinValue); |
| 95 | + var scope = CreateOtlpScope(context); |
| 96 | + var span = CreateOtlpSpan(resource, trace, scope, spanId: "abc", parentSpanId: null, startDate: s_testTime, attributes: [ |
| 97 | + KeyValuePair.Create(GenAIHelpers.GenAISystemInstructions, systemInstruction), |
| 98 | + KeyValuePair.Create(GenAIHelpers.GenAIInputMessages, inputMessages), |
| 99 | + KeyValuePair.Create(GenAIHelpers.GenAIOutputInstructions, outputMessages) |
| 100 | + ]); |
| 101 | + |
| 102 | + var cut = SetUpDialog(out var dialogService); |
| 103 | + await GenAIVisualizerDialog.OpenDialogAsync( |
| 104 | + viewportInformation: new ViewportInformation(IsDesktop: true, IsUltraLowHeight: false, IsUltraLowWidth: false), |
| 105 | + dialogService: dialogService, |
| 106 | + dialogsLoc: Services.GetRequiredService<IStringLocalizer<Aspire.Dashboard.Resources.Dialogs>>(), |
| 107 | + span: span, |
| 108 | + selectedLogEntryId: null, |
| 109 | + telemetryRepository: Services.GetRequiredService<TelemetryRepository>(), |
| 110 | + resources: [], |
| 111 | + getContextGenAISpans: () => [] |
| 112 | + ); |
| 113 | + |
| 114 | + var instance = cut.FindComponent<GenAIVisualizerDialog>().Instance; |
| 115 | + |
| 116 | + Assert.Equal(5, instance.Content.Items.Count); |
| 117 | + } |
| 118 | + |
| 119 | + private IRenderedFragment SetUpDialog(out IDialogService dialogService) |
| 120 | + { |
| 121 | + var version = typeof(FluentMain).Assembly.GetName().Version!; |
| 122 | + |
| 123 | + Services.AddFluentUIComponents(); |
| 124 | + Services.AddSingleton<LibraryConfiguration>(); |
| 125 | + Services.AddSingleton<TelemetryRepository>(); |
| 126 | + Services.AddSingleton<PauseManager>(); |
| 127 | + Services.AddSingleton(new ThemeManager(new TestThemeResolver())); |
| 128 | + |
| 129 | + Services.AddLocalization(); |
| 130 | + Services.AddSingleton<BrowserTimeProvider, TestTimeProvider>(); |
| 131 | + |
| 132 | + var cut = Render(builder => |
| 133 | + { |
| 134 | + builder.OpenComponent<FluentDialogProvider>(0); |
| 135 | + builder.CloseComponent(); |
| 136 | + }); |
| 137 | + |
| 138 | + // Setting a provider ID on menu service is required to simulate <FluentMenuProvider> on the page. |
| 139 | + // This makes FluentMenu render without error. |
| 140 | + var menuService = Services.GetRequiredService<IMenuService>(); |
| 141 | + menuService.ProviderId = "Test"; |
| 142 | + |
| 143 | + dialogService = Services.GetRequiredService<IDialogService>(); |
| 144 | + return cut; |
| 145 | + } |
| 146 | +} |
0 commit comments