microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6594a29aa91c928c547a8821d305758bc8d340ed

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Tests/Microsoft.Teams.AI.Tests/ChatPluginTests.cs

167lines · modecode

1using Microsoft.Teams.AI.Messages;
2using Microsoft.Teams.AI.Prompts;
3using Microsoft.Teams.AI.Tests.Utils;
4
5using Moq;
6
7namespace Microsoft.Teams.AI.Tests;
8
9public 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 [Fact]
91 public async Task Test_ChatPrompt_Send_StringWithStreaming()
92 {
93 // Arrange
94 var prompt = new TestChatPrompt();
95 var testText = "Hello, world!";
96 var onChunk = new OnStreamChunk(async (chunk) => { await Task.CompletedTask; });
97
98 // Act
99 var result = await prompt.Send(testText, onChunk);
100
101 // Assert
102 Assert.NotNull(result);
103 Assert.IsType<ModelMessage<string>>(result);
104 }
105
106 [Fact]
107 public async Task Test_ChatPrompt_Send_StringWithOptions()
108 {
109 // Arrange
110 var prompt = new TestChatPrompt();
111 var testText = "Hello, world!";
112 var testOptions = new IChatPrompt<TestModelOptions>.RequestOptions();
113
114 // Act
115 var result = await prompt.Send(testText, testOptions);
116
117 // Assert
118 Assert.NotNull(result);
119 Assert.IsType<ModelMessage<string>>(result);
120 }
121
122 [Fact]
123 public async Task Test_ChatPrompt_Send_ContentArray()
124 {
125 // Arrange
126 var prompt = new TestChatPrompt();
127 var testContent = new IContent[] { new TextContent { Text = "Test content" } };
128
129 // Act
130 var result = await prompt.Send(testContent);
131
132 // Assert
133 Assert.NotNull(result);
134 Assert.IsType<ModelMessage<string>>(result);
135 }
136
137 [Fact]
138 public async Task Test_ChatPrompt_Send_UserMessageString()
139 {
140 // Arrange
141 var prompt = new TestChatPrompt();
142 var testUserMessage = UserMessage.Text("Test user message");
143
144 // Act
145 var result = await prompt.Send(testUserMessage);
146
147 // Assert
148 Assert.NotNull(result);
149 Assert.IsType<ModelMessage<string>>(result);
150 }
151
152 [Fact]
153 public async Task Test_ChatPrompt_Send_UserMessageContent()
154 {
155 // Arrange
156 var prompt = new TestChatPrompt();
157 var testContent = new IContent[] { new TextContent { Text = "Test content" } };
158 var testUserMessage = UserMessage.Text(testContent);
159
160 // Act
161 var result = await prompt.Send(testUserMessage);
162
163 // Assert
164 Assert.NotNull(result);
165 Assert.IsType<ModelMessage<string>>(result);
166 }
167}