using System.Globalization; using System.Text.Json; using System.Text.Json.Nodes; using Febe.SitoDiProva; // Local test site (spec §14.2). // febe-sito-di-prova [--porta 8481] [--porta-cdn 8482] [--indirizzo-cdn 127.0.0.2] [--seme 13] // febe-sito-di-prova campioni --cartella [--seme 13] if (args.Length > 0 && args[0] == "campioni") { return await DumpSamplesAsync(args[1..]); } var options = new TestSiteOptions { Port = IntOption(args, "--porta", 8481), CdnPort = IntOption(args, "--porta-cdn", 8482), CdnAddress = Option(args, "--indirizzo-cdn") ?? "127.0.0.2", Seed = IntOption(args, "--seme", 13), ConsoleLog = true, }; await using var site = await TestSite.StartAsync(options); Console.WriteLine($"Sito di prova: {site.SiteUrl}/ · CDN: {site.CdnUrl}/ · seme {options.Seed.ToString(CultureInfo.InvariantCulture)}"); Console.WriteLine($"Casi di arresto: {site.SiteUrl}/_controllo/modo?valore=troppe-richieste|verifica|sessione-scaduta|vuoto|normale"); Console.WriteLine("Ctrl+C per fermare."); var stop = new TaskCompletionSource(); Console.CancelKeyPress += (_, e) => { e.Cancel = true; stop.TrySetResult(); }; await stop.Task; return 0; // Writes the JSON responses of the main pages in the raw format of the probe (descriptor + body), to be cleaned with // «febe-sonda ripulisci». The data is synthetic, but the samples follow the same path as real ones (spec §14.5). static async Task DumpSamplesAsync(string[] args) { var folder = Option(args, "--cartella"); if (string.IsNullOrWhiteSpace(folder)) { Console.Error.WriteLine("Indicare --cartella ."); return 2; } Directory.CreateDirectory(folder); await using var site = await TestSite.StartAsync(new TestSiteOptions { Seed = IntOption(args, "--seme", 13) }); string marta, luca, code; lock (site.World.Gate) { marta = site.World.FindByUsername("marta.prova")!.Username; luca = site.World.FindByUsername("luca.prova")!.Username; code = site.World.FindByUsername("marta.prova")!.Posts.First(p => p.Files.Count > 1).Code; } var samples = new (string Name, string Path)[] { ("inizio", "/dati/inizio"), ("profilo-pubblico", $"/dati/profilo/{marta}?scheda=post"), ("profilo-pagina-successiva", $"/dati/profilo/{luca}/altri?scheda=post&dopo=c12"), ("profilo-privato-non-seguito", "/dati/profilo/riservato.prova?scheda=post"), ("post-serie", $"/dati/post/{code}"), ("storie", $"/dati/storie/{marta}"), ("raccolta", $"/dati/evidenza/{site.World.FindByUsername("marta.prova")!.Highlights[0].Id}"), ("seguiti", "/dati/seguiti"), ("taggati", $"/dati/profilo/{marta}?scheda=taggati"), }; var number = 0; foreach (var (name, path) in samples) { var body = JsonNode.Parse(await site.GetDataAsync(path)); await WriteSampleAsync(folder, ++number, name, site.Url(path), 200, body); } site.Control.Set(StopMode.EmptyPages); await WriteSampleAsync(folder, ++number, "pagina-vuota", site.Url($"/dati/profilo/{luca}/altri?scheda=post&dopo=c48"), 200, JsonNode.Parse(await site.GetDataAsync($"/dati/profilo/{luca}/altri?scheda=post&dopo=c48"))); site.Control.Set(StopMode.Normal); // 000011 is «forma-sconosciuta», written by hand. The reels of every account fit in one page (12): no next page. number++; await WriteSampleAsync(folder, ++number, "reel", site.Url($"/dati/profilo/{luca}?scheda=reel"), 200, JsonNode.Parse(await site.GetDataAsync($"/dati/profilo/{luca}?scheda=reel"))); Console.WriteLine($"Scritti {(number - 1).ToString(CultureInfo.InvariantCulture)} campioni grezzi in {folder} (il numero 11 si scrive a mano)."); return 0; } static async Task WriteSampleAsync(string folder, int number, string name, string url, int status, JsonNode? body) { var sample = new JsonObject { ["descrittore"] = new JsonObject { ["indirizzo"] = url.Split('?')[0], ["host"] = "sito", ["stato"] = status, ["tipo"] = "application/json", ["risorsa"] = "Fetch", ["nome"] = name, }, ["corpo"] = body, }; var file = Path.Combine(folder, $"{number.ToString("000000", CultureInfo.InvariantCulture)}-{name}.json"); await File.WriteAllTextAsync(file, sample.ToJsonString(new JsonSerializerOptions { WriteIndented = true })); } static string? Option(string[] args, string name) { var index = Array.IndexOf(args, name); return index >= 0 && index + 1 < args.Length ? args[index + 1] : null; } static int IntOption(string[] args, string name, int fallback) => int.TryParse(Option(args, name), NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) ? value : fallback;