This is the first judgement of SPEC 10.2 point 4, without a language model. - Serious cases: cyberattack, suspension, fraud, bankruptcy, investigation and delisting are found by whole-word phrases in English and Italian, so an Italian headline still counts. - Tone: the five tone grades follow the bounds of the specification. - Importance: 4 for a serious case, 3 for a negative tone, 2 when the article is linked to an instrument, otherwise 1. - Classifier: it sits behind IToneClassifier and is asked only for English titles. The ONNX model plugs in there; without it the tone stays unknown and everything else works. Every article stores its score, case and importance. Each story keeps the mean score, the case and the highest importance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
372 lines
24 KiB
C#
372 lines
24 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.");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void TheToneHasFiveGradesWithTheBoundsOfTheSpecification()
|
||
{
|
||
Assert.AreEqual(Tone.Negative, NewsJudge.Grade(-1));
|
||
Assert.AreEqual(Tone.Negative, NewsJudge.Grade(-0.6));
|
||
Assert.AreEqual(Tone.SlightlyNegative, NewsJudge.Grade(-0.59));
|
||
Assert.AreEqual(Tone.SlightlyNegative, NewsJudge.Grade(-0.2));
|
||
Assert.AreEqual(Tone.Neutral, NewsJudge.Grade(-0.19));
|
||
Assert.AreEqual(Tone.Neutral, NewsJudge.Grade(0.2));
|
||
Assert.AreEqual(Tone.SlightlyPositive, NewsJudge.Grade(0.21));
|
||
Assert.AreEqual(Tone.SlightlyPositive, NewsJudge.Grade(0.6));
|
||
Assert.AreEqual(Tone.Positive, NewsJudge.Grade(0.61));
|
||
Assert.AreEqual(0.5, new ToneScore(0.7, 0.2, 0.1).Score, 1e-9);
|
||
}
|
||
|
||
[TestMethod]
|
||
public void SeriousCasesAreFoundInBothLanguagesAndOnlyAsWholeWords()
|
||
{
|
||
Assert.AreEqual(SevereKinds.Cyberattack, NewsJudge.Severe("Exchange halts withdrawals after hack, Solana tokens drained", null));
|
||
Assert.AreEqual(SevereKinds.Suspension, NewsJudge.Severe("Exchange halts withdrawals for maintenance", null));
|
||
Assert.AreEqual(SevereKinds.Fraud, NewsJudge.Severe("SEC charges founder with securities fraud", null));
|
||
Assert.AreEqual(SevereKinds.Bankruptcy, NewsJudge.Severe("Retailer files for Chapter 11 protection", null));
|
||
Assert.AreEqual(SevereKinds.Investigation, NewsJudge.Severe("Regulators open a probe into the bank", null));
|
||
Assert.AreEqual(SevereKinds.Delisting, NewsJudge.Severe("Exchange to delist three tokens", null));
|
||
Assert.AreEqual(SevereKinds.Suspension, NewsJudge.Severe("Il titolo sospeso dalle negoziazioni", null));
|
||
Assert.AreEqual(SevereKinds.Fraud, NewsJudge.Severe("Inchiesta per frode sulla società", null), "Two cases: the first in the order of 10.2.");
|
||
Assert.AreEqual(SevereKinds.Cyberattack, NewsJudge.Severe("Banca colpita da un attacco informatico", null));
|
||
Assert.AreEqual(SevereKinds.Bankruptcy, NewsJudge.Severe("Shares fall", "The company warned of a possible bankruptcy."), "The summary counts too.");
|
||
Assert.IsNull(NewsJudge.Severe("Students win the bank's hackathon", null));
|
||
Assert.IsNull(NewsJudge.Severe("Bitcoin drifts below $90,000 as traders await U.S. data", null));
|
||
Assert.IsNull(NewsJudge.Severe("Borse europee in rialzo, Piazza Affari guida", null));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void ImportanceComesFromTheSeriousCasesTheToneAndTheLinks()
|
||
{
|
||
var classifier = new WordClassifier();
|
||
Assert.AreEqual(new NewsJudgement(null, SevereKinds.Fraud, 4), NewsJudge.Judge("Inchiesta per frode", null, "it", true, classifier));
|
||
Assert.AreEqual(0, classifier.Calls, "Italian titles are never given to the classifier.");
|
||
Assert.AreEqual(new NewsJudgement(-0.8125, null, 3), NewsJudge.Judge("Nvidia slides", null, "en", false, classifier));
|
||
Assert.AreEqual(new NewsJudgement(0.75, null, 2), NewsJudge.Judge("Apple shares jump", null, "en", true, classifier));
|
||
Assert.AreEqual(new NewsJudgement(0, null, 1), NewsJudge.Judge("Markets wait", null, "en", false, classifier));
|
||
Assert.AreEqual(new NewsJudgement(-0.8125, SevereKinds.Cyberattack, 4), NewsJudge.Judge("Tokens drained in a hack", null, "en", true, classifier));
|
||
Assert.AreEqual(new NewsJudgement(null, null, 2), NewsJudge.Judge("Nvidia slides", null, "en", true, null), "Without the classifier the tone is unknown.");
|
||
Assert.AreEqual(Tone.Negative, NewsJudge.Judge("Nvidia slides", null, "en", false, classifier).Tone);
|
||
Assert.IsNull(NewsJudge.Judge("Nvidia slides", null, "en", false, null).Tone);
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Integration)]
|
||
public async Task EveryArticleKeepsItsJudgementAndTheStorySumsThemUp()
|
||
{
|
||
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
|
||
var clock = new ManualClock(Now);
|
||
foreach (var venue in new[] { Venues.Alpaca, Venues.Okx })
|
||
{
|
||
await new InstrumentCatalog(test.Db, clock).SyncAsync(venue, [.. Instruments.Where(i => i.Venue == venue)], complete: false, CancellationToken.None)
|
||
.ConfigureAwait(false);
|
||
}
|
||
|
||
var store = new NewsStore(test.Db, clock);
|
||
await store.EnsureDefaultsAsync(CancellationToken.None).ConfigureAwait(false);
|
||
var sources = (await store.ListSourcesAsync(CancellationToken.None).ConfigureAwait(false)).ToDictionary(s => s.Name);
|
||
var linker = await new NewsCollector(new HttpClient(), test.Db, clock).DictionaryAsync(CancellationToken.None).ConfigureAwait(false);
|
||
var classifier = new WordClassifier();
|
||
foreach (var (file, source) in new[] { ("cnbc", "CNBC"), ("marketwatch", "MarketWatch"), ("coindesk", "CoinDesk"), ("sole24ore", "Il Sole 24 Ore") })
|
||
{
|
||
await store.IngestAsync(sources[source], FeedParser.Parse(Fixture.Bytes($"news/{file}.xml")), linker, CancellationToken.None, classifier).ConfigureAwait(false);
|
||
}
|
||
|
||
var stories = await test.Db.ReadAsync(s => s.QueryAsync("SELECT title, score, severe, importance FROM news_stories",
|
||
r => (Title: r.Text("title"), Score: r.IsNull("score") ? (double?)null : r.Measure("score"), Severe: r.TextOrNull("severe"), Importance: (int)r.Number("importance"))))
|
||
.ConfigureAwait(false);
|
||
(string Title, double? Score, string? Severe, int Importance) Story(string start) => stories.Single(s => s.Title.StartsWith(start, StringComparison.Ordinal));
|
||
|
||
Assert.AreEqual((0.75, (string?)null, 2), (Story("Apple shares jump").Score, Story("Apple shares jump").Severe, Story("Apple shares jump").Importance));
|
||
Assert.AreEqual(3, Story("Target cuts").Importance, "Negative, though linked to nothing.");
|
||
Assert.AreEqual(3, Story("Fire at a chip plant").Importance);
|
||
Assert.AreEqual((SevereKinds.Cyberattack, 4), (Story("Exchange halts withdrawals").Severe, Story("Exchange halts withdrawals").Importance));
|
||
Assert.AreEqual(((double?)null, 2), (Story("Il Bitcoin scende").Score, Story("Il Bitcoin scende").Importance), "Italian: no tone, linked to Bitcoin.");
|
||
Assert.AreEqual((0d, 1), (Story("Weekly wrap").Score ?? double.NaN, Story("Weekly wrap").Importance));
|
||
Assert.AreEqual(1, Story("Borse europee").Importance);
|
||
}
|
||
|
||
/// <summary>A stand-in for the classifier of 4.5: a few words decide the tone.</summary>
|
||
private sealed class WordClassifier : IToneClassifier
|
||
{
|
||
public int Calls { get; private set; }
|
||
|
||
public ToneScore Classify(string text)
|
||
{
|
||
Calls++;
|
||
var lower = text.ToLowerInvariant();
|
||
return new[] { "slides", "cuts", "drained", "hits" }.Any(lower.Contains) ? new ToneScore(0.0625, 0.875, 0.0625)
|
||
: new[] { "jump", "gains" }.Any(lower.Contains) ? new ToneScore(0.875, 0.125, 0)
|
||
: new ToneScore(0.125, 0.125, 0.75);
|
||
}
|
||
}
|
||
}
|