Files
wehub-resource-sync db620d33df
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:25 +08:00

102 lines
3.6 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Agents.AI.UnitTests.AgentSkills;
/// <summary>
/// Unit tests for <see cref="DeduplicatingAgentSkillsSource"/>.
/// </summary>
public sealed class DeduplicatingAgentSkillsSourceTests
{
[Fact]
public async Task GetSkillsAsync_NoDuplicates_ReturnsAllSkillsAsync()
{
// Arrange
var inner = new AgentInMemorySkillsSource(new AgentSkill[]
{
new AgentInlineSkill("skill-a", "A", "Instructions A."),
new AgentInlineSkill("skill-b", "B", "Instructions B."),
});
var source = new DeduplicatingAgentSkillsSource(inner);
// Act
var result = await source.GetSkillsAsync(TestAgentSkillsSourceContextFactory.Create(), CancellationToken.None);
// Assert
Assert.Equal(2, result.Count);
}
[Fact]
public async Task GetSkillsAsync_WithDuplicates_KeepsFirstOccurrenceAsync()
{
// Arrange
var skills = new AgentSkill[]
{
new AgentInlineSkill("dupe", "First", "Instructions 1."),
new AgentInlineSkill("dupe", "Second", "Instructions 2."),
new AgentInlineSkill("unique", "Unique", "Instructions 3."),
};
var inner = new AgentInMemorySkillsSource(skills);
var source = new DeduplicatingAgentSkillsSource(inner);
// Act
var result = await source.GetSkillsAsync(TestAgentSkillsSourceContextFactory.Create(), CancellationToken.None);
// Assert
Assert.Equal(2, result.Count);
Assert.Equal("First", result.First(s => s.Frontmatter.Name == "dupe").Frontmatter.Description);
Assert.Contains(result, s => s.Frontmatter.Name == "unique");
}
[Fact]
public async Task GetSkillsAsync_CaseInsensitiveDuplication_KeepsFirstAsync()
{
// Arrange - Use a custom source that returns skills with same name but different casing
var inner = new FakeDuplicateCaseSource();
var source = new DeduplicatingAgentSkillsSource(inner);
// Act
var result = await source.GetSkillsAsync(TestAgentSkillsSourceContextFactory.Create(), CancellationToken.None);
// Assert
Assert.Single(result);
Assert.Equal("First", result[0].Frontmatter.Description);
}
[Fact]
public async Task GetSkillsAsync_EmptySource_ReturnsEmptyAsync()
{
// Arrange
var inner = new AgentInMemorySkillsSource(System.Array.Empty<AgentSkill>());
var source = new DeduplicatingAgentSkillsSource(inner);
// Act
var result = await source.GetSkillsAsync(TestAgentSkillsSourceContextFactory.Create(), CancellationToken.None);
// Assert
Assert.Empty(result);
}
/// <summary>
/// A fake source that returns skills with names differing only by case.
/// </summary>
private sealed class FakeDuplicateCaseSource : AgentSkillsSource
{
public override Task<IList<AgentSkill>> GetSkillsAsync(AgentSkillsSourceContext context, CancellationToken cancellationToken = default)
{
// AgentSkillFrontmatter validates names must be lowercase, so we build
// two skills with the same lowercase name to test case-insensitive dedup.
var skills = new List<AgentSkill>
{
new AgentInlineSkill("my-skill", "First", "Instructions 1."),
new AgentInlineSkill("my-skill", "Second", "Instructions 2."),
};
return Task.FromResult<IList<AgentSkill>>(skills);
}
}
}