Skip to content

Commit

Permalink
action output message section tests
Browse files Browse the repository at this point in the history
  • Loading branch information
singhk97 committed Oct 1, 2024
1 parent 7818dee commit 596c066
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Microsoft.Teams.AI.AI.Models;
using Microsoft.Teams.AI.AI.Prompts.Sections;
using Microsoft.Teams.AI.State;
using Microsoft.Teams.AI.Tests.TestUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Teams.AI.Tests.AITests.PromptsTests.SectionsTests
{
public class ActionOutputMessageSectionTests
{
[Fact]
public async Task Test_RenderAsMessages_NoHistory_ReturnsEmptyList()
{
// Arrange
var historyVariable = "temp.history";
var turnContext = TurnStateConfig.CreateConfiguredTurnContext();
var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext);
turnState.SetValue(historyVariable, new List<ChatMessage>() { });
turnState.SetValue(TempState.ActionOutputsKey, new Dictionary<string, string>());
var section = new ActionOutputMessageSection(historyVariable);

// Act
var sections = await section.RenderAsMessagesAsync(turnContext, turnState, null!, null!, 0);

// Assert
Assert.Empty(sections.Output);
}

[Fact]
public async Task Test_RenderAsMessages_HistoryWithoutActionCalls_ReturnsEmptyList()
{
// Arrange
var historyVariable = "temp.history";
var turnContext = TurnStateConfig.CreateConfiguredTurnContext();
var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext);
turnState.SetValue(historyVariable, new List<ChatMessage>() { new ChatMessage(ChatRole.Assistant) });
turnState.SetValue(TempState.ActionOutputsKey, new Dictionary<string, string>());
var section = new ActionOutputMessageSection(historyVariable);

// Act
var sections = await section.RenderAsMessagesAsync(turnContext, turnState, null!, null!, 0);

// Assert
Assert.Empty(sections.Output);
}

[Fact]
public async Task Test_RenderAsMessages_WithActionCalls_AddsToolMessage()
{
// Arrange
var historyVariable = "temp.history";
var turnContext = TurnStateConfig.CreateConfiguredTurnContext();
var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext);
turnState.SetValue(historyVariable, new List<ChatMessage>() {
new ChatMessage(ChatRole.Assistant) {
ActionCalls = new List<ActionCall> { new ActionCall("testId", new ActionFunction("testName", "{}")) }
}
});
turnState.SetValue(TempState.ActionOutputsKey, new Dictionary<string, string>()
{
{ "testId", "testOutput" }
});
var section = new ActionOutputMessageSection(historyVariable);

// Act
var sections = await section.RenderAsMessagesAsync(turnContext, turnState, null!, null!, 0);

// Assert
Assert.Single(sections.Output);
Assert.Equal("testOutput", sections.Output[0].Content);
}

[Fact]
public async Task Test_RenderAsMessages_WithInvalidActionCalls_AddsToolMessage_WithEmptyOutput()
{
// Arrange
var historyVariable = "temp.history";
var turnContext = TurnStateConfig.CreateConfiguredTurnContext();
var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext);
turnState.SetValue(historyVariable, new List<ChatMessage>() {
new ChatMessage(ChatRole.Assistant) {
ActionCalls = new List<ActionCall> { new ActionCall("testId", new ActionFunction("testName", "{}")) }
}
});
turnState.SetValue(TempState.ActionOutputsKey, new Dictionary<string, string>()
{
{ "InvalidTestId", "testOutput" }
});
var section = new ActionOutputMessageSection(historyVariable);

// Act
var sections = await section.RenderAsMessagesAsync(turnContext, turnState, null!, null!, 0);

// Assert
Assert.Empty(sections.Output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override Task<RenderedPromptSection<List<ChatMessage>>> RenderAsMessagesA
List<ChatMessage> history = memory.GetValue(_HistoryVariable) as List<ChatMessage> ?? new();
List<ChatMessage> messages = new();

if (history.Count > 1)
if (history.Count >= 1)
{
Dictionary<string, string> actionOutputs = memory.GetValue(_OutputVariable) as Dictionary<string, string> ?? new();
List<ActionCall> actionCalls = history.Last().ActionCalls ?? new();
Expand Down

0 comments on commit 596c066

Please sign in to comment.