Repository navigation
Expand file tree
/
Copy pathtypes_test.cpp
More file actions
167 lines (139 loc) · 5.02 KB
/
Copy pathtypes_test.cpp
File metadata and controls
167 lines (139 loc) · 5.02 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2026 Bontal LLC
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
#include "typesafe/types.h"
#include <gtest/gtest.h>
#include <nlohmann/json.hpp>
#include "typesafe/testing.h"
namespace
{
using namespace typesafe;
TEST(ChoiceSerialization, WritesTypeAndCriteria)
{
const Choice question{"What is the tone?",
{{"angry", "An upset or hostile message"},
{"calm", nlohmann::json{{"note", "polite"}}}}};
const nlohmann::json wire = question;
EXPECT_EQ(wire.at("type"), "choice");
EXPECT_EQ(wire.at("criteria").at("angry"), "An upset or hostile message");
EXPECT_TRUE(wire.at("criteria").at("calm").is_object());
}
TEST(ChoiceSerialization, OmitsInstructionsWhenAbsent)
{
const nlohmann::json wire = Choice{std::nullopt, {{"yes", std::nullopt}}};
EXPECT_FALSE(wire.contains("instructions"));
}
TEST(ScoreSerialization, KeepsCriteriaOrder)
{
const nlohmann::json wire
= Score{"How urgent?",
{"can wait", "this week", nlohmann::json{{"label", "today"}}}};
EXPECT_EQ(wire.at("type"), "score");
EXPECT_TRUE(wire.at("criteria").is_array());
EXPECT_EQ(wire.at("criteria").size(), 3u);
EXPECT_EQ(wire.at("criteria").at(1), "this week");
}
TEST(NoulSerialization, WritesCriteriaUnderTrueAndFalse)
{
const nlohmann::json wire = Noul{
std::nullopt, NoulCriteria{nlohmann::json("unsolicited advertising"),
nlohmann::json("a real conversation")}};
EXPECT_EQ(wire.at("type"), "noul");
EXPECT_EQ(wire.at("criteria").at("true"), "unsolicited advertising");
EXPECT_EQ(wire.at("criteria").at("false"), "a real conversation");
}
ScoreAnswer Decoded(nlohmann::json answer)
{
return (answer.get<ScoreAnswer>());
}
TEST(ScoreAnswerDecoding, ReadsStructuredLegendAndProbabilities)
{
const ScoreAnswer answer = Decoded({
{"type", "score"},
{"score", 1.7},
{"confidence", 0.9},
{"legend", {{"0", "bad"}, {"1", nlohmann::json{{"label", "ok"}}}}},
{"probabilities", {{"0", 0.1}, {"1", 0.9}}},
});
EXPECT_DOUBLE_EQ(answer.score, 1.7);
EXPECT_DOUBLE_EQ(answer.confidence, 0.9);
EXPECT_EQ(answer.legend.at(0), "bad");
EXPECT_EQ(answer.legend.at(1).at("label"), "ok");
EXPECT_DOUBLE_EQ(answer.probabilities.at(1), 0.9);
}
TEST(ScoreAnswerDecoding, RequiresEveryMandatoryField)
{
EXPECT_THROW(Decoded({{"type", "score"}, {"score", 1.7}}),
nlohmann::json::exception);
EXPECT_THROW(Decoded({{"type", "score"},
{"score", 1.7},
{"confidence", 0.9},
{"probabilities", {{"0", 1.0}}}}),
nlohmann::json::exception);
}
TEST(ScoreAnswerDecoding, RejectsNonIntegerLegendKeys)
{
EXPECT_THROW(Decoded({{"type", "score"},
{"score", 1.7},
{"confidence", 0.9},
{"legend", {{"zero", "bad"}}},
{"probabilities", {{"0", 1.0}}}}),
nlohmann::json::exception);
}
TEST(ScoreAnswerDecoding, RejectsValuesTheSchemaDoesNotAllow)
{
EXPECT_THROW(Decoded({{"type", "score"},
{"score", 1.7},
{"confidence", 0.9},
{"legend", {{"0", 5}}},
{"probabilities", {{"0", 1.0}}}}),
nlohmann::json::exception);
}
TEST(ResponseDecoding, DispatchesEachAnswerByType)
{
const SystemOneResponse response = nlohmann::json::parse(R"json({
"model": "jev-1.13.0",
"usage": {"input_tokens": 1, "output_tokens": 2},
"answers": {
"billing": {"type": "noul", "noul": 0.98},
"tone": {
"type": "choice",
"choice": "angry",
"confidence": 0.8,
"probabilities": {"angry": 0.8, "calm": 0.2}
},
"urgency": {
"type": "score",
"score": 2.0,
"confidence": 0.7,
"legend": {"0": "can wait", "1": "soon", "2": "now"},
"probabilities": {"0": 0.0, "1": 0.2, "2": 0.8}
}
}
})json")
.get<SystemOneResponse>();
EXPECT_EQ(response.model, "jev-1.13.0");
EXPECT_EQ(response.usage.input_tokens, 1);
EXPECT_DOUBLE_EQ(response.nouls.at("billing").noul, 0.98);
EXPECT_EQ(response.choices.at("tone").choice, "angry");
EXPECT_DOUBLE_EQ(response.choices.at("tone").confidence, 0.8);
EXPECT_DOUBLE_EQ(response.scores.at("urgency").score, 2.0);
}
TEST(ResponseDecoding, RejectsAnEmptyAnswersObject)
{
const nlohmann::json response{
{"model", "jev-1.13.0"},
{"usage", {{"input_tokens", 1}, {"output_tokens", 2}}},
{"answers", nlohmann::json::object()}};
EXPECT_THROW(response.get<SystemOneResponse>(), nlohmann::json::exception);
}
TEST(ResponseDecoding, RejectsAMissingAnswersObject)
{
const nlohmann::json response{
{"model", "jev-1.13.0"},
{"usage", {{"input_tokens", 1}, {"output_tokens", 2}}}};
EXPECT_THROW(response.get<SystemOneResponse>(), nlohmann::json::exception);
}
} // namespace