Files
moloapp/tests/src/vendors/compat/support.h
T

64 lines
2.3 KiB
C++

#pragma once
// Shared rig for the compat tests: a Provider over a MockTransport with a
// fake key. The mock only records the requests the real client builds;
// replies are documented examples taken verbatim from OpenAI's API reference
// (the shape every row shares) and labelled FIXTURE where they appear.
// Nothing here verifies a provider's behavior (see SPEC.md ## Unverified).
#include <memory>
#include <string>
#include "clients/transports/mock_transport.h"
#include "framework.h"
#include "vendors/compat/provider.h"
namespace compat_test {
using molo::clients::ClientResponse;
using molo::clients::HeaderMap;
using molo::clients::Method;
using molo::clients::transports::MockTransport;
using Json = molo::clients::body::Json;
inline ClientResponse reply(const std::string& body, int status = 200,
const std::string& type = "application/json") {
HeaderMap h;
h.set("Content-Type", type);
return ClientResponse(status, h, body);
}
inline ClientResponse sse(const std::string& body) { return reply(body, 200, "text/event-stream"); }
inline const char* kKey = "sk-TESTKEY-DO-NOT-LEAK";
inline bool has(const std::string& hay, const std::string& needle) { return hay.find(needle) != std::string::npos; }
// "https://host/a/b" -> "/a/b"
inline std::string path_of(const std::string& url) {
std::size_t scheme = url.find("://");
std::size_t slash = url.find('/', scheme == std::string::npos ? 0 : scheme + 3);
return slash == std::string::npos ? "" : url.substr(slash);
}
struct Rig {
MockTransport* transport = nullptr;
molo::compat::Provider provider;
explicit Rig(const std::string& id, const std::string& key = kKey, const std::string& base_override = "",
const std::string& account = "acct123")
: provider(make(id, key, base_override, account, &transport)) {}
std::string path(const char* suffix) const { return path_of(provider.base_url()) + suffix; }
private:
static molo::compat::Provider make(const std::string& id, const std::string& key, const std::string& base,
const std::string& account, MockTransport** out) {
auto t = std::make_unique<MockTransport>();
*out = t.get();
return molo::compat::Provider::with(id, std::move(t), key, base, account);
}
};
} // namespace compat_test