diff --git a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs index d86bae6..e2daa83 100644 --- a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs +++ b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs @@ -29,6 +29,8 @@ public class ProductService : IProductService public async IAsyncEnumerable SearchProductAsync(string wishlistId, MessageCreateDto message, CancellationToken cancellationToken) { + string firstMessageForUser = "What are you looking for?"; + string promptForGpt = "You are a Shopping Assistant that helps people find product recommendations. Ask user additional questions if more context needed." + "\nYou must return data with one of the prefixes:" + @@ -56,7 +58,7 @@ public class ProductService : IProductService new OpenAiMessage() { Role = OpenAiRole.System.ToString().ToLower(), - Content = "What are you looking for?" + Content = firstMessageForUser } }, Stream = true @@ -64,13 +66,13 @@ public class ProductService : IProductService _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto() { - Text = "What are you looking for?", + Text = firstMessageForUser, }, cancellationToken); yield return new ServerSentEvent { Event = SearchEventType.Message, - Data = "What are you looking for?" + Data = firstMessageForUser }; } @@ -177,6 +179,7 @@ public class ProductService : IProductService }; productBuffer.Name = string.Empty; + //a complete description of the entity when the Amazon API is connected await _wishlistsService.AddProductToPersonalWishlistAsync(wishlistId, new ProductCreateDto() { Url = "", diff --git a/ShoppingAssistantApi.Tests/Tests/ProductsTests.cs b/ShoppingAssistantApi.Tests/Tests/ProductsTests.cs index bb3119b..a017f06 100644 --- a/ShoppingAssistantApi.Tests/Tests/ProductsTests.cs +++ b/ShoppingAssistantApi.Tests/Tests/ProductsTests.cs @@ -31,4 +31,40 @@ public class ProductsTests : TestsBase Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(responseContent); } + + [Fact] + public async Task StreamDataToClientFirstly_ReturnsExpectedResponse() + { + await LoginAsync("wishlists@gmail.com", "Yuiop12345"); + // Arrange + var wishlistId = "ab7c8c2d9edf39abcd1ef9ab"; + var message = new MessageCreateDto { Text = "I want new powerful laptop" }; + + // Act + var response = await _httpClient.PostAsJsonAsync($"http://127.0.0.1:5183/api/ProductsSearch/search/{wishlistId}", message); + var responseContent = await response.Content.ReadAsStringAsync(); + var sseEvents = responseContent.Split("\n\n", StringSplitOptions.RemoveEmptyEntries); + bool foundMessageEvent = false; + + // Assert + foreach (var sseEvent in sseEvents) + { + var sseParts = sseEvent.Split('\n'); + if (sseParts.Length >= 2) + { + var eventName = sseParts[0]; + var eventData = sseParts[1].Substring("data: ".Length); + if (eventName == "event: Message") + { + foundMessageEvent = true; + Assert.Equal("\"What are you looking for?\"", eventData); + break; + } + } + } + + Assert.True(foundMessageEvent, "Message event not found"); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(responseContent); + } } \ No newline at end of file