using System.Net; using System.Text.Json; using Anubis.Data; using Anubis.Server; namespace Anubis.Tests; /// Market data inside the running server, on a fake network (issues #19, #20, #22). [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); // The news of Phase 7 run on the same network: their feeds and the pinned classifier model, nothing else. var news = Anubis.News.NewsStore.Defaults.Select(s => new Uri(s.Url).Host).Append(Anubis.News.ToneModel.Url.Host).ToHashSet(); 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" || news.Contains(u.Host)), 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 |"); // The settings page says which source is off and why, and edits the central bank list. var page = await server.TextAsync("/impostazioni"); StringAssert.Contains(page, "Alpaca (azioni, conto di prova)"); StringAssert.Contains(page, "mancano le chiavi del conto di prova Alpaca"); StringAssert.Contains(page, "FED 2026-12-09"); using (var wrong = await server.PostFormAsync("/impostazioni", "/azioni/impostazioni/calendar.central_banks", ("valore", "FED il 9 dicembre"), ("ritorno", "/impostazioni#dati"))) { Assert.AreEqual("/impostazioni?errore=riunioni#dati", ServerHarness.Location(wrong)); } using (var right = await server.PostFormAsync("/impostazioni", "/azioni/impostazioni/calendar.central_banks", ("valore", "FED 2027-01-27\r\nBCE 2027-02-04"), ("ritorno", "/impostazioni#dati"))) { Assert.AreEqual("/impostazioni#dati", ServerHarness.Location(right)); } StringAssert.Contains(await server.TextAsync("/impostazioni"), "BCE 2027-02-04"); } [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); } }