Add ITool + ToolRegistry + GetCurrentTimeTool
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using System.Text.Json;
|
||||
using backend.Tools;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace backend.tests;
|
||||
|
||||
public class GetCurrentTimeToolTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Returns_now_field_with_iso_utc_timestamp()
|
||||
{
|
||||
var tool = new GetCurrentTimeTool();
|
||||
var args = JsonDocument.Parse("{}").RootElement;
|
||||
var ctx = new DeviceContext(Guid.NewGuid(), Guid.NewGuid(), new NoopChannel());
|
||||
|
||||
var res = await tool.ExecuteAsync(new ToolInvocation("call_1", args), ctx, default);
|
||||
|
||||
res.Output.TryGetProperty("now", out var now).Should().BeTrue();
|
||||
now.GetString().Should().MatchRegex(@"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}");
|
||||
now.GetString()!.Should().EndWith("Z");
|
||||
}
|
||||
|
||||
private class NoopChannel : IDeviceChannel
|
||||
{
|
||||
public Task<JsonElement> CallPiToolAsync(string name, JsonElement args, CancellationToken ct)
|
||||
=> throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using backend.Tools;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace backend.tests;
|
||||
|
||||
public class ToolRegistryTests
|
||||
{
|
||||
private readonly ITool[] _all = new ITool[]
|
||||
{
|
||||
new GetCurrentTimeTool(),
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void Get_returns_tool_by_exact_name()
|
||||
{
|
||||
var reg = new ToolRegistry(_all);
|
||||
reg.Get("get_current_time").Should().NotBeNull();
|
||||
reg.Get("does_not_exist").Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Filter_returns_only_enabled_tools_preserving_order()
|
||||
{
|
||||
var reg = new ToolRegistry(_all);
|
||||
var enabled = new HashSet<string> { "get_current_time", "ghost" };
|
||||
reg.EnabledFor(enabled).Select(t => t.Name).Should().Equal("get_current_time");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user