|
| 1 | +// ------------------------------------------------------------------------ |
| 2 | +// This file is licensed to you under the MIT License. |
| 3 | +// ------------------------------------------------------------------------ |
| 4 | + |
| 5 | +using FluentUI.Demo.DocApiGen.Models; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Linq; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace FluentUI.Demo.DocApiGen.IntegrationTests; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Integration tests using the real Microsoft.FluentUI.AspNetCore.Components.xml file. |
| 15 | +/// These tests validate documentation generation for actual FluentUI components with |
| 16 | +/// the ApiClassGenerator (Summary mode focus). |
| 17 | +/// Note: MCP (All mode) tests are skipped for now - will be implemented later. |
| 18 | +/// </summary> |
| 19 | +public class FluentUIComponentsIntegrationTests : IDisposable |
| 20 | +{ |
| 21 | + private readonly FileInfo _xmlDocumentation; |
| 22 | + private readonly string _tempOutputDirectory; |
| 23 | + private readonly string _xmlPath; |
| 24 | + private readonly Assembly _fluentUIAssembly; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the <see cref="FluentUIComponentsIntegrationTests"/> class. |
| 28 | + /// </summary> |
| 29 | + public FluentUIComponentsIntegrationTests() |
| 30 | + { |
| 31 | + _tempOutputDirectory = Path.Combine(Path.GetTempPath(), $"DocApiGen_FluentUI_Tests_{Guid.NewGuid()}"); |
| 32 | + Directory.CreateDirectory(_tempOutputDirectory); |
| 33 | + |
| 34 | + // Get project root directory |
| 35 | + var projectRoot = GetProjectRootDirectory(); |
| 36 | + _xmlPath = Path.Combine(projectRoot, "examples", "Tools", "FluentUI.Demo.DocApiGen", "Microsoft.FluentUI.AspNetCore.Components.xml"); |
| 37 | + |
| 38 | + if (!File.Exists(_xmlPath)) |
| 39 | + { |
| 40 | + throw new FileNotFoundException($"XML documentation file not found at: {_xmlPath}"); |
| 41 | + } |
| 42 | + |
| 43 | + _xmlDocumentation = new FileInfo(_xmlPath); |
| 44 | + |
| 45 | + // Load the FluentUI assembly dynamically |
| 46 | + var fluentUIAssemblyPath = Path.Combine(projectRoot, "src", "Core", "bin", "Debug", "net9.0", "Microsoft.FluentUI.AspNetCore.Components.dll"); |
| 47 | + |
| 48 | + if (!File.Exists(fluentUIAssemblyPath)) |
| 49 | + { |
| 50 | + // Try alternative path (Release build) |
| 51 | + fluentUIAssemblyPath = Path.Combine(projectRoot, "src", "Core", "bin", "Release", "net9.0", "Microsoft.FluentUI.AspNetCore.Components.dll"); |
| 52 | + |
| 53 | + if (!File.Exists(fluentUIAssemblyPath)) |
| 54 | + { |
| 55 | + throw new FileNotFoundException($"FluentUI assembly not found. Please build the Core project first. Looked for: {fluentUIAssemblyPath}"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + _fluentUIAssembly = Assembly.LoadFrom(fluentUIAssemblyPath); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets the project root directory by walking up from the current directory. |
| 64 | + /// </summary> |
| 65 | + private static string GetProjectRootDirectory() |
| 66 | + { |
| 67 | + var directory = new DirectoryInfo(Directory.GetCurrentDirectory()); |
| 68 | + |
| 69 | + // Look for solution file |
| 70 | + while (directory != null) |
| 71 | + { |
| 72 | + var solutionFiles = directory.GetFiles("*.sln"); |
| 73 | + if (solutionFiles.Length > 0) |
| 74 | + { |
| 75 | + return directory.FullName; |
| 76 | + } |
| 77 | + |
| 78 | + directory = directory.Parent; |
| 79 | + } |
| 80 | + |
| 81 | + throw new InvalidOperationException($"Could not find project root directory. Current directory: {Directory.GetCurrentDirectory()}"); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Cleanup temporary files and directories. |
| 86 | + /// </summary> |
| 87 | + public void Dispose() |
| 88 | + { |
| 89 | + if (Directory.Exists(_tempOutputDirectory)) |
| 90 | + { |
| 91 | + try |
| 92 | + { |
| 93 | + Directory.Delete(_tempOutputDirectory, recursive: true); |
| 94 | + } |
| 95 | + catch |
| 96 | + { |
| 97 | + // Ignore cleanup errors |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + #region ApiClassGenerator (Summary Mode) Tests |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void ApiGenerator_ShouldGenerateJsonSuccessfully() |
| 106 | + { |
| 107 | + // Arrange |
| 108 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 109 | + |
| 110 | + // Act |
| 111 | + var json = generator.GenerateJson(GenerationMode.Summary); |
| 112 | + |
| 113 | + // Assert |
| 114 | + Assert.NotNull(json); |
| 115 | + Assert.NotEmpty(json); |
| 116 | + Assert.Contains("__Generated__", json); |
| 117 | + Assert.Contains("\"Mode\": \"Summary\"", json); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void ApiGenerator_ShouldGenerateCSharpSuccessfully() |
| 122 | + { |
| 123 | + // Arrange |
| 124 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 125 | + |
| 126 | + // Act |
| 127 | + var code = generator.GenerateCSharp(GenerationMode.Summary); |
| 128 | + |
| 129 | + // Assert |
| 130 | + Assert.NotNull(code); |
| 131 | + Assert.NotEmpty(code); |
| 132 | + Assert.Contains("public class CodeComments", code); |
| 133 | + Assert.Contains("Mode: Summary", code); |
| 134 | + Assert.Contains("SummaryData", code); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public void ApiGenerator_JsonOutput_ShouldContainFluentUIComponents() |
| 139 | + { |
| 140 | + // Arrange |
| 141 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 142 | + |
| 143 | + // Act |
| 144 | + var json = generator.GenerateJson(GenerationMode.Summary); |
| 145 | + |
| 146 | + // Assert |
| 147 | + // Should contain common FluentUI component names |
| 148 | + Assert.Contains("FluentButton", json); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public void ApiGenerator_CSharpOutput_ShouldContainFluentUIComponents() |
| 153 | + { |
| 154 | + // Arrange |
| 155 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 156 | + |
| 157 | + // Act |
| 158 | + var code = generator.GenerateCSharp(GenerationMode.Summary); |
| 159 | + |
| 160 | + // Assert |
| 161 | + // Should contain common FluentUI component names |
| 162 | + Assert.Contains("FluentButton", code); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public void ApiGenerator_JsonOutput_ShouldBeValidJson() |
| 167 | + { |
| 168 | + // Arrange |
| 169 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 170 | + |
| 171 | + // Act |
| 172 | + var json = generator.GenerateJson(GenerationMode.Summary); |
| 173 | + |
| 174 | + // Assert - Verify it's valid JSON |
| 175 | + var exception = Record.Exception(() => JsonDocument.Parse(json)); |
| 176 | + Assert.Null(exception); |
| 177 | + } |
| 178 | + |
| 179 | + [Fact] |
| 180 | + public void ApiGenerator_SaveToFile_JsonShouldSucceed() |
| 181 | + { |
| 182 | + // Arrange |
| 183 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 184 | + var outputPath = Path.Combine(_tempOutputDirectory, "fluentui_summary.json"); |
| 185 | + |
| 186 | + // Act |
| 187 | + generator.SaveToFile(outputPath, "json", GenerationMode.Summary); |
| 188 | + |
| 189 | + // Assert |
| 190 | + Assert.True(File.Exists(outputPath)); |
| 191 | + |
| 192 | + var content = File.ReadAllText(outputPath); |
| 193 | + Assert.NotEmpty(content); |
| 194 | + Assert.Contains("__Generated__", content); |
| 195 | + |
| 196 | + // Verify valid JSON |
| 197 | + var exception = Record.Exception(() => JsonDocument.Parse(content)); |
| 198 | + Assert.Null(exception); |
| 199 | + } |
| 200 | + |
| 201 | + [Fact] |
| 202 | + public void ApiGenerator_SaveToFile_CSharpShouldSucceed() |
| 203 | + { |
| 204 | + // Arrange |
| 205 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 206 | + var outputPath = Path.Combine(_tempOutputDirectory, "fluentui_summary.cs"); |
| 207 | + |
| 208 | + // Act |
| 209 | + generator.SaveToFile(outputPath, "csharp", GenerationMode.Summary); |
| 210 | + |
| 211 | + // Assert |
| 212 | + Assert.True(File.Exists(outputPath)); |
| 213 | + |
| 214 | + var content = File.ReadAllText(outputPath); |
| 215 | + Assert.NotEmpty(content); |
| 216 | + Assert.Contains("public class CodeComments", content); |
| 217 | + } |
| 218 | + |
| 219 | + [Fact] |
| 220 | + public void ApiGenerator_LargeScale_ShouldCompleteWithoutErrors() |
| 221 | + { |
| 222 | + // Arrange |
| 223 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 224 | + |
| 225 | + // Act & Assert - Should complete without throwing |
| 226 | + var exception = Record.Exception(() => |
| 227 | + { |
| 228 | + var json = generator.GenerateJson(GenerationMode.Summary); |
| 229 | + Assert.NotNull(json); |
| 230 | + Assert.NotEmpty(json); |
| 231 | + |
| 232 | + var code = generator.GenerateCSharp(GenerationMode.Summary); |
| 233 | + Assert.NotNull(code); |
| 234 | + Assert.NotEmpty(code); |
| 235 | + }); |
| 236 | + |
| 237 | + Assert.Null(exception); |
| 238 | + } |
| 239 | + |
| 240 | + [Fact] |
| 241 | + public void ApiGenerator_OutputSize_ShouldBeReasonable() |
| 242 | + { |
| 243 | + // Arrange |
| 244 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 245 | + |
| 246 | + // Act |
| 247 | + var json = generator.GenerateJson(GenerationMode.Summary); |
| 248 | + var code = generator.GenerateCSharp(GenerationMode.Summary); |
| 249 | + |
| 250 | + // Assert - Output should be substantial but not excessive |
| 251 | + Assert.True(json.Length > 1000, "JSON output should be substantial"); |
| 252 | + Assert.True(json.Length < 50_000_000, "JSON output should not be excessive"); |
| 253 | + |
| 254 | + Assert.True(code.Length > 1000, "C# output should be substantial"); |
| 255 | + Assert.True(code.Length < 50_000_000, "C# output should not be excessive"); |
| 256 | + } |
| 257 | + |
| 258 | + [Fact] |
| 259 | + public void ApiGenerator_JsonMetadata_ShouldBePresent() |
| 260 | + { |
| 261 | + // Arrange |
| 262 | + var generator = new ApiClassGenerator(_fluentUIAssembly, _xmlDocumentation); |
| 263 | + |
| 264 | + // Act |
| 265 | + var json = generator.GenerateJson(GenerationMode.Summary); |
| 266 | + using var doc = JsonDocument.Parse(json); |
| 267 | + |
| 268 | + // Assert |
| 269 | + var root = doc.RootElement; |
| 270 | + Assert.True(root.TryGetProperty("__Generated__", out var generated)); |
| 271 | + |
| 272 | + var generatedObj = generated; |
| 273 | + Assert.True(generatedObj.TryGetProperty("AssemblyVersion", out _)); |
| 274 | + Assert.True(generatedObj.TryGetProperty("DateUtc", out _)); |
| 275 | + Assert.True(generatedObj.TryGetProperty("Mode", out var mode)); |
| 276 | + Assert.Equal("Summary", mode.GetString()); |
| 277 | + } |
| 278 | + |
| 279 | + #endregion |
| 280 | + |
| 281 | + #region MCP Tests (Skipped - Future Implementation) |
| 282 | + |
| 283 | + // Note: MCP/All mode tests are commented out for now |
| 284 | + // These will be implemented in a future phase |
| 285 | + |
| 286 | + /* |
| 287 | + [Fact(Skip = "MCP implementation deferred")] |
| 288 | + public void McpGenerator_WillBeImplementedLater() |
| 289 | + { |
| 290 | + // MCP tests will be added when ready to work on All mode |
| 291 | + Assert.True(true); |
| 292 | + } |
| 293 | + */ |
| 294 | + |
| 295 | + #endregion |
| 296 | +} |
0 commit comments