|
| 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; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Xunit; |
| 8 | +using Xunit.Abstractions; |
| 9 | + |
| 10 | +namespace Microsoft.Extensions.AI.Templates.Tests; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Contains execution tests for the "AI Chat Web" template. |
| 14 | +/// </summary> |
| 15 | +/// <remarks> |
| 16 | +/// In addition to validating that the templates build and restore correctly, |
| 17 | +/// these tests are also responsible for template component governance reporting. |
| 18 | +/// This is because the generated output is left on disk after tests complete, |
| 19 | +/// most importantly the project.assets.json file that gets created during restore. |
| 20 | +/// Therefore, it's *critical* that these tests remain in a working state, |
| 21 | +/// as disabling them will also disable CG reporting. |
| 22 | +/// </remarks> |
| 23 | +public class AIChatWebExecutionTests : TemplateExecutionTestBase<AIChatWebExecutionTests>, ITemplateExecutionTestConfigurationProvider |
| 24 | +{ |
| 25 | + public AIChatWebExecutionTests(TemplateExecutionTestFixture fixture, ITestOutputHelper outputHelper) |
| 26 | + : base(fixture, outputHelper) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public static TemplateExecutionTestConfiguration Configuration { get; } = new() |
| 31 | + { |
| 32 | + TemplatePackageName = "Microsoft.Extensions.AI.Templates", |
| 33 | + TestOutputFolderPrefix = "AIChatWeb" |
| 34 | + }; |
| 35 | + |
| 36 | + public static IEnumerable<object[]> GetBasicTemplateOptions() |
| 37 | + => GetFilteredTemplateOptions("--aspire", "false"); |
| 38 | + |
| 39 | + public static IEnumerable<object[]> GetAspireTemplateOptions() |
| 40 | + => GetFilteredTemplateOptions("--aspire", "true"); |
| 41 | + |
| 42 | + // Do not skip. See XML docs for this test class. |
| 43 | + [Theory] |
| 44 | + [MemberData(nameof(GetBasicTemplateOptions))] |
| 45 | + public async Task CreateRestoreAndBuild_BasicTemplate(params string[] args) |
| 46 | + { |
| 47 | + const string ProjectName = "BasicApp"; |
| 48 | + var project = await Fixture.CreateProjectAsync( |
| 49 | + templateName: "aichatweb", |
| 50 | + projectName: ProjectName, |
| 51 | + args); |
| 52 | + |
| 53 | + await Fixture.RestoreProjectAsync(project); |
| 54 | + await Fixture.BuildProjectAsync(project); |
| 55 | + } |
| 56 | + |
| 57 | + // Do not skip. See XML docs for this test class. |
| 58 | + [Theory] |
| 59 | + [MemberData(nameof(GetAspireTemplateOptions))] |
| 60 | + public async Task CreateRestoreAndBuild_AspireTemplate(params string[] args) |
| 61 | + { |
| 62 | + const string ProjectName = "AspireApp"; |
| 63 | + var project = await Fixture.CreateProjectAsync( |
| 64 | + templateName: "aichatweb", |
| 65 | + ProjectName, |
| 66 | + args); |
| 67 | + |
| 68 | + project.StartupProjectRelativePath = $"{ProjectName}.AppHost"; |
| 69 | + |
| 70 | + await Fixture.RestoreProjectAsync(project); |
| 71 | + await Fixture.BuildProjectAsync(project); |
| 72 | + } |
| 73 | + |
| 74 | + private static readonly (string name, string[] values)[] _templateOptions = [ |
| 75 | + ("--provider", ["azureopenai", "githubmodels", "ollama", "openai"]), |
| 76 | + ("--vector-store", ["azureaisearch", "local", "qdrant"]), |
| 77 | + ("--managed-identity", ["true", "false"]), |
| 78 | + ("--aspire", ["true", "false"]), |
| 79 | + ]; |
| 80 | + |
| 81 | + private static IEnumerable<object[]> GetFilteredTemplateOptions(params string[] filter) |
| 82 | + { |
| 83 | + foreach (var options in GetAllPossibleOptions(_templateOptions)) |
| 84 | + { |
| 85 | + if (!MatchesFilter()) |
| 86 | + { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (HasOption("--managed-identity", "true")) |
| 91 | + { |
| 92 | + if (HasOption("--aspire", "true")) |
| 93 | + { |
| 94 | + // The managed identity option is disabled for the Aspire template. |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if (!HasOption("--vector-store", "azureaisearch") && |
| 99 | + !HasOption("--aspire", "false")) |
| 100 | + { |
| 101 | + // Can only use managed identity when using Azure in the non-Aspire template. |
| 102 | + continue; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (HasOption("--vector-store", "qdrant") && |
| 107 | + HasOption("--aspire", "false")) |
| 108 | + { |
| 109 | + // Can't use Qdrant without Aspire. |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + yield return options; |
| 114 | + |
| 115 | + bool MatchesFilter() |
| 116 | + { |
| 117 | + for (var i = 0; i < filter.Length; i += 2) |
| 118 | + { |
| 119 | + if (!HasOption(filter[i], filter[i + 1])) |
| 120 | + { |
| 121 | + return false; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + bool HasOption(string name, string value) |
| 129 | + { |
| 130 | + for (var i = 0; i < options.Length; i += 2) |
| 131 | + { |
| 132 | + if (string.Equals(name, options[i], StringComparison.Ordinal) && |
| 133 | + string.Equals(value, options[i + 1], StringComparison.Ordinal)) |
| 134 | + { |
| 135 | + return true; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private static IEnumerable<string[]> GetAllPossibleOptions(ReadOnlyMemory<(string name, string[] values)> options) |
| 145 | + { |
| 146 | + if (options.Length == 0) |
| 147 | + { |
| 148 | + yield return []; |
| 149 | + yield break; |
| 150 | + } |
| 151 | + |
| 152 | + var first = options.Span[0]; |
| 153 | + foreach (var restSelection in GetAllPossibleOptions(options[1..])) |
| 154 | + { |
| 155 | + foreach (var value in first.values) |
| 156 | + { |
| 157 | + yield return [first.name, value, .. restSelection]; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments