Files
Anubis/tests/Anubis.Tests/MarketDataServerTests.cs
T
Alby96andClaude Opus 5.5 1e71038209 Run market data inside the server (refs #19 #20 #22 #24)
MarketDataService wires the collector, the reference sources, the
universes and the corporate actions into the scheduler. Short jobs (rates,
calendar, mood, spread samples, corporate actions) run on their schedule;
long downloads (catalogues, the first history, thousands of stocks, the
Binance archives) run on a background worker the jobs only wake, so they
never delay the backup or each other. The live crypto feed follows the
crypto universe, or BTC, ETH and SOL until one exists.

Alpaca uses only the paper account keys; without them the stock part is off
and the sources list says why. Market data is off in demo mode, with
ANUBIS_MARKET_DATA=off, and in tests that bring no fake network.

New: GET /api/data/quality (JSON or Markdown) and /api/data/sources, and
"anubis data report|binance|csv" on the command line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:42:10 +01:00

68 lines
3.9 KiB
C#

using System.Net;
using System.Text.Json;
using Anubis.Data;
using Anubis.Server;
namespace Anubis.Tests;
/// <summary>Market data inside the running server, on a fake network (issues #19, #20, #22).</summary>
[TestClass]
public sealed class MarketDataServerTests
{
[TestMethod]
[TestCategory(Category.Integration)]
public async Task ServerCollectsWithoutAlpacaKeysAndSaysWhatIsOff()
{
var empty = """{"code":"0","msg":"","data":[]}""";
var network = new FakeHttp()
.On("/api/v5/public/instruments?instType=SPOT", Fixture.Text("okx/instruments-spot.json"))
.On("candles", empty)
.On("/market/ticker?", Fixture.Text("okx/ticker-btc-eur.json"))
.On(_ => true, _ => FakeHttp.Respond("not here", HttpStatusCode.NotFound));
await using var server = await ServerHarness.StartAsync("docker", ServerHarness.DockerSqlite(), marketHttp: network);
await server.SignInFirstTimeAsync();
var upcoming = server.Host.Services!.Scheduler.Upcoming(100).Select(u => u.Name).ToList();
CollectionAssert.IsSubsetOf(new[] { "backup", "dati-storico", "dati-cambi-mattina", "dati-cambi-pomeriggio", "dati-calendario", "dati-umore", "dati-spread" },
upcoming);
Assert.IsFalse(upcoming.Contains("dati-azioni-chiusura"), "without the broker's calendar the stock jobs have no time");
// The first history round runs at start: OKX answers, Alpaca is off for lack of the paper account keys.
JsonElement sources = default;
await OkxTests.WaitForAsync(() =>
{
sources = JsonDocument.Parse(server.TextAsync("/api/data/sources").GetAwaiter().GetResult()).RootElement;
return sources.GetProperty("sources").EnumerateArray().Any(s => s.GetProperty("source").GetString() == MarketDataService.HistorySource);
}).ConfigureAwait(false);
Assert.IsTrue(sources.GetProperty("enabled").GetBoolean());
var bySource = sources.GetProperty("sources").EnumerateArray().ToDictionary(s => s.GetProperty("source").GetString()!);
Assert.IsFalse(bySource[MarketDataService.AlpacaSource].GetProperty("enabled").GetBoolean());
StringAssert.Contains(bySource[MarketDataService.AlpacaSource].GetProperty("note").GetString(), "ALPACA_API_KEY_DEMO");
StringAssert.Contains(bySource[MarketDataService.OkxSource].GetProperty("note").GetString(), "3 nuovi");
Assert.AreEqual(JsonValueKind.Null, bySource[MarketDataService.OkxSource].GetProperty("lastError").ValueKind);
Assert.IsTrue(network.Urls.All(u => u.Host is "eea.okx.com" or "data.binance.vision" or "nfs.faireconomy.media" or "www.ecb.europa.eu"
or "api.alternative.me" or "cdn.cboe.com"), string.Join(", ", network.Urls.Select(u => u.Host).Distinct()));
Assert.IsFalse(network.Urls.Any(u => u.Host.Contains("alpaca", StringComparison.Ordinal)), "no call to Alpaca without keys");
var report = await server.TextAsync("/api/data/quality?format=md");
StringAssert.Contains(report, "# Qualità dei dati");
StringAssert.Contains(report, "| alpaca | no |");
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task MarketDataStaysOffWhenSwitchedOff()
{
var env = ServerHarness.DockerSqlite();
env["ANUBIS_MARKET_DATA"] = "off";
var network = new FakeHttp();
await using var server = await ServerHarness.StartAsync("docker", env, marketHttp: network);
await server.SignInFirstTimeAsync();
Assert.IsNull(server.Host.Services!.MarketData);
Assert.IsFalse(server.Host.Services.Scheduler.Upcoming(100).Any(u => u.Name.StartsWith("dati-", StringComparison.Ordinal)));
var sources = JsonDocument.Parse(await server.TextAsync("/api/data/sources")).RootElement;
Assert.IsFalse(sources.GetProperty("enabled").GetBoolean());
Assert.AreEqual(0, network.Requests.Count);
}
}