Dopo una prova dal vivo andata male, Alberto deve poter mandare un solo file che spieghi perché, senza che nessuno veda dati di altre persone. Attività ha ora il pulsante «Diagnostica», che scarica il rapporto delle sessioni degli ultimi 14 giorni: pagine aperte e forma degli indirizzi, risposte per tipo, stato e dimensione con ciò che i lettori ne hanno fatto, struttura stretta delle forme sconosciute, ricette, segnali, permessi negati, file in coda e scaricati, e il motivo di ogni arresto. Nel rapporto entrano solo valori da elenchi chiusi, numeri, orari e forme: mai nomi, indirizzi completi, codici, cursori, cookie o pezzi di risposte. Le forme degli indirizzi e la regola delle chiavi-dato passano dalla sonda a Core, e la sonda le richiama. Il registratore non lancia mai eccezioni verso la raccolta e tiene i file in Dati/diagnostica per 14 giorni. Prova che il rapporto non contiene valori, con controlli di mutazione; tre giri di revisione avversaria (25 difetti confermati, tutti corretti). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
847 lines
49 KiB
C#
847 lines
49 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using Febe.Core.Abstractions;
|
|
using Febe.Core.Collection;
|
|
using Febe.Core.Collection.Diagnostics;
|
|
using Febe.Core.Collection.Downloads;
|
|
using Febe.Core.Collection.Privacy;
|
|
using Febe.Core.Collection.Readers;
|
|
using Febe.Core.Collection.Recipes;
|
|
using Febe.Core.Domain;
|
|
using Febe.Core.Engine;
|
|
using Febe.Sonda;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Collection;
|
|
|
|
/// <summary>
|
|
/// The pieces of the diagnostic report (issue #39): path patterns, page names of the site map, the strict key structure,
|
|
/// the recorder with its caps and its buckets, the log of two weeks and the exported file. The proof that the report holds
|
|
/// no data value is <see cref="DiagnosticPrivacyTests"/>.
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class DiagnosticsTests
|
|
{
|
|
private static readonly DateTimeOffset Start = new(2026, 11, 13, 8, 0, 0, TimeSpan.Zero);
|
|
|
|
// ---- Path patterns (moved from the probe into Core) ----
|
|
|
|
[TestMethod]
|
|
[DataRow("/mario.rossi/", "/{testo}/")]
|
|
[DataRow("/api/v1/feed/user/5512345678/", "/api/v1/feed/user/{numero}/")]
|
|
[DataRow("/p/AbC12x/", "/p/{testo}/")]
|
|
[DataRow("/stories/highlights/17912345678901234/", "/stories/highlights/{numero}/")]
|
|
[DataRow("/graphql/query", "/graphql/query")]
|
|
[DataRow("/profilo/marta.prova/?dopo=QVFI#x", "/{testo}/{testo}/")]
|
|
[DataRow("", "")]
|
|
public void APathPatternKeepsOnlyTheWordsOfTheSite(string path, string expected)
|
|
{
|
|
Assert.AreEqual(expected, PathPatterns.Of(path));
|
|
Assert.AreEqual(PathPatterns.Of(path.Split('?')[0]), RequestShapes.PathPattern(path.Split('?')[0]), "The probe uses the same code.");
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("/v/t51.2885-15/foto_n.jpg", "jpg")]
|
|
[DataRow("/o1/v/t16/video.MP4", "mp4")]
|
|
[DataRow("/profilo/marta.prova", null)]
|
|
[DataRow("/senza-estensione/", null)]
|
|
[DataRow("/file.", null)]
|
|
[DataRow(null, null)]
|
|
public void OnlyKnownFileExtensionsAreKept(string? path, string? expected) =>
|
|
Assert.AreEqual(expected, PathPatterns.ExtensionOf(path));
|
|
|
|
// ---- Page names of the site map ----
|
|
|
|
[TestMethod]
|
|
public void AnAddressIsNamedByThePageWithMoreWordsOfItsOwnFirst()
|
|
{
|
|
var instagram = SiteMap.LoadInstagram();
|
|
Assert.AreEqual(SitePages.HighlightViewer, instagram.Classify("/stories/highlights/17912345678901234/"), "Not the stories of «highlights».");
|
|
Assert.AreEqual(SitePages.StoryViewer, instagram.Classify("/stories/marta.prova/3456789012345678901/"));
|
|
Assert.AreEqual(SitePages.Post, instagram.Classify("/p/CzX9abQ1kLm/"));
|
|
Assert.AreEqual(SitePages.Profile, instagram.Classify("/marta.prova/"), "The profile comes last.");
|
|
Assert.AreEqual(SitePages.Home, instagram.Classify("/"));
|
|
Assert.IsNull(instagram.Classify("/api/v1/users/web_profile_info/"));
|
|
Assert.IsNull(instagram.Classify("/graphql/query"));
|
|
Assert.IsNull(instagram.Classify(null));
|
|
Assert.AreEqual("/{username}/", instagram.PatternOf(SitePages.Profile));
|
|
Assert.AreEqual("/stories/highlights/{highlight}/", instagram.PatternOf(SitePages.HighlightViewer));
|
|
Assert.IsNull(instagram.PatternOf(SitePages.Following), "Not set on Instagram yet.");
|
|
|
|
var test = SiteMap.LoadTestSite();
|
|
Assert.AreEqual(SitePages.Following, test.Classify("/profilo/marta.prova/seguiti/"));
|
|
Assert.AreEqual(SitePages.Profile, test.Classify("/profilo/marta.prova/"), "The shape without a query before the same shape with one.");
|
|
Assert.AreEqual(SitePages.ReelsFrom, test.Classify("/profilo/marta.prova/reel/"));
|
|
Assert.AreEqual(SitePages.StoryViewer, test.Classify("/storie/marta.prova/12345/"));
|
|
Assert.AreEqual("/profilo/{username}/", test.PatternOf(SitePages.ProfileFrom), "Never the query of the shape.");
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("/challenge/")]
|
|
[DataRow("/explore/")]
|
|
[DataRow("/reels/")]
|
|
[DataRow("/direct/")]
|
|
[DataRow("/accounts/")]
|
|
[DataRow("/p/")]
|
|
[DataRow("/stories/")]
|
|
public void AFixedWordOfTheSiteIsNotAProfile(string path)
|
|
{
|
|
var instagram = SiteMap.LoadInstagram();
|
|
Assert.IsNull(instagram.Classify(path), "A placeholder never takes a word of the site.");
|
|
var diagnostics = new CollectionDiagnostics(new ManualClock(Start)) { SiteMap = instagram };
|
|
diagnostics.OnNavigation(new NavigationDescriptor(HostKind.Site, "https://indirizzo.non.usato" + path + "?next=/marta.prova/", "indirizzo.non.usato", path, Start));
|
|
var navigation = diagnostics.Snapshot(Start).Single().Navigations.Single();
|
|
Assert.IsNull(navigation.Page);
|
|
Assert.AreEqual(path, navigation.Pattern, "The word itself, not «/{testo}/» and not «/{username}/».");
|
|
Assert.AreEqual(SitePages.Profile, instagram.Classify("/marta_92/"), "A real profile is still one.");
|
|
Assert.IsNull(SiteMap.LoadTestSite().Classify("/profilo/seguiti/"), "Nor a word of the map itself.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AShapeWrittenInTheDataFolderIsReducedLikeAnyPath()
|
|
{
|
|
// Alberto fills «following» with his own address instead of «/{username}/following/».
|
|
var map = SiteMap.Parse("""
|
|
{
|
|
"site": "instagram",
|
|
"pages": {
|
|
"profile": "/{username}/",
|
|
"following": "/alberto.rossi/following/",
|
|
"followingFrom": "/alberto.rossi/following/{cursor}/{nome}/1234/",
|
|
"post": "/p/{code}/"
|
|
}
|
|
}
|
|
""", "bersagli.instagram.json");
|
|
Assert.AreEqual("/{testo}/following/", map.PatternOf(SitePages.Following));
|
|
Assert.AreEqual("/{testo}/following/{cursor}/{testo}/{numero}/", map.PatternOf(SitePages.FollowingFrom), "Only the placeholders of the shipped maps.");
|
|
Assert.AreEqual("/p/{code}/", map.PatternOf(SitePages.Post));
|
|
|
|
var clock = new ManualClock(Start);
|
|
var diagnostics = new CollectionDiagnostics(clock, () => 1) { SiteMap = map };
|
|
diagnostics.SessionStarted(1, clock.Now, 1);
|
|
diagnostics.PageOpened(1, SitePages.Following, ok: true);
|
|
diagnostics.OnNavigation(new NavigationDescriptor(HostKind.Site, "https://indirizzo.non.usato/alberto.rossi/following/", "indirizzo.non.usato", "/alberto.rossi/following/", clock.Now));
|
|
diagnostics.OnResponse(Response("f1", HostKind.Site, "/alberto.rossi/following/", mime: "text/html", resourceType: "Document", mainFrame: true));
|
|
var trace = diagnostics.Snapshot(clock.Now).Single();
|
|
Assert.AreEqual("/{testo}/following/", trace.Pages.Single().Pattern);
|
|
Assert.AreEqual((SitePages.Following, "/{testo}/following/"), (trace.Navigations.Single().Page, trace.Navigations.Single().Pattern));
|
|
Assert.AreEqual("/{testo}/following/", trace.Responses.Single().Pattern);
|
|
}
|
|
|
|
// ---- Strict key structure ----
|
|
|
|
[TestMethod]
|
|
public void TheStrictKeyStructureHidesNamesUsedAsKeys()
|
|
{
|
|
using var body = JsonDocument.Parse("""
|
|
{
|
|
"utenti": {
|
|
"marta.prova": { "seguito": true },
|
|
"jerry": { "nome": "Jerry" },
|
|
"luca": { "x": 1 }
|
|
},
|
|
"17841401234567": { "a": 1 },
|
|
"Città": { "b": 2 },
|
|
"stato": "ok"
|
|
}
|
|
""");
|
|
var strict = KeyStructure.DescribeStrict(body.RootElement);
|
|
Assert.AreEqual("{stato,#:{#:{seguito}}}", strict, "Nor «utenti», «luca» and «jerry»: not words of the closed list.");
|
|
Assert.AreEqual("{utenti:{marta.prova:{seguito},jerry:{nome},luca:{x}},Città:{b},stato,#:{a}}", KeyStructure.Describe(body.RootElement), "The event of «formato non riconosciuto» is unchanged.");
|
|
Assert.IsTrue(KeyStructure.IsDataKey("marta.prova"));
|
|
Assert.IsTrue(KeyStructure.IsDataKey("a@b"));
|
|
Assert.IsTrue(KeyStructure.IsDataKey("due parole"));
|
|
Assert.IsTrue(KeyStructure.IsDataKey("17841401234567"));
|
|
Assert.IsFalse(KeyStructure.IsDataKey("image_versions2"));
|
|
Assert.IsFalse(KeyStructure.IsDataKey("ultima-storia"));
|
|
Assert.AreEqual(KeyStructure.IsDataKey("lucia.rossi"), SensitiveTokens.IsDataKey("lucia.rossi"), "The probe uses the same rule.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AKeyThatIsNotMadeOfWordsOfTheClosedListIsHiddenEvenWhenTheBodyDoesNotRepeatIt()
|
|
{
|
|
// A map keyed by user and by post, with no name repeated as a value (only «@marta_92» inside a text).
|
|
using var body = JsonDocument.Parse("""
|
|
{
|
|
"data": {
|
|
"users": { "marta_92": { "is_private": false, "follower_count": 3 }, "jerry": { "is_private": true } },
|
|
"media": { "CzX9abQ1kLm": { "taken_at": 1, "image_versions2": { "width": 1 } } },
|
|
"page_info": { "has_next_page": true, "end_cursor": "QVFIUjBz", "next_max_id": "x" },
|
|
"max": { "a": 1 },
|
|
"__typename": "XDTUserDict",
|
|
"text": "con @marta_92"
|
|
}
|
|
}
|
|
""");
|
|
var strict = KeyStructure.DescribeStrict(body.RootElement);
|
|
Assert.AreEqual(
|
|
"{data:{users:{#:{is_private,follower_count}},media:{#:{taken_at,image_versions2:{width}}},page_info:{has_next_page,end_cursor,#},__typename,text,#:{#}}}",
|
|
strict);
|
|
foreach (var name in new[] { "marta_92", "jerry", "CzX9abQ1kLm" })
|
|
{
|
|
Assert.IsFalse(StructureWords.IsStructural(name), "A name or a post code is not structure.");
|
|
}
|
|
|
|
Assert.IsFalse(StructureWords.IsStructural("max"), "A word that is also a first name passes only inside a compound key.");
|
|
Assert.IsTrue(StructureWords.IsStructural("next_max_id"));
|
|
Assert.IsTrue(StructureWords.IsStructural("pubblicazioni"), "The words of the test site's vocabulary.");
|
|
Assert.IsFalse(StructureWords.IsStructural("marta.prova"));
|
|
Assert.IsFalse(StructureWords.IsStructural("17841401234567"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void NamesMadeOfWordsOfTheListAreHiddenAsEntriesOfAMapAndCompoundOnlyWordsNeverStandTogether()
|
|
{
|
|
// Two first names together are a name, not a key: a compound-only word passes only next to a word of the list.
|
|
Assert.IsFalse(StructureWords.IsStructural("max_mark"));
|
|
Assert.IsFalse(StructureWords.IsStructural("mark-max"));
|
|
Assert.IsTrue(StructureWords.IsStructural("max_music"), "«music» is a word of the list: only the map rule can hide it.");
|
|
Assert.IsTrue(StructureWords.IsKnownKey("user"));
|
|
Assert.IsTrue(StructureWords.IsKnownKey("owner"));
|
|
Assert.IsFalse(StructureWords.IsKnownKey("the_artist"));
|
|
Assert.IsFalse(StructureWords.IsKnownKey("max"), "A known key is still a structural one.");
|
|
|
|
// A map keyed by user name, every name made of words of the list, none repeated as a value: the shape of the
|
|
// values gives it away. «user» and «owner» of a post have the same shape too, and stay: the readers know them.
|
|
using var body = JsonDocument.Parse("""
|
|
{
|
|
"users": { "the_artist": { "is_private": false }, "max_music": { "is_private": true } },
|
|
"playlists": { "street_style": [ { "pk": 1 } ], "live_music": [ { "pk": 2 } ] },
|
|
"item": { "user": { "pk": 1, "username": "x" }, "owner": { "pk": 1, "username": "x" }, "caption": { "text": "y" } },
|
|
"max_mark": { "count": 1 },
|
|
"status": "ok"
|
|
}
|
|
""");
|
|
var strict = KeyStructure.DescribeStrict(body.RootElement);
|
|
Assert.AreEqual(
|
|
"{users:{#:{is_private}},playlists:{#:[{pk}]},item:{user:{pk,username},owner:{pk,username},caption:{text}},status,#:{#}}",
|
|
strict);
|
|
foreach (var name in new[] { "the_artist", "max_music", "street_style", "live_music", "max_mark" })
|
|
{
|
|
Assert.IsFalse(strict.Contains(name, StringComparison.OrdinalIgnoreCase), "A name is in the structure.");
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void NamesMadeOfWordsOfTheListAreHiddenInMapsOfPlainValuesEmptyValuesAndUnlikeEntries()
|
|
{
|
|
// Maps keyed by user name, every name made of words of the list, none repeated as a value. Known fields among
|
|
// the entries stay.
|
|
(string Map, string Expected)[] cases =
|
|
[
|
|
// Counts and flags: a key with a plain value stays only when it is a field.
|
|
("""{ "the_artist": 3, "street_style": 5, "like_count": 2 }""", "{like_count,#}"),
|
|
("""{ "max_music": true, "live_music": false }""", "{#}"),
|
|
("""{ "the_artist": null }""", "{#}"),
|
|
|
|
// Empty values and lists of codes.
|
|
("""{ "the_artist": [], "street_style": [], "carousel_media": [] }""", "{carousel_media:[],#:[]}"),
|
|
("""{ "the_artist": {}, "street_style": {} }""", "{#:{}}"),
|
|
("""{ "the_artist": [ "c1" ], "street_style": [ "c2", "c3" ] }""", "{#:[]}"),
|
|
("""{ "the_artist": [], "street_style": [ "c1" ] }""", "{#:[]}"),
|
|
("""{ "the_artist": [], "street_style": [ { "pk": "2" } ] }""", "{#:[]}"),
|
|
("""{ "the_artist": {}, "street_style": { "pk": "2" } }""", "{#:{}}"),
|
|
|
|
// Entries with different optional fields: only «pk» in common, or one field more.
|
|
("""{ "the_artist": { "pk": "1", "seen": 2 }, "street_style": { "pk": "2" }, "live_music": { "pk": "3", "is_private": true } }""", "{#:{pk,seen}}"),
|
|
("""{ "the_artist": { "seen": 1, "is_private": true }, "street_style": { "seen": 2 } }""", "{#:{seen,is_private}}"),
|
|
("""{ "the_artist": { "pk": "1", "seen": 1, "is_private": true }, "street_style": { "pk": "2", "like_count": 1, "follower_count": 3 } }""", "{#:{pk,seen,is_private}}"),
|
|
("""{ "the_artist": [ { "pk": "1", "seen": 1 } ], "street_style": [ { "pk": "2" } ] }""", "{#:[{pk,seen}]}"),
|
|
|
|
// A list of maps with one entry each.
|
|
("""[ { "the_artist": { "pk": "1" } }, { "street_style": { "pk": "2" } } ]""", "[{#:{pk}}]"),
|
|
|
|
// Not a map: the fields of a post stay, and so do the keys every element of a list has.
|
|
("""{ "user": { "pk": 1 }, "owner": { "pk": 2 }, "caption": { "text": "y" }, "image_versions2": { "candidates": [] } }""",
|
|
"{user:{pk},owner:{pk},caption:{text},image_versions2:{candidates:[]}}"),
|
|
("""[ { "pk": 1, "is_private": true }, { "pk": 2, "is_private": false, "seen": 1 } ]""", "[{pk,is_private}]"),
|
|
];
|
|
|
|
foreach (var (map, expected) in cases)
|
|
{
|
|
using var body = JsonDocument.Parse("{ \"data\": " + map + " }");
|
|
var strict = KeyStructure.DescribeStrict(body.RootElement);
|
|
Assert.AreEqual("{data:" + expected + "}", strict);
|
|
foreach (var name in new[] { "the_artist", "street_style", "max_music", "live_music" })
|
|
{
|
|
Assert.IsFalse(strict.Contains(name, StringComparison.OrdinalIgnoreCase), "A name is in the structure.");
|
|
}
|
|
}
|
|
|
|
Assert.IsTrue(StructureWords.IsFieldKey("is_private"));
|
|
Assert.IsTrue(StructureWords.IsFieldKey("user"));
|
|
Assert.IsFalse(StructureWords.IsFieldKey("the_artist"));
|
|
Assert.IsFalse(StructureWords.IsFieldKey("max"), "A field is still structural.");
|
|
}
|
|
|
|
// ---- The recorder ----
|
|
|
|
[TestMethod]
|
|
public void ListsStopAtTheirCapsAndCountWhatTheyLeaveOut()
|
|
{
|
|
var clock = new ManualClock(Start);
|
|
long? current = 5;
|
|
var diagnostics = new CollectionDiagnostics(clock, () => current);
|
|
diagnostics.SessionStarted(5, clock.Now, 1.0);
|
|
for (var i = 0; i < CollectionDiagnostics.MaxPages + 5; i++)
|
|
{
|
|
diagnostics.PageOpened(5, SitePages.Profile, ok: true);
|
|
}
|
|
|
|
for (var i = 0; i < CollectionDiagnostics.MaxUnknownShapes + 2; i++)
|
|
{
|
|
diagnostics.ReadObserved(Response($"u{i}", HostKind.Site, "/api/forma/"), new ReadResult { Shape = ReadShape.Unrecognized }, $"{{chiave{Letters(i)}}}");
|
|
}
|
|
|
|
for (var i = 0; i < CollectionDiagnostics.MaxSignals + 3; i++)
|
|
{
|
|
diagnostics.SignalRaised(new Signal(SignalLevel.Throttle, "http-429", null, clock.Now));
|
|
}
|
|
|
|
for (var i = 0; i < CollectionDiagnostics.MaxRecipes + 4; i++)
|
|
{
|
|
diagnostics.RecipeEnded(5, RecipeKind.ProfileCheck, RecipeStatus.Completed, null, 1, 0);
|
|
}
|
|
|
|
current = null;
|
|
diagnostics.SessionEnded(5, clock.Now, SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
|
|
// Outside sessions: response groups by depth of the address and resource type.
|
|
string[] types = ["Document", "Image", "Media", "Font", "Script", "XHR", "Fetch", "Ping", "Other", "Manifest"];
|
|
for (var i = 0; i < CollectionDiagnostics.MaxResponseGroups + 7; i++)
|
|
{
|
|
var path = string.Concat(Enumerable.Repeat("/x", 1 + (i / types.Length))) + "/";
|
|
diagnostics.OnResponse(Response($"g{i}", HostKind.Site, path, resourceType: types[i % types.Length]));
|
|
}
|
|
|
|
var session = diagnostics.Recent.Single();
|
|
Assert.HasCount(CollectionDiagnostics.MaxPages, session.Pages);
|
|
Assert.AreEqual(5, session.Overflow.Pages);
|
|
Assert.HasCount(CollectionDiagnostics.MaxUnknownShapes, session.UnknownShapes);
|
|
Assert.AreEqual(2, session.Overflow.UnknownShapes);
|
|
Assert.HasCount(CollectionDiagnostics.MaxSignals, session.Signals);
|
|
Assert.AreEqual(3, session.Overflow.Signals);
|
|
Assert.HasCount(CollectionDiagnostics.MaxRecipes, session.Recipes);
|
|
Assert.AreEqual(4, session.Overflow.Recipes);
|
|
Assert.AreEqual(SessionStopCause.WorkDone, session.StopCause);
|
|
|
|
// An overflowing body is counted once, not again when it is read (nor once per block of a page).
|
|
var late = Response("over", HostKind.Site, "/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/y/");
|
|
diagnostics.OnResponse(late);
|
|
diagnostics.OnLoadingFinished("over", 10);
|
|
diagnostics.ReadObserved(late, new ReadResult { Shape = ReadShape.Recognized }, null);
|
|
diagnostics.ReadObserved(late, ReadResult.EmptyResult, null);
|
|
|
|
var outside = diagnostics.Snapshot(clock.Now).Single();
|
|
Assert.IsNull(outside.Session);
|
|
Assert.HasCount(CollectionDiagnostics.MaxResponseGroups, outside.Responses);
|
|
Assert.AreEqual(8, outside.Overflow.Responses);
|
|
Assert.AreEqual(0, outside.Overflow.Requests);
|
|
Assert.AreEqual(0, diagnostics.Faults);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void OnlyBodiesStillWaitingCountAsForgottenAndLateNewsIsCountedWhereItArrives()
|
|
{
|
|
var clock = new ManualClock(Start);
|
|
long? current = 6;
|
|
var diagnostics = new CollectionDiagnostics(clock, () => current);
|
|
diagnostics.SessionStarted(6, clock.Now, 1);
|
|
|
|
// A long session: thousands of images and data responses, each settled (size, reading or drop).
|
|
for (var i = 0; i < 6000; i++)
|
|
{
|
|
var image = Response($"img{i}", HostKind.Cdn, "/v/t51/foto.jpg", mime: "image/jpeg", resourceType: "Image");
|
|
diagnostics.OnResponse(image);
|
|
diagnostics.OnLoadingFinished(image.RequestId, 100);
|
|
if (i % 3 == 0)
|
|
{
|
|
var data = Response($"dat{i}", HostKind.Site, "/api/v1/feed/", resourceType: "XHR");
|
|
diagnostics.OnResponse(data);
|
|
diagnostics.OnLoadingFinished(data.RequestId, 50);
|
|
if (i % 2 == 0)
|
|
{
|
|
diagnostics.ReadObserved(data, ReadResult.EmptyResult, null);
|
|
}
|
|
else
|
|
{
|
|
diagnostics.OnBodyDropped(data.RequestId, BodyDropReason.NotJson);
|
|
}
|
|
}
|
|
}
|
|
|
|
// One body that never got its outcome, then enough new responses to push it out.
|
|
var waiting = Response("waiting", HostKind.Site, "/api/v1/feed/", resourceType: "XHR");
|
|
diagnostics.OnResponse(waiting);
|
|
for (var i = 0; i < 4100; i++)
|
|
{
|
|
diagnostics.OnResponse(Response($"open{i}", HostKind.Cdn, "/v/t51/video.mp4", mime: "video/mp4", resourceType: "Media"));
|
|
}
|
|
|
|
var trace = diagnostics.Snapshot(clock.Now).Single();
|
|
Assert.AreEqual(1, trace.Overflow.Requests, "Only the body still waiting; settled responses and media without a size are not losses.");
|
|
var feed = trace.Responses.Single(r => r.Pattern == "/api/v1/feed/");
|
|
Assert.AreEqual((2001, 1000, 1000), (feed.Count, feed.Empty, feed.Dropped.Single().Count));
|
|
Assert.AreEqual(600_000L, trace.Responses.Single(r => r.Pattern == "cdn *.jpg").Bytes);
|
|
|
|
// The session ends; what arrives afterwards for its responses goes to the trace open now, as late.
|
|
var last = Response("last", HostKind.Site, "/api/v1/feed/", resourceType: "XHR");
|
|
diagnostics.OnResponse(last);
|
|
current = null;
|
|
diagnostics.SessionEnded(6, clock.Now, SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
diagnostics.OnLoadingFinished("last", 70);
|
|
diagnostics.ReadObserved(last, ReadResult.EmptyResult, null);
|
|
var ended = diagnostics.Recent.Single();
|
|
Assert.AreEqual(2002, ended.Responses.Single(r => r.Pattern == "/api/v1/feed/").Count);
|
|
Assert.AreEqual(1000, ended.Responses.Single(r => r.Pattern == "/api/v1/feed/").Empty, "A closed trace is not changed.");
|
|
var outside = diagnostics.Snapshot(clock.Now).Single();
|
|
Assert.IsNull(outside.Session);
|
|
Assert.AreEqual(2, outside.Overflow.Late);
|
|
Assert.AreEqual(0, outside.Responses.Count);
|
|
Assert.AreEqual(0, diagnostics.Faults);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AFailedRecipeSaysTheTypeOfItsErrorAndAFailedDownloadItsCause()
|
|
{
|
|
var clock = new ManualClock(Start);
|
|
var diagnostics = new CollectionDiagnostics(clock, () => 8);
|
|
diagnostics.SessionStarted(8, clock.Now, 1);
|
|
diagnostics.RecipeEnded(8, RecipeKind.Highlight, RecipeStatus.Failed, null, 2, 0, nameof(TimeoutException));
|
|
diagnostics.RecipeEnded(8, RecipeKind.Highlight, RecipeStatus.Failed, null, 2, 0, "System.Collections.Generic.List`1 di marta.prova");
|
|
diagnostics.DownloadSettled(new DownloadProgress(1, 2, 3, EntryKind.Post, DownloadStep.Retried) { Cause = DownloadCauses.Of(503) });
|
|
diagnostics.DownloadSettled(new DownloadProgress(1, 2, 3, EntryKind.Post, DownloadStep.Retried) { Cause = DownloadCauses.Of(503) });
|
|
diagnostics.DownloadSettled(new DownloadProgress(1, 2, 3, EntryKind.Post, DownloadStep.Failed) { Cause = DownloadCauses.Of(null) });
|
|
diagnostics.DownloadSettled(new DownloadProgress(1, 2, 3, EntryKind.Post, DownloadStep.Stored, 10));
|
|
diagnostics.SessionEnded(8, clock.Now, SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
|
|
var trace = diagnostics.Recent.Single();
|
|
CollectionAssert.AreEqual(new[] { nameof(TimeoutException), CollectionDiagnostics.Other }, trace.Recipes.Select(r => r.Error).ToArray());
|
|
Assert.AreEqual(2, trace.Downloads.Single(d => d.Cause == "http-503").Count);
|
|
Assert.AreEqual(DownloadStep.Failed, trace.Downloads.Single(d => d.Cause == DownloadCauses.Network).Step);
|
|
Assert.IsNull(trace.Downloads.Single(d => d.Step == DownloadStep.Stored).Cause);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void WhatTheCaptureSeesGoesToTheSessionInProgressOrToTheDayOutsideSessions()
|
|
{
|
|
var clock = new ManualClock(Start);
|
|
long? current = null;
|
|
var diagnostics = new CollectionDiagnostics(clock, () => current) { SiteMap = SiteMap.LoadTestSite() };
|
|
|
|
// Alberto at the browser before any session: the day outside sessions.
|
|
diagnostics.OnNavigation(new NavigationDescriptor(HostKind.Site, "http://sito/profilo/a/", "sito", "/profilo/a/", clock.Now));
|
|
|
|
current = 9;
|
|
diagnostics.SessionStarted(9, clock.Now, 0.87654);
|
|
var document = Response("d1", HostKind.Site, "/profilo/marta.prova/", mime: "text/html", resourceType: "Document", mainFrame: true);
|
|
diagnostics.OnResponse(document);
|
|
diagnostics.OnLoadingFinished("d1", 50_000);
|
|
diagnostics.ReadObserved(document, new ReadResult { Shape = ReadShape.Recognized, Contents = [] }, null);
|
|
diagnostics.ReadObserved(document, ReadResult.EmptyResult, null);
|
|
var image = Response("i1", HostKind.Cdn, "/v/t51/foto.jpg", mime: "image/jpeg", resourceType: "Image");
|
|
diagnostics.OnResponse(image);
|
|
diagnostics.OnLoadingFinished("i1", 120_000);
|
|
var data = Response("x1", HostKind.Site, "/api/dati/", resourceType: "Fetch");
|
|
diagnostics.OnResponse(data);
|
|
diagnostics.OnBodyDropped("x1", BodyDropReason.Timeout);
|
|
diagnostics.PermitDenied(null, ActionKind.ProfileOpen, DenialReason.HourlyBudgetReached);
|
|
diagnostics.FilesQueued(9, EntryKind.Post, 3, nameof(SaveOutcome.Created), skipped: 1);
|
|
diagnostics.FilesQueued(9, EntryKind.Post, 0, nameof(SaveOutcome.Known));
|
|
diagnostics.StepJudged(9, CollectionDiagnostics.VerdictRecognized);
|
|
diagnostics.TargetClicked(9, SiteTargets.ProfileStoryRing, TargetCheckStatus.Clicked);
|
|
current = null;
|
|
diagnostics.SessionEnded(9, clock.Now, SessionOutcome.Stopped, SessionStopCause.StoppedEarly, RecipeStatus.TargetNotFound);
|
|
|
|
// After the session: a download, outside sessions; a late body of the session's request goes nowhere new.
|
|
diagnostics.DownloadSettled(new DownloadProgress(1, 2, 3, EntryKind.Post, DownloadStep.Stored, 120_000));
|
|
diagnostics.DownloadSettled(new DownloadProgress(1, 2, 3, EntryKind.Post, DownloadStep.Retried));
|
|
diagnostics.ReadObserved(document, ReadResult.EmptyResult, null);
|
|
|
|
var session = diagnostics.Recent.Single();
|
|
Assert.AreEqual(9L, session.Session);
|
|
Assert.AreEqual(0.88, session.Mood);
|
|
Assert.AreEqual(SessionStopCause.StoppedEarly, session.StopCause);
|
|
Assert.AreEqual(RecipeStatus.TargetNotFound, session.StoppedBy);
|
|
Assert.IsFalse(session.InProgress);
|
|
var page = session.Responses.Single(r => r.Page == SitePages.Profile);
|
|
Assert.AreEqual("/profilo/{username}/", page.Pattern);
|
|
Assert.AreEqual((1, 50_000L, 1, 1, 0), (page.Count, page.Bytes, page.Recognized, page.Empty, page.NotRead));
|
|
var cdn = session.Responses.Single(r => r.HostKind == HostKind.Cdn);
|
|
Assert.AreEqual(("cdn *.jpg", 1, 120_000L), (cdn.Pattern, cdn.NotRead, cdn.MaxBytes));
|
|
var dropped = session.Responses.Single(r => r.Pattern == PathPatterns.Of("/api/dati/"));
|
|
Assert.AreEqual(BodyDropReason.Timeout, dropped.Dropped.Single().Reason);
|
|
Assert.AreEqual(new DenialCount(ActionKind.ProfileOpen, DenialReason.HourlyBudgetReached, 1), session.Denials.Single());
|
|
Assert.AreEqual(new QueuedFiles(EntryKind.Post, "Created", 1, 3, 1), session.FilesQueued.Single(q => q.Outcome == "Created"));
|
|
Assert.AreEqual(new TargetCount(SiteTargets.ProfileStoryRing, TargetCheckStatus.Clicked, 1), session.Targets.Single());
|
|
|
|
var outside = diagnostics.Snapshot(clock.Now).Single();
|
|
Assert.IsNull(outside.Session);
|
|
Assert.IsTrue(outside.InProgress);
|
|
Assert.AreEqual(SitePages.Profile, outside.Navigations.Single().Page);
|
|
Assert.AreEqual(2, outside.Downloads.Count);
|
|
Assert.AreEqual(120_000L, outside.DownloadedBytes);
|
|
Assert.AreEqual(0, outside.Responses.Count, "The late body is not a response of the day outside sessions.");
|
|
Assert.AreEqual(1, outside.Overflow.Late, "It is counted as late where it arrived.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TheDayOutsideSessionsIsKeptAtTheEndOfItsLocalDayAndOpenSessionsWhenFebeStops()
|
|
{
|
|
var clock = new ManualClock(new DateTimeOffset(2026, 11, 13, 21, 0, 0, TimeSpan.Zero), TestZones.Rome);
|
|
var diagnostics = new CollectionDiagnostics(clock);
|
|
diagnostics.SignalRaised(new Signal(SignalLevel.Throttle, "http-429", "https://sito/profilo/marta.prova/", clock.Now));
|
|
clock.Now = clock.Now.AddHours(2); // 00:00 in Rome: a new local day
|
|
diagnostics.SignalRaised(new Signal(SignalLevel.Throttle, "http-429", null, clock.Now));
|
|
var yesterday = diagnostics.Recent.Single();
|
|
Assert.IsNull(yesterday.Session);
|
|
Assert.IsFalse(yesterday.InProgress);
|
|
Assert.HasCount(1, yesterday.Signals);
|
|
|
|
diagnostics.SessionStarted(12, clock.Now, 1);
|
|
diagnostics.PageOpened(12, SitePages.Home, ok: true);
|
|
diagnostics.Dispose();
|
|
diagnostics.PageOpened(12, SitePages.Home, ok: true);
|
|
|
|
var traces = diagnostics.Recent;
|
|
Assert.HasCount(3, traces);
|
|
var stopped = traces.Single(t => t.Session == 12);
|
|
Assert.IsTrue(stopped.InProgress);
|
|
Assert.AreEqual(SessionStopCause.Shutdown, stopped.StopCause);
|
|
Assert.HasCount(1, stopped.Pages, "Nothing is recorded after the flush.");
|
|
Assert.HasCount(1, traces.Single(t => t.Session is null && t.Start >= clock.Now).Signals);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void NamesOutsideTheClosedListsBecomeOther()
|
|
{
|
|
var clock = new ManualClock(Start);
|
|
var diagnostics = new CollectionDiagnostics(clock, () => 3);
|
|
diagnostics.SessionStarted(3, clock.Now, 1);
|
|
diagnostics.PageOpened(3, "marta.prova", ok: false);
|
|
diagnostics.TargetClicked(3, "marta.prova", TargetCheckStatus.NotFound);
|
|
diagnostics.StepJudged(3, "marta.prova");
|
|
diagnostics.RecipeEnded(3, RecipeKind.ProfileCheck, RecipeStatus.StoppedBySignal, "https://sito/marta.prova", 0, 0);
|
|
diagnostics.FilesQueued(3, EntryKind.Post, 1, "marta.prova");
|
|
diagnostics.OnResponse(Response("m1", HostKind.Other, "/marta.prova", mime: "text/marta prova", resourceType: "marta.prova"));
|
|
diagnostics.SessionEnded(3, clock.Now, SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
var trace = diagnostics.Recent.Single();
|
|
Assert.AreEqual(CollectionDiagnostics.Other, trace.Pages.Single().Page);
|
|
Assert.IsNull(trace.Pages.Single().Pattern);
|
|
Assert.AreEqual(CollectionDiagnostics.Other, trace.Targets.Single().Target);
|
|
Assert.AreEqual(CollectionDiagnostics.Other, trace.Steps.Single().Verdict);
|
|
Assert.AreEqual(CollectionDiagnostics.Other, trace.Recipes.Single().Signal);
|
|
Assert.AreEqual(CollectionDiagnostics.Other, trace.FilesQueued.Single().Outcome);
|
|
var response = trace.Responses.Single();
|
|
Assert.AreEqual((CollectionDiagnostics.Other, "text/altro", CollectionDiagnostics.Other), (response.Pattern, response.Mime, response.ResourceType));
|
|
Assert.AreEqual("image/jpeg", CollectionDiagnostics.MimeOf("image/JPEG; charset=binary"));
|
|
Assert.AreEqual(CollectionDiagnostics.Other, CollectionDiagnostics.MimeOf("marta/prova"));
|
|
}
|
|
|
|
// ---- The log ----
|
|
|
|
[TestMethod]
|
|
public void TheLogKeepsTwoWeeksSkipsDamagedLinesAndTouchesNoOtherFile()
|
|
{
|
|
using var folder = new TempFolder("diagnostica");
|
|
var clock = new ManualClock(new DateTimeOffset(2026, 11, 13, 9, 0, 0, TimeSpan.Zero), TestZones.Rome);
|
|
var log = new DiagnosticLog(folder.Combine("diagnostica"), clock);
|
|
Directory.CreateDirectory(log.Folder);
|
|
var today = LocalCalendar.Today(clock.Now, clock.LocalZone);
|
|
File.WriteAllText(Path.Combine(log.Folder, DiagnosticLog.FileNameFor(today.AddDays(-14))), "{}\n");
|
|
File.WriteAllText(Path.Combine(log.Folder, DiagnosticLog.FileNameFor(today.AddDays(-20))), "{}\n");
|
|
var kept = Path.Combine(log.Folder, DiagnosticLog.FileNameFor(today.AddDays(-13)));
|
|
File.WriteAllText(kept, "{\"session\":1,\"start\":\"2026-10-31T09:00:00+00:00\",\"inProgress\":false}\n{\"session\":tronca");
|
|
var other = Path.Combine(log.Folder, "appunti.txt");
|
|
File.WriteAllText(other, "di Alberto");
|
|
|
|
var diagnostics = new CollectionDiagnostics(clock, log: log);
|
|
diagnostics.SessionStarted(2, clock.Now, 1.1);
|
|
diagnostics.PageOpened(2, SitePages.Home, ok: true);
|
|
diagnostics.RecipeEnded(2, RecipeKind.FollowingSweep, RecipeStatus.Completed, null, 1, 4);
|
|
clock.Now = clock.Now.AddMinutes(9);
|
|
diagnostics.SessionEnded(2, clock.Now, SessionOutcome.Completed, SessionStopCause.PlannedEnd);
|
|
|
|
var contents = log.ReadAll();
|
|
Assert.AreEqual(1, contents.DamagedLines);
|
|
CollectionAssert.AreEqual(new long?[] { 1, 2 }, contents.Traces.Select(t => t.Session).ToArray(), "Oldest first.");
|
|
var read = contents.Traces[1];
|
|
Assert.AreEqual(SessionStopCause.PlannedEnd, read.StopCause);
|
|
Assert.AreEqual(SitePages.Home, read.Pages.Single().Page);
|
|
Assert.AreEqual(RecipeKind.FollowingSweep, read.Recipes.Single().Kind);
|
|
Assert.AreEqual(clock.Now, read.End);
|
|
Assert.IsFalse(File.Exists(Path.Combine(log.Folder, DiagnosticLog.FileNameFor(today.AddDays(-14)))));
|
|
Assert.IsFalse(File.Exists(Path.Combine(log.Folder, DiagnosticLog.FileNameFor(today.AddDays(-20)))));
|
|
Assert.IsTrue(File.Exists(kept));
|
|
Assert.IsTrue(File.Exists(other), "Only its own files, by their name.");
|
|
Assert.IsTrue(File.Exists(Path.Combine(log.Folder, DiagnosticLog.FileNameFor(today))));
|
|
|
|
// Febe stopped in the middle of a line of today: the next trace starts on a line of its own.
|
|
var todayFile = Path.Combine(log.Folder, DiagnosticLog.FileNameFor(today));
|
|
File.AppendAllText(todayFile, "{\"session\":3,\"start\":\"2026-11-13T09:");
|
|
diagnostics.SessionStarted(4, clock.Now, 1);
|
|
diagnostics.SessionEnded(4, clock.Now, SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
var afterTear = log.ReadAll();
|
|
Assert.AreEqual(2, afterTear.DamagedLines, "The torn line only, besides the one of 13 days ago.");
|
|
CollectionAssert.AreEqual(new long?[] { 1, 2, 4 }, afterTear.Traces.Select(t => t.Session).ToArray());
|
|
Assert.AreEqual(2, diagnostics.Gather().DamagedLines);
|
|
|
|
// Two weeks later the day of the session is gone too.
|
|
clock.Now = clock.Now.AddDays(14);
|
|
Assert.HasCount(0, log.ReadAll().Traces);
|
|
Assert.IsFalse(diagnostics.Collect().Any(t => t.Session is not null), "Nothing left in the log, nor in memory beyond the days kept.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ATraceWhoseWriteFailedIsStillInTheReportAndTheReportSaysSo()
|
|
{
|
|
using var folder = new TempFolder("diagnostica");
|
|
var clock = new ManualClock(new DateTimeOffset(2026, 11, 13, 9, 0, 0, TimeSpan.Zero), TestZones.Rome);
|
|
|
|
// A file where the folder of the log should be: every write fails.
|
|
var blocked = folder.Combine("diagnostica");
|
|
File.WriteAllText(blocked, "non una cartella");
|
|
var diagnostics = new CollectionDiagnostics(clock, log: new DiagnosticLog(blocked, clock));
|
|
diagnostics.SessionStarted(5, clock.Now, 1);
|
|
diagnostics.PageOpened(5, SitePages.Home, ok: true);
|
|
diagnostics.SessionEnded(5, clock.Now, SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
diagnostics.SessionStarted(6, clock.Now, 1);
|
|
|
|
var gathered = diagnostics.Gather();
|
|
CollectionAssert.AreEqual(new long?[] { 5, 6 }, gathered.Traces.Select(t => t.Session).ToArray(), "The finished one from memory, the open one in progress, once each.");
|
|
Assert.IsFalse(gathered.Traces[0].InProgress);
|
|
Assert.IsTrue(gathered.Traces[1].InProgress);
|
|
Assert.IsGreaterThan(0, gathered.RecorderFaults);
|
|
|
|
using var stream = new MemoryStream();
|
|
DiagnosticReport.Write(stream, gathered.Traces, new DiagnosticReportHeader("0.1.2", "docker", clock.Now, TestZones.Rome, DamagedLines: 3, RecorderFaults: gathered.RecorderFaults));
|
|
using var json = JsonDocument.Parse(stream.ToArray());
|
|
Assert.AreEqual(3, json.RootElement.GetProperty("righeRovinate").GetInt32());
|
|
Assert.AreEqual(gathered.RecorderFaults, json.RootElement.GetProperty("erroriRegistratore").GetInt32());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ADayFileThatCannotBeReadIsCountedAndTheOtherDaysAreStillThere()
|
|
{
|
|
using var folder = new TempFolder("diagnostica");
|
|
var clock = new ManualClock(new DateTimeOffset(2026, 11, 13, 9, 0, 0, TimeSpan.Zero), TestZones.Rome);
|
|
var log = new DiagnosticLog(folder.Combine("diagnostica"), clock);
|
|
var diagnostics = new CollectionDiagnostics(clock, log: log);
|
|
diagnostics.SessionStarted(1, clock.Now, 1);
|
|
diagnostics.SessionEnded(1, clock.Now.AddMinutes(5), SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
clock.Now = clock.Now.AddDays(1);
|
|
diagnostics.SessionStarted(2, clock.Now, 1);
|
|
diagnostics.SessionEnded(2, clock.Now.AddMinutes(5), SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
var yesterday = Path.Combine(log.Folder, DiagnosticLog.FileNameFor(LocalCalendar.Today(clock.Now, clock.LocalZone).AddDays(-1)));
|
|
|
|
// Another program (a backup, a synchronization, an antivirus) holds yesterday's file without sharing it.
|
|
DiagnosticLogContents contents;
|
|
DiagnosticCollection gathered;
|
|
using (new FileStream(yesterday, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
|
|
{
|
|
contents = log.ReadAll();
|
|
gathered = new CollectionDiagnostics(clock, log: log).Gather();
|
|
}
|
|
|
|
Assert.AreEqual(1, contents.UnreadableFiles);
|
|
Assert.AreEqual(0, contents.DamagedLines);
|
|
CollectionAssert.AreEqual(new long?[] { 2 }, contents.Traces.Select(t => t.Session).ToArray(), "Today is still read.");
|
|
Assert.AreEqual(1, gathered.UnreadableFiles, "The report is told that a day is missing.");
|
|
Assert.AreEqual(0, gathered.RecorderFaults, "Not the whole log lost: one file.");
|
|
Assert.AreEqual(1, gathered.Traces.Count);
|
|
|
|
using var stream = new MemoryStream();
|
|
DiagnosticReport.Write(stream, gathered.Traces, new DiagnosticReportHeader("0.1.2", "docker", clock.Now, TestZones.Rome, UnreadableFiles: gathered.UnreadableFiles));
|
|
using var json = JsonDocument.Parse(stream.ToArray());
|
|
Assert.AreEqual(1, json.RootElement.GetProperty("fileIlleggibili").GetInt32());
|
|
Assert.AreEqual(2, log.ReadAll().Traces.Count, "Released: both days again.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ASessionCutShortByACrashIsStillInTheReportAsASessionWithoutAnEnd()
|
|
{
|
|
using var folder = new TempFolder("diagnostica");
|
|
var clock = new ManualClock(new DateTimeOffset(2026, 11, 13, 9, 0, 0, TimeSpan.Zero), TestZones.Rome);
|
|
var folderOfLog = folder.Combine("diagnostica");
|
|
var before = new CollectionDiagnostics(clock, log: new DiagnosticLog(folderOfLog, clock));
|
|
before.SessionStarted(7, clock.Now, 1);
|
|
Assert.AreEqual(1, new DiagnosticLog(folderOfLog, clock).ReadAll().Traces.Count, "The start is in the log at once.");
|
|
before.PageOpened(7, SitePages.Home, ok: true);
|
|
clock.Now = clock.Now.AddMinutes(6);
|
|
before.RecipeEnded(7, RecipeKind.FollowingSweep, RecipeStatus.Completed, null, 2, 3);
|
|
clock.Now = clock.Now.AddMinutes(1);
|
|
before.RecipeEnded(7, RecipeKind.ProfileCheck, RecipeStatus.Completed, null, 1, 0);
|
|
|
|
// While it runs, the report has it once, as it is now (not as its last line in the log).
|
|
var running = before.Gather();
|
|
Assert.AreEqual(2, running.Traces.Single().Recipes.Count);
|
|
Assert.AreEqual(0, running.UnfinishedSessions);
|
|
|
|
// Febe is killed: no end, no flush. After the restart the session is there up to its last line in progress.
|
|
clock.Now = clock.Now.AddMinutes(30);
|
|
var after = new CollectionDiagnostics(clock, log: new DiagnosticLog(folderOfLog, clock));
|
|
var gathered = after.Gather();
|
|
var trace = gathered.Traces.Single();
|
|
Assert.AreEqual(7, trace.Session);
|
|
Assert.IsTrue(trace.InProgress);
|
|
Assert.IsNull(trace.StopCause);
|
|
Assert.AreEqual(SitePages.Home, trace.Pages.Single().Page);
|
|
Assert.AreEqual(RecipeKind.FollowingSweep, trace.Recipes.Single().Kind, "The checkpoint at the end of the first recipe; the second came too soon after it.");
|
|
Assert.AreEqual(1, gathered.UnfinishedSessions);
|
|
|
|
using var stream = new MemoryStream();
|
|
DiagnosticReport.Write(stream, gathered.Traces, new DiagnosticReportHeader("0.1.2", "docker", clock.Now, TestZones.Rome, UnfinishedSessions: gathered.UnfinishedSessions));
|
|
using var json = JsonDocument.Parse(stream.ToArray());
|
|
Assert.AreEqual(1, json.RootElement.GetProperty("sessioniSenzaFine").GetInt32());
|
|
|
|
// A session that ends, and one closed by a shutdown, are not without an end; each is in the report once.
|
|
after.SessionStarted(8, clock.Now, 1);
|
|
after.RecipeEnded(8, RecipeKind.FollowingSweep, RecipeStatus.Completed, null, 1, 0);
|
|
after.SessionEnded(8, clock.Now.AddMinutes(2), SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
after.SessionStarted(9, clock.Now.AddMinutes(3), 1);
|
|
after.Dispose();
|
|
var last = new CollectionDiagnostics(clock, log: new DiagnosticLog(folderOfLog, clock)).Gather();
|
|
CollectionAssert.AreEqual(new long?[] { 7, 8, 9 }, last.Traces.Select(t => t.Session).ToArray());
|
|
Assert.IsFalse(last.Traces[1].InProgress);
|
|
Assert.AreEqual(SessionStopCause.Shutdown, last.Traces[2].StopCause);
|
|
Assert.AreEqual(1, last.UnfinishedSessions, "Only the one of the crash.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AnEndWhoseWriteFailedIsWrittenLaterAndTheSessionDoesNotLookCutShortAfterARestart()
|
|
{
|
|
using var folder = new TempFolder("diagnostica");
|
|
var clock = new ManualClock(new DateTimeOffset(2026, 11, 13, 9, 0, 0, TimeSpan.Zero), TestZones.Rome);
|
|
var folderOfLog = folder.Combine("diagnostica");
|
|
var before = new CollectionDiagnostics(clock, log: new DiagnosticLog(folderOfLog, clock));
|
|
var today = Path.Combine(folderOfLog, DiagnosticLog.FileNameFor(LocalCalendar.Today(clock.Now, clock.LocalZone)));
|
|
|
|
// The start is written; the end is not (another program holds the file for a moment).
|
|
before.SessionStarted(7, clock.Now, 1);
|
|
using (new FileStream(today, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
|
|
{
|
|
before.SessionEnded(7, clock.Now.AddMinutes(5), SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
}
|
|
|
|
Assert.AreEqual(1, before.Faults, "The failed write is counted.");
|
|
Assert.IsTrue(new DiagnosticLog(folderOfLog, clock).ReadAll().Traces.Single().InProgress, "Only the start is in the log so far.");
|
|
|
|
// The next line written brings the end along.
|
|
before.SessionStarted(8, clock.Now.AddMinutes(10), 1);
|
|
before.SessionEnded(8, clock.Now.AddMinutes(15), SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
|
|
// The same with the report: gathering it writes what is still waiting.
|
|
before.SessionStarted(9, clock.Now.AddMinutes(20), 1);
|
|
using (new FileStream(today, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
|
|
{
|
|
before.SessionEnded(9, clock.Now.AddMinutes(25), SessionOutcome.Completed, SessionStopCause.WorkDone);
|
|
}
|
|
|
|
Assert.AreEqual(2, before.Faults);
|
|
Assert.AreEqual(0, before.Gather().UnfinishedSessions);
|
|
|
|
// Febe is killed and restarted: every session ended, none without an end.
|
|
var after = new CollectionDiagnostics(clock, log: new DiagnosticLog(folderOfLog, clock)).Gather();
|
|
CollectionAssert.AreEqual(new long?[] { 7, 8, 9 }, after.Traces.Select(t => t.Session).ToArray());
|
|
Assert.IsTrue(after.Traces.All(t => !t.InProgress && t.StopCause == SessionStopCause.WorkDone));
|
|
Assert.AreEqual(0, after.UnfinishedSessions);
|
|
Assert.AreEqual(2, before.Faults, "Nothing failed again.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ASignalReportedStraightToTheBreakerIsInTheTraceOnceEvenWhenTheDetectorSaidItToo()
|
|
{
|
|
var clock = new ManualClock(Start);
|
|
long? current = 3;
|
|
var diagnostics = new CollectionDiagnostics(clock, () => current);
|
|
diagnostics.SessionStarted(3, clock.Now, 1);
|
|
|
|
// The session cookie lost: the connection tells the breaker, never the detector.
|
|
var lost = new Signal(SignalLevel.Disconnect, SignalRules.SessionCookieMissingCode, "https://sito.esempio/accedi/", clock.Now);
|
|
diagnostics.BreakerChanged(new BreakerState(BreakerStatus.Disconnected, null, lost, null));
|
|
|
|
// A throttle the detector saw: the breaker changes first, then the detector raises it; once in the trace.
|
|
clock.Now = clock.Now.AddMinutes(1);
|
|
var throttle = new Signal(SignalLevel.Throttle, "http-429", null, clock.Now);
|
|
diagnostics.BreakerChanged(new BreakerState(BreakerStatus.Throttled, clock.Now.AddHours(1), throttle, null));
|
|
diagnostics.SignalRaised(throttle);
|
|
|
|
// The breaker reopening is not a signal.
|
|
diagnostics.BreakerChanged(new BreakerState(BreakerStatus.Closed, null, throttle, null));
|
|
diagnostics.SessionEnded(3, clock.Now, SessionOutcome.Throttled, SessionStopCause.BreakerOpen);
|
|
|
|
var trace = diagnostics.Recent.Single();
|
|
CollectionAssert.AreEqual(
|
|
new[] { SignalRules.SessionCookieMissingCode, "http-429" },
|
|
trace.Signals.Select(s => s.Code).ToArray());
|
|
Assert.AreEqual(SignalLevel.Disconnect, trace.Signals[0].Level);
|
|
}
|
|
|
|
// ---- The exported file ----
|
|
|
|
[TestMethod]
|
|
public void TheReportIsIndentedJsonWithoutBomWithItsHeadAndLocalTimes()
|
|
{
|
|
var clock = new ManualClock(new DateTimeOffset(2026, 11, 13, 9, 30, 0, TimeSpan.Zero), TestZones.Rome);
|
|
var diagnostics = new CollectionDiagnostics(clock);
|
|
diagnostics.SessionStarted(4, clock.Now, 1);
|
|
diagnostics.SessionEnded(4, clock.Now.AddMinutes(5), SessionOutcome.Throttled, SessionStopCause.BreakerOpen);
|
|
diagnostics.SignalRaised(new Signal(SignalLevel.Challenge, "challenge-url", "https://sito/challenge/", clock.Now));
|
|
|
|
using var stream = new MemoryStream();
|
|
DiagnosticReport.Write(stream, diagnostics.Collect(), new DiagnosticReportHeader("0.1.2", "windows", clock.Now, TestZones.Rome));
|
|
var bytes = stream.ToArray();
|
|
Assert.AreEqual((byte)'{', bytes[0], "No BOM.");
|
|
var text = Encoding.UTF8.GetString(bytes);
|
|
StringAssert.Contains(text, "\n \"rapporto\": \"diagnostica della raccolta\"", "Indented.");
|
|
using var json = JsonDocument.Parse(text);
|
|
var root = json.RootElement;
|
|
Assert.AreEqual("0.1.2", root.GetProperty("versione").GetString());
|
|
Assert.AreEqual("windows", root.GetProperty("edizione").GetString());
|
|
Assert.AreEqual("2026-11-13T10:30:00+01:00", root.GetProperty("creato").GetString());
|
|
Assert.AreEqual(DiagnosticReport.ZoneName(TestZones.Rome), root.GetProperty("fuso").GetString());
|
|
Assert.AreNotEqual(CollectionDiagnostics.Other, root.GetProperty("fuso").GetString());
|
|
Assert.AreEqual(14, root.GetProperty("giorni").GetInt32());
|
|
Assert.AreEqual(0, root.GetProperty("righeRovinate").GetInt32());
|
|
Assert.AreEqual(0, root.GetProperty("erroriRegistratore").GetInt32());
|
|
var session = root.GetProperty("sessioni").EnumerateArray().Single();
|
|
Assert.AreEqual(4, session.GetProperty("session").GetInt64());
|
|
Assert.AreEqual("BreakerOpen", session.GetProperty("stopCause").GetString());
|
|
Assert.AreEqual("Throttled", session.GetProperty("outcome").GetString());
|
|
Assert.AreEqual("2026-11-13T10:30:00+01:00", session.GetProperty("start").GetString(), "Times in Alberto's zone.");
|
|
var outside = root.GetProperty("fuoriSessione").EnumerateArray().Single();
|
|
Assert.IsFalse(outside.TryGetProperty("session", out _));
|
|
Assert.AreEqual("challenge-url", outside.GetProperty("signals")[0].GetProperty("code").GetString());
|
|
Assert.AreEqual("febe-diagnostica-2026-11-13.json", DiagnosticReport.FileName(new DateTimeOffset(2026, 11, 13, 22, 59, 0, TimeSpan.Zero), TestZones.Rome));
|
|
Assert.AreEqual("febe-diagnostica-2026-11-14.json", DiagnosticReport.FileName(new DateTimeOffset(2026, 11, 13, 23, 0, 0, TimeSpan.Zero), TestZones.Rome), "The local day.");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TheZoneOfTheWindowsEditionIsWrittenToo()
|
|
{
|
|
// The clock of Febe for Windows has TimeZoneInfo.Local, whose name there is a Windows one, with spaces.
|
|
var windows = TimeZoneInfo.CreateCustomTimeZone("W. Europe Standard Time", TimeSpan.FromHours(1), "W. Europe Standard Time", "W. Europe Standard Time");
|
|
using var stream = new MemoryStream();
|
|
DiagnosticReport.Write(stream, [], new DiagnosticReportHeader("0.1.2", "windows", Start, windows));
|
|
using var json = JsonDocument.Parse(stream.ToArray());
|
|
var zone = json.RootElement.GetProperty("fuso").GetString();
|
|
Assert.AreNotEqual(CollectionDiagnostics.Other, zone);
|
|
Assert.IsTrue(zone is "W._Europe_Standard_Time" || zone!.StartsWith("Europe/", StringComparison.Ordinal), zone);
|
|
}
|
|
|
|
private static ResponseDescriptor Response(string id, HostKind host, string path, string mime = "application/json", string? resourceType = "Fetch", bool mainFrame = false, int status = 200) =>
|
|
new(id, host, "https://indirizzo.non.usato" + path, "indirizzo.non.usato", path, status, mime, resourceType, mainFrame, Start);
|
|
|
|
private static string Letters(int i)
|
|
{
|
|
var sb = new StringBuilder();
|
|
do
|
|
{
|
|
sb.Append((char)('a' + (i % 26)));
|
|
i /= 26;
|
|
}
|
|
while (i > 0);
|
|
return sb.ToString();
|
|
}
|
|
}
|