|
| 1 | +using EssentialCSharp.Chat.Common.Services; |
| 2 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 5 | + |
| 6 | +namespace EssentialCSharp.Web.Tests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// The chat widget reads <c>window.CHAT_SUPPORTS_CONTEXTUAL_SEARCH</c> to decide whether to request |
| 10 | +/// contextual search; the API rejects that request for backends that cannot perform it. |
| 11 | +/// </summary> |
| 12 | +public class ChatContextualSearchCapabilityTests : IntegrationTestBase |
| 13 | +{ |
| 14 | + [Test] |
| 15 | + [Arguments(true)] |
| 16 | + [Arguments(false)] |
| 17 | + public async Task Layout_RendersChatBackendContextualSearchSupport(bool supportsContextualSearch) |
| 18 | + { |
| 19 | + await using WebApplicationFactory<Program> factory = Factory.Inner.WithWebHostBuilder(builder => |
| 20 | + builder.ConfigureServices(services => |
| 21 | + { |
| 22 | + services.RemoveAll<ChatBackendCapabilities>(); |
| 23 | + services.AddSingleton(new ChatBackendCapabilities(supportsContextualSearch)); |
| 24 | + })); |
| 25 | + using HttpClient client = factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); |
| 26 | + |
| 27 | + using HttpResponseMessage response = await GetFollowingGetRedirectsAsync(client, "/home"); |
| 28 | + string body = await response.Content.ReadAsStringAsync(); |
| 29 | + |
| 30 | + await Assert.That(response.IsSuccessStatusCode).IsTrue(); |
| 31 | + await Assert.That(body).Contains( |
| 32 | + $"window.CHAT_SUPPORTS_CONTEXTUAL_SEARCH = {(supportsContextualSearch ? "true" : "false")};"); |
| 33 | + } |
| 34 | + |
| 35 | + [Test] |
| 36 | + public async Task Layout_WithDefaultTestBackend_DoesNotRequestContextualSearch() |
| 37 | + { |
| 38 | + // The test host has no AI configuration, so the Unavailable backend (no contextual search) is selected. |
| 39 | + using HttpClient client = CreateClientWithoutAutoRedirect(); |
| 40 | + |
| 41 | + using HttpResponseMessage response = await GetFollowingGetRedirectsAsync(client, "/home"); |
| 42 | + string body = await response.Content.ReadAsStringAsync(); |
| 43 | + |
| 44 | + await Assert.That(body).Contains("window.CHAT_SUPPORTS_CONTEXTUAL_SEARCH = false;"); |
| 45 | + } |
| 46 | +} |
0 commit comments