using System.Globalization;
using Febe.SitoDiProva;
namespace Febe.Tests.Support;
///
/// The one way the tests start the local test site (spec §14.2), so that a run can choose the seed of its synthetic
/// world: when it is set, otherwise (nothing changes when it is unset).
/// The CI runs the whole suite with the default seed, then the tests of again with a seed of
/// its own that it prints in the log: a failure is reproduced by setting the same variable.
///
internal static class SyntheticSite
{
/// Environment variable with the seed of the synthetic world.
public const string Variable = "FEBE_SEME_PROVA";
/// The seed when the variable is not set: the one of the command-line site and of the cleaned samples.
public const int DefaultSeed = 13;
/// Test category of the tests whose world comes from the seed (the CI runs them with a second seed).
public const string Category = "SitoDiProva";
/// The seed of this run.
public static int Seed => ParseSeed(Environment.GetEnvironmentVariable(Variable));
/// Starts the test site with the seed of this run, and says which one in the output of the test.
/// Clock of the site (dates, signed addresses); the system clock by default.
public static Task StartAsync(TimeProvider? time = null)
{
var seed = Seed;
Console.WriteLine($"Sito di prova con il seme {seed.ToString(CultureInfo.InvariantCulture)} (si cambia con {Variable}).");
return TestSite.StartAsync(new TestSiteOptions { Seed = seed, Time = time ?? TimeProvider.System });
}
/// The seed written in : the default when it is empty, an error when it is not a number.
public static int ParseSeed(string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
return DefaultSeed;
}
return int.TryParse(value.Trim(), NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var seed)
? seed
: throw new InvalidOperationException($"{Variable} deve essere un numero intero (fra -2147483648 e 2147483647), non «{value}».");
}
}