|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <coinbase/utils.hpp> |
| 3 | +#include <coinbase/market_data.hpp> |
| 4 | +#include <nlohmann/json.hpp> |
| 5 | + |
| 6 | +using json = nlohmann::json; |
| 7 | + |
| 8 | +namespace coinbase::tests { |
| 9 | + |
| 10 | +class TimestampParsingTests : public ::testing::Test {}; |
| 11 | + |
| 12 | +TEST_F(TimestampParsingTests, ParseL2UpdateMessage) { |
| 13 | + // Real L2 update message from Coinbase WebSocket |
| 14 | + std::string msg = R"({ |
| 15 | + "channel":"l2_data", |
| 16 | + "timestamp":"2026-03-05T09:05:32.483569449Z", |
| 17 | + "sequence_num":229, |
| 18 | + "events":[{ |
| 19 | + "type":"update", |
| 20 | + "product_id":"BIP-20DEC30-CDE", |
| 21 | + "updates":[ |
| 22 | + {"side":"bid","event_time":"2026-03-05T09:05:32.449176Z","price_level":"72575","new_quantity":"0"}, |
| 23 | + {"side":"bid","event_time":"2026-03-05T09:05:32.449176Z","price_level":"72570","new_quantity":"68"}, |
| 24 | + {"side":"offer","event_time":"2026-03-05T09:05:32.449176Z","price_level":"72580","new_quantity":"13"} |
| 25 | + ] |
| 26 | + }] |
| 27 | + })"; |
| 28 | + |
| 29 | + auto j = json::parse(msg); |
| 30 | + |
| 31 | + // Test main timestamp parsing (nanoseconds - 9 fractional digits) |
| 32 | + ASSERT_TRUE(j.contains("timestamp")); |
| 33 | + uint64_t main_timestamp = nanoseconds_from_json(j, "timestamp"); |
| 34 | + |
| 35 | + // Expected: 2026-03-05T09:05:32.483569449Z |
| 36 | + // Breakdown: 2026-03-05 09:05:32 = Unix timestamp base |
| 37 | + // .483569449 = 483569449 nanoseconds |
| 38 | + |
| 39 | + // The timestamp should be in nanoseconds since it has 9 fractional digits |
| 40 | + EXPECT_EQ(main_timestamp, 1772701532483569449ULL); // Exact nanosecond timestamp |
| 41 | + |
| 42 | + // Test Level2Update parsing (microseconds - 6 fractional digits) |
| 43 | + ASSERT_TRUE(j.contains("events")); |
| 44 | + ASSERT_GT(j["events"].size(), 0); |
| 45 | + |
| 46 | + Level2UpdateBatch batch = j["events"][0]; |
| 47 | + ASSERT_EQ(batch.product_id, "BIP-20DEC30-CDE"); |
| 48 | + ASSERT_EQ(batch.updates.size(), 3); |
| 49 | + |
| 50 | + // Expected: 2026-03-05T09:05:32.449176Z |
| 51 | + // .449176 = 6 fractional digits = microseconds |
| 52 | + |
| 53 | + EXPECT_EQ(batch.updates[0].event_time, 1772701532449176000ULL); // Exact microsecond timestamp |
| 54 | + |
| 55 | + // Verify side parsing |
| 56 | + EXPECT_EQ(batch.updates[0].side, Side::BUY); // "bid" = BUY |
| 57 | + |
| 58 | + // Verify price and quantity parsing |
| 59 | + EXPECT_DOUBLE_EQ(batch.updates[0].price_level, 72575.0); |
| 60 | + EXPECT_DOUBLE_EQ(batch.updates[0].new_quantity, 0.0); |
| 61 | + |
| 62 | + // Test the third update (offer side) |
| 63 | + EXPECT_EQ(batch.updates[2].side, Side::SELL); // "offer" = SELL |
| 64 | + EXPECT_DOUBLE_EQ(batch.updates[2].price_level, 72580.0); |
| 65 | + EXPECT_DOUBLE_EQ(batch.updates[2].new_quantity, 13.0); |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace coinbase::tests |
0 commit comments