// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Slick Quant
// https://github.com/SlickQuant/alpaca-cpp
//
// Integration tests against the live Market Data API.
//
// Skipped unless credentials are configured. Unlike the trading tests, these work with
// either paper or live keys: data.alpaca.markets is not account-scoped, so the paper pair
// (APCA_PAPER_API_*) and the unprefixed pair are both accepted.
// Every request is read-only and pinned to the IEX feed, which needs no data subscription.
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace alpaca::tests {
namespace {
/// A window well in the past, so the data exists and the result is stable over time.
constexpr const char *window_start = "2024-01-02";
constexpr const char *window_end = "2024-01-06";
template
auto run_awaitable(Awaitable &&awaitable) {
asio::io_context io;
auto future = asio::co_spawn(io, std::forward(awaitable), asio::use_future);
io.run();
return future.get();
}
} // namespace
class DataIntegration : public ::testing::Test {
protected:
static void SetUpTestSuite() {
// `data_client` defaults to the paper environment, so it resolves the paper pair
// first and falls back to the unprefixed one. Gate on the same resolution, or a
// paper-only setup would skip a group its credentials can in fact reach.
if (credentials::from_env(environment::paper).empty()) {
blocker_ = "APCA_PAPER_API_KEY_ID / APCA_PAPER_API_SECRET_KEY "
"(or APCA_API_KEY_ID / APCA_API_SECRET_KEY) not set";
return;
}
// Probe once so a credentials problem is reported as one clear reason rather than
// as a dozen unrelated-looking failures.
data_client probe;
probe.set_retry_policy({1, 0, 0});
try {
probe.get_stock_exchange_codes();
blocker_.clear();
}
catch (const api_error &e) {
blocker_ = std::format("market data unreachable (HTTP {}: {})", e.http_status, e.message);
}
catch (const std::exception &e) {
blocker_ = std::format("market data unreachable: {}", e.what());
}
}
void SetUp() override {
if (!blocker_.empty()) {
GTEST_SKIP()