Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions AzureSearchEmulator.IntegrationTests/DashboardIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ public async Task Root_ServesTheDashboard()
Assert.Contains("Running normally", html);
}

/// <remarks>
/// The search page renders only once its circuit connects (issue #92), so the static response
/// carries the layout rather than results; what this guards is the route existing alongside the
/// API's own routes and the navigation reaching it.
/// </remarks>
[Fact]
public async Task Search_ServesThePage()
{
await factory.WaitUntilServingAsync();
using var client = factory.CreateHttpClient();

var response = await client.GetAsync("/search", TestContext.Current.CancellationToken);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/html", response.Content.Headers.ContentType?.MediaType);

var html = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);

Assert.Contains("href=\"search\" class=\"topnav-link active\"", html);
}

[Fact]
public async Task Dashboard_StylesheetIsServed()
{
Expand All @@ -50,6 +71,23 @@ public async Task Dashboard_StylesheetIsServed()
Assert.Equal("text/css", response.Content.Headers.ContentType?.MediaType);
}

/// <remarks>
/// Without Blazor's script the pages still prerender, so the dashboard looks fine, but nothing
/// interactive ever starts and the search page, which is not prerendered, stays blank. The
/// script comes from a package the SDK can leave out of a publish while <c>dotnet run</c> keeps
/// working, so it is checked here against the built image (issue #92).
/// </remarks>
[Fact]
public async Task Dashboard_BlazorScriptIsServed()
{
await factory.WaitUntilServingAsync();
using var client = factory.CreateHttpClient();

var response = await client.GetAsync("/_framework/blazor.web.js", TestContext.Current.CancellationToken);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

/// <remarks>
/// The container has a writable indexes volume and no malformed definitions, so a healthy
/// answer here is the real thing rather than a check that cannot fail — the unit tests cover
Expand Down
28 changes: 28 additions & 0 deletions AzureSearchEmulator.IntegrationTests/EmulatorIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ public async Task CreateIndex_ShouldSucceed()
await indexClient.DeleteIndexAsync(indexName, TestContext.Current.CancellationToken);
}

/// <remarks>
/// The SDK lists names with <c>$select=name</c>, which the listing endpoint used to throw on
/// (found while building the dashboard's search page, issue #92).
/// </remarks>
[Fact]
public async Task GetIndexNames_ShouldListCreatedIndex()
{
const string indexName = "test-get-index-names";
var indexClient = factory.CreateSearchIndexClient();
await CreateIndexAsync(indexClient, indexName);

try
{
var names = new List<string>();

await foreach (var name in indexClient.GetIndexNamesAsync(TestContext.Current.CancellationToken))
{
names.Add(name);
}

Assert.Contains(indexName, names);
}
finally
{
await indexClient.DeleteIndexAsync(indexName, TestContext.Current.CancellationToken);
}
}

[Fact]
public async Task UploadDocuments_ShouldSucceed()
{
Expand Down
157 changes: 157 additions & 0 deletions AzureSearchEmulator.UnitTests/SearchPageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using Azure.Search.Documents.Models;
using AzureSearchEmulator.Dashboard;
using Xunit;

namespace AzureSearchEmulator.UnitTests;

/// <summary>
/// Tests for the pieces behind the dashboard's search page (issue #92) that do not need a running
/// emulator: how its inputs become SDK options, how it reaches itself, and how it renders JSON and
/// highlights.
/// </summary>
public class SearchPageTests
{
[Theory]
[InlineData(new[] { "http://localhost:5123" }, "http://localhost:5123/")]
[InlineData(new[] { "https://+:8443" }, "https://localhost:8443/")]
[InlineData(new[] { "http://*:80" }, "http://localhost/")]
[InlineData(new[] { "http://0.0.0.0:8080" }, "http://127.0.0.1:8080/")]
[InlineData(new[] { "http://[::]:8080" }, "http://localhost:8080/")]
[InlineData(new[] { "https://localhost:5001", "http://localhost:5000" }, "http://localhost:5000/")]
public void LoopbackEndpoint_ConnectsToABinding(string[] addresses, string expected)
{
Assert.Equal(new Uri(expected), LoopbackEndpoint.Resolve(addresses));
}

[Fact]
public void LoopbackEndpoint_WithNoUsableBinding_IsNull()
{
Assert.Null(LoopbackEndpoint.Resolve([]));
Assert.Null(LoopbackEndpoint.Resolve(["unix:/tmp/emulator.sock"]));
}

[Fact]
public void SplitList_KeepsCommasInsideFunctionsAndQuotes()
{
var items = SearchQueryForm.SplitList("geo.distance(location, geography'POINT(-122 47)') asc, rating desc,, name ");

Assert.Equal(["geo.distance(location, geography'POINT(-122 47)') asc", "rating desc", "name"], items);
}

[Fact]
public void SplitFacets_KeepsParametersWithTheirField()
{
var facets = SearchQueryForm.SplitFacets("category, rating,interval:1, tags,count:5,sort:value");

Assert.Equal(["category", "rating,interval:1", "tags,count:5,sort:value"], facets);
}

[Fact]
public void ToSearchOptions_OmitsBlankInputs()
{
var options = new SearchQueryForm { Filter = " " }.ToSearchOptions();

Assert.Null(options.Filter);
Assert.Null(options.Skip);
Assert.Null(options.SemanticSearch);
Assert.Empty(options.OrderBy);
Assert.Empty(options.Facets);
Assert.Equal(SearchQueryForm.DefaultTop, options.Size);
Assert.True(options.IncludeTotalCount);
}

[Fact]
public void ToSearchOptions_CarriesEveryInput()
{
var form = new SearchQueryForm
{
QueryType = "semantic",
SearchMode = "all",
Filter = "rating ge 4",
OrderBy = "rating desc, name",
Select = "id, name",
SearchFields = "name",
Facets = "category",
Highlight = "description",
ScoringProfile = "boost",
SemanticConfiguration = "default",
Top = 5,
Skip = 10,
IncludeTotalCount = false
};

var options = form.ToSearchOptions();

Assert.Equal(SearchQueryType.Semantic, options.QueryType);
Assert.Equal(SearchMode.All, options.SearchMode);
Assert.Equal("rating ge 4", options.Filter);
Assert.Equal(["rating desc", "name"], options.OrderBy);
Assert.Equal(["id", "name"], options.Select);
Assert.Equal(["name"], options.SearchFields);
Assert.Equal(["category"], options.Facets);
Assert.Equal(["description"], options.HighlightFields);
Assert.Equal("boost", options.ScoringProfile);
Assert.Equal("default", options.SemanticSearch.SemanticConfigurationName);
Assert.Equal(5, options.Size);
Assert.Equal(10, options.Skip);
Assert.False(options.IncludeTotalCount);
}

[Fact]
public void JsonHighlighter_ClassifiesTokens()
{
var tokens = JsonHighlighter.Tokenize("""{"name": "Fancy \"Inn\"", "rating": -4.5e1, "open": true, "tags": null}""");

Assert.Contains(new JsonToken(JsonTokenKind.Key, "\"name\""), tokens);
Assert.Contains(new JsonToken(JsonTokenKind.String, "\"Fancy \\\"Inn\\\"\""), tokens);
Assert.Contains(new JsonToken(JsonTokenKind.Number, "-4.5e1"), tokens);
Assert.Contains(new JsonToken(JsonTokenKind.Boolean, "true"), tokens);
Assert.Contains(new JsonToken(JsonTokenKind.Null, "null"), tokens);
Assert.Contains(new JsonToken(JsonTokenKind.Punctuation, "{"), tokens);
}

[Theory]
[InlineData("""{"a": [1, 2, {"b": "c"}]}""")]
[InlineData("""{"unterminated": "oops""")]
[InlineData("not json at all")]
public void JsonHighlighter_TokensReproduceTheInput(string json)
{
var tokens = JsonHighlighter.Tokenize(json);

Assert.Equal(json, string.Concat(tokens.Select(i => i.Text)));
}

[Fact]
public void JsonHighlighter_Format_IndentsWithoutEscapingMarkup()
{
var formatted = JsonHighlighter.Format("""{"text":"a <em>b</em>"}""");

Assert.Equal("{\n \"text\": \"a <em>b</em>\"\n}", formatted.ReplaceLineEndings("\n"));
}

[Fact]
public void JsonHighlighter_Format_LeavesInvalidJsonAlone()
{
Assert.Equal("<html>oops</html>", JsonHighlighter.Format("<html>oops</html>"));
}

[Fact]
public void HighlightMarkup_EncodesDocumentTextButKeepsEmphasis()
{
var markup = HighlightMarkup.Render("<script>x</script> the <em>hotel</em> & spa");

Assert.Equal("&lt;script&gt;x&lt;/script&gt; the <mark>hotel</mark> &amp; spa", markup.Value);
}

[Fact]
public void DocumentValueFormatter_SummarisesVectors()
{
var vector = Enumerable.Range(0, 1536).Select(i => (object)(double)i).ToArray();

Assert.Equal("[1536 numbers]", DocumentValueFormatter.Format(vector));
Assert.Equal("[1,2,3]", DocumentValueFormatter.Format(new object[] { 1, 2, 3 }));
Assert.Equal("[\"a\",\"b\"]", DocumentValueFormatter.Format(new object[] { "a", "b" }));
Assert.Equal("4.5", DocumentValueFormatter.Format(4.5));
Assert.Equal("null", DocumentValueFormatter.Format(null));
}
}
6 changes: 6 additions & 0 deletions AzureSearchEmulator/AzureSearchEmulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@
<Authors>Paul Irwin</Authors>
<PackageIcon>logo.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<!-- Blazor's own script (blazor.web.js) ships in a package the SDK only adds when it sees
.razor files at restore time. The Dockerfile restores from the project file alone, before
the sources are copied in, so the image was published without the script: the dashboard
prerendered but never went interactive, and the search page stayed blank (issue #92). -->
<RequiresAspNetWebAssets>true</RequiresAspNetWebAssets>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Search.Documents" Version="12.0.0" />
<PackageReference Include="Lucene.Net" Version="4.8.0-beta00017" />
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00017" />
<PackageReference Include="Lucene.Net.Highlighter" Version="4.8.0-beta00017" />
Expand Down
4 changes: 4 additions & 0 deletions AzureSearchEmulator/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<small>Local development service</small>
</span>
</a>
<nav class="topnav" aria-label="Dashboard">
<NavLink class="topnav-link" href="" Match="NavLinkMatch.All">Status</NavLink>
<NavLink class="topnav-link" href="search">Search</NavLink>
</nav>
</header>

<main class="content">
Expand Down
Loading
Loading