News is the first input of Phase 7. Every two minutes the "notizie" job asks each enabled feed conditionally and backs off a failing one up to an hour. - Parsing: RSS 2.0, RSS 1.0 and Atom are read without DTDs or entity expansion, so a hostile feed reads nothing from disk. - Deduplication: articles are deduplicated by canonical URL. - Grouping: retold titles join one story by word similarity within 48 hours, and independent publishers are counted by registrable domain. - Linking: stories are linked to instruments by source symbols, cashtags, exchange tickers, coin tickers and capitalised company names; to the crypto or equity class or to the market by theme; to the source topic as a fallback. ADR 0014 records the thresholds. The tests read a synthetic archive in each source's real format and run on both engines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
273 lines
18 KiB
C#
273 lines
18 KiB
C#
using System.Net;
|
||
using Anubis.Core;
|
||
using Anubis.Data;
|
||
using Anubis.News;
|
||
|
||
namespace Anubis.Tests;
|
||
|
||
/// <summary>Collection, cleaning, grouping and linking of the news (SPEC 10.2, points 1–3, issue #56).</summary>
|
||
[TestClass]
|
||
public sealed class NewsTests
|
||
{
|
||
private static readonly DateTimeOffset Now = new(2026, 11, 14, 18, 0, 0, TimeSpan.Zero);
|
||
|
||
private static readonly string[] Archive =
|
||
["cnbc", "marketwatch", "coindesk", "cointelegraph", "investing", "fxstreet", "sole24ore", "ansa", "fed", "atom"];
|
||
|
||
private static readonly InstrumentSpec[] Instruments =
|
||
[
|
||
new(Venues.Alpaca, "AAPL", "Apple Inc. Common Stock", AssetClasses.Equity, "USD"),
|
||
new(Venues.Alpaca, "NVDA", "NVIDIA Corporation Common Stock", AssetClasses.Equity, "USD"),
|
||
new(Venues.Alpaca, "TSLA", "Tesla, Inc. Common Stock", AssetClasses.Equity, "USD"),
|
||
new(Venues.Alpaca, "TGT", "Target Corporation Common Stock", AssetClasses.Equity, "USD"),
|
||
new(Venues.Alpaca, "MSFT", "Microsoft Corporation Common Stock", AssetClasses.Equity, "USD"),
|
||
new(Venues.Alpaca, "AMZN", "Amazon.com, Inc. Common Stock", AssetClasses.Equity, "USD"),
|
||
new(Venues.Alpaca, "SPY", "SPDR S&P 500 ETF Trust", AssetClasses.Equity, "USD") { IsFund = true },
|
||
new(Venues.Okx, "BTC-EUR", "BTC-EUR", AssetClasses.Crypto, "EUR") { BaseCurrency = "BTC" },
|
||
new(Venues.Okx, "ETH-EUR", "ETH-EUR", AssetClasses.Crypto, "EUR") { BaseCurrency = "ETH" },
|
||
new(Venues.Okx, "SOL-EUR", "SOL-EUR", AssetClasses.Crypto, "EUR") { BaseCurrency = "SOL" },
|
||
];
|
||
|
||
[TestMethod]
|
||
public void FeedDatesAreReadInEveryFormTheSourcesUse()
|
||
{
|
||
var expected = new DateTimeOffset(2026, 11, 14, 16, 46, 11, TimeSpan.Zero);
|
||
foreach (var text in new[]
|
||
{
|
||
"Sat, 14 Nov 2026 16:46:11 GMT", "Sat, 14 Nov 2026 16:46:11 +0000", "Sat, 14 Nov 2026 17:46:11 +0100", "Sat, 14 Nov 2026 16:46:11 Z",
|
||
"Sat, 14 Nov 2026 11:46:11 EST", "14 Nov 2026 16:46:11 UT", "2026-11-14T16:46:11Z", "2026-11-14T17:46:11+01:00", "2026-11-14 16:46:11",
|
||
" Sat, 14 Nov 2026 16:46:11 GMT ",
|
||
})
|
||
{
|
||
Assert.IsTrue(NewsDates.TryParse(text, out var at), text);
|
||
Assert.AreEqual(expected, at, text);
|
||
Assert.AreEqual(TimeSpan.Zero, at.Offset, text);
|
||
}
|
||
|
||
Assert.IsFalse(NewsDates.TryParse("ieri sera", out _));
|
||
Assert.IsFalse(NewsDates.TryParse(null, out _));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void TheSameArticleHasOneAddressAndAPublisherIsCountedOnce()
|
||
{
|
||
var key = NewsMatching.UrlKey("https://www.coindesk.com/markets/2026/11/14/bitcoin-drifts-below-90000?utm_medium=referral&utm_source=rss");
|
||
Assert.AreEqual("coindesk.com/markets/2026/11/14/bitcoin-drifts-below-90000", key);
|
||
Assert.AreEqual(key, NewsMatching.UrlKey("http://coindesk.com//markets/2026/11/14/bitcoin-drifts-below-90000/#comments"));
|
||
Assert.AreEqual("example.org/a?id=7&page=2", NewsMatching.UrlKey("https://example.org/a/?page=2&id=7&fbclid=x&mod=rss"));
|
||
Assert.AreNotEqual(NewsMatching.UrlKey("https://example.org/a?id=7"), NewsMatching.UrlKey("https://example.org/a?id=8"));
|
||
|
||
Assert.AreEqual("marketwatch.com", NewsMatching.Publisher("https://www.marketwatch.com/story/x"));
|
||
Assert.AreEqual("cnbc.com", NewsMatching.Publisher("https://search.cnbc.com/rs/x"));
|
||
Assert.AreEqual("bbc.co.uk", NewsMatching.Publisher("https://news.bbc.co.uk/x"));
|
||
Assert.AreEqual("ilsole24ore.com", NewsMatching.Publisher("https://www.ilsole24ore.com/art/x"));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void TitlesThatRetellAStoryAreTheSameStoryAndOthersAreNot()
|
||
{
|
||
bool Same(string a, string b) => NewsMatching.Similar(NewsMatching.Words(a), NewsMatching.Words(b));
|
||
Assert.IsTrue(Same("Apple shares jump after record iPhone sales in China", "Apple Shares Jump After Record iPhone Sales In China"));
|
||
Assert.IsTrue(Same("Bitcoin drifts below $90,000 as traders await U.S. data", "Bitcoin drifts below $90K as traders await US data"));
|
||
Assert.IsTrue(Same("Fed holds rates steady, signals two cuts next year", "Fed holds rates steady and signals two cuts next year, Powell says"));
|
||
Assert.IsTrue(Same("L'inflazione è vicina all'obiettivo", "L’inflazione e vicina all’obiettivo"));
|
||
Assert.IsFalse(Same("Apple shares rise after earnings", "Apple shares fall after lawsuit"));
|
||
Assert.IsFalse(Same("Bitcoin falls", "Bitcoin rises"), "Two short titles must be equal.");
|
||
Assert.IsFalse(Same("Tesla recalls vehicles over a software issue", "Tesla delivers a record number of vehicles"));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void FeedsInRssAndAtomAreReadAndAHostileOneReadsNothingFromDisk()
|
||
{
|
||
var atom = FeedParser.Parse(Fixture.Bytes("news/atom.xml"));
|
||
Assert.HasCount(1, atom, "An entry without a link is skipped.");
|
||
Assert.AreEqual("Microsoft & OpenAI sign a new cloud deal", atom[0].Title);
|
||
Assert.AreEqual("en", atom[0].Language);
|
||
Assert.AreEqual(new DateTimeOffset(2026, 11, 14, 12, 0, 0, TimeSpan.Zero), atom[0].PublishedAt);
|
||
|
||
var coindesk = FeedParser.Parse(Fixture.Bytes("news/coindesk.xml"));
|
||
Assert.AreEqual("BTC fell 2% while ETH held steady.", coindesk[0].Summary, "Tags are taken out of the summary.");
|
||
Assert.AreEqual("it", FeedParser.Parse(Fixture.Bytes("news/sole24ore.xml"))[0].Language);
|
||
Assert.AreEqual("Vendite sulle criptovalute in attesa dell'inflazione americana.", FeedParser.Parse(Fixture.Bytes("news/sole24ore.xml"))[0].Summary);
|
||
Assert.AreEqual(new DateTimeOffset(2026, 11, 14, 1, 17, 16, TimeSpan.Zero), FeedParser.Parse(Fixture.Bytes("news/fxstreet.xml"))[0].PublishedAt);
|
||
|
||
try
|
||
{
|
||
var hostile = FeedParser.Parse(Fixture.Bytes("news/hostile.xml"));
|
||
Assert.IsTrue(hostile.All(i => !(i.Summary ?? "").Contains("[fonts]", StringComparison.OrdinalIgnoreCase) && (i.Summary ?? "").Length < 1000));
|
||
}
|
||
catch (FormatException)
|
||
{
|
||
// Refusing the document is as good as reading it without its entities.
|
||
}
|
||
|
||
Assert.ThrowsExactly<FormatException>(() => FeedParser.Parse("<html><body>not a feed</body></html>"u8.ToArray()));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void ArticlesAreLinkedToTheRightInstrumentsAndOrdinaryWordsAreNot()
|
||
{
|
||
var linker = NewsLinker.Build(Instruments);
|
||
string[] Targets(string title, string? summary = null, string? topic = null, params string[] symbols) =>
|
||
[.. linker.Link(title, summary, symbols, topic).Select(l => l.Target).Order(StringComparer.Ordinal)];
|
||
|
||
CollectionAssert.AreEqual(new[] { "alpaca:AAPL" }, Targets("Apple shares jump after record iPhone sales"));
|
||
CollectionAssert.AreEqual(new[] { "alpaca:AAPL" }, Targets("Apple's suppliers report a strong quarter"));
|
||
CollectionAssert.AreEqual(Array.Empty<string>(), Targets("why your apple pie recipe needs more cinnamon"));
|
||
CollectionAssert.AreEqual(Array.Empty<string>(), Targets("Target cuts its holiday outlook"), "Target is an ordinary word without its ticker.");
|
||
CollectionAssert.AreEqual(new[] { "alpaca:TGT" }, Targets("Target cuts its holiday outlook (NYSE: TGT)"));
|
||
CollectionAssert.AreEqual(new[] { "alpaca:NVDA" }, Targets("Fire at a chip plant hits Nvidia supply, $NVDA slides"));
|
||
CollectionAssert.AreEqual(new[] { "alpaca:AMZN" }, Targets("Amazon opens a new warehouse"));
|
||
CollectionAssert.AreEqual(new[] { "alpaca:TSLA" }, Targets("Recall", null, null, "TSLA", "XYZ"), "Symbols from the source, unknown ones left out.");
|
||
CollectionAssert.AreEqual(new[] { "okx:BTC-EUR", "okx:ETH-EUR" }, Targets("Bitcoin drifts lower", "BTC fell while ETH held steady"));
|
||
CollectionAssert.AreEqual(new[] { "okx:SOL-EUR" }, Targets("Solana tokens drained in a hack"));
|
||
CollectionAssert.AreEqual(new[] { "crypto", "market", "okx:BTC-EUR" }, Targets("Il Bitcoin scende", "Vendite sulle criptovalute in attesa dell'inflazione"));
|
||
CollectionAssert.AreEqual(new[] { "market" }, Targets("Fed officials signal patience on interest rates"));
|
||
CollectionAssert.AreEqual(new[] { "equity" }, Targets("Wall Street closes higher on Friday"));
|
||
CollectionAssert.AreEqual(new[] { "crypto" }, Targets("Weekly wrap", null, AssetClasses.Crypto), "The topic of the source, when nothing else is found.");
|
||
CollectionAssert.AreEqual(new[] { "market" }, Targets("Agencies announce results", null, LinkScopes.Market));
|
||
CollectionAssert.AreEqual(Array.Empty<string>(), Targets("SPDR S&P funds see inflows"), "Funds are never linked as instruments.");
|
||
|
||
Assert.AreEqual("Apple", NewsLinker.CompanyName("Apple Inc. Common Stock"));
|
||
Assert.AreEqual("Meta Platforms", NewsLinker.CompanyName("Meta Platforms, Inc. Class A Common Stock"));
|
||
Assert.AreEqual("Goldman Sachs", NewsLinker.CompanyName("The Goldman Sachs Group, Inc. Common Stock")?.Replace("The ", "", StringComparison.Ordinal));
|
||
Assert.IsNull(NewsLinker.CompanyName("Target Corporation Common Stock"));
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Integration)]
|
||
public async Task TheRecordedArchiveIsGroupedAndLinkedOnBothEngines()
|
||
{
|
||
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
|
||
var clock = new ManualClock(Now);
|
||
await new InstrumentCatalog(test.Db, clock).SyncAsync(Venues.Alpaca, [.. Instruments.Where(i => i.Venue == Venues.Alpaca)], complete: false, CancellationToken.None)
|
||
.ConfigureAwait(false);
|
||
await new InstrumentCatalog(test.Db, clock).SyncAsync(Venues.Okx, [.. Instruments.Where(i => i.Venue == Venues.Okx)], complete: false, CancellationToken.None)
|
||
.ConfigureAwait(false);
|
||
var store = new NewsStore(test.Db, clock);
|
||
await store.EnsureDefaultsAsync(CancellationToken.None).ConfigureAwait(false);
|
||
await store.EnsureDefaultsAsync(CancellationToken.None).ConfigureAwait(false);
|
||
var sources = (await store.ListSourcesAsync(CancellationToken.None).ConfigureAwait(false)).ToDictionary(s => s.Name);
|
||
Assert.HasCount(NewsStore.Defaults.Count, sources, "The defaults are written once.");
|
||
Assert.IsTrue(sources["CNBC"].Trusted);
|
||
Assert.IsFalse(sources["Cointelegraph"].Trusted);
|
||
var collector = new NewsCollector(new HttpClient(), test.Db, clock);
|
||
var linker = await collector.DictionaryAsync(CancellationToken.None).ConfigureAwait(false);
|
||
var bySource = new Dictionary<string, string>
|
||
{
|
||
["cnbc"] = "CNBC",
|
||
["marketwatch"] = "MarketWatch",
|
||
["coindesk"] = "CoinDesk",
|
||
["cointelegraph"] = "Cointelegraph",
|
||
["investing"] = "Investing.com",
|
||
["fxstreet"] = "FXStreet",
|
||
["sole24ore"] = "Il Sole 24 Ore",
|
||
["ansa"] = "ANSA",
|
||
["fed"] = "Federal Reserve",
|
||
["atom"] = "Decrypt",
|
||
};
|
||
|
||
var results = new Dictionary<string, IngestResult>();
|
||
foreach (var name in Archive)
|
||
{
|
||
results[name] = await store.IngestAsync(sources[bySource[name]], FeedParser.Parse(Fixture.Bytes($"news/{name}.xml")), linker, CancellationToken.None)
|
||
.ConfigureAwait(false);
|
||
}
|
||
|
||
Assert.AreEqual(new IngestResult(4, 0, 4, 1), results["cnbc"], "The story of 9 November is history, not news.");
|
||
Assert.AreEqual(1, results["cointelegraph"].Duplicates, "The repost of the CoinDesk article, with other tracking, is the same article.");
|
||
var again = await store.IngestAsync(sources["CNBC"], FeedParser.Parse(Fixture.Bytes("news/cnbc.xml")), linker, CancellationToken.None).ConfigureAwait(false);
|
||
Assert.AreEqual(new IngestResult(0, 4, 0, 1), again, "A second read of the same feed adds nothing.");
|
||
|
||
var stories = await test.Db.ReadAsync(s => s.QueryAsync("SELECT id, title, sources, trusted FROM news_stories",
|
||
r => (Id: r.Id("id"), Title: r.Text("title"), Sources: (int)r.Number("sources"), Trusted: r.Bool("trusted")))).ConfigureAwait(false);
|
||
var links = await test.Db.ReadAsync(s => s.QueryAsync("SELECT story_id, target, via FROM news_links",
|
||
r => (Story: r.Id("story_id"), Target: r.Text("target"), Via: r.Text("via")))).ConfigureAwait(false);
|
||
Assert.AreEqual(19L, await test.Db.ReadAsync(s => s.CountAsync("SELECT COUNT(*) FROM news_items")).ConfigureAwait(false));
|
||
Assert.HasCount(17, stories, "Apple told by CNBC and MarketWatch, Bitcoin by CoinDesk and Cointelegraph: two stories with two articles each.");
|
||
|
||
(Guid Id, string Title, int Sources, bool Trusted) Story(string start) => stories.Single(s => s.Title.StartsWith(start, StringComparison.Ordinal));
|
||
void Expect(string[] expected, string start)
|
||
{
|
||
string[] actual = [.. links.Where(l => l.Story == Story(start).Id).Select(l => l.Target).Order(StringComparer.Ordinal)];
|
||
CollectionAssert.AreEqual(expected, actual, $"{start}: {string.Join(", ", actual)}");
|
||
}
|
||
|
||
|
||
Assert.AreEqual((2, true), (Story("Apple shares jump").Sources, Story("Apple shares jump").Trusted));
|
||
Expect(new[] { "alpaca:AAPL" }, "Apple shares jump");
|
||
Assert.AreEqual((2, true), (Story("Bitcoin drifts below $90,000").Sources, Story("Bitcoin drifts below $90,000").Trusted));
|
||
Expect(new[] { "okx:BTC-EUR", "okx:ETH-EUR" }, "Bitcoin drifts below $90,000");
|
||
Assert.AreEqual((1, false), (Story("Rumor: a large fund").Sources, Story("Rumor: a large fund").Trusted), "One untrusted source: it waits for a second one.");
|
||
Expect(new[] { "okx:ETH-EUR" }, "Rumor: a large fund");
|
||
Expect(new[] { "alpaca:NVDA" }, "Fire at a chip plant");
|
||
Expect(Array.Empty<string>(), "Target cuts");
|
||
Expect(Array.Empty<string>(), "Opinion: why your apple pie");
|
||
Expect(new[] { "alpaca:TSLA" }, "Tesla recalls");
|
||
Expect(new[] { "market" }, "Fed officials");
|
||
Expect(new[] { "market" }, "EUR/USD holds gains");
|
||
Expect(new[] { "market" }, "Bce, Lagarde");
|
||
Expect(new[] { "market" }, "Agencies announce");
|
||
Expect(new[] { "crypto" }, "Weekly wrap");
|
||
Expect(new[] { "crypto" }, "New stablecoin rules");
|
||
Expect(new[] { "okx:SOL-EUR" }, "Exchange halts withdrawals");
|
||
Expect(new[] { "crypto", "market", "okx:BTC-EUR" }, "Il Bitcoin scende");
|
||
Expect(Array.Empty<string>(), "Borse europee");
|
||
Expect(new[] { "alpaca:MSFT" }, "Microsoft & OpenAI");
|
||
|
||
var languages = await test.Db.ReadAsync(s => s.QueryAsync("SELECT language AS l, COUNT(*) AS n FROM news_items GROUP BY language",
|
||
r => (r.Text("l"), r.Number("n")))).ConfigureAwait(false);
|
||
CollectionAssert.AreEquivalent(new[] { ("en", 16L), ("it", 3L) }, languages, string.Join(", ", languages));
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Integration)]
|
||
public async Task SourcesAreAskedConditionallyAndAFailingOneIsSpacedOut()
|
||
{
|
||
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
|
||
var clock = new ManualClock(Now);
|
||
var http = new FakeHttp();
|
||
var feed = Fixture.Bytes("news/cnbc.xml");
|
||
http.On(u => u.Host == "search.cnbc.com", request =>
|
||
{
|
||
if (request.Headers.IfNoneMatch.Any(t => t.Tag == "\"v1\""))
|
||
{
|
||
return new HttpResponseMessage(HttpStatusCode.NotModified);
|
||
}
|
||
|
||
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(feed) };
|
||
response.Headers.ETag = new System.Net.Http.Headers.EntityTagHeaderValue("\"v1\"");
|
||
return response;
|
||
});
|
||
http.On(u => u.Host != "search.cnbc.com", _ => new HttpResponseMessage(HttpStatusCode.ServiceUnavailable));
|
||
var collector = new NewsCollector(http.Client(), test.Db, clock) { Pause = TimeSpan.Zero };
|
||
|
||
await collector.RunOnceAsync(CancellationToken.None).ConfigureAwait(false);
|
||
Assert.HasCount(NewsStore.Defaults.Count, http.Requests, "One request per source.");
|
||
Assert.AreEqual(4L, await test.Db.ReadAsync(s => s.CountAsync("SELECT COUNT(*) FROM news_items")).ConfigureAwait(false));
|
||
var sources = (await collector.Store.ListSourcesAsync(CancellationToken.None).ConfigureAwait(false)).ToDictionary(s => s.Name);
|
||
Assert.AreEqual("\"v1\"", sources["CNBC"].ETag);
|
||
Assert.AreEqual(0, sources["CNBC"].Failures);
|
||
Assert.AreEqual(1, sources["ANSA"].Failures);
|
||
Assert.Contains("503", sources["ANSA"].LastError!);
|
||
|
||
// Two minutes later CNBC is asked with its ETag and answers "not modified"; the failing ones wait 2, 4, 8 minutes.
|
||
http.Requests.Clear();
|
||
clock.Set(Now.AddMinutes(2));
|
||
await collector.RunOnceAsync(CancellationToken.None).ConfigureAwait(false);
|
||
Assert.AreEqual("\"v1\"", http.Requests.Single(r => r.RequestUri!.Host == "search.cnbc.com").Headers.IfNoneMatch.Single().Tag);
|
||
Assert.HasCount(NewsStore.Defaults.Count, http.Requests, "After one failure a source is tried again at the next round.");
|
||
|
||
http.Requests.Clear();
|
||
clock.Set(Now.AddMinutes(4));
|
||
await collector.RunOnceAsync(CancellationToken.None).ConfigureAwait(false);
|
||
Assert.HasCount(1, http.Requests, "After two failures the others wait 4 minutes from their last try.");
|
||
clock.Set(Now.AddMinutes(6));
|
||
Assert.IsTrue(NewsCollector.Due((await collector.Store.ListSourcesAsync(CancellationToken.None).ConfigureAwait(false)).First(s => s.Name == "ANSA"), clock.UtcNow));
|
||
|
||
var never = new NewsSource(Guid.Empty, "u", "n", NewsSourceKinds.Rss, false, true, null) { Failures = 30, LastCheckedAt = Now };
|
||
Assert.IsFalse(NewsCollector.Due(never, Now.AddMinutes(59)));
|
||
Assert.IsTrue(NewsCollector.Due(never, Now.AddHours(1)), "Never more than an hour between two tries.");
|
||
}
|
||
}
|