Skip to content

Commit

Permalink
[C#] feat: add SequenceAugmentation unit tests (#1148)
Browse files Browse the repository at this point in the history
## Linked issues

closes: #1117

## Details

Add unit tests to `SequenceAugmentation`

## Attestation Checklist

- [X] My code follows the style guidelines of this project

- I have checked for/fixed spelling, linting, and other errors
- I have commented my code for clarity
- I have made corresponding changes to the documentation (we use
[TypeDoc](https://typedoc.org/) to document our code)
- My changes generate no new warnings
- I have added tests that validates my changes, and provides sufficient
test coverage. I have tested with:
  - Local testing
  - E2E testing in Teams
- New and existing unit tests pass locally with my changes

### Additional information

> Feel free to add other relevant information below
  • Loading branch information
swatDong committed Jan 11, 2024
1 parent 699c2dc commit 071f35a
Showing 1 changed file with 226 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
using Json.Schema;
using Microsoft.Bot.Builder;
using Microsoft.Teams.AI.AI.Augmentations;
using Microsoft.Teams.AI.AI.Models;
using Microsoft.Teams.AI.AI.Planners;
using Microsoft.Teams.AI.AI.Prompts;
using Microsoft.Teams.AI.AI.Tokenizers;
using Microsoft.Teams.AI.State;
using Moq;

namespace Microsoft.Teams.AI.Tests.AITests.Augmentations
{
public class SequenceAugmentationTests
{
[Fact]
public void Test_CreatePromptSection_NotNull()
{
// Arrange
SequenceAugmentation augmentation = new(new());

// Act
var section = augmentation.CreatePromptSection();

// Assert
Assert.NotNull(section);
}

[Fact]
public async Task Test_CreatePlanFromResponseAsync_ValidPlan_ShouldSucceed()
{
// Arrange
Mock<ITurnContext> context = new();
MemoryFork memory = new();
SequenceAugmentation augmentation = new(new());
PromptResponse promptResponse = new()
{
Status = PromptResponseStatus.Success,
Message = new(ChatRole.Assistant)
{
Content = @"{
""type"": ""plan"",
""commands"": [
{
""type"": ""DO"",
""action"": ""test""
},
{
""type"": ""SAY"",
""response"": ""hello""
}
]
}"
}
};

// Act
var plan = await augmentation.CreatePlanFromResponseAsync(context.Object, memory, promptResponse);

// Assert
Assert.NotNull(plan);
Assert.Equal(2, plan.Commands.Count);
Assert.Equal("DO", plan.Commands[0].Type);
Assert.Equal("test", (plan.Commands[0] as PredictedDoCommand)?.Action);
Assert.Equal("SAY", plan.Commands[1].Type);
Assert.Equal("hello", (plan.Commands[1] as PredictedSayCommand)?.Response);
}

[Fact]
public async Task Test_CreatePlanFromResponseAsync_EmptyResponse_ReturnsNull()
{
// Arrange
Mock<ITurnContext> context = new();
MemoryFork memory = new();
SequenceAugmentation augmentation = new(new());
PromptResponse promptResponse = new()
{
Status = PromptResponseStatus.Success
};

// Act
var plan = await augmentation.CreatePlanFromResponseAsync(context.Object, memory, promptResponse);

// Assert
Assert.Null(plan);
}

[Fact]
public async Task Test_CreatePlanFromResponseAsync_InvalidContent_ReturnsNull()
{
// Arrange
Mock<ITurnContext> context = new();
MemoryFork memory = new();
SequenceAugmentation augmentation = new(new());
PromptResponse promptResponse = new()
{
Status = PromptResponseStatus.Success,
Message = new(ChatRole.Assistant)
{
Content = @"{ ""type"": ""invalid"" }"
}
};

// Act
var plan = await augmentation.CreatePlanFromResponseAsync(context.Object, memory, promptResponse);

// Assert
Assert.Null(plan);
}

[Fact]
public async Task Test_ValidateResponseAsync_ShouldSucceed()
{
// Arrange
Mock<ITurnContext> context = new();
MemoryFork memory = new();
GPTTokenizer tokenizer = new();
SequenceAugmentation augmentation = new(new()
{
new("test")
{
Description = "test action",
Parameters = new JsonSchemaBuilder()
.Type(SchemaValueType.Object)
.Properties(
(
"foo",
new JsonSchemaBuilder()
.Type(SchemaValueType.String)
)
)
.Required(new string[] { "foo" })
.Build()
}
});
PromptResponse promptResponse = new()
{
Status = PromptResponseStatus.Success,
Message = new(ChatRole.Assistant)
{
Content = @"{
""type"": ""plan"",
""commands"": [
{
""type"": ""DO"",
""action"": ""test"",
""parameters"": {
""foo"": ""bar""
}
},
{
""type"": ""SAY"",
""response"": ""hello""
}
]
}"
}
};

// Act
var res = await augmentation.ValidateResponseAsync(context.Object, memory, tokenizer, promptResponse, 0);

// Assert
Assert.True(res.Valid);
}

[Fact]
public async Task Test_ValidateResponseAsync_InvalidJson_ShouldFail()
{
// Arrange
Mock<ITurnContext> context = new();
MemoryFork memory = new();
GPTTokenizer tokenizer = new();
SequenceAugmentation augmentation = new(new());
PromptResponse promptResponse = new()
{
Status = PromptResponseStatus.Success,
Message = new(ChatRole.Assistant)
{
Content = @"{invalid-json,)}"
}
};

// Act
var res = await augmentation.ValidateResponseAsync(context.Object, memory, tokenizer, promptResponse, 0);

// Assert
Assert.False(res.Valid);
Assert.Equal("Return a JSON object that uses the SAY command to say what you're thinking.", res.Feedback);
Assert.Null(res.Value);
}

[Fact]
public async Task Test_ValidateResponseAsync_InvalidAction_ShouldFail()
{
// Arrange
Mock<ITurnContext> context = new();
MemoryFork memory = new();
GPTTokenizer tokenizer = new();
SequenceAugmentation augmentation = new(new());
PromptResponse promptResponse = new()
{
Status = PromptResponseStatus.Success,
Message = new(ChatRole.Assistant)
{
Content = @"{
""type"": ""plan"",
""commands"": [
{
""type"": ""DO"",
""action"": ""test""
}
]
}"
}
};

// Act
var res = await augmentation.ValidateResponseAsync(context.Object, memory, tokenizer, promptResponse, 0);

// Assert
Assert.False(res.Valid);
Assert.True(res.Feedback?.StartsWith("Unknown action"));
Assert.Null(res.Value);
}
}
}

0 comments on commit 071f35a

Please sign in to comment.