Files
Chronos/tests/Titano.App.Tests/About/UpdateCheckTests.cs
T
Claude 8d4b00aceb test(app): screen smoke tests and thumbnail decoding on Windows
Every screen is rendered from the --capture sample data in an off-screen
main window in both themes; thumbnails are decoded from embedded previews
with orientation. The sign-in flow now stores the login token on every
IServerApi, and thumbnail pending loads include index lookups so capture
waits for them. A cancelled-load test no longer depends on scheduling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 12:40:57 +00:00

75 lines
3.6 KiB
C#

using System.Net;
using System.Net.Http;
using System.Text;
using Titano.App.About;
using Titano.Testing;
namespace Titano.App.Tests.About;
public sealed class UpdateCheckTests
{
[Theory]
[Case("v1.2.3", "1.2.3")]
[Case("1.2", "1.2.0")]
[Case("V0.10.0-beta.2", "0.10.0")]
[Case("v2.0.1+abc", "2.0.1")]
public void TagsParseToVersions(string tag, string expected) =>
Assert.Equal(Version.Parse(expected), UpdateCheck.ParseVersion(tag));
[Theory]
[Case(new object?[] { null })]
[Case("")]
[Case("latest")]
[Case("v-one")]
public void OtherTagsAreIgnored(string? tag) => Assert.Null(UpdateCheck.ParseVersion(tag));
[Test]
public void TheLatestReleaseIsComparedWithTheRunningVersion()
{
var release = UpdateCheck.Parse("""{ "tag_name": "v0.2.0", "html_url": "https://github.com/o/r/releases/tag/v0.2.0", "draft": false, "prerelease": false }""");
Assert.NotNull(release);
Assert.Equal(new Version(0, 2, 0), release!.Version);
Assert.True(UpdateCheck.IsNewer(release, UpdateCheck.RunningVersion("0.1.0")));
Assert.False(UpdateCheck.IsNewer(release, UpdateCheck.RunningVersion("0.2.0+local")));
Assert.False(UpdateCheck.IsNewer(release, new Version(0, 2, 0, 0)));
Assert.False(UpdateCheck.IsNewer(release, UpdateCheck.RunningVersion("1.0.0")));
Assert.Equal("0.2.0", UpdateCheck.Display(release.Version));
}
[Test]
public void DraftsPrereleasesAndOddLinksAreIgnored()
{
Assert.Null(UpdateCheck.Parse("""{ "tag_name": "v9.0.0", "html_url": "https://x", "prerelease": true }"""));
Assert.Null(UpdateCheck.Parse("""{ "tag_name": "v9.0.0", "html_url": "https://x", "draft": true }"""));
Assert.Null(UpdateCheck.Parse("""{ "tag_name": "v9.0.0", "html_url": "javascript:alert(1)" }"""));
Assert.Null(UpdateCheck.Parse("""{ "html_url": "https://x" }"""));
Assert.Null(UpdateCheck.Parse("[]"));
}
[Test]
public async Task FetchReadsTheApiAndToleratesMissingReleases()
{
using var ok = new HttpClient(new Reply(HttpStatusCode.OK, """{ "tag_name": "v1.0.0", "html_url": "https://github.com/o/r/releases/tag/v1.0.0" }"""));
var release = await UpdateCheck.FetchAsync(ok, "https://api.github.com/repos/o/r/releases/latest", "0.1.0", CancellationToken.None);
Assert.Equal(new Version(1, 0, 0), release!.Version);
using var none = new HttpClient(new Reply(HttpStatusCode.NotFound, "{}"));
Assert.Null(await UpdateCheck.FetchAsync(none, "https://api.github.com/repos/o/r/releases/latest", "0.1.0", CancellationToken.None));
using var garbage = new HttpClient(new Reply(HttpStatusCode.OK, "<html>"));
Assert.Null(await UpdateCheck.FetchAsync(garbage, "https://api.github.com/repos/o/r/releases/latest", "0.1.0", CancellationToken.None));
using var broken = new HttpClient(new Reply(HttpStatusCode.InternalServerError, "{}"));
await Assert.ThrowsAsync<HttpRequestException>(() => UpdateCheck.FetchAsync(broken, "https://api.github.com/repos/o/r/releases/latest", "0.1.0", CancellationToken.None));
}
private sealed class Reply(HttpStatusCode status, string body) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Assert.True(request.Headers.UserAgent.ToString().StartsWith("Titano/", StringComparison.Ordinal), "GitHub requires a User-Agent");
return Task.FromResult(new HttpResponseMessage(status) { Content = new StringContent(body, Encoding.UTF8, "application/json") });
}
}
}