From 2394db1b74a7f79ed4fc242cd95fece20d884843 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 15:59:57 +0000 Subject: [PATCH 1/2] Initial plan From f392e68b3f182143068089f65322cd85a0a3397a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 16:08:14 +0000 Subject: [PATCH 2/2] Fix case-sensitivity bug in Cosmos queries and add tests Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> --- .../CosmosChatHistoryProvider.cs | 4 +- .../CosmosChatHistoryProviderTests.cs | 110 ++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs b/dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs index 41c9a211dc..17bb77ac78 100644 --- a/dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs +++ b/dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs @@ -552,7 +552,7 @@ public async Task GetMessageCountAsync(CancellationToken cancellationToken #pragma warning restore CA1513 // Efficient count query - var query = new QueryDefinition("SELECT VALUE COUNT(1) FROM c WHERE c.conversationId = @conversationId AND c.Type = @type") + var query = new QueryDefinition("SELECT VALUE COUNT(1) FROM c WHERE c.conversationId = @conversationId AND c.type = @type") .WithParameter("@conversationId", this.ConversationId) .WithParameter("@type", "ChatMessage"); @@ -582,7 +582,7 @@ public async Task ClearMessagesAsync(CancellationToken cancellationToken = #pragma warning restore CA1513 // Batch delete for efficiency - var query = new QueryDefinition("SELECT VALUE c.id FROM c WHERE c.conversationId = @conversationId AND c.Type = @type") + var query = new QueryDefinition("SELECT VALUE c.id FROM c WHERE c.conversationId = @conversationId AND c.type = @type") .WithParameter("@conversationId", this.ConversationId) .WithParameter("@type", "ChatMessage"); diff --git a/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs b/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs index ab2f58dfd5..6a70634e35 100644 --- a/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs @@ -815,5 +815,115 @@ public async Task MaxMessagesToRetrieve_Null_ShouldReturnAllMessagesAsync() Assert.Equal("Message 10", messageList[9].Text); } + [SkippableFact] + [Trait("Category", "CosmosDB")] + public async Task GetMessageCountAsync_WithMessages_ShouldReturnCorrectCountAsync() + { + // Arrange + this.SkipIfEmulatorNotAvailable(); + const string ConversationId = "count-test-conversation"; + + using var provider = new CosmosChatHistoryProvider(this._connectionString, s_testDatabaseId, TestContainerId, ConversationId); + + // Add 5 messages + var messages = new List(); + for (int i = 1; i <= 5; i++) + { + messages.Add(new ChatMessage(ChatRole.User, $"Message {i}")); + } + + var context = new ChatHistoryProvider.InvokedContext(messages, []); + await provider.InvokedAsync(context); + + // Wait for eventual consistency + await Task.Delay(100); + + // Act + var count = await provider.GetMessageCountAsync(); + + // Assert + Assert.Equal(5, count); + } + + [SkippableFact] + [Trait("Category", "CosmosDB")] + public async Task GetMessageCountAsync_WithNoMessages_ShouldReturnZeroAsync() + { + // Arrange + this.SkipIfEmulatorNotAvailable(); + const string ConversationId = "empty-count-test-conversation"; + + using var provider = new CosmosChatHistoryProvider(this._connectionString, s_testDatabaseId, TestContainerId, ConversationId); + + // Act + var count = await provider.GetMessageCountAsync(); + + // Assert + Assert.Equal(0, count); + } + + [SkippableFact] + [Trait("Category", "CosmosDB")] + public async Task ClearMessagesAsync_WithMessages_ShouldDeleteAndReturnCountAsync() + { + // Arrange + this.SkipIfEmulatorNotAvailable(); + const string ConversationId = "clear-test-conversation"; + + using var provider = new CosmosChatHistoryProvider(this._connectionString, s_testDatabaseId, TestContainerId, ConversationId); + + // Add 3 messages + var messages = new List + { + new ChatMessage(ChatRole.User, "Message 1"), + new ChatMessage(ChatRole.Assistant, "Message 2"), + new ChatMessage(ChatRole.User, "Message 3") + }; + + var context = new ChatHistoryProvider.InvokedContext(messages, []); + await provider.InvokedAsync(context); + + // Wait for eventual consistency + await Task.Delay(100); + + // Verify messages exist + var countBefore = await provider.GetMessageCountAsync(); + Assert.Equal(3, countBefore); + + // Act + var deletedCount = await provider.ClearMessagesAsync(); + + // Wait for eventual consistency + await Task.Delay(100); + + // Assert + Assert.Equal(3, deletedCount); + + // Verify messages are deleted + var countAfter = await provider.GetMessageCountAsync(); + Assert.Equal(0, countAfter); + + var invokingContext = new ChatHistoryProvider.InvokingContext([]); + var retrievedMessages = await provider.InvokingAsync(invokingContext); + Assert.Empty(retrievedMessages); + } + + [SkippableFact] + [Trait("Category", "CosmosDB")] + public async Task ClearMessagesAsync_WithNoMessages_ShouldReturnZeroAsync() + { + // Arrange + this.SkipIfEmulatorNotAvailable(); + const string ConversationId = "empty-clear-test-conversation"; + + using var provider = new CosmosChatHistoryProvider(this._connectionString, s_testDatabaseId, TestContainerId, ConversationId); + + // Act + var deletedCount = await provider.ClearMessagesAsync(); + + // Assert + Assert.Equal(0, deletedCount); + } + #endregion }