-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchPluginTests.cs
More file actions
180 lines (162 loc) · 7.09 KB
/
Copy pathSearchPluginTests.cs
File metadata and controls
180 lines (162 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Netdocs.Abstractions;
using Netdocs.Plugins;
using Xunit;
namespace Netdocs.Core.Tests;
/// <summary>
/// Verifies the search index emitter honors its (previously hard-coded) lunr config knobs:
/// <c>lang</c> (single or list), <c>separator</c>, and <c>pipeline</c>.
/// </summary>
public class SearchPluginTests
{
private sealed class FakeContext(IReadOnlyDictionary<string, object?> options) : IPluginContext
{
public SiteConfig Config { get; } = new();
public BuildOptions Options { get; } = new();
public ILogger Logger { get; } = NullLogger.Instance;
public IServiceCollection Services { get; } = new ServiceCollection();
public IReadOnlyDictionary<string, object?> PluginOptions { get; } = options;
public void AddStylesheet(string href) { }
public void AddScript(string src, bool defer = true) { }
public void AddInlineScript(string javascript) { }
public void AddAsset(string sourcePath, string destRelative) { }
}
private static JsonElement EmitConfig(IReadOnlyDictionary<string, object?> options)
{
var dir = Path.Combine(Path.GetTempPath(), "netdocs-search-" + Guid.NewGuid().ToString("N"));
try
{
var plugin = new SearchPlugin();
plugin.Configure(new FakeContext(options));
var config = new SiteConfig { ProjectRoot = dir };
var site = new SiteContext
{
Config = config,
Options = new BuildOptions(),
LoggerFactory = NullLoggerFactory.Instance,
};
site.Pages.Add(new Page
{
SourcePath = "",
RelativePath = "index.md",
Url = "",
Title = "Home",
HtmlContent = "<p>Hello world.</p>",
PlainText = "Hello world.",
});
plugin.OnBuildCompleteAsync(site, CancellationToken.None).GetAwaiter().GetResult();
var json = File.ReadAllText(Path.Combine(config.AbsoluteSiteDir, "search", "search_index.json"));
return JsonDocument.Parse(json).RootElement.GetProperty("config").Clone();
}
finally
{
if (Directory.Exists(dir)) Directory.Delete(dir, recursive: true);
}
}
private static JsonElement EmitIndex(Page page)
{
var dir = Path.Combine(Path.GetTempPath(), "netdocs-search-" + Guid.NewGuid().ToString("N"));
try
{
var plugin = new SearchPlugin();
plugin.Configure(new FakeContext(new Dictionary<string, object?>()));
var config = new SiteConfig { ProjectRoot = dir };
var site = new SiteContext
{
Config = config,
Options = new BuildOptions(),
LoggerFactory = NullLoggerFactory.Instance,
};
site.Pages.Add(page);
plugin.OnBuildCompleteAsync(site, CancellationToken.None).GetAwaiter().GetResult();
var json = File.ReadAllText(Path.Combine(config.AbsoluteSiteDir, "search", "search_index.json"));
return JsonDocument.Parse(json).RootElement.Clone();
}
finally
{
if (Directory.Exists(dir)) Directory.Delete(dir, recursive: true);
}
}
[Fact]
public void PageLevelDoc_CarriesTextAfterH1_ForTeaser()
{
// Material shows the page-level doc's text as the teaser on each result's main line.
// The H1 is the page title, so the paragraph beneath it must land in the page-level doc,
// not be swallowed by an H1 "section" (which leaves the teaser empty).
var page = new Page
{
SourcePath = "",
RelativePath = "guide.md",
Url = "guide/",
Title = "Guide",
HtmlContent = "<h1 id=\"guide\">Guide</h1><p>Intro paragraph here.</p><h2 id=\"details\">Details</h2><p>Section body.</p>",
PlainText = "Guide Intro paragraph here. Details Section body.",
};
var index = EmitIndex(page);
var docs = index.GetProperty("docs").EnumerateArray().ToList();
var pageDoc = docs.First(d => d.GetProperty("location").GetString() == "guide/");
Assert.Contains("Intro paragraph here.", pageDoc.GetProperty("text").GetString());
// The H1 must not become its own anchored section.
Assert.DoesNotContain(docs, d => d.GetProperty("location").GetString() == "guide/#guide");
// The H2 still produces a section doc.
Assert.Contains(docs, d => d.GetProperty("location").GetString() == "guide/#details");
}
[Fact]
public void SearchText_StripsOnlyBloatElements()
{
var page = new Page
{
SourcePath = "",
RelativePath = "index.md",
Url = "",
Title = "Home",
HtmlContent = "<h1 id=\"home\">Home</h1><p>Hello <strong>world</strong>.</p><svg>diagram</svg><img src=\"photo.jpg\" /><figure>caption</figure>",
PlainText = "Home Hello world.",
};
var index = EmitIndex(page);
var pageDoc = index.GetProperty("docs").EnumerateArray()
.First(d => d.GetProperty("location").GetString() == "");
var text = pageDoc.GetProperty("text").GetString();
// Semantic HTML is preserved for rich teaser rendering
Assert.Contains("<p>Hello <strong>world</strong>.</p>", text);
// Bloat elements (SVG, img, figure) are removed
Assert.DoesNotContain("<svg>", text);
Assert.DoesNotContain("<img", text);
Assert.DoesNotContain("<figure>", text);
Assert.DoesNotContain("diagram", text); // SVG content stripped too
Assert.DoesNotContain("caption", text); // figure content stripped too
}
[Fact]
public void Defaults_MatchMaterialLunrConfig()
{
var cfg = EmitConfig(new Dictionary<string, object?>());
Assert.Equal("en", cfg.GetProperty("lang")[0].GetString());
Assert.Equal("[\\s\\-]+", cfg.GetProperty("separator").GetString());
Assert.Equal(
new[] { "stemmer", "stopWordFilter", "trimmer" },
cfg.GetProperty("pipeline").EnumerateArray().Select(e => e.GetString()).ToArray());
}
[Fact]
public void Lang_AcceptsAList()
{
var cfg = EmitConfig(new Dictionary<string, object?>
{
["lang"] = new List<object?> { "en", "de" },
});
Assert.Equal(new[] { "en", "de" }, cfg.GetProperty("lang").EnumerateArray().Select(e => e.GetString()).ToArray());
}
[Fact]
public void SeparatorAndPipeline_AreConfigurable()
{
var cfg = EmitConfig(new Dictionary<string, object?>
{
["separator"] = "[\\s]+",
["pipeline"] = new List<object?> { "trimmer" },
});
Assert.Equal("[\\s]+", cfg.GetProperty("separator").GetString());
Assert.Equal(new[] { "trimmer" }, cfg.GetProperty("pipeline").EnumerateArray().Select(e => e.GetString()).ToArray());
}
}