Skip to content

Commit 9844518

Browse files
author
AndrewMorgan1
committed
fixed some missing test cases
1 parent 2612d7b commit 9844518

2 files changed

Lines changed: 232 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using PromptStream.AI.Integration.Legacy;
2+
using System.Text.Json;
3+
4+
namespace PromptStream.AI.Tests.Models
5+
{
6+
public class ModelSpecShimTests
7+
{
8+
[Fact]
9+
public void ToModelSpec_ShouldMapModernFieldsDirectly()
10+
{
11+
var shim = new ModelSpecShim
12+
{
13+
Id = "gpt-4o",
14+
Family = "OpenAI",
15+
TokenizerName = "tiktoken",
16+
MaxInputTokens = 16000,
17+
MaxOutputTokens = 2048,
18+
InputPricePer1K = 2.5m,
19+
OutputPricePer1K = 10.0m
20+
};
21+
22+
var model = shim.ToModelSpec();
23+
24+
Assert.Equal("gpt-4o", model.Id);
25+
Assert.Equal("OpenAI", model.Family);
26+
Assert.Equal("tiktoken", model.TokenizerName);
27+
Assert.Equal(16000, model.MaxInputTokens);
28+
Assert.Equal(2048, model.MaxOutputTokens);
29+
Assert.Equal(2.5m, model.InputPricePer1K);
30+
Assert.Equal(10.0m, model.OutputPricePer1K);
31+
}
32+
33+
[Fact]
34+
public void LegacyPromptCost_ShouldSetInputPrice_WhenZero()
35+
{
36+
var shim = new ModelSpecShim();
37+
shim.LegacyPromptCostPer1K = 0.75m; // triggers setter
38+
39+
Assert.Equal(0.75m, shim.InputPricePer1K);
40+
}
41+
42+
[Fact]
43+
public void LegacyPromptCost_ShouldNotOverride_WhenAlreadySet()
44+
{
45+
var shim = new ModelSpecShim { InputPricePer1K = 2.0m };
46+
shim.LegacyPromptCostPer1K = 0.75m;
47+
48+
Assert.Equal(2.0m, shim.InputPricePer1K);
49+
}
50+
51+
[Fact]
52+
public void LegacyCompletionCost_ShouldSetOutputPrice_WhenZero()
53+
{
54+
var shim = new ModelSpecShim();
55+
shim.LegacyCompletionCostPer1K = 5.0m; // triggers setter
56+
57+
Assert.Equal(5.0m, shim.OutputPricePer1K);
58+
}
59+
60+
[Fact]
61+
public void LegacyCompletionCost_ShouldNotOverride_WhenAlreadySet()
62+
{
63+
var shim = new ModelSpecShim { OutputPricePer1K = 8.0m };
64+
shim.LegacyCompletionCostPer1K = 10.0m;
65+
66+
Assert.Equal(8.0m, shim.OutputPricePer1K);
67+
}
68+
69+
[Fact]
70+
public void ToModelSpec_ShouldApplyDefaultMaxInput_WhenZero()
71+
{
72+
var shim = new ModelSpecShim
73+
{
74+
Id = "claude-sonnet-4",
75+
Family = "Anthropic",
76+
TokenizerName = "cltk",
77+
MaxInputTokens = 0, // triggers fallback
78+
InputPricePer1K = 1.5m,
79+
OutputPricePer1K = 6.0m
80+
};
81+
82+
var model = shim.ToModelSpec();
83+
Assert.Equal(128000, model.MaxInputTokens);
84+
Assert.Equal(1.5m, model.InputPricePer1K);
85+
Assert.Equal(6.0m, model.OutputPricePer1K);
86+
}
87+
88+
[Fact]
89+
public void JsonDeserialization_ShouldMapLegacyFields_WhenModernMissing()
90+
{
91+
var json = """
92+
{
93+
"Id": "gpt-3.5-turbo",
94+
"Family": "OpenAI",
95+
"PromptCostPer1K": 0.0015,
96+
"CompletionCostPer1K": 0.0020
97+
}
98+
""";
99+
100+
var shim = JsonSerializer.Deserialize<ModelSpecShim>(json);
101+
Assert.NotNull(shim);
102+
Assert.Equal("gpt-3.5-turbo", shim!.Id);
103+
Assert.Equal(0.0015m, shim.InputPricePer1K);
104+
Assert.Equal(0.0020m, shim.OutputPricePer1K);
105+
}
106+
}
107+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)