Problem
DiffPaneViewModel.LoadAndScrollToFirstHunkAsync (DiffViewer/ViewModels/DiffPaneViewModel.cs:362-381) has a guard against rapid file re-selection:
public Task LoadAndScrollToFirstHunkAsync(FileEntryViewModel? entry)
{
LoadAsync(entry);
var requested = entry;
LastLoadTask = LastLoadTask.ContinueWith(_ =>
{
if (!ReferenceEquals(_currentEntry, requested)) return; // ← guard
JumpToFirstHunk();
}, CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.FromCurrentSynchronizationContext());
return LastLoadTask;
}
The ReferenceEquals(_currentEntry, requested) check exists so that if the user clicks file A, then quickly clicks file B before A's load completes, the orphaned A-jump doesn't fire and accidentally land on hunk 0 of file B (or worse, a stale hunk list).
There is currently no test for this guard. If someone removes the check thinking it's redundant, no test will fail.
Suggested test
Add to MainViewModelKeyboardShortcutTests.cs (next to SelectingFile_AutoScrollsCaretToFirstHunk, line 243):
[Fact]
public async Task RapidFileReselection_DropsStaleAutoScroll()
{
var repo = new FakeRepoForKeyboard();
repo.SetChanges(
ModifiedChange("a.cs"),
ModifiedChange("b.cs"));
repo.LeftText = "alpha\n";
repo.RightText = "ALPHA\n";
await RunOnUiSyncContextAsync(async vm =>
{
vm.RefreshChangeList();
var e0 = vm.FileList.FlatEntries[0];
var e1 = vm.FileList.FlatEntries[1];
// Select e0, then immediately select e1 BEFORE awaiting either load.
vm.FileList.SelectedEntry = e0;
vm.FileList.SelectedEntry = e1;
await vm.DiffPane.LastLoadTask;
// The final state should reflect e1 only - if the e0 continuation
// had fired, it would have raised HunkNavigationRequested with
// e0's hunks, potentially scrolling to a now-invalid line.
vm.FileList.SelectedEntry.Should().Be(e1);
vm.DiffPane.CurrentHunkIndex.Should().Be(0,
"the e1 continuation should have run and set idx to 0");
}, repo);
}
A stronger version would also subscribe to HunkNavigationRequested and assert the event was raised exactly once (for e1), not twice (e0 and e1).
Why this matters
Rapid clicking is a real user action — common when scrolling through a long file list with arrow keys or holding F8. Without test coverage, the guard could be silently removed in a future "simplification" pass.
Related
Citations
DiffViewer/ViewModels/DiffPaneViewModel.cs:362-381 — LoadAndScrollToFirstHunkAsync and the guard
DiffViewer/ViewModels/MainViewModel.cs:526-538 — OnFileListPropertyChanged (only caller)
DiffViewer.Tests/ViewModels/MainViewModelKeyboardShortcutTests.cs:243 — existing SelectingFile_AutoScrollsCaretToFirstHunk (template)
Severity
Low (currently safe). Test gap that protects an under-the-hood guard.
Problem
DiffPaneViewModel.LoadAndScrollToFirstHunkAsync(DiffViewer/ViewModels/DiffPaneViewModel.cs:362-381) has a guard against rapid file re-selection:The
ReferenceEquals(_currentEntry, requested)check exists so that if the user clicks file A, then quickly clicks file B before A's load completes, the orphaned A-jump doesn't fire and accidentally land on hunk 0 of file B (or worse, a stale hunk list).There is currently no test for this guard. If someone removes the check thinking it's redundant, no test will fail.
Suggested test
Add to
MainViewModelKeyboardShortcutTests.cs(next toSelectingFile_AutoScrollsCaretToFirstHunk, line 243):A stronger version would also subscribe to
HunkNavigationRequestedand assert the event was raised exactly once (for e1), not twice (e0 and e1).Why this matters
Rapid clicking is a real user action — common when scrolling through a long file list with arrow keys or holding F8. Without test coverage, the guard could be silently removed in a future "simplification" pass.
Related
ApplyResultreset-to--1contract (issue Find with regex #7 in this set)Citations
DiffViewer/ViewModels/DiffPaneViewModel.cs:362-381—LoadAndScrollToFirstHunkAsyncand the guardDiffViewer/ViewModels/MainViewModel.cs:526-538—OnFileListPropertyChanged(only caller)DiffViewer.Tests/ViewModels/MainViewModelKeyboardShortcutTests.cs:243— existingSelectingFile_AutoScrollsCaretToFirstHunk(template)Severity
Low (currently safe). Test gap that protects an under-the-hood guard.