added some fix for tests

This commit is contained in:
stasex 2023-10-24 23:24:23 +03:00
parent 3372a0910b
commit f5d9c3e80e
3 changed files with 76 additions and 78 deletions

View File

@ -1,4 +1,5 @@
using ShoppingAssistantApi.Application.IServices; using System.Diagnostics;
using ShoppingAssistantApi.Application.IServices;
using ShoppingAssistantApi.Application.Models.CreateDtos; using ShoppingAssistantApi.Application.Models.CreateDtos;
using ShoppingAssistantApi.Application.Models.Dtos; using ShoppingAssistantApi.Application.Models.Dtos;
using ShoppingAssistantApi.Application.Models.OpenAi; using ShoppingAssistantApi.Application.Models.OpenAi;
@ -24,7 +25,6 @@ public class ProductService : IProductService
public async IAsyncEnumerable<ServerSentEvent> SearchProductAsync(string wishlistId, MessageCreateDto message, CancellationToken cancellationToken) public async IAsyncEnumerable<ServerSentEvent> SearchProductAsync(string wishlistId, MessageCreateDto message, CancellationToken cancellationToken)
{ {
bool checker = false;
var isFirstMessage = _wishlistsService var isFirstMessage = _wishlistsService
.GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 1, cancellationToken).Result; .GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 1, cancellationToken).Result;
@ -78,11 +78,9 @@ public class ProductService : IProductService
Event = SearchEventType.Suggestion, Event = SearchEventType.Suggestion,
Data = "Laptop" Data = "Laptop"
}; };
checker = true;
} }
if(isFirstMessage!=null && checker==false) if(isFirstMessage!=null)
{ {
var previousMessages = _wishlistsService var previousMessages = _wishlistsService
.GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 1, cancellationToken).Result.Items.ToList(); .GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 1, cancellationToken).Result.Items.ToList();
@ -105,91 +103,91 @@ public class ProductService : IProductService
}; };
var suggestionBuffer = new Suggestion(); var suggestionBuffer = new Suggestion();
var messageBuffer = new MessagePart(); var messageBuffer = new MessagePart();
var productBuffer = new ProductName(); var productBuffer = new ProductName();
var currentDataType = SearchEventType.Wishlist; var currentDataType = SearchEventType.Wishlist;
var dataTypeHolder = string.Empty; var dataTypeHolder = string.Empty;
await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken)) await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken))
{
if (data.Contains("["))
{ {
if (dataTypeHolder=="[Message]" && messageBuffer.Text!=null) if (data.Contains("["))
{ {
_wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto() if (dataTypeHolder=="[Message]" && messageBuffer.Text!=null)
{ {
Text = messageBuffer.Text, _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto()
}, cancellationToken); {
} Text = messageBuffer.Text,
if (dataTypeHolder=="[Products]" && productBuffer.Name!=null) }, cancellationToken);
{ }
_wishlistsService.AddProductToPersonalWishlistAsync(wishlistId, new ProductCreateDto() if (dataTypeHolder=="[Products]" && productBuffer.Name!=null)
{ {
Url = "", _wishlistsService.AddProductToPersonalWishlistAsync(wishlistId, new ProductCreateDto()
Name = productBuffer.Name, {
Rating = 0, Url = "",
Description = "", Name = productBuffer.Name,
ImagesUrls = new []{"", ""}, Rating = 0,
WasOpened = false Description = "",
}, cancellationToken); ImagesUrls = new []{"", ""},
WasOpened = false
}, cancellationToken);
}
dataTypeHolder = string.Empty;
dataTypeHolder += data;
} }
dataTypeHolder = string.Empty;
dataTypeHolder += data;
}
else if (data.Contains("]")) else if (data.Contains("]"))
{
dataTypeHolder += data;
currentDataType = DetermineDataType(dataTypeHolder);
}
else if (dataTypeHolder=="[" && !data.Contains("["))
{
dataTypeHolder += data;
}
else
{
switch (currentDataType)
{ {
case SearchEventType.Message: dataTypeHolder += data;
yield return new ServerSentEvent currentDataType = DetermineDataType(dataTypeHolder);
{ }
Event = SearchEventType.Message,
Data = data
};
messageBuffer.Text += data;
break;
case SearchEventType.Suggestion: else if (dataTypeHolder=="[" && !data.Contains("["))
suggestionBuffer.Text += data; {
if (data.Contains(";")) dataTypeHolder += data;
{ }
else
{
switch (currentDataType)
{
case SearchEventType.Message:
yield return new ServerSentEvent yield return new ServerSentEvent
{ {
Event = SearchEventType.Suggestion, Event = SearchEventType.Message,
Data = suggestionBuffer.Text Data = data
}; };
suggestionBuffer.Text = string.Empty; messageBuffer.Text += data;
} break;
break;
case SearchEventType.Product: case SearchEventType.Suggestion:
productBuffer.Name += data; suggestionBuffer.Text += data;
if (data.Contains(";")) if (data.Contains(";"))
{
yield return new ServerSentEvent
{ {
Event = SearchEventType.Product, yield return new ServerSentEvent
Data = productBuffer.Name {
}; Event = SearchEventType.Suggestion,
productBuffer.Name = string.Empty; Data = suggestionBuffer.Text
} };
break; suggestionBuffer.Text = string.Empty;
}
break;
case SearchEventType.Product:
productBuffer.Name += data;
if (data.Contains(";"))
{
yield return new ServerSentEvent
{
Event = SearchEventType.Product,
Data = productBuffer.Name
};
productBuffer.Name = string.Empty;
}
break;
}
} }
} }
} }
}
} }
private SearchEventType DetermineDataType(string dataTypeHolder) private SearchEventType DetermineDataType(string dataTypeHolder)
@ -215,4 +213,5 @@ public class ProductService : IProductService
return SearchEventType.Wishlist; return SearchEventType.Wishlist;
} }
} }
} }

View File

@ -19,15 +19,13 @@ public class ProductsTests : TestsBase
public async Task StreamDataToClient_ReturnsExpectedResponse() public async Task StreamDataToClient_ReturnsExpectedResponse()
{ {
// Arrange // Arrange
var wishlistId = "your_wishlist_id"; var wishlistId = "ab79cde6f69abcd3efab65cd";
var message = new MessageCreateDto { Text = "Your message text" }; var message = new MessageCreateDto { Text = "Your message text" };
// Act // Act
var response = await _httpClient.PostAsJsonAsync($"http://localhost:5183/api/products/search/{"ab79cde6f69abcd3efab65cd"}", message); var response = await _httpClient.PostAsJsonAsync($"http://127.0.0.1:5183//api/products/search/{wishlistId}", message);
// Assert // Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
// Додайте додаткові перевірки на відповідь, якщо необхідно
} }
} }

View File

@ -168,5 +168,6 @@ public class ProductTests
// Assert // Assert
Assert.NotNull(actualSseEvents); Assert.NotNull(actualSseEvents);
Assert.Equal(3, actualSseEvents.Count);
} }
} }