|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.SemanticKernel; |
| 7 | +using Microsoft.SemanticKernel.Agents; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace SemanticKernel.Agents.UnitTests.Core; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Contains tests for the <see cref="AgentThread"/> class. |
| 14 | +/// </summary> |
| 15 | +public class AgentThreadTests |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Tests that the CreateAsync method sets the Id and invokes CreateInternalAsync once. |
| 19 | + /// </summary> |
| 20 | + [Fact] |
| 21 | + public async Task CreateShouldSetIdAndInvokeCreateInternalOnceAsync() |
| 22 | + { |
| 23 | + // Arrange |
| 24 | + var thread = new TestAgentThread(); |
| 25 | + |
| 26 | + // Act |
| 27 | + await thread.CreateAsync(); |
| 28 | + await thread.CreateAsync(); |
| 29 | + |
| 30 | + // Assert |
| 31 | + Assert.Equal("test-thread-id", thread.Id); |
| 32 | + Assert.Equal(1, thread.CreateInternalAsyncCount); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Tests that the CreateAsync method throws an InvalidOperationException if the thread is deleted. |
| 37 | + /// </summary> |
| 38 | + [Fact] |
| 39 | + public async Task CreateShouldThrowIfThreadDeletedAsync() |
| 40 | + { |
| 41 | + // Arrange |
| 42 | + var thread = new TestAgentThread(); |
| 43 | + await thread.CreateAsync(); |
| 44 | + await thread.DeleteAsync(); |
| 45 | + |
| 46 | + // Act & Assert |
| 47 | + await Assert.ThrowsAsync<InvalidOperationException>(() => thread.CreateAsync()); |
| 48 | + Assert.Equal(1, thread.CreateInternalAsyncCount); |
| 49 | + Assert.Equal(1, thread.DeleteInternalAsyncCount); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Tests that the DeleteAsync method sets IsDeleted and invokes DeleteInternalAsync. |
| 54 | + /// </summary> |
| 55 | + [Fact] |
| 56 | + public async Task DeleteShouldSetIsDeletedAndInvokeDeleteInternalAsync() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var thread = new TestAgentThread(); |
| 60 | + await thread.CreateAsync(); |
| 61 | + |
| 62 | + // Act |
| 63 | + await thread.DeleteAsync(); |
| 64 | + |
| 65 | + // Assert |
| 66 | + Assert.True(thread.IsDeleted); |
| 67 | + Assert.Equal(1, thread.CreateInternalAsyncCount); |
| 68 | + Assert.Equal(1, thread.DeleteInternalAsyncCount); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Tests that the DeleteAsync method does not invoke DeleteInternalAsync if the thread is already deleted. |
| 73 | + /// </summary> |
| 74 | + [Fact] |
| 75 | + public async Task DeleteShouldNotInvokeDeleteInternalIfAlreadyDeletedAsync() |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + var thread = new TestAgentThread(); |
| 79 | + await thread.CreateAsync(); |
| 80 | + await thread.DeleteAsync(); |
| 81 | + |
| 82 | + // Act |
| 83 | + await thread.DeleteAsync(); |
| 84 | + |
| 85 | + // Assert |
| 86 | + Assert.True(thread.IsDeleted); |
| 87 | + Assert.Equal(1, thread.CreateInternalAsyncCount); |
| 88 | + Assert.Equal(1, thread.DeleteInternalAsyncCount); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Tests that the DeleteAsync method throws an InvalidOperationException if the thread was never created. |
| 93 | + /// </summary> |
| 94 | + [Fact] |
| 95 | + public async Task DeleteShouldThrowIfNeverCreatedAsync() |
| 96 | + { |
| 97 | + // Arrange |
| 98 | + var thread = new TestAgentThread(); |
| 99 | + |
| 100 | + // Act & Assert |
| 101 | + await Assert.ThrowsAsync<InvalidOperationException>(() => thread.DeleteAsync()); |
| 102 | + Assert.Equal(0, thread.CreateInternalAsyncCount); |
| 103 | + Assert.Equal(0, thread.DeleteInternalAsyncCount); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Tests that the OnNewMessageAsync method creates the thread if it is not already created. |
| 108 | + /// </summary> |
| 109 | + [Fact] |
| 110 | + public async Task OnNewMessageShouldCreateThreadIfNotCreatedAsync() |
| 111 | + { |
| 112 | + // Arrange |
| 113 | + var thread = new TestAgentThread(); |
| 114 | + var message = new ChatMessageContent(); |
| 115 | + |
| 116 | + // Act |
| 117 | + await thread.OnNewMessageAsync(message); |
| 118 | + |
| 119 | + // Assert |
| 120 | + Assert.Equal("test-thread-id", thread.Id); |
| 121 | + Assert.Equal(1, thread.CreateInternalAsyncCount); |
| 122 | + Assert.Equal(1, thread.OnNewMessageInternalAsyncCount); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Tests that the OnNewMessageAsync method throws an InvalidOperationException if the thread is deleted. |
| 127 | + /// </summary> |
| 128 | + [Fact] |
| 129 | + public async Task OnNewMessageShouldThrowIfThreadDeletedAsync() |
| 130 | + { |
| 131 | + // Arrange |
| 132 | + var thread = new TestAgentThread(); |
| 133 | + await thread.CreateAsync(); |
| 134 | + await thread.DeleteAsync(); |
| 135 | + var message = new ChatMessageContent(); |
| 136 | + |
| 137 | + // Act & Assert |
| 138 | + await Assert.ThrowsAsync<InvalidOperationException>(() => thread.OnNewMessageAsync(message)); |
| 139 | + Assert.Equal(1, thread.CreateInternalAsyncCount); |
| 140 | + Assert.Equal(1, thread.DeleteInternalAsyncCount); |
| 141 | + Assert.Equal(0, thread.OnNewMessageInternalAsyncCount); |
| 142 | + } |
| 143 | + |
| 144 | + private sealed class TestAgentThread : AgentThread |
| 145 | + { |
| 146 | + public int CreateInternalAsyncCount { get; private set; } |
| 147 | + public int DeleteInternalAsyncCount { get; private set; } |
| 148 | + public int OnNewMessageInternalAsyncCount { get; private set; } |
| 149 | + |
| 150 | + protected override Task<string?> CreateInternalAsync(CancellationToken cancellationToken) |
| 151 | + { |
| 152 | + this.CreateInternalAsyncCount++; |
| 153 | + return Task.FromResult<string?>("test-thread-id"); |
| 154 | + } |
| 155 | + |
| 156 | + protected override Task DeleteInternalAsync(CancellationToken cancellationToken) |
| 157 | + { |
| 158 | + this.DeleteInternalAsyncCount++; |
| 159 | + return Task.CompletedTask; |
| 160 | + } |
| 161 | + |
| 162 | + protected override Task OnNewMessageInternalAsync(ChatMessageContent newMessage, CancellationToken cancellationToken = default) |
| 163 | + { |
| 164 | + this.OnNewMessageInternalAsyncCount++; |
| 165 | + return Task.CompletedTask; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments