110 lines
3.2 KiB
C++
110 lines
3.2 KiB
C++
#pragma once
|
|
|
|
// Shared test rig: an AzureClient over RecordingTransports. A recorder only
|
|
// records what was sent and answers a fixed body (a documented example where
|
|
// a call parses its answer, "{}" otherwise); no vendor behavior is scripted.
|
|
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "clients/transport.h"
|
|
#include "framework.h"
|
|
#include "llm/error.h"
|
|
#include "vendors/azure/client.h"
|
|
|
|
namespace az_test {
|
|
|
|
using molo::azure::AzureClient;
|
|
using molo::azure::Channel;
|
|
using molo::azure::Settings;
|
|
using molo::clients::body::Json;
|
|
|
|
struct Seen {
|
|
std::string method;
|
|
std::string target; // path?query
|
|
std::string body;
|
|
molo::clients::HeaderMap headers;
|
|
};
|
|
|
|
class Recorder : public molo::clients::Transport {
|
|
public:
|
|
const char* name() const override { return "recorder"; }
|
|
molo::clients::Task<molo::clients::ClientResponse> send(const molo::clients::ClientRequest& r) override {
|
|
Seen s;
|
|
s.method = molo::clients::method_name(r.method());
|
|
s.target = r.url().target();
|
|
s.headers = r.headers();
|
|
if (r.has_body() && !r.is_stream_body()) s.body = r.body_bytes();
|
|
{
|
|
std::lock_guard<std::mutex> lock(mu);
|
|
seen.push_back(std::move(s));
|
|
}
|
|
molo::clients::HeaderMap h;
|
|
h.set("Content-Type", content_type);
|
|
co_return molo::clients::ClientResponse(status, h, answer);
|
|
}
|
|
std::mutex mu;
|
|
std::vector<Seen> seen;
|
|
std::string answer = "{}";
|
|
std::string content_type = "application/json";
|
|
int status = 200;
|
|
};
|
|
|
|
struct Rig {
|
|
std::shared_ptr<AzureClient> client;
|
|
std::map<Channel, Recorder*> rec;
|
|
|
|
explicit Rig(Settings s, molo::azure::EntraTokenSource::Clock clock = nullptr) {
|
|
client = AzureClient::create(
|
|
std::move(s),
|
|
[this](Channel ch) -> std::unique_ptr<molo::clients::Transport> {
|
|
auto r = std::make_unique<Recorder>();
|
|
rec[ch] = r.get();
|
|
return r;
|
|
},
|
|
std::move(clock));
|
|
}
|
|
Recorder& openai() { return *rec.at(Channel::kOpenAi); }
|
|
Recorder& foundry() { return *rec.at(Channel::kFoundry); }
|
|
Recorder& safety() { return *rec.at(Channel::kSafety); }
|
|
Recorder& token() { return *rec.at(Channel::kToken); }
|
|
const Seen& last() { return openai().seen.back(); }
|
|
Json last_body() { return Json::parse(openai().seen.back().body); }
|
|
};
|
|
|
|
inline Settings key_settings(const std::string& version = "") {
|
|
Settings s;
|
|
s.endpoint = "https://res.openai.azure.com";
|
|
s.foundry_endpoint = "https://res.services.ai.azure.com";
|
|
s.safety_endpoint = "https://res.cognitiveservices.azure.com";
|
|
s.api_key = "az-test-KEY";
|
|
s.safety_key = "az-safety-KEY";
|
|
s.api_version = molo::azure::ApiVersion::parse(version);
|
|
return s;
|
|
}
|
|
|
|
inline Settings entra_settings(const std::string& version = "") {
|
|
Settings s = key_settings(version);
|
|
s.api_key.clear();
|
|
s.safety_key.clear();
|
|
s.entra.tenant_id = "contoso.onmicrosoft.com";
|
|
s.entra.client_id = "11111111-2222-3333-4444-555555555555";
|
|
s.entra.client_secret = "s3cr3t-value";
|
|
return s;
|
|
}
|
|
|
|
template <typename F>
|
|
bool rejects(F&& fn, const std::string& needle = "") {
|
|
try {
|
|
fn();
|
|
} catch (const molo::llm::ApiError& e) {
|
|
return needle.empty() || e.message().find(needle) != std::string::npos;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
} // namespace az_test
|