|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.IO.Abstractions.TestingHelpers; |
| 6 | +using Elastic.Documentation.Configuration.DocSet; |
| 7 | +using Elastic.Documentation.Diagnostics; |
| 8 | +using Elastic.Documentation.Extensions; |
| 9 | +using Elastic.Documentation.Navigation.Isolated; |
| 10 | +using FluentAssertions; |
| 11 | + |
| 12 | +namespace Elastic.Documentation.Navigation.Tests.Isolation; |
| 13 | + |
| 14 | +public class FolderIndexFileRefTests(ITestOutputHelper output) : DocumentationSetNavigationTestBase(output) |
| 15 | +{ |
| 16 | + [Fact] |
| 17 | + public async Task FolderWithFileCreatesCorrectStructure() |
| 18 | + { |
| 19 | + // language=yaml |
| 20 | + var yaml = """ |
| 21 | + project: 'test-project' |
| 22 | + toc: |
| 23 | + - folder: getting-started |
| 24 | + file: getting-started.md |
| 25 | + children: |
| 26 | + - file: install.md |
| 27 | + - file: configure.md |
| 28 | + """; |
| 29 | + |
| 30 | + var fileSystem = new MockFileSystem(); |
| 31 | + fileSystem.AddDirectory("/docs"); |
| 32 | + var context = CreateContext(fileSystem); |
| 33 | + var docSet = DocumentationSetFile.LoadAndResolve(context.Collector, yaml, fileSystem.NewDirInfo("docs")); |
| 34 | + _ = context.Collector.StartAsync(TestContext.Current.CancellationToken); |
| 35 | + |
| 36 | + var navigation = new DocumentationSetNavigation<IDocumentationFile>(docSet, context, GenericDocumentationFileFactory.Instance); |
| 37 | + |
| 38 | + await context.Collector.StopAsync(TestContext.Current.CancellationToken); |
| 39 | + |
| 40 | + // Should create a FolderNavigation with the file as index |
| 41 | + navigation.NavigationItems.Should().HaveCount(1); |
| 42 | + var folder = navigation.NavigationItems.First().Should().BeOfType<FolderNavigation>().Subject; |
| 43 | + |
| 44 | + // Children should be scoped to the folder |
| 45 | + folder.NavigationItems.Should().HaveCount(3); // getting-started.md, install.md, configure.md |
| 46 | + |
| 47 | + // Verify no errors |
| 48 | + context.Collector.Errors.Should().Be(0); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task FolderWithFileChildrenPathsAreScopedToFolder() |
| 53 | + { |
| 54 | + // language=yaml |
| 55 | + var yaml = """ |
| 56 | + project: 'test-project' |
| 57 | + toc: |
| 58 | + - folder: getting-started |
| 59 | + file: getting-started.md |
| 60 | + children: |
| 61 | + - file: install.md |
| 62 | + """; |
| 63 | + |
| 64 | + var fileSystem = new MockFileSystem(); |
| 65 | + fileSystem.AddDirectory("/docs"); |
| 66 | + var context = CreateContext(fileSystem); |
| 67 | + var docSet = DocumentationSetFile.LoadAndResolve(context.Collector, yaml, fileSystem.NewDirInfo("docs")); |
| 68 | + _ = context.Collector.StartAsync(TestContext.Current.CancellationToken); |
| 69 | + |
| 70 | + _ = new DocumentationSetNavigation<IDocumentationFile>(docSet, context, GenericDocumentationFileFactory.Instance); |
| 71 | + |
| 72 | + await context.Collector.StopAsync(TestContext.Current.CancellationToken); |
| 73 | + |
| 74 | + // Verify that the FileRef for getting-started.md is a FolderIndexFileRef |
| 75 | + var folderItem = docSet.TableOfContents.First().Should().BeOfType<FolderRef>().Subject; |
| 76 | + folderItem.Children.Should().HaveCount(2); // getting-started.md and install.md |
| 77 | + |
| 78 | + var indexFile = folderItem.Children.First().Should().BeOfType<FolderIndexFileRef>().Subject; |
| 79 | + indexFile.Path.Should().Be("getting-started/getting-started.md"); |
| 80 | + |
| 81 | + var childFile = folderItem.Children.ElementAt(1).Should().BeOfType<FileRef>().Subject; |
| 82 | + childFile.Path.Should().Be("getting-started/install.md"); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task FolderWithFileEmitsHintWhenFileNameDoesNotMatchFolder() |
| 87 | + { |
| 88 | + // language=yaml |
| 89 | + var yaml = """ |
| 90 | + project: 'test-project' |
| 91 | + toc: |
| 92 | + - folder: getting-started |
| 93 | + file: intro.md |
| 94 | + children: |
| 95 | + - file: install.md |
| 96 | + """; |
| 97 | + |
| 98 | + var fileSystem = new MockFileSystem(); |
| 99 | + fileSystem.AddDirectory("/docs"); |
| 100 | + var context = CreateContext(fileSystem); |
| 101 | + var docSet = DocumentationSetFile.LoadAndResolve(context.Collector, yaml, fileSystem.NewDirInfo("docs")); |
| 102 | + _ = context.Collector.StartAsync(TestContext.Current.CancellationToken); |
| 103 | + |
| 104 | + _ = new DocumentationSetNavigation<IDocumentationFile>(docSet, context, GenericDocumentationFileFactory.Instance); |
| 105 | + |
| 106 | + await context.Collector.StopAsync(TestContext.Current.CancellationToken); |
| 107 | + |
| 108 | + // Should emit hint about file name not matching folder name |
| 109 | + context.Collector.Hints.Should().BeGreaterThan(0); |
| 110 | + var diagnostics = context.Diagnostics; |
| 111 | + diagnostics.Should().Contain(d => |
| 112 | + d.Severity == Severity.Hint && |
| 113 | + d.Message.Contains("intro.md") && |
| 114 | + d.Message.Contains("getting-started") && |
| 115 | + d.Message.Contains("Best practice")); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public async Task FolderWithFileDoesNotEmitHintWhenFileNameMatchesFolder() |
| 120 | + { |
| 121 | + // language=yaml |
| 122 | + var yaml = """ |
| 123 | + project: 'test-project' |
| 124 | + toc: |
| 125 | + - folder: getting-started |
| 126 | + file: getting-started.md |
| 127 | + children: |
| 128 | + - file: install.md |
| 129 | + """; |
| 130 | + |
| 131 | + var fileSystem = new MockFileSystem(); |
| 132 | + fileSystem.AddDirectory("/docs"); |
| 133 | + var context = CreateContext(fileSystem); |
| 134 | + var docSet = DocumentationSetFile.LoadAndResolve(context.Collector, yaml, fileSystem.NewDirInfo("docs")); |
| 135 | + _ = context.Collector.StartAsync(TestContext.Current.CancellationToken); |
| 136 | + |
| 137 | + _ = new DocumentationSetNavigation<IDocumentationFile>(docSet, context, GenericDocumentationFileFactory.Instance); |
| 138 | + |
| 139 | + await context.Collector.StopAsync(TestContext.Current.CancellationToken); |
| 140 | + |
| 141 | + // Should not emit any hints |
| 142 | + context.Collector.Hints.Should().Be(0); |
| 143 | + context.Diagnostics.Should().BeEmpty(); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public async Task FolderWithFileEmitsErrorForDeepLinkingInFile() |
| 148 | + { |
| 149 | + // language=yaml |
| 150 | + var yaml = """ |
| 151 | + project: 'test-project' |
| 152 | + toc: |
| 153 | + - folder: getting-started |
| 154 | + file: intro/file.md |
| 155 | + children: |
| 156 | + - file: install.md |
| 157 | + """; |
| 158 | + |
| 159 | + var fileSystem = new MockFileSystem(); |
| 160 | + fileSystem.AddDirectory("/docs"); |
| 161 | + var context = CreateContext(fileSystem); |
| 162 | + var docSet = DocumentationSetFile.LoadAndResolve(context.Collector, yaml, fileSystem.NewDirInfo("docs")); |
| 163 | + _ = context.Collector.StartAsync(TestContext.Current.CancellationToken); |
| 164 | + |
| 165 | + _ = new DocumentationSetNavigation<IDocumentationFile>(docSet, context, GenericDocumentationFileFactory.Instance); |
| 166 | + |
| 167 | + await context.Collector.StopAsync(TestContext.Current.CancellationToken); |
| 168 | + |
| 169 | + // Should emit error about deep linking in the file attribute |
| 170 | + context.Collector.Errors.Should().BeGreaterThan(0); |
| 171 | + var diagnostics = context.Diagnostics; |
| 172 | + diagnostics.Should().Contain(d => |
| 173 | + d.Severity == Severity.Error && |
| 174 | + d.Message.Contains("Deep linking on folder 'file' is not supported") && |
| 175 | + d.Message.Contains("intro/file.md")); |
| 176 | + } |
| 177 | + |
| 178 | + [Fact] |
| 179 | + public async Task FolderWithIndexMdFileDoesNotNeedToMatchFolderName() |
| 180 | + { |
| 181 | + // language=yaml |
| 182 | + var yaml = """ |
| 183 | + project: 'test-project' |
| 184 | + toc: |
| 185 | + - folder: getting-started |
| 186 | + file: index.md |
| 187 | + children: |
| 188 | + - file: install.md |
| 189 | + """; |
| 190 | + |
| 191 | + var fileSystem = new MockFileSystem(); |
| 192 | + fileSystem.AddDirectory("/docs"); |
| 193 | + var context = CreateContext(fileSystem); |
| 194 | + var docSet = DocumentationSetFile.LoadAndResolve(context.Collector, yaml, fileSystem.NewDirInfo("docs")); |
| 195 | + _ = context.Collector.StartAsync(TestContext.Current.CancellationToken); |
| 196 | + |
| 197 | + _ = new DocumentationSetNavigation<IDocumentationFile>(docSet, context, GenericDocumentationFileFactory.Instance); |
| 198 | + |
| 199 | + await context.Collector.StopAsync(TestContext.Current.CancellationToken); |
| 200 | + |
| 201 | + // index.md is a special case - should not emit hint |
| 202 | + // (Though the hint check doesn't exclude index.md, it's a reasonable best practice to allow it) |
| 203 | + context.Collector.Errors.Should().Be(0); |
| 204 | + } |
| 205 | + |
| 206 | + [Fact] |
| 207 | + public async Task FolderWithFileCaseInsensitiveMatch() |
| 208 | + { |
| 209 | + // language=yaml |
| 210 | + var yaml = """ |
| 211 | + project: 'test-project' |
| 212 | + toc: |
| 213 | + - folder: GettingStarted |
| 214 | + file: getting-started.md |
| 215 | + children: |
| 216 | + - file: install.md |
| 217 | + """; |
| 218 | + |
| 219 | + var fileSystem = new MockFileSystem(); |
| 220 | + fileSystem.AddDirectory("/docs"); |
| 221 | + var context = CreateContext(fileSystem); |
| 222 | + var docSet = DocumentationSetFile.LoadAndResolve(context.Collector, yaml, fileSystem.NewDirInfo("docs")); |
| 223 | + _ = context.Collector.StartAsync(TestContext.Current.CancellationToken); |
| 224 | + |
| 225 | + _ = new DocumentationSetNavigation<IDocumentationFile>(docSet, context, GenericDocumentationFileFactory.Instance); |
| 226 | + |
| 227 | + await context.Collector.StopAsync(TestContext.Current.CancellationToken); |
| 228 | + |
| 229 | + // Case-insensitive match should not emit hint |
| 230 | + context.Collector.Hints.Should().Be(0); |
| 231 | + context.Diagnostics.Should().BeEmpty(); |
| 232 | + } |
| 233 | +} |
0 commit comments