Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 78 additions & 5 deletions turn-one-backend/API/Controllers/SimracingLinkController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ namespace API.Controllers;

/// <summary>
/// Release metadata for the Turn One Link desktop app, so the download pages can render real
/// version/size/checksum information instead of hardcoding it.
/// version/size/checksum information instead of hardcoding it, plus the release channels and
/// minimum versions Link checks at startup (HCR-014).
/// </summary>
[ApiController]
[Route("api/simracing/link")]
public class SimracingLinkController : ControllerBase
{
private const string DefaultFeedUrl = "https://github.com/MihaiM21/Turn-One-Link";

private readonly IConfiguration _config;

public SimracingLinkController(IConfiguration config)
Expand All @@ -29,6 +32,11 @@ public class LinkReleaseDto
public long? SizeBytes { get; set; }
public string MinWindowsBuild { get; set; } = "Windows 10 (build 19041) or later";
public List<ChangelogEntry> Changelog { get; set; } = new();

// HCR-014 additions (absent from older clients' expectations, so purely additive).
public string Channel { get; set; } = "stable";
public string? MinSupportedVersion { get; set; }
public string VelopackFeedUrl { get; set; } = DefaultFeedUrl;
}

public class ChangelogEntry
Expand All @@ -38,12 +46,32 @@ public class ChangelogEntry
public List<string> Changes { get; set; } = new();
}

public class CompatDto
{
public bool Supported { get; set; }
public bool UploadsAccepted { get; set; }
public string? LatestVersion { get; set; }
public string? Message { get; set; }
}

/// <summary>
/// Stable comes from the flat <c>TurnOneLink</c> keys (as before) or <c>TurnOneLink:Channels:stable</c>;
/// beta from <c>TurnOneLink:Channels:beta</c>, falling back to stable when no beta is configured.
/// </summary>
[HttpGet("release")]
[AllowAnonymous]
[ResponseCache(Duration = 300, Location = ResponseCacheLocation.Any)]
public ActionResult<LinkReleaseDto> GetRelease()
public ActionResult<LinkReleaseDto> GetRelease([FromQuery] string? channel = null)
{
var section = _config.GetSection("TurnOneLink");
var root = _config.GetSection("TurnOneLink");
var wantBeta = string.Equals(channel, "beta", StringComparison.OrdinalIgnoreCase);

var beta = root.GetSection("Channels:beta");
var stable = root.GetSection("Channels:stable");
var (section, resolved) = wantBeta && beta.Exists()
? (beta, "beta")
: (stable.Exists() ? stable : root, "stable");

var downloadUrl = section["DownloadUrl"];

var dto = new LinkReleaseDto
Expand All @@ -52,7 +80,10 @@ public ActionResult<LinkReleaseDto> GetRelease()
Version = section["Version"],
DownloadUrl = string.IsNullOrWhiteSpace(downloadUrl) ? null : downloadUrl,
Sha256 = section["Sha256"],
MinWindowsBuild = section["MinWindowsBuild"] ?? "Windows 10 (build 19041) or later"
MinWindowsBuild = root["MinWindowsBuild"] ?? "Windows 10 (build 19041) or later",
Channel = resolved,
MinSupportedVersion = root["MinSupportedVersion"],
VelopackFeedUrl = root["VelopackFeedUrl"] ?? DefaultFeedUrl,
};

if (DateTime.TryParse(section["ReleasedAt"], out var released))
Expand All @@ -61,8 +92,50 @@ public ActionResult<LinkReleaseDto> GetRelease()
if (long.TryParse(section["SizeBytes"], out var size))
dto.SizeBytes = size;

dto.Changelog = section.GetSection("Changelog").Get<List<ChangelogEntry>>() ?? new List<ChangelogEntry>();
dto.Changelog = root.GetSection("Changelog").Get<List<ChangelogEntry>>() ?? new List<ChangelogEntry>();

return Ok(dto);
}

/// <summary>Whether a Link build may still run and upload. Link calls this at startup.</summary>
[HttpGet("compat")]
[AllowAnonymous]
[ResponseCache(Duration = 300, Location = ResponseCacheLocation.Any)]
public ActionResult<CompatDto> Compat([FromQuery] string? version)
{
if (!TryParseVersion(version, out var current))
return BadRequest(new { message = "version must look like 1.2.3" });

var root = _config.GetSection("TurnOneLink");
var supported = IsAtLeast(current, root["MinSupportedVersion"]);
var uploads = supported && IsAtLeast(current, root["MinUploadVersion"]);
var latest = root.GetSection("Channels:stable")["Version"] ?? root["Version"];

return Ok(new CompatDto
{
Supported = supported,
UploadsAccepted = uploads,
LatestVersion = latest,
Message = !supported
? "This version of Turn One Link is no longer supported. Please update."
: !uploads
? "Update Turn One Link to keep uploading laps — this version has a known timing bug."
: null,
});
}

/// <summary>True when <paramref name="minimum"/> is unset or <paramref name="current"/> is at least it.</summary>
public static bool IsAtLeast(Version current, string? minimum) =>
!TryParseVersion(minimum, out var min) || current >= min;

/// <summary>Accepts "1.2", "1.2.3", "v1.2.3" and "1.2.3-beta" (pre-release suffix ignored).</summary>
public static bool TryParseVersion(string? value, out Version version)
{
version = new Version(0, 0);
if (string.IsNullOrWhiteSpace(value)) return false;
var core = value.Trim().TrimStart('v', 'V').Split('-', '+')[0];
if (!Version.TryParse(core, out var parsed)) return false;
version = parsed;
return true;
}
}
82 changes: 82 additions & 0 deletions turn-one-backend/Tests/API/SimracingLinkControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using API.Controllers;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Tests.API;

/// <summary>HCR-014: release channels, minimum versions, unchanged default response.</summary>
public class SimracingLinkControllerTests
{
private static SimracingLinkController Controller(Dictionary<string, string?> settings) =>
new(new ConfigurationBuilder().AddInMemoryCollection(settings).Build());

private static readonly Dictionary<string, string?> Legacy = new()
{
["TurnOneLink:Version"] = "1.2.0",
["TurnOneLink:DownloadUrl"] = "https://example.test/TurnOneLink-win-Setup.exe",
["TurnOneLink:Sha256"] = "abc",
["TurnOneLink:ReleasedAt"] = "2026-11-06",
};

private static T Ok<T>(ActionResult<T> result) => (T)((OkObjectResult)result.Result!).Value!;

[Fact]
public void Default_Request_Still_Returns_The_Flat_Stable_Release()
{
var dto = Ok(Controller(Legacy).GetRelease());

dto.Available.Should().BeTrue();
dto.Version.Should().Be("1.2.0");
dto.DownloadUrl.Should().EndWith("Setup.exe");
dto.Channel.Should().Be("stable");
dto.VelopackFeedUrl.Should().Contain("Turn-One-Link");
}

[Fact]
public void Beta_Channel_Uses_Its_Own_Section_Or_Falls_Back_To_Stable()
{
Ok(Controller(Legacy).GetRelease("beta")).Channel.Should().Be("stable");

var withBeta = new Dictionary<string, string?>(Legacy)
{
["TurnOneLink:Channels:beta:Version"] = "1.3.0",
["TurnOneLink:Channels:beta:DownloadUrl"] = "https://example.test/beta.exe",
};
var dto = Ok(Controller(withBeta).GetRelease("beta"));
dto.Channel.Should().Be("beta");
dto.Version.Should().Be("1.3.0");
}

[Theory]
[InlineData("1.1.0", false, false)]
[InlineData("1.2.0", true, false)]
[InlineData("1.2.1", true, true)]
[InlineData("v1.3.0-beta", true, true)]
public void Compat_Applies_Minimum_Versions(string version, bool supported, bool uploads)
{
var settings = new Dictionary<string, string?>(Legacy)
{
["TurnOneLink:MinSupportedVersion"] = "1.2.0",
["TurnOneLink:MinUploadVersion"] = "1.2.1",
};
var dto = Ok(Controller(settings).Compat(version));

dto.Supported.Should().Be(supported);
dto.UploadsAccepted.Should().Be(uploads);
}

[Fact]
public void Compat_Accepts_Everything_When_No_Minimum_Is_Configured()
{
var dto = Ok(Controller(Legacy).Compat("0.0.1"));
dto.Supported.Should().BeTrue();
dto.UploadsAccepted.Should().BeTrue();
}

[Fact]
public void Compat_Rejects_A_Malformed_Version()
{
Controller(Legacy).Compat("latest").Result.Should().BeOfType<BadRequestObjectResult>();
}
}
7 changes: 6 additions & 1 deletion turn-one-client/lib/simracing/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export interface LinkReleaseInfo {
sizeBytes: number | null;
minWindowsBuild: string;
changelog: { version: string; date: string | null; changes: string[] }[];
/** HCR-014: "stable" or "beta" — the channel this release belongs to. */
channel?: "stable" | "beta";
minSupportedVersion?: string | null;
velopackFeedUrl?: string;
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -242,7 +246,8 @@ export const setVisibility = (id: string, visibility: number) =>
export const deleteSession = (id: string) =>
request<void>(`/api/telemetry/sessions/${id}`, { method: "DELETE" });

export const getLinkRelease = () => request<LinkReleaseInfo>("/api/simracing/link/release");
export const getLinkRelease = (channel: "stable" | "beta" = "stable") =>
request<LinkReleaseInfo>(`/api/simracing/link/release${channel === "beta" ? "?channel=beta" : ""}`);

// ---------------------------------------------------------------------------
// Lap-telemetry API v2
Expand Down
Loading