mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
626b418622
* .NET: Add a TODO AIContextProvider (#5233) * Add a TODO AIContextProvider * Add unit tests * Address PR comments * Address PR comments * Fix test after removing one tool * .NET: Add a ModeProvider for managing agent modes (#5247) * Add a ModeProvider for managing agent modes * Fix typo * Fix typo * Fix typo * Address PR comments * .NET: Add sample to show how to build a harness (#5268) * Add sample to show how to build a harness * Improve sample * Sample max output tokens and model * Fix encoding * Fix model name in readme * Address PR comments * .NET: Add context window size compaction strategy for harness (#5304) * Add context window size compaction strategy for harness * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Address PR comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * .NET: Add a file memory provider (#5315) * Add a file memory provider * Address PR comments * Fix review comments. * Add additional unit tests * Addressing PR comments. * .NET: Harness: Improve prompts and add FileSystem store (#5365) * Harness: Improve prompts and add FileSystem store * Address PR comments * .NET: Harness: Improve path validation (#5404) * Harness: Improve path validation * Address PR comments * .NET: Add always approve helpers, improve sample and fix bug (#5451) * Add always approve helpers, improve sample and fix bug * Address PR comments * .NET: Make Todo, Mode and FileMemory providers more configurable (#5477) * Make Todo, Mode and FileMemory providers more configurable * Address PR comments. * .NET: Add subagents provider and sample (#5518) * Add subagents provider and sample * Addressing PR comments. * .NET: Harness filememory index plus instructions consistency (#5540) * Add FileMemoryProvider index and improve instruction consistency * Address PR comments. * Address PR comments * Address PR comments. * Apply suggestion from @rogerbarreto Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> --------- Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> * .NET: Refactor harness console to be more extensible and easy to understand with better UX (#5573) * Refactor harness console to be more extensible and easy to understand with better UX. * Fix formatting issues. * Allow multiple clarifications in one response * Address PR comments * .NET: Add FileAccessProvdider and concurrency fix for FileMemoryProvider (#5583) * Add FileAccessProvdider and concurrency fix for FileMemoryProvider * Address PR comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
155 lines
4.5 KiB
C#
155 lines
4.5 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
|
|
namespace Microsoft.Agents.AI.UnitTests;
|
|
|
|
/// <summary>
|
|
/// Unit tests for the <see cref="ToolApprovalRule"/> class.
|
|
/// </summary>
|
|
public class ToolApprovalRuleTests
|
|
{
|
|
#region Construction and Defaults
|
|
|
|
/// <summary>
|
|
/// Verify that a new rule has the expected default values.
|
|
/// </summary>
|
|
[Fact]
|
|
public void NewRule_HasDefaultValues()
|
|
{
|
|
// Act
|
|
var rule = new ToolApprovalRule();
|
|
|
|
// Assert
|
|
Assert.Equal(string.Empty, rule.ToolName);
|
|
Assert.Null(rule.Arguments);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verify that ToolName can be set.
|
|
/// </summary>
|
|
[Fact]
|
|
public void ToolName_CanBeSet()
|
|
{
|
|
// Arrange & Act
|
|
var rule = new ToolApprovalRule { ToolName = "ReadFile" };
|
|
|
|
// Assert
|
|
Assert.Equal("ReadFile", rule.ToolName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verify that Arguments can be set.
|
|
/// </summary>
|
|
[Fact]
|
|
public void Arguments_CanBeSet()
|
|
{
|
|
// Arrange & Act
|
|
var args = new Dictionary<string, string> { ["path"] = "test.txt" };
|
|
var rule = new ToolApprovalRule { ToolName = "ReadFile", Arguments = args };
|
|
|
|
// Assert
|
|
Assert.NotNull(rule.Arguments);
|
|
Assert.Equal("test.txt", rule.Arguments["path"]);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region JSON Serialization
|
|
|
|
/// <summary>
|
|
/// Verify that a tool-level rule round-trips through JSON serialization.
|
|
/// </summary>
|
|
[Fact]
|
|
public void Serialize_ToolLevelRule_RoundTrips()
|
|
{
|
|
// Arrange
|
|
var rule = new ToolApprovalRule { ToolName = "MyTool" };
|
|
|
|
// Act
|
|
var json = JsonSerializer.Serialize(rule, AgentJsonUtilities.DefaultOptions);
|
|
var deserialized = JsonSerializer.Deserialize<ToolApprovalRule>(json, AgentJsonUtilities.DefaultOptions);
|
|
|
|
// Assert
|
|
Assert.NotNull(deserialized);
|
|
Assert.Equal("MyTool", deserialized!.ToolName);
|
|
Assert.Null(deserialized.Arguments);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verify that a tool+arguments rule round-trips through JSON serialization.
|
|
/// </summary>
|
|
[Fact]
|
|
public void Serialize_ToolWithArgsRule_RoundTrips()
|
|
{
|
|
// Arrange
|
|
var rule = new ToolApprovalRule
|
|
{
|
|
ToolName = "ReadFile",
|
|
Arguments = new Dictionary<string, string> { ["path"] = "test.txt", ["encoding"] = "utf-8" },
|
|
};
|
|
|
|
// Act
|
|
var json = JsonSerializer.Serialize(rule, AgentJsonUtilities.DefaultOptions);
|
|
var deserialized = JsonSerializer.Deserialize<ToolApprovalRule>(json, AgentJsonUtilities.DefaultOptions);
|
|
|
|
// Assert
|
|
Assert.NotNull(deserialized);
|
|
Assert.Equal("ReadFile", deserialized!.ToolName);
|
|
Assert.NotNull(deserialized.Arguments);
|
|
Assert.Equal(2, deserialized.Arguments!.Count);
|
|
Assert.Equal("test.txt", deserialized.Arguments["path"]);
|
|
Assert.Equal("utf-8", deserialized.Arguments["encoding"]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verify that JSON property names are correctly applied.
|
|
/// </summary>
|
|
[Fact]
|
|
public void Serialize_UsesJsonPropertyNames()
|
|
{
|
|
// Arrange
|
|
var rule = new ToolApprovalRule
|
|
{
|
|
ToolName = "MyTool",
|
|
Arguments = new Dictionary<string, string> { ["key"] = "value" },
|
|
};
|
|
|
|
// Act
|
|
var json = JsonSerializer.Serialize(rule, AgentJsonUtilities.DefaultOptions);
|
|
|
|
// Assert
|
|
Assert.Contains("\"toolName\"", json);
|
|
Assert.Contains("\"arguments\"", json);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verify that a list of rules round-trips through JSON serialization.
|
|
/// </summary>
|
|
[Fact]
|
|
public void Serialize_RuleList_RoundTrips()
|
|
{
|
|
// Arrange
|
|
var rules = new List<ToolApprovalRule>
|
|
{
|
|
new() { ToolName = "ToolA" },
|
|
new() { ToolName = "ToolB", Arguments = new Dictionary<string, string> { ["x"] = "1" } },
|
|
};
|
|
|
|
// Act
|
|
var json = JsonSerializer.Serialize(rules, AgentJsonUtilities.DefaultOptions);
|
|
var deserialized = JsonSerializer.Deserialize<List<ToolApprovalRule>>(json, AgentJsonUtilities.DefaultOptions);
|
|
|
|
// Assert
|
|
Assert.NotNull(deserialized);
|
|
Assert.Equal(2, deserialized!.Count);
|
|
Assert.Equal("ToolA", deserialized[0].ToolName);
|
|
Assert.Null(deserialized[0].Arguments);
|
|
Assert.Equal("ToolB", deserialized[1].ToolName);
|
|
Assert.NotNull(deserialized[1].Arguments);
|
|
}
|
|
|
|
#endregion
|
|
}
|