added new test

This commit is contained in:
stasex 2023-10-25 21:36:30 +03:00
parent 40f294f61b
commit adf956b44c
2 changed files with 42 additions and 3 deletions

View File

@ -29,6 +29,8 @@ public class ProductService : IProductService
public async IAsyncEnumerable<ServerSentEvent> 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 = "",

View File

@ -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);
}
}