using System.Globalization;
using Chronos.App.Jobs;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Data.Scheduling;
using Chronos.Protocol;
using Chronos.Testing;
namespace Chronos.App.Tests.Jobs;
///
/// Jobs › Scheduled in another language (owner review 2, R10). These switch the process-wide UI language, so the class is
/// serial: in parallel it would race with the English assertions of the classes running beside it.
///
[Serial]
public sealed class ScheduleLanguageTests(TestContext context)
{
/// The days of a weekly schedule read in the order of the editor's toggles: from the UI language's first day.
[Test]
public void WeeklyDaysFollowTheWeekOfTheUiLanguage()
{
var weekend = ScheduleRecurrence.WeeklyAt([DayOfWeek.Saturday, DayOfWeek.Sunday], new TimeOnly(3, 0));
try
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
Assert.Equal(DayOfWeek.Sunday, Loc.Culture.DateTimeFormat.FirstDayOfWeek);
Assert.Equal(Loc.L("Jobs.Schedule.DayList", ScheduleText.DayShort(DayOfWeek.Sunday), ScheduleText.DayShort(DayOfWeek.Saturday)),
ScheduleText.Days(weekend.Days));
Assert.Equal(Loc.L("Jobs.Schedule.When.Weekly", ScheduleText.Days(weekend.Days), ScheduleText.Time(new TimeOnly(3, 0))), ScheduleText.When(weekend));
Loc.SetCulture(CultureInfo.GetCultureInfo("it"));
Assert.Equal(DayOfWeek.Monday, Loc.Culture.DateTimeFormat.FirstDayOfWeek);
Assert.Equal(Loc.L("Jobs.Schedule.DayList", ScheduleText.DayShort(DayOfWeek.Saturday), ScheduleText.DayShort(DayOfWeek.Sunday)),
ScheduleText.Days(weekend.Days));
Assert.Equal(Loc.L("Jobs.Schedule.DayList", ScheduleText.DayShort(DayOfWeek.Monday), ScheduleText.DayShort(DayOfWeek.Thursday)),
ScheduleText.Days([DayOfWeek.Thursday, DayOfWeek.Monday]));
}
finally
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
}
}
/// The editor's dropdowns say their choices in the new language, and keep what was chosen.
[Test]
public async Task TheOpenEditorSaysItsChoicesInTheNewLanguage()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
await using var fx = await ScheduleFixture.CreateAsync(context);
using var vm = await fx.ViewModelAsync();
vm.NewCommand.Execute(null);
var editor = Assert.NotNull(vm.Editor);
editor.SelectedAction = editor.ActionOptions.Single(a => a.Name == ScheduleActions.Verify);
editor.SelectedUnit = editor.Units.Single(u => u.Value == RecurrenceUnit.Minutes);
editor.SelectedPriority = editor.Priorities.Single(p => p.Value == JobPriority.Normal);
Assert.Equal(("minutes", "Normal"), (editor.SelectedUnit.Label, editor.SelectedPriority.Label));
var actions = editor.ActionOptions;
try
{
Loc.SetCulture(CultureInfo.GetCultureInfo("it"));
Assert.SequenceEqual(Enum.GetValues().Select(u => Loc.L("Jobs.Editor.Unit." + u)), editor.Units.Select(u => u.Label));
Assert.SequenceEqual(["In background", "Normale", "Alta"], editor.Priorities.Select(p => p.Label));
Assert.Equal((RecurrenceUnit.Minutes, "minuti"), (editor.SelectedUnit.Value, editor.SelectedUnit.Label));
Assert.Equal((JobPriority.Normal, "Normale"), (editor.SelectedPriority.Value, editor.SelectedPriority.Label));
Assert.True(editor.Units.Any(u => ReferenceEquals(u, editor.SelectedUnit)) && editor.Priorities.Any(p => ReferenceEquals(p, editor.SelectedPriority)),
"the choices are among the new options");
// The actions are new options too, so their dropdown reads their names again.
Assert.False(ReferenceEquals(actions, editor.ActionOptions), "the action list was built again");
Assert.Equal(("Scansione archivio", ScheduleActions.Verify), (editor.ActionOptions[0].Label, editor.SelectedAction.Name));
Assert.True(editor.ActionOptions.Any(o => ReferenceEquals(o, editor.SelectedAction)), "the action is among the new options");
}
finally
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
}
}
}