|
| 1 | +using System.Text.Json; |
| 2 | +using BotSharp.Abstraction.Routing.Models; |
| 3 | +using BotSharp.Core.Agents.Services; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace BotSharp.Core.UnitTests.Routing; |
| 7 | + |
| 8 | +public class RoutingRuleTests |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public void AllowLlmFill_defaults_to_true_for_existing_rules() |
| 12 | + { |
| 13 | + var rule = JsonSerializer.Deserialize<RoutingRule>("""{"field":"department_type","required":true}"""); |
| 14 | + |
| 15 | + Assert.NotNull(rule); |
| 16 | + Assert.True(rule.AllowLlmFill); |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void AllowLlmFill_maps_from_json_and_survives_clone() |
| 21 | + { |
| 22 | + var rule = JsonSerializer.Deserialize<RoutingRule>("""{"field":"department_type","required":true,"allow_llm_fill":false}"""); |
| 23 | + |
| 24 | + Assert.NotNull(rule); |
| 25 | + Assert.False(rule.AllowLlmFill); |
| 26 | + Assert.False(rule.Clone().AllowLlmFill); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void MergeRoutingRules_preserves_existing_llm_fill_flag_for_matching_rule() |
| 31 | + { |
| 32 | + var existingRules = new List<RoutingRule> |
| 33 | + { |
| 34 | + new() |
| 35 | + { |
| 36 | + Field = "department_type", |
| 37 | + Type = "data_validation", |
| 38 | + Required = true, |
| 39 | + AllowLlmFill = false |
| 40 | + } |
| 41 | + }; |
| 42 | + var incomingRules = new List<RoutingRule> |
| 43 | + { |
| 44 | + new() |
| 45 | + { |
| 46 | + Field = "department_type", |
| 47 | + Type = "data_validation", |
| 48 | + Required = true, |
| 49 | + AllowLlmFill = true |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + var mergedRules = MergeRoutingRules(existingRules, incomingRules); |
| 54 | + |
| 55 | + Assert.False(mergedRules.Single().AllowLlmFill); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void MergeRoutingRules_uses_incoming_llm_fill_flag_for_new_rule() |
| 60 | + { |
| 61 | + var incomingRules = new List<RoutingRule> |
| 62 | + { |
| 63 | + new() |
| 64 | + { |
| 65 | + Field = "new_field", |
| 66 | + Type = "data_validation", |
| 67 | + Required = true, |
| 68 | + AllowLlmFill = false |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + var mergedRules = MergeRoutingRules(existingRules: [], incomingRules); |
| 73 | + |
| 74 | + Assert.False(mergedRules.Single().AllowLlmFill); |
| 75 | + } |
| 76 | + |
| 77 | + private static List<RoutingRule> MergeRoutingRules(List<RoutingRule>? existingRules, List<RoutingRule>? incomingRules) |
| 78 | + { |
| 79 | + var method = typeof(AgentService).GetMethod("MergeRoutingRules", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); |
| 80 | + Assert.NotNull(method); |
| 81 | + |
| 82 | + return Assert.IsType<List<RoutingRule>>(method.Invoke(null, [existingRules, incomingRules])); |
| 83 | + } |
| 84 | +} |
0 commit comments