-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.h
More file actions
92 lines (79 loc) · 2.44 KB
/
Copy pathtesting.h
File metadata and controls
92 lines (79 loc) · 2.44 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
// 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.
#pragma once
#include <chrono>
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <thread>
#include <utility>
#include <vector>
#include "typesafe/client.h"
namespace typesafe::testing
{
/**
* @brief A transport that records every request and replays a queue of
* canned responses.
*
* An empty queue makes the request throw, as a connection failure would.
* `delay` sleeps 20 ms per request. Not synchronized: one instance per test,
* no concurrent requests.
*/
struct RecordingTransport final : Transport
{
std::vector<HttpResponse> responses;
std::vector<HttpRequest> requests;
bool delay = false;
HttpResponse request(const HttpRequest &request) override
{
requests.push_back(request);
if (delay)
std::this_thread::sleep_for(std::chrono::milliseconds(20));
if (responses.empty())
throw std::runtime_error(
"connection failed (recording transport)");
HttpResponse response = responses.front();
responses.erase(responses.begin());
return (response);
}
};
/** @brief A well-formed 200 carrying the given answers object. */
inline HttpResponse ValidResponse(nlohmann::json answers)
{
return HttpResponse{
200,
nlohmann::json{
{"model", "jev-1.13.0"},
{"answers", std::move(answers)},
{"usage", {{"input_tokens", 120}, {"output_tokens", 12}}}}
.dump(),
{}};
}
/** @brief A well-formed 200 from GET /v1/models. */
inline HttpResponse ModelsResponse()
{
nlohmann::json alias
= nlohmann::json{{"name", "jev-latest"},
{"description", "General-purpose system one model."},
{"release_date", "2026-09-15"}};
nlohmann::json versioned
= nlohmann::json{{"name", "jev-1.13.0"},
{"description", "Versioned System One model."},
{"release_date", "2026-09-15"}};
return (HttpResponse{
200,
nlohmann::json{{"models", nlohmann::json::array({alias, versioned})}}
.dump(),
{}});
}
/** @brief The smallest request that passes client-side validation. */
inline SystemOneRequest BasicRequest()
{
SystemOneRequest request;
request.state = "hello";
request.add("safe", Noul{"Is this safe?", std::nullopt});
return (request);
}
} // namespace typesafe::testing