-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathFakeToolRegistry.cs
More file actions
57 lines (52 loc) · 2.14 KB
/
FakeToolRegistry.cs
File metadata and controls
57 lines (52 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
namespace Temporalio.Extensions.ToolRegistry.Testing
{
/// <summary>
/// Wraps <see cref="ToolRegistry"/> and records every <see cref="Dispatch"/> call.
/// Implements <see cref="IDispatcher"/> for use with <see cref="MockProvider.WithRegistry(IDispatcher)"/>.
/// </summary>
/// <remarks>
/// Example:
/// <code>
/// var fake = new FakeToolRegistry();
/// fake.Register(def, input => "result");
/// // Use fake.WithRegistry(fake) on MockProvider to record calls.
/// Assert.Single(fake.Calls);
/// Assert.Equal("tool-name", fake.Calls[0].Name);
/// </code>
/// </remarks>
public sealed class FakeToolRegistry : IDispatcher
{
private readonly ToolRegistry inner = new();
private readonly List<DispatchCall> calls = new();
/// <summary>
/// Gets all tool dispatch invocations in order.
/// </summary>
public IList<DispatchCall> Calls => calls;
/// <summary>
/// Registers a tool definition and its handler.
/// </summary>
/// <param name="def">Tool definition.</param>
/// <param name="handler">Function called when the tool is dispatched.</param>
public void Register(ToolDef def, Func<IReadOnlyDictionary<string, object?>, string> handler) =>
inner.Register(def, handler);
/// <summary>
/// Records the call and delegates dispatch to the underlying registry.
/// </summary>
/// <param name="name">Tool name.</param>
/// <param name="input">Tool input.</param>
/// <returns>String result from the handler.</returns>
public string Dispatch(string name, IReadOnlyDictionary<string, object?> input)
{
var result = inner.Dispatch(name, input);
calls.Add(new(name, input, result));
return result;
}
/// <summary>
/// Returns the underlying registry's definitions.
/// </summary>
/// <returns>Read-only list of registered tool definitions.</returns>
public IReadOnlyList<ToolDef> Definitions() => inner.Definitions();
}
}