-
Notifications
You must be signed in to change notification settings - Fork 30
Implement ghost system with leader boards, downloads, local saves + tests #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ebce510
added ghost data
jorge-2016 5ae137c
Refactor ghost track mapping to lazy per-course hash resolution
jorge-2016 b392f4c
test: add ghost mapping and track service coverage
jorge-2016 551b40e
fix: address coderabbit ghost UI and test findings
jorge-2016 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
WheelWizard.Test/Features/Ghosts/GhostTrackServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| using System.Collections.Concurrent; | ||
| using WheelWizard.Services; | ||
|
|
||
| namespace WheelWizard.Test.Features.Ghosts; | ||
|
|
||
| public class GhostTrackServiceTests | ||
| { | ||
| [Fact] | ||
| public async Task GetAllTracksAsync_LoadsTracksWithoutEagerHashResolution_AndCachesResults() | ||
| { | ||
| var handler = new StubHttpMessageHandler(new Dictionary<string, string> | ||
| { | ||
| ["https://rwfc.net/api/timetrial/tracks"] = | ||
| """ | ||
| [ | ||
| { "id": 1, "name": "Main Track", "courseId": 300, "category": "retro", "laps": 3, "supportsGlitch": true, "sortOrder": 1 }, | ||
| { "id": 2, "name": "Variant Track", "courseId": 300, "category": "retro", "laps": 3, "supportsGlitch": true, "sortOrder": 2 } | ||
| ] | ||
| """, | ||
| ["https://rwfc.net/api/timetrial/worldrecords/all?glitchAllowed=true&cc=150"] = | ||
| """ | ||
| [ | ||
| { "trackId": 1, "trackName": "Main Track", "activeWorldRecord": { "id": 100, "trackId": 1, "trackName": "Main Track", "playerName": "P1", "cc": 150, "finishTimeMs": 100000, "finishTimeDisplay": "1:40.000", "miiName": "Mii", "dateSet": "2026-01-01", "submittedAt": "2026-01-01T00:00:00Z", "rank": 1 } }, | ||
| { "trackId": 2, "trackName": "Variant Track", "activeWorldRecord": { "id": 101, "trackId": 2, "trackName": "Variant Track", "playerName": "P2", "cc": 150, "finishTimeMs": 101000, "finishTimeDisplay": "1:41.000", "miiName": "Mii", "dateSet": "2026-01-01", "submittedAt": "2026-01-01T00:00:00Z", "rank": 1 } } | ||
| ] | ||
| """ | ||
| }); | ||
|
|
||
| using var client = new HttpClient(handler); | ||
| var hexMapping = new TrackHexMappingService(); | ||
| var variantMapping = new TrackVariantMappingService(); | ||
| var service = new GhostTrackService(client, hexMapping, variantMapping); | ||
|
|
||
| var firstLoad = await service.GetAllTracksAsync(); | ||
| var secondLoad = await service.GetAllTracksAsync(); | ||
|
|
||
| Assert.Equal(2, firstLoad.Count); | ||
| Assert.All(firstLoad, t => Assert.Equal(string.Empty, t.HexValue)); | ||
| Assert.Equal(firstLoad.Count, secondLoad.Count); | ||
|
|
||
| Assert.Equal(1, handler.GetRequestCount("https://rwfc.net/api/timetrial/tracks")); | ||
| Assert.Equal(1, handler.GetRequestCount("https://rwfc.net/api/timetrial/worldrecords/all?glitchAllowed=true&cc=150")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task EnsureTrackMappingsInitializedAsync_InitializesVariantMappingAfterTrackLoad() | ||
| { | ||
| var handler = new StubHttpMessageHandler(new Dictionary<string, string> | ||
| { | ||
| ["https://rwfc.net/api/timetrial/tracks"] = | ||
| """ | ||
| [ | ||
| { "id": 10, "name": "Main Track", "courseId": 410, "category": "retro", "laps": 3, "supportsGlitch": true, "sortOrder": 1 }, | ||
| { "id": 11, "name": "Variant Track", "courseId": 410, "category": "retro", "laps": 3, "supportsGlitch": true, "sortOrder": 2 } | ||
| ] | ||
| """, | ||
| ["https://rwfc.net/api/timetrial/worldrecords/all?glitchAllowed=true&cc=150"] = "[]" | ||
| }); | ||
|
|
||
| using var client = new HttpClient(handler); | ||
| var hexMapping = new TrackHexMappingService(); | ||
| var variantMapping = new TrackVariantMappingService(); | ||
| var service = new GhostTrackService(client, hexMapping, variantMapping); | ||
|
|
||
| await service.GetAllTracksAsync(); | ||
| Assert.False(variantMapping.IsVariantTrack("Variant Track")); | ||
|
|
||
| await service.EnsureTrackMappingsInitializedAsync(); | ||
|
|
||
| Assert.True(variantMapping.IsVariantTrack("Variant Track")); | ||
| Assert.Equal("Main Track", variantMapping.GetMainTrackName("Variant Track")); | ||
| } | ||
|
|
||
| private sealed class StubHttpMessageHandler : HttpMessageHandler | ||
| { | ||
| private readonly Dictionary<string, string> _responses; | ||
| private readonly ConcurrentDictionary<string, int> _requestCounts = new(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| public StubHttpMessageHandler(Dictionary<string, string> responses) | ||
| { | ||
| _responses = responses; | ||
| } | ||
|
|
||
| public int GetRequestCount(string url) | ||
| { | ||
| return _requestCounts.TryGetValue(url, out var count) ? count : 0; | ||
| } | ||
|
|
||
| protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
| { | ||
| var url = request.RequestUri!.ToString(); | ||
| _requestCounts.AddOrUpdate(url, 1, (_, current) => current + 1); | ||
|
|
||
| if (!_responses.TryGetValue(url, out var content)) | ||
| { | ||
| return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound) | ||
| { | ||
| Content = new StringContent(string.Empty) | ||
| }); | ||
| } | ||
|
|
||
| return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) | ||
| { | ||
| Content = new StringContent(content, Encoding.UTF8, "application/json") | ||
| }); | ||
| } | ||
| } | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
WheelWizard.Test/Features/Ghosts/TrackVariantMappingServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using WheelWizard.Models; | ||
| using WheelWizard.Services; | ||
|
|
||
| namespace WheelWizard.Test.Features.Ghosts; | ||
|
|
||
| public class TrackVariantMappingServiceTests | ||
| { | ||
| [Fact] | ||
| public void InitializeFromTrackInfo_BuildsVariantAndMainMappingsByCourseId() | ||
| { | ||
| var service = new TrackVariantMappingService(); | ||
|
|
||
| var tracks = new[] | ||
| { | ||
| new TrackInfo { Id = 10, Name = "Main Track", CourseId = 300 }, | ||
| new TrackInfo { Id = 11, Name = "Variant Track", CourseId = 300 }, | ||
| new TrackInfo { Id = 20, Name = "Solo Track", CourseId = 301 } | ||
| }; | ||
|
|
||
| service.InitializeFromTrackInfo(tracks); | ||
|
|
||
| Assert.True(service.IsVariantTrack("Variant Track")); | ||
| Assert.False(service.IsVariantTrack("Main Track")); | ||
| Assert.Equal("Main Track", service.GetMainTrackName("Variant Track")); | ||
| Assert.Equal("Solo Track", service.GetMainTrackName("Solo Track")); | ||
|
|
||
| var variants = service.GetVariantsForMainTrack("Main Track"); | ||
| Assert.Single(variants); | ||
| Assert.Contains("Variant Track", variants); | ||
| Assert.Empty(service.GetVariantsForMainTrack("Solo Track")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AddAndRemoveVariantMapping_UpdatesVariantState() | ||
| { | ||
| var service = new TrackVariantMappingService(); | ||
|
|
||
| service.AddVariantMapping("Main A", "Variant A1"); | ||
|
|
||
| Assert.True(service.IsVariantTrack("Variant A1")); | ||
| Assert.Equal("Main A", service.GetMainTrackName("Variant A1")); | ||
| Assert.Contains("Variant A1", service.GetVariantsForMainTrack("Main A")); | ||
|
|
||
| service.RemoveVariantMapping("Variant A1"); | ||
|
|
||
| Assert.False(service.IsVariantTrack("Variant A1")); | ||
| Assert.Equal("Variant A1", service.GetMainTrackName("Variant A1")); | ||
| Assert.Empty(service.GetVariantsForMainTrack("Main A")); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider using
ConcurrentDictionaryfor thread-safety in the stub handler.While unlikely to cause issues in these sequential tests,
_requestCountscould have race conditions if tests were parallelized or ifSendAsyncwere called concurrently. UsingConcurrentDictionarywithAddOrUpdatewould be more robust.♻️ Proposed thread-safe improvement
🤖 Prompt for AI Agents