|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Reflection; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.Extensions.AI; |
| 11 | + |
| 12 | +public class DelegatingAIFunctionTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void Constructor_NullInnerFunction_ThrowsArgumentNullException() |
| 16 | + { |
| 17 | + Assert.Throws<ArgumentNullException>("innerFunction", () => new DerivedFunction(null!)); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void DefaultOverrides_DelegateToInnerFunction() |
| 22 | + { |
| 23 | + AIFunction expected = AIFunctionFactory.Create(() => 42); |
| 24 | + DerivedFunction actual = new(expected); |
| 25 | + |
| 26 | + Assert.Same(expected, actual.InnerFunction); |
| 27 | + Assert.Equal(expected.Name, actual.Name); |
| 28 | + Assert.Equal(expected.Description, actual.Description); |
| 29 | + Assert.Equal(expected.JsonSchema, actual.JsonSchema); |
| 30 | + Assert.Equal(expected.ReturnJsonSchema, actual.ReturnJsonSchema); |
| 31 | + Assert.Same(expected.JsonSerializerOptions, actual.JsonSerializerOptions); |
| 32 | + Assert.Same(expected.UnderlyingMethod, actual.UnderlyingMethod); |
| 33 | + Assert.Same(expected.AdditionalProperties, actual.AdditionalProperties); |
| 34 | + Assert.Equal(expected.ToString(), actual.ToString()); |
| 35 | + } |
| 36 | + |
| 37 | + private sealed class DerivedFunction(AIFunction innerFunction) : DelegatingAIFunction(innerFunction) |
| 38 | + { |
| 39 | + public new AIFunction InnerFunction => base.InnerFunction; |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Virtuals_AllOverridden() |
| 44 | + { |
| 45 | + Assert.All(typeof(DelegatingAIFunction).GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance), m => |
| 46 | + { |
| 47 | + switch (m) |
| 48 | + { |
| 49 | + case MethodInfo methodInfo when methodInfo.IsVirtual && methodInfo.Name is not ("Finalize" or "Equals" or "GetHashCode"): |
| 50 | + Assert.True(methodInfo.DeclaringType == typeof(DelegatingAIFunction), $"{methodInfo.Name} not overridden"); |
| 51 | + break; |
| 52 | + |
| 53 | + case PropertyInfo propertyInfo when propertyInfo.GetMethod?.IsVirtual is true: |
| 54 | + Assert.True(propertyInfo.DeclaringType == typeof(DelegatingAIFunction), $"{propertyInfo.Name} not overridden"); |
| 55 | + break; |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public async Task OverriddenInvocation_SuccessfullyInvoked() |
| 62 | + { |
| 63 | + bool innerInvoked = false; |
| 64 | + AIFunction inner = AIFunctionFactory.Create(int () => |
| 65 | + { |
| 66 | + innerInvoked = true; |
| 67 | + throw new Exception("uh oh"); |
| 68 | + }, "TestFunction", "A test function for DelegatingAIFunction"); |
| 69 | + |
| 70 | + AIFunction actual = new OverridesInvocation(inner, (args, ct) => new ValueTask<object?>(84)); |
| 71 | + |
| 72 | + Assert.Equal(inner.Name, actual.Name); |
| 73 | + Assert.Equal(inner.Description, actual.Description); |
| 74 | + Assert.Equal(inner.JsonSchema, actual.JsonSchema); |
| 75 | + Assert.Equal(inner.ReturnJsonSchema, actual.ReturnJsonSchema); |
| 76 | + Assert.Same(inner.JsonSerializerOptions, actual.JsonSerializerOptions); |
| 77 | + Assert.Same(inner.UnderlyingMethod, actual.UnderlyingMethod); |
| 78 | + Assert.Same(inner.AdditionalProperties, actual.AdditionalProperties); |
| 79 | + Assert.Equal(inner.ToString(), actual.ToString()); |
| 80 | + |
| 81 | + object? result = await actual.InvokeAsync(new(), CancellationToken.None); |
| 82 | + Assert.Contains("84", result?.ToString()); |
| 83 | + |
| 84 | + Assert.False(innerInvoked); |
| 85 | + } |
| 86 | + |
| 87 | + private sealed class OverridesInvocation(AIFunction innerFunction, Func<AIFunctionArguments, CancellationToken, ValueTask<object?>> invokeAsync) : DelegatingAIFunction(innerFunction) |
| 88 | + { |
| 89 | + protected override ValueTask<object?> InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken) => |
| 90 | + invokeAsync(arguments, cancellationToken); |
| 91 | + } |
| 92 | +} |
0 commit comments