Skip to content

Commit

Permalink
Merge pull request #62 from mule/pr-56-collapsible-session-groups
Browse files Browse the repository at this point in the history
Pr 56 collapsible session groups
  • Loading branch information
mule authored Nov 4, 2023
2 parents 41890a3 + d1ec285 commit e86c2e2
Show file tree
Hide file tree
Showing 20 changed files with 541 additions and 44 deletions.
6 changes: 6 additions & 0 deletions Abotti.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorComponents", "BlazorC
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorComponentTests", "BlazorComponentTests\BlazorComponentTests.csproj", "{47C2433D-4D13-424D-807F-A7922AB25860}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorComponents.Contracts", "BlazorComponents.Contracts\BlazorComponents.Contracts.csproj", "{66F93E90-3D87-4BA5-B0C7-5FCA9EECCC63}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -72,5 +74,9 @@ Global
{47C2433D-4D13-424D-807F-A7922AB25860}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47C2433D-4D13-424D-807F-A7922AB25860}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47C2433D-4D13-424D-807F-A7922AB25860}.Release|Any CPU.Build.0 = Release|Any CPU
{66F93E90-3D87-4BA5-B0C7-5FCA9EECCC63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66F93E90-3D87-4BA5-B0C7-5FCA9EECCC63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66F93E90-3D87-4BA5-B0C7-5FCA9EECCC63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66F93E90-3D87-4BA5-B0C7-5FCA9EECCC63}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 2 additions & 0 deletions BlazorApp/BlazorApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<ProjectReference Include="..\AbottiCore\AbottiCore.csproj"/>
<ProjectReference Include="..\BlazorComponents.Contracts\BlazorComponents.Contracts.csproj"/>
<ProjectReference Include="..\BlazorComponents\BlazorComponents.csproj"/>
<ProjectReference Include="..\DataAccessLayer\DataAccessLayer.csproj"/>
<ProjectReference Include="..\ServiceAccessLayer\ServiceAccessLayer.csproj"/>
Expand All @@ -20,6 +21,7 @@
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2"/>
<PackageReference Include="Azure.Identity" Version="1.10.1"/>
<PackageReference Include="Blazored.Toast" Version="4.1.0"/>
<PackageReference Include="FluentDateTime" Version="2.1.0"/>
<PackageReference Include="Markdig" Version="0.33.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.11"/>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.11"/>
Expand Down
28 changes: 18 additions & 10 deletions BlazorApp/Pages/Chat.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
@using Abotti.Core.QueryResults
@using Abotti.Core.Repositories
@using Abotti.ServiceAccessLayer.AiServices
@using BlazorComponentBus
@using Blazored.Toast.Services
@using Abotti.BlazorComponents.ViewModels
@using Abotti.BlazorComponents.Contracts
@using Abotti.BlazorComponents
@inject IJSRuntime JS
@inject IOpenAiClient OpenAiClient
Expand All @@ -15,14 +18,15 @@
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IUserRepository UserRepository
@inject IChatSessionRepository ChatSessionRepository
@inject ComponentBus Bus


@inject IStringLocalizer<Chat> Loc


<div class="row">
<div class="col-md-3">
<ChatSessionsList OnDeleteSession="DeleteSession" SessionTopics="@_sessionTopics"/>
<ChatSessionsList ChatSessionsListVm="@_chatSessionsListVm"/>
</div>
<div class="col-md-9">
<div class="row">
Expand Down Expand Up @@ -93,8 +97,7 @@
bool _waitingForResponse;
string _currentPrompt = "";
ChatSession? _currentSession;

TopicQueryResult[] _sessionTopics = Array.Empty<TopicQueryResult>();
ChatSessionsListVm _chatSessionsListVm = new();

protected override async Task OnAfterRenderAsync(bool firstRender)
{
Expand All @@ -111,14 +114,13 @@
var authState = AuthenticationStateProvider.GetAuthenticationStateAsync();
var principal = authState.Result.User;


var queryResult = await
UserRepository.GetByNameAsync(principal.Identity.Name);

if (queryResult.Result != null)
{
var topicQueryResult = await ChatSessionRepository.GetTopicByUserIdsAsync(queryResult.Result.Id);
_sessionTopics = topicQueryResult.Result.OrderByDescending(topic => topic.Created).ToArray();
_chatSessionsListVm = new ChatSessionsListVm(topicQueryResult.Result);
}

if (SessionId.HasValue)
Expand All @@ -133,6 +135,8 @@
{
SessionId = Guid.NewGuid();
}

Bus.Subscribe<ChatSessionDeleteRequest>(ChatSessionDeleteRequestHandler);
}

protected override async Task OnParametersSetAsync()
Expand Down Expand Up @@ -165,7 +169,7 @@
{
topics = await GetSessionTopics(_currentSession.UserId);
}
_sessionTopics = topics.OrderByDescending(topic => topic.Created).ToArray();
_chatSessionsListVm = new ChatSessionsListVm(topics);


await base.OnParametersSetAsync();
Expand Down Expand Up @@ -200,7 +204,7 @@
_currentSession.Topic = topicResponse.Content;

var updatedTopics = await GetSessionTopics(_currentSession.UserId);
_sessionTopics = updatedTopics;
_chatSessionsListVm = new ChatSessionsListVm(updatedTopics);
}
else
{
Expand Down Expand Up @@ -285,7 +289,8 @@
else
{
var updatedTopics = await GetSessionTopics(_currentSession.UserId);
_sessionTopics = updatedTopics;
_chatSessionsListVm = new ChatSessionsListVm(updatedTopics);
InvokeAsync(StateHasChanged);
}
}

Expand Down Expand Up @@ -316,7 +321,10 @@
NavigationManager.NavigateTo($"/Chat/{sessionId}");
}



private async Task ChatSessionDeleteRequestHandler(MessageArgs args, CancellationToken ct)
{
var request = args.GetMessage<ChatSessionDeleteRequest>();
await DeleteSession(request.SessionId);
}

}
4 changes: 2 additions & 2 deletions BlazorApp/Pages/UserProfile.razor
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
public bool IsCurrentUsersProfile { get; set; }

public CultureInfo[] SupportedCultures { get; set; } = {
new CultureInfo("en-US"),
new CultureInfo("fi-Fi")
new("en-US"),
new("fi-Fi")
};

protected override async Task OnInitializedAsync()
Expand Down
2 changes: 2 additions & 0 deletions BlazorApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Abotti.ServiceAccessLayer.AiServices;
using Azure.Identity;
using Azure.Storage.Blobs;
using BlazorComponentBus;
using Blazored.Toast;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -94,6 +95,7 @@
provider.GetService<IOpenAIService>(),
provider.GetService<ILogger<OpenAiClient>>(), Models.Gpt_4));
builder.Services.AddBlazoredToast();
builder.Services.AddScoped<ComponentBus>();
builder.Services.AddControllersWithViews(options => { }).AddMicrosoftIdentityUI();

builder.Services.AddAuthorization(options =>
Expand Down
1 change: 1 addition & 0 deletions BlazorComponentTests/BlazorComponentTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.0"/>
<PackageReference Include="bunit" Version="1.22.19"/>
<PackageReference Include="FluentAssertions" Version="6.11.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
Expand Down
76 changes: 76 additions & 0 deletions BlazorComponentTests/ChatSessionListRowTests.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
@using FluentAssertions
@using BlazorComponentBus
@using Abotti.BlazorComponents
@using Abotti.BlazorComponents.Contracts
@inherits TestContext

@code {

[Fact]
public void ChatSessionListRow_should_render_successfully()
{
// Arrange
Services.AddScoped<ComponentBus>();
// Act
var action = () => Render(@<ChatSessionListRow/>);

// Assert
action.Should().NotThrow();
}

[Fact]
public void ChatSessionListRow_should_show_chat_topic()
{
// Arrange
var testTopic = "Test topic";
Services.AddScoped<ComponentBus>();

// Act
var cut = Render(@<ChatSessionListRow Topic="@testTopic"/>);

// Assert
cut.Markup.Should().Contain(testTopic);
}

[Fact]
public void ChatSessionListRow_should_show_chat_session_id()
{
// Arrange
var testSessionId = Guid.NewGuid();
Services.AddScoped<ComponentBus>();

// Act
var cut = Render(@<ChatSessionListRow SessionId="@testSessionId"/>);

// Assert
cut.Markup.Should().Contain(testSessionId.ToString());
}


[Fact]
public void ChatSessionListRow_should_publish_delete_event()
{
// Arrange
var testSessionId = Guid.NewGuid();
var eventFired = false;
Services.AddScoped<ComponentBus>();

var bus = Services.GetService(typeof(ComponentBus)) as ComponentBus;

bus.Subscribe<ChatSessionDeleteRequest>(async (args, ct) =>
{
eventFired = true;
var msg = args.GetMessage<ChatSessionDeleteRequest>();
msg.SessionId.Should().Be(testSessionId);
});

// Act
var cut = Render(@<ChatSessionListRow SessionId="@testSessionId"/>);

var deleteButton = cut.Find("button");
deleteButton.Click();
eventFired.Should().BeTrue();
}

}
59 changes: 59 additions & 0 deletions BlazorComponentTests/ChatSessionsGroupTests.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@using FluentAssertions
@using AutoFixture
@using BlazorComponentBus
@using Abotti.BlazorComponents
@using Abotti.Core.QueryResults
@inherits TestContext

@code {

[Fact]
public void ChatSessionsGroup_should_render_succesfully()
{
//Arrange
Services.AddScoped<ComponentBus>();

//Act
var act = () => Render(@<ChatSessionsGroup GroupName="Test group"/>);

//Assert
act.Should().NotThrow();
}

[Fact]
public void ChatSessionsGroup_should_render_group_name()
{
//Arrange
Services.AddScoped<ComponentBus>();

//Act
var cut = Render(@<ChatSessionsGroup GroupName="Test group"/>);

//Assert
cut.Markup.Should().Contain("Test group");
}

[Fact]
public void ChatSessionsGroup_should_render_given_topics()
{
//Arrange
Services.AddScoped<ComponentBus>();
var fixture = new Fixture();
var testTopics = fixture.CreateMany<TopicQueryResult>().ToArray();

//Act
var cut = Render(@<ChatSessionsGroup GroupName="Test group" SessionTopics="@testTopics" IsCollapsed="false"/>);

//Assert
foreach (var testTopic in testTopics)
{
//Markup should contain all topics
cut.Markup.Should().Contain(testTopic.Topic);
}
}


}
57 changes: 57 additions & 0 deletions BlazorComponentTests/ChatSessionsListTests.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@using FluentAssertions
@using BlazorComponentBus
@using Abotti.BlazorComponents
@using Abotti.BlazorComponents.ViewModels
@using Abotti.BlazorComponentTests.TestData
@inherits TestContext


@code {

private bool _commonSetupExecuted;

public void SetUp()
{
if (!_commonSetupExecuted)
{
Services.AddLocalization();

_commonSetupExecuted = true;
}
}

[Fact]
public void ChatSessionsList_should_render_successfully()
{
//Arrange
Services.AddScoped<ComponentBus>();
SetUp();


//Act
var act = () => Render(@<ChatSessionsList/>);

//Assert
act.Should().NotThrow();
}

[Fact]
public void ChatSessionsList_should_render_groups_correctly()
{
//Arrange
Services.AddScoped<ComponentBus>();
SetUp();
var vm = new ChatSessionsListVm(ChatSessionsListData.Topics);


//Act
var cut = Render(@<ChatSessionsList ChatSessionsListVm="@vm" ReferenceDate="@ChatSessionsListData.ReferenceDate"/>);

//Assert
//find all groups
var groups = cut.FindAll(".chat-sessions-group");
groups.Should().HaveCount(5);
}

}
Loading

0 comments on commit e86c2e2

Please sign in to comment.