Skip to content

Commit b6c5b3a

Browse files
committed
Phase 3: Fix diagnostics infrastructure tests
- Remove UpdateDisplay() wrapper, use Render.UpdateDisplay() directly - Add BeginFrame() call to RenderCoordinator.UpdateDisplay() - Fix test expectations for window borders (48x23 not 50x25) - Enhance MockConsoleDriver with ConsoleBuffer for full rendering pipeline - Fix diagnostics initialization order (before driver.Initialize()) - All 8 diagnostics infrastructure tests passing
1 parent aec32a9 commit b6c5b3a

4 files changed

Lines changed: 43 additions & 28 deletions

File tree

SharpConsoleUI.Tests/Infrastructure/MockConsoleDriver.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class MockConsoleDriver : IConsoleDriver
2424
private Point _cursorPosition;
2525
private bool _cursorVisible;
2626
private ConsoleWindowSystem? _windowSystem;
27+
private ConsoleBuffer? _consoleBuffer;
2728

2829
/// <summary>
2930
/// Gets the history of all WriteToConsole calls.
@@ -97,11 +98,11 @@ public void Clear()
9798
}
9899

99100
/// <summary>
100-
/// Does nothing (no buffering in mock driver).
101+
/// Flushes the console buffer, triggering actual rendering with diagnostics capture.
101102
/// </summary>
102103
public void Flush()
103104
{
104-
// No-op for mock driver
105+
_consoleBuffer?.Render();
105106
}
106107

107108
/// <summary>
@@ -158,22 +159,34 @@ public void ResetCursorShape()
158159
public void Initialize(ConsoleWindowSystem windowSystem)
159160
{
160161
_windowSystem = windowSystem;
162+
163+
// Create ConsoleBuffer for buffered rendering with diagnostics support
164+
_consoleBuffer = new ConsoleBuffer(_screenSize.Width, _screenSize.Height, windowSystem.Options, null);
165+
166+
// Connect diagnostics to ConsoleBuffer
167+
if (windowSystem.RenderingDiagnostics != null)
168+
{
169+
_consoleBuffer.Diagnostics = windowSystem.RenderingDiagnostics;
170+
}
161171
}
162172

163173
/// <summary>
164-
/// Captures the output instead of writing to the console.
174+
/// Writes content to the console buffer for buffered rendering.
165175
/// </summary>
166176
public void WriteToConsole(int x, int y, string value)
167177
{
168178
_outputHistory.Add(value);
179+
180+
// Add content to console buffer for double-buffered rendering
181+
_consoleBuffer?.AddContent(x, y, value);
169182
}
170183

171184
/// <summary>
172-
/// Returns 0 (mock driver doesn't track dirty characters).
185+
/// Returns the count of dirty characters from the ConsoleBuffer.
173186
/// </summary>
174187
public int GetDirtyCharacterCount()
175188
{
176-
return 0;
189+
return _consoleBuffer?.GetDirtyCharacterCount() ?? 0;
177190
}
178191

179192
/// <summary>

SharpConsoleUI.Tests/Rendering/Unit/DiagnosticsInfrastructureTests.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ public void Diagnostics_CapturesCharacterBufferSnapshot()
5353
// Assert
5454
var snapshot = system.RenderingDiagnostics?.LastBufferSnapshot;
5555
Assert.NotNull(snapshot);
56-
Assert.Equal(50, snapshot.Width);
57-
Assert.Equal(25, snapshot.Height);
56+
// Window content buffer is 2 chars narrower/shorter due to borders
57+
Assert.Equal(48, snapshot.Width);
58+
Assert.Equal(23, snapshot.Height);
5859
}
5960

6061
[Fact]
@@ -100,14 +101,15 @@ public void Diagnostics_MultipleFrames_RetainsHistory()
100101
var window = new Window(system) { Width = 40, Height = 20 };
101102
var markup = new MarkupControl(new List<string> { "Frame 1" });
102103
window.AddControl(markup);
104+
system.WindowStateService.AddWindow(window);
103105

104106
// Act - Render frame 1
105-
window.RenderAndGetVisibleContent();
107+
system.Render.UpdateDisplay();
106108
var frame1Number = system.RenderingDiagnostics!.CurrentFrameNumber;
107109

108110
// Render frame 2 (invalidate and render again)
109111
window.Invalidate(true);
110-
window.RenderAndGetVisibleContent();
112+
system.Render.UpdateDisplay();
111113
var frame2Number = system.RenderingDiagnostics.CurrentFrameNumber;
112114

113115
// Assert
@@ -142,9 +144,10 @@ public void Diagnostics_CapturesMetrics()
142144
var window = new Window(system) { Width = 40, Height = 20 };
143145
var markup = new MarkupControl(new List<string> { "Test Metrics" });
144146
window.AddControl(markup);
147+
system.WindowStateService.AddWindow(window);
145148

146-
// Act
147-
window.RenderAndGetVisibleContent();
149+
// Act - Full render pipeline to trigger metrics capture
150+
system.Render.UpdateDisplay();
148151

149152
// Assert
150153
var metrics = system.RenderingDiagnostics?.LastMetrics;
@@ -160,9 +163,10 @@ public void Diagnostics_QualityAnalysis_GeneratesReport()
160163
var window = new Window(system) { Width = 40, Height = 20 };
161164
var markup = new MarkupControl(new List<string> { "Quality Test" });
162165
window.AddControl(markup);
166+
system.WindowStateService.AddWindow(window);
163167

164-
// Act
165-
window.RenderAndGetVisibleContent();
168+
// Act - Full render pipeline to trigger quality analysis
169+
system.Render.UpdateDisplay();
166170

167171
// Assert
168172
var qualityReport = system.RenderingDiagnostics?.LastQualityReport;

SharpConsoleUI/ConsoleWindowSystem.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,13 @@ public ConsoleWindowSystem(IConsoleDriver driver, ITheme theme, PluginConfigurat
225225
Render,
226226
() => this);
227227

228-
// NOW initialize driver with 'this' reference (after services exist)
228+
// Initialize diagnostics BEFORE driver.Initialize() so driver can connect them
229+
if (_options.EnableDiagnostics)
230+
{
231+
_renderingDiagnostics = new Diagnostics.RenderingDiagnostics(_options);
232+
}
233+
234+
// NOW initialize driver with 'this' reference (after services and diagnostics exist)
229235
_consoleDriver.Initialize(this);
230236

231237
// Set window system context on ThemeStateService for window invalidation
@@ -236,12 +242,6 @@ public ConsoleWindowSystem(IConsoleDriver driver, ITheme theme, PluginConfigurat
236242
{
237243
_pluginStateService.LoadPluginsFromDirectory(pluginConfiguration.GetEffectivePluginsDirectory());
238244
}
239-
240-
// Initialize diagnostics if enabled (for testing and debugging)
241-
if (_options.EnableDiagnostics)
242-
{
243-
_renderingDiagnostics = new Diagnostics.RenderingDiagnostics(_options);
244-
}
245245
}
246246

247247
#endregion
@@ -549,7 +549,7 @@ public int Run()
549549
// Frame rate limiting enabled: only render if enough time elapsed
550550
if (shouldRender && elapsed >= Performance.MinFrameTime)
551551
{
552-
UpdateDisplay();
552+
Render.UpdateDisplay();
553553
_lastRenderTime = now;
554554
_idleTime = (int)Performance.MinFrameTime;
555555
}
@@ -564,7 +564,7 @@ public int Run()
564564
// Frame rate limiting disabled: render immediately when dirty
565565
if (shouldRender)
566566
{
567-
UpdateDisplay();
567+
Render.UpdateDisplay();
568568
_lastRenderTime = now;
569569
_idleTime = Configuration.SystemDefaults.FastLoopIdleMs; // Fast loop when dirty, no frame rate cap
570570
}
@@ -643,7 +643,7 @@ public void Shutdown(int exitCode = 0)
643643
public void ProcessOnce()
644644
{
645645
Input.ProcessInput();
646-
UpdateDisplay();
646+
Render.UpdateDisplay();
647647
UpdateCursor();
648648
}
649649

@@ -792,11 +792,6 @@ private void UpdateCursor()
792792
}
793793
}
794794

795-
private void UpdateDisplay()
796-
{
797-
Render.UpdateDisplay();
798-
}
799-
800795
private void UpdateStatusBarBounds()
801796
{
802797
Render.UpdateStatusBarBounds();

SharpConsoleUI/Rendering/RenderCoordinator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ public void AddWindowNeedingRegionUpdate(Window window)
186186
/// </summary>
187187
public void UpdateDisplay()
188188
{
189+
// Begin new frame for diagnostics tracking
190+
_windowSystemContext.RenderingDiagnostics?.BeginFrame();
191+
189192
lock (_renderLock)
190193
{
191194
// ATOMIC DESKTOP CLEARING: Clear old window positions before rendering

0 commit comments

Comments
 (0)