-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructuredOutputSchemaTests.cs
More file actions
77 lines (66 loc) · 3.61 KB
/
StructuredOutputSchemaTests.cs
File metadata and controls
77 lines (66 loc) · 3.61 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using ManagedCode.ClaudeCodeSharpSDK.Models;
namespace ManagedCode.ClaudeCodeSharpSDK.Tests.Unit;
public class StructuredOutputSchemaTests
{
private const string AdditionalPropertiesPropertyName = "additionalProperties";
private const string BlankPropertyName = " ";
private const string BooleanSchemaType = "boolean";
private const string MustExistInSchemaPropertiesMessageFragment = "must exist in schema properties";
private const string NumberSchemaType = "number";
private const string ObjectSchemaType = "object";
private const string PropertiesPropertyName = "properties";
private const string RequiredPropertyName = "required";
private const string TypePropertyName = "type";
private const string StringSchemaType = "string";
private sealed record ScoreStatusResponse(string Status, double Score, bool Ok);
private sealed record MissingPropertyResponse(string Missing);
[Test]
public async Task Object_BuildsExpectedSchema()
{
var schema = StructuredOutputSchema.Map<ScoreStatusResponse>(
additionalProperties: false,
(response => response.Status, StructuredOutputSchema.PlainText()),
(response => response.Score, StructuredOutputSchema.Numeric()),
(response => response.Ok, StructuredOutputSchema.Flag()));
var json = schema.ToJsonObject();
await Assert.That(json[TypePropertyName]!.GetValue<string>()).IsEqualTo(ObjectSchemaType);
await Assert.That(json[PropertiesPropertyName]![nameof(ScoreStatusResponse.Status)]![TypePropertyName]!.GetValue<string>()).IsEqualTo(StringSchemaType);
await Assert.That(json[PropertiesPropertyName]![nameof(ScoreStatusResponse.Score)]![TypePropertyName]!.GetValue<string>()).IsEqualTo(NumberSchemaType);
await Assert.That(json[PropertiesPropertyName]![nameof(ScoreStatusResponse.Ok)]![TypePropertyName]!.GetValue<string>()).IsEqualTo(BooleanSchemaType);
await Assert.That(json[RequiredPropertyName]![0]!.GetValue<string>()).IsEqualTo(nameof(ScoreStatusResponse.Status));
await Assert.That(json[AdditionalPropertiesPropertyName]!.GetValue<bool>()).IsFalse();
}
[Test]
public async Task Object_ThrowsForInvalidPropertyName()
{
var action = () => StructuredOutputSchema.Map(
new Dictionary<string, StructuredOutputSchema>
{
[BlankPropertyName] = StructuredOutputSchema.PlainText(),
});
var exception = await Assert.That(action).ThrowsException();
await Assert.That(exception).IsTypeOf<ArgumentException>();
}
[Test]
public async Task Object_ThrowsWhenRequiredPropertyMissingFromProperties()
{
var action = () => StructuredOutputSchema.Map(
new Dictionary<string, StructuredOutputSchema>
{
[nameof(ScoreStatusResponse.Status)] = StructuredOutputSchema.PlainText(),
},
required: [nameof(MissingPropertyResponse.Missing)]);
var exception = await Assert.That(action).ThrowsException();
await Assert.That(exception).IsTypeOf<ArgumentException>();
await Assert.That(exception!.Message).Contains(MustExistInSchemaPropertiesMessageFragment);
}
[Test]
public async Task Object_ThrowsForUnsupportedPropertySelector()
{
var action = () => StructuredOutputSchema.Map<ScoreStatusResponse>(
additionalProperties: false,
(response => response.Status.Trim(), StructuredOutputSchema.PlainText()));
var exception = await Assert.That(action).ThrowsException();
await Assert.That(exception).IsTypeOf<ArgumentException>();
}
}