Files
Chronos/tests/Chronos.Core.Tests/Math/SolarPositionTests.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

112 lines
6.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Globalization;
using Chronos.Core.Mathematics;
using Chronos.Testing;
namespace Chronos.Core.Tests.Mathematics;
/// <summary>
/// Reference values are NOAA Solar Calculator results (spreadsheet equations as implemented by the
/// <c>astral</c> package, cross-checked against PyEphem/VSOP87 to within 35 s), UTC, for the local solar day.
/// </summary>
public sealed class SolarPositionTests(TestContext context)
{
private const double OneMinute = 60;
[Theory]
[Case("Greenwich summer", 51.4769, -0.0005, "2024-06-21", "03:43:13", "20:20:36", "02:54:34", "21:09:14", "01:40:14", "22:23:33", null, null)]
[Case("Greenwich winter", 51.4769, -0.0005, "2024-12-21", "08:03:42", "15:52:56", "07:22:37", "16:34:01", "06:39:40", "17:16:58", "05:58:57", "17:57:41")]
[Case("New York equinox", 40.7128, -74.0060, "2024-03-20", "10:58:42", "23:08:28", "10:30:54", "23:36:20", "09:59:06", "+00:08:13", "09:26:36", "+00:40:50")]
[Case("Sydney winter", -33.8688, 151.2093, "2024-06-21", "-21:00:18", "06:53:41", "-20:32:02", "07:21:57", "-20:00:56", "07:53:03", "-19:30:30", "08:23:28")]
[Case("Quito equinox", -0.1807, -78.4678, "2024-09-22", "11:03:17", "23:09:26", "10:42:14", "23:30:29", "10:18:21", "23:54:22", "09:54:23", "+00:18:19")]
[Case("Tromso March", 69.6492, 18.9553, "2024-03-01", "06:08:32", "15:46:01", "05:06:36", "16:48:12", "03:57:47", "17:57:27", "02:44:21", "19:11:43")]
// The sun grazes −6° here, so tiny ephemeris differences move the crossing by minutes: astral's single-pass
// NOAA value is 77 s early, while the iterated NOAA solution and PyEphem agree on 08:31:31 / 12:53:21.
[Case("Tromso polar night", 69.6492, 18.9553, "2024-12-21", null, null, "08:31:31", "12:53:21", "06:46:35", "14:38:21", "05:28:22", "15:56:34")]
[Case("Tromso midnight sun", 69.6492, 18.9553, "2024-06-21", null, null, null, null, null, null, null, null)]
[Case("Golden NREL SPA site", 39.742476, -105.1786, "2003-10-17", "13:12:57", "+00:18:36", "12:45:10", "+00:46:21", "12:13:59", "+01:17:30", "11:42:47", "+01:48:39")]
public void EventsMatchNoaaWithinOneMinute(string place, double latitude, double longitude, string date,
string? sunrise, string? sunset, string? civilDawn, string? civilDusk, string? nauticalDawn, string? nauticalDusk, string? astroDawn, string? astroDusk)
{
var day = DateOnly.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
var events = SolarPosition.Events(day, latitude, longitude);
double[] deviations =
[
Check(place + " sunrise", day, sunrise, events.Sunrise),
Check(place + " sunset", day, sunset, events.Sunset),
Check(place + " civil dawn", day, civilDawn, events.CivilDawn),
Check(place + " civil dusk", day, civilDusk, events.CivilDusk),
Check(place + " nautical dawn", day, nauticalDawn, events.NauticalDawn),
Check(place + " nautical dusk", day, nauticalDusk, events.NauticalDusk),
Check(place + " astronomical dawn", day, astroDawn, events.AstronomicalDawn),
Check(place + " astronomical dusk", day, astroDusk, events.AstronomicalDusk),
];
context.Log(string.Create(CultureInfo.InvariantCulture, $"{place}: worst deviation {deviations.Max():F1} s"));
}
[Theory]
[Case(51.4769, -0.0005, "2024-06-21", "12:01:49")]
[Case(-33.8688, 151.2093, "2024-06-21", "01:56:58")]
[Case(39.742476, -105.1786, "2003-10-17", "18:46:14")]
public void SolarNoonMatchesNoaa(double latitude, double longitude, string date, string noon)
{
_ = latitude;
var day = DateOnly.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
Check("noon", day, noon, SolarPosition.SolarNoon(day, longitude));
}
[Theory]
[Case(39.742476, -105.1786, "2003-10-17T19:30:30", 39.8914, 39.8721, 194.3426)]
[Case(51.4769, -0.0005, "2024-06-21T12:00:00", 61.9665, 61.9579, 179.0598)]
[Case(-33.8688, 151.2093, "2024-01-15T06:30:00", 30.7712, 30.7442, 263.8208)]
[Case(35.6762, 139.6503, "2024-09-01T21:00:00", 8.4899, 8.3861, 86.3181)]
[Case(64.1466, -21.9426, "2024-12-01T15:00:00", 1.9248, 1.6140, 203.7679)]
public void PositionMatchesNoaa(double latitude, double longitude, string utc, double elevation, double geometric, double azimuth)
{
var time = DateTime.SpecifyKind(DateTime.ParseExact(utc, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture), DateTimeKind.Utc);
var sun = SolarPosition.Compute(time, latitude, longitude);
Assert.Approximately(geometric, sun.GeometricElevation, 0.01);
Assert.Approximately(elevation, sun.Elevation, 0.01);
Assert.Approximately(azimuth, sun.Azimuth, 0.01);
}
[Theory]
[Case(10.0, DayPhase.Day)]
[Case(3.0, DayPhase.GoldenHour)]
[Case(-0.5, DayPhase.GoldenHour)]
[Case(-3.0, DayPhase.CivilTwilight)]
[Case(-8.0, DayPhase.NauticalTwilight)]
[Case(-15.0, DayPhase.AstronomicalTwilight)]
[Case(-30.0, DayPhase.Night)]
public void ClassifiesElevation(double elevation, DayPhase expected) => Assert.Equal(expected, SolarPosition.Classify(elevation));
[Test]
public void PhaseFollowsTheEvents()
{
var day = new DateOnly(2024, 12, 21);
var events = SolarPosition.Events(day, 51.4769, -0.0005);
Assert.Equal(DayPhase.Day, SolarPosition.Phase(events.SolarNoon, 51.4769, -0.0005));
Assert.Equal(DayPhase.GoldenHour, SolarPosition.Phase(events.Sunset!.Value.AddMinutes(-5), 51.4769, -0.0005));
Assert.Equal(DayPhase.CivilTwilight, SolarPosition.Phase(events.Sunset!.Value.AddMinutes(5), 51.4769, -0.0005));
Assert.Equal(DayPhase.Night, SolarPosition.Phase(events.AstronomicalDusk!.Value.AddMinutes(5), 51.4769, -0.0005));
Assert.Throws<ArgumentOutOfRangeException>(() => SolarPosition.Compute(DateTime.UtcNow, 91, 0));
}
/// <summary>Expected "HH:mm:ss" on <paramref name="day"/>; a leading '-' or '+' means the previous or next UTC day.
/// Returns the absolute deviation in seconds (0 when the event is correctly absent).</summary>
private static double Check(string label, DateOnly day, string? expected, DateTime? actual)
{
if (expected is null)
{
Assert.Null(actual);
return 0;
}
var offsetDays = expected[0] switch { '-' => -1, '+' => 1, _ => 0 };
var clock = TimeOnly.ParseExact(expected.TrimStart('+', '-'), "HH:mm:ss", CultureInfo.InvariantCulture);
var reference = day.AddDays(offsetDays).ToDateTime(clock, DateTimeKind.Utc);
var value = Assert.NotNull(actual, label);
var deviation = (value - reference).TotalSeconds;
Assert.Approximately(0, deviation, OneMinute, label);
return Math.Abs(deviation);
}
}