|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reflection; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Flow.AI.Core.Interfaces; |
| 6 | +using Flow.AI.Core.Models; |
| 7 | +using PromptStream.AI.Models; |
| 8 | +using PromptStream.AI.Services; |
| 9 | +using TokenFlow.Core.Models; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace PromptStream.AI.Tests.Services |
| 13 | +{ |
| 14 | + public class MockModelClient : IModelClient |
| 15 | + { |
| 16 | + public Task<PromptResponse> GenerateAsync(string prompt) |
| 17 | + { |
| 18 | + return Task.FromResult(new PromptResponse |
| 19 | + { |
| 20 | + Content = "Mocked response", |
| 21 | + TimestampUtc = DateTime.UtcNow |
| 22 | + }); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public class PromptStreamServiceTests_Extra |
| 27 | + { |
| 28 | + private readonly PromptStreamService _service; |
| 29 | + private readonly Type _shimType; |
| 30 | + |
| 31 | + public PromptStreamServiceTests_Extra() |
| 32 | + { |
| 33 | + _service = new PromptStreamService( |
| 34 | + new MockTokenProvider(), |
| 35 | + null, |
| 36 | + new MockModelClient(), |
| 37 | + "nonexistent-model", |
| 38 | + 50); |
| 39 | + |
| 40 | + _shimType = typeof(PromptStreamService) |
| 41 | + .GetNestedType("ModelSpecShim", BindingFlags.NonPublic)!; |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void AnalyzePrompt_ShouldHandleMissingRegistryResource_AndHitCatchBlock() |
| 46 | + { |
| 47 | + var template = new PromptTemplate { Template = "Test prompt" }; |
| 48 | + var variables = new Dictionary<string, string>(); |
| 49 | + |
| 50 | + var (validation, summary) = _service.AnalyzePrompt(template, variables); |
| 51 | + |
| 52 | + Assert.NotNull(validation); |
| 53 | + Assert.True(validation.IsValid); |
| 54 | + Assert.NotNull(summary); |
| 55 | + Assert.True(summary.TotalTokens >= 0); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void AnalyzePrompt_ShouldFallbackToDefaultModel_WhenModelNotFound() |
| 60 | + { |
| 61 | + var template = new PromptTemplate { Template = "Fallback model test" }; |
| 62 | + var variables = new Dictionary<string, string>(); |
| 63 | + |
| 64 | + var (validation, summary) = _service.AnalyzePrompt(template, variables); |
| 65 | + |
| 66 | + Assert.True(validation.IsValid); |
| 67 | + Assert.Equal(validation.TokenCount, summary.TotalTokens); |
| 68 | + Assert.True(summary.EstimatedCost >= 0); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void ModelSpecShim_LegacySetters_ShouldAssignValuesOnlyWhenZero() |
| 73 | + { |
| 74 | + var shim = Activator.CreateInstance(_shimType)!; |
| 75 | + |
| 76 | + // Accessors via reflection |
| 77 | + var inProp = _shimType.GetProperty("InputPricePer1K")!; |
| 78 | + var outProp = _shimType.GetProperty("OutputPricePer1K")!; |
| 79 | + var legacyInProp = _shimType.GetProperty("LegacyPromptCostPer1K")!; |
| 80 | + var legacyOutProp = _shimType.GetProperty("LegacyCompletionCostPer1K")!; |
| 81 | + |
| 82 | + inProp.SetValue(shim, 0m); |
| 83 | + outProp.SetValue(shim, 0m); |
| 84 | + |
| 85 | + // Case 1: assign legacy values when zero |
| 86 | + legacyInProp.SetValue(shim, 1.5m); |
| 87 | + legacyOutProp.SetValue(shim, 3.0m); |
| 88 | + |
| 89 | + Assert.Equal(1.5m, (decimal)inProp.GetValue(shim)!); |
| 90 | + Assert.Equal(3.0m, (decimal)outProp.GetValue(shim)!); |
| 91 | + |
| 92 | + // Case 2: should NOT override non-zero values |
| 93 | + legacyInProp.SetValue(shim, 9.9m); |
| 94 | + legacyOutProp.SetValue(shim, 9.9m); |
| 95 | + |
| 96 | + Assert.Equal(1.5m, (decimal)inProp.GetValue(shim)!); |
| 97 | + Assert.Equal(3.0m, (decimal)outProp.GetValue(shim)!); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void ModelSpecShim_ToModelSpec_ShouldReturnValidModelSpec() |
| 102 | + { |
| 103 | + var shim = Activator.CreateInstance(_shimType)!; |
| 104 | + |
| 105 | + // Reflection property setup |
| 106 | + _shimType.GetProperty("Id")!.SetValue(shim, "gpt-4o-mini"); |
| 107 | + _shimType.GetProperty("Family")!.SetValue(shim, "OpenAI"); |
| 108 | + _shimType.GetProperty("TokenizerName")!.SetValue(shim, "tiktoken"); |
| 109 | + _shimType.GetProperty("MaxInputTokens")!.SetValue(shim, 0); |
| 110 | + _shimType.GetProperty("InputPricePer1K")!.SetValue(shim, 2.5m); |
| 111 | + _shimType.GetProperty("OutputPricePer1K")!.SetValue(shim, 10.0m); |
| 112 | + |
| 113 | + // Call ToModelSpec() via reflection |
| 114 | + var toModelSpecMethod = _shimType.GetMethod("ToModelSpec")!; |
| 115 | + var modelSpec = (ModelSpec)toModelSpecMethod.Invoke(shim, null)!; |
| 116 | + |
| 117 | + Assert.Equal("gpt-4o-mini", modelSpec.Id); |
| 118 | + Assert.Equal("OpenAI", modelSpec.Family); |
| 119 | + Assert.Equal("tiktoken", modelSpec.TokenizerName); |
| 120 | + Assert.Equal(128000, modelSpec.MaxInputTokens); |
| 121 | + Assert.Equal(2.5m, modelSpec.InputPricePer1K); |
| 122 | + Assert.Equal(10.0m, modelSpec.OutputPricePer1K); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments