microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.AI.Tests/ChatPluginTests.cs
89lines · modecode
| 1 | using Microsoft.Teams.AI.Messages; |
| 2 | using Microsoft.Teams.AI.Prompts; |
| 3 | using Microsoft.Teams.AI.Tests.Utils; |
| 4 | |
| 5 | using Moq; |
| 6 | |
| 7 | namespace Microsoft.Teams.AI.Tests; |
| 8 | |
| 9 | public class ChatPluginTests |
| 10 | { |
| 11 | [Theory] |
| 12 | [InlineData("OnBeforeSend")] |
| 13 | [InlineData("OnAfterSend")] |
| 14 | [InlineData("OnBeforeFunctionCall")] |
| 15 | [InlineData("OnAfterFunctionCall")] |
| 16 | [InlineData("OnBuildFunctions")] |
| 17 | [InlineData("OnBuildInstructions")] |
| 18 | public async Task Test_ChatPlugin_HooksCalled(string hookName) |
| 19 | { |
| 20 | // Arrange |
| 21 | var chatPlugin = new Mock<TestChatPlugin>() { CallBase = true }; |
| 22 | var prompt = new TestChatPrompt(); |
| 23 | prompt.Plugin(chatPlugin.Object); |
| 24 | |
| 25 | var message = UserMessage.Text("Hello"); |
| 26 | var options = new TestModelOptions(); |
| 27 | |
| 28 | // Act |
| 29 | var result = await prompt.Send(message, new()); |
| 30 | |
| 31 | // Assert |
| 32 | Assert.NotNull(result); |
| 33 | |
| 34 | switch (hookName) |
| 35 | { |
| 36 | case "OnBeforeSend": |
| 37 | chatPlugin.Verify(p => p.OnBeforeSend(It.IsAny<IChatPrompt<TestModelOptions>>(), It.IsAny<IMessage>(), It.IsAny<TestModelOptions?>(), It.IsAny<CancellationToken>()), Times.Once); |
| 38 | break; |
| 39 | case "OnAfterSend": |
| 40 | chatPlugin.Verify(p => p.OnAfterSend(It.IsAny<IChatPrompt<TestModelOptions>>(), It.IsAny<IMessage>(), It.IsAny<TestModelOptions?>(), It.IsAny<CancellationToken>()), Times.Once); |
| 41 | break; |
| 42 | case "OnBeforeFunctionCall": |
| 43 | chatPlugin.Verify(p => p.OnBeforeFunctionCall(It.IsAny<IChatPrompt<TestModelOptions>>(), It.IsAny<IFunction>(), It.IsAny<FunctionCall>(), It.IsAny<CancellationToken>()), Times.Once); |
| 44 | break; |
| 45 | case "OnAfterFunctionCall": |
| 46 | chatPlugin.Verify(p => p.OnAfterFunctionCall(It.IsAny<IChatPrompt<TestModelOptions>>(), It.IsAny<IFunction>(), It.IsAny<FunctionCall>(), It.IsAny<object?>(), It.IsAny<CancellationToken>()), Times.Once); |
| 47 | break; |
| 48 | case "OnBuildFunctions": |
| 49 | chatPlugin.Verify(p => p.OnBuildFunctions(It.IsAny<IChatPrompt<TestModelOptions>>(), It.IsAny<FunctionCollection>(), It.IsAny<CancellationToken>()), Times.Once); |
| 50 | break; |
| 51 | case "OnBuildInstructions": |
| 52 | chatPlugin.Verify(p => p.OnBuildInstructions(It.IsAny<IChatPrompt<TestModelOptions>>(), It.IsAny<DeveloperMessage?>()), Times.Once); |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | [Fact] |
| 59 | public async Task Test_ChatPlugin_OnBuildFunctions_AddFunction() |
| 60 | { |
| 61 | // Arrange |
| 62 | var testFunctionInvoked = false; |
| 63 | var chatPlugin = new Mock<TestChatPlugin>() { CallBase = true }; |
| 64 | chatPlugin.Setup(p => p.OnBuildFunctions(It.IsAny<IChatPrompt<TestModelOptions>>(), It.IsAny<FunctionCollection>(), It.IsAny<CancellationToken>())) |
| 65 | .ReturnsAsync((IChatPrompt<TestModelOptions> prompt, FunctionCollection functions, CancellationToken cancellationToken) => |
| 66 | { |
| 67 | var newFunction = new Function("injected function", "a test function", () => testFunctionInvoked = true); |
| 68 | functions.Add(newFunction); |
| 69 | return functions; |
| 70 | }); |
| 71 | |
| 72 | var prompt = new TestChatPrompt(); |
| 73 | prompt.Plugin(chatPlugin.Object); |
| 74 | |
| 75 | var message = UserMessage.Text("Hello"); |
| 76 | var options = new TestModelOptions(); |
| 77 | |
| 78 | // Act |
| 79 | var result = await prompt.Send(message, new()); |
| 80 | |
| 81 | // Assert |
| 82 | Assert.NotNull(result); |
| 83 | chatPlugin.Verify(p => p.OnBuildFunctions(It.IsAny<IChatPrompt<TestModelOptions>>(), It.IsAny<FunctionCollection>(), It.IsAny<CancellationToken>()), Times.Once); |
| 84 | Assert.True(testFunctionInvoked, "The injected function should have been invoked."); |
| 85 | |
| 86 | // injected function does not persist in the prompt's function collection |
| 87 | Assert.False(prompt.Functions.Has("injected function")); |
| 88 | } |
| 89 | } |
| 90 | |