From f31ce9fdb7c2fb0aa5b9072f1d9c16adf5d65383 Mon Sep 17 00:00:00 2001 From: gramps Date: Thu, 6 Aug 2026 15:16:58 -0700 Subject: [PATCH] Hermetic real-MainWindow tests: LayoutPathOverride temp-DB seam (InternalsVisibleTo); fix tests persisting fake webcam sources over the real layout DB; LayoutStore delete-roundtrip guard --- ViewModels/MainViewModel.cs | 7 ++- ai.md | 20 ++++++++- ytLive.Tests/LayoutStorePersistenceTests.cs | 50 +++++++++++++++++++++ ytLive.Tests/RoundClipInteractionTests.cs | 9 ++++ ytLive.csproj | 6 +++ 5 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 ytLive.Tests/LayoutStorePersistenceTests.cs diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index d4be41d..59b5ced 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -481,7 +481,7 @@ public class MainViewModel : ViewModelBase SaveLayoutAsCommand = new RelayCommand(_ => SaveLayoutAs()); OpenLayoutCommand = new RelayCommand(_ => OpenLayoutFile()); - _layoutStore = new LayoutStore(DefaultLayoutPath); + _layoutStore = new LayoutStore(LayoutPathOverride ?? DefaultLayoutPath); _activeLayoutPath = _layoutStore.ActivePath; _cameraEnumerator = new MediaCaptureCameraEnumerator(); @@ -531,6 +531,11 @@ public class MainViewModel : ViewModelBase "ytLlive", "ytLlive.db"); + // Test seam: a real MainWindow test must never read or write the user's + // real layout DB (it would persist test sources over real ones). Tests set + // this to a temp path before constructing MainWindow and reset it after. + internal static string? LayoutPathOverride { get; set; } + private void LoadLayout() { _isLoading = true; diff --git a/ai.md b/ai.md index be70fe6..f9afcde 100644 --- a/ai.md +++ b/ai.md @@ -45,7 +45,25 @@ dotnet.exe vstest "C:\...\ytLive.Tests\bin\Debug\net8.0-windows10.0.19041.0\ytLi Good Dog Rule: ONE integration test per branch, ONE test per PR. Current: TokenStore DPAPI roundtrip/corrupt/missing/clear, OAuth exchange/refresh/ClearSession, CameraManager refcount + -frame pump + failure handling (fakes for the WinRT seams) — 11 passing. +frame pump + failure handling (fakes for the WinRT seams), real-`MainWindow` round-clip +interaction test, LayoutStore delete roundtrip — 13 passing. + +### Real-MainWindow tests MUST be hermetic (DB pollution bug) + +The integration test boots a real `MainWindow` → `MainViewModel` → real `LayoutStore` +(`%APPDATA%\ytLlive\ytLlive.db`). `Shutdown()` on close **saves the layout** (full rewrite: +DELETE all scenes/sources, re-insert), so any source a test adds would be persisted over the +user's real ones — this happened and wiped the real webcam source (DeviceId replaced by the +test's fake `test-camera`). Rule: a test that constructs `MainWindow` MUST first set +`MainViewModel.LayoutPathOverride` to a temp DB path and reset it (plus +`SqliteConnection.ClearAllPools()` + delete) in `finally`. The seam is +`internal static string? LayoutPathOverride` (line ~529 in `MainViewModel.cs`), +`ytLive.csproj` has `InternalsVisibleTo("ytLive.Tests")`. + +The layout DB is a **full rewrite per save** (delete all, re-insert from memory), so +save/load round trips are exact: a source removed in the UI (`RemoveSource` → +`scene.Sources.Remove` → `OnSourcesChanged` → debounced `ScheduleSave`, plus `Shutdown` on +close) does **not** come back after reload (`LayoutStorePersistenceTests` guards this). ## Architecture diff --git a/ytLive.Tests/LayoutStorePersistenceTests.cs b/ytLive.Tests/LayoutStorePersistenceTests.cs new file mode 100644 index 0000000..36db8eb --- /dev/null +++ b/ytLive.Tests/LayoutStorePersistenceTests.cs @@ -0,0 +1,50 @@ +using System; +using System.IO; +using Microsoft.Data.Sqlite; +using Xunit; +using ytLive.Models; +using ytLive.Services; + +namespace ytLive.Tests; + +/// +/// The layout DB is a full rewrite on every save (DELETE all scenes/sources, +/// re-insert from memory). This guards the round trip: sources the user deletes +/// in the UI must not come back after a save + reload. +/// +public class LayoutStorePersistenceTests +{ + [Fact] + public void Deleted_Webcam_Source_Does_Not_Return_After_Save_And_Reload() + { + var path = Path.Combine(Path.GetTempPath(), $"ytLlive-layout-{Guid.NewGuid():N}.db"); + try + { + using var store = new LayoutStore(path); + var scene = new Scene { Name = "Starting" }; + scene.Sources.Add(new Source + { + Name = "Webcam", + Type = SourceType.Webcam, + DeviceId = "real-device", + Width = 480, + Height = 270, + }); + store.Save(new[] { scene }); + + var reloaded = store.Load(); + Assert.Single(reloaded[0].Sources); + + reloaded[0].Sources.RemoveAt(0); + store.Save(reloaded); + + var afterDelete = store.Load(); + Assert.Empty(afterDelete[0].Sources); + } + finally + { + SqliteConnection.ClearAllPools(); + try { File.Delete(path); } catch { /* best-effort cleanup */ } + } + } +} diff --git a/ytLive.Tests/RoundClipInteractionTests.cs b/ytLive.Tests/RoundClipInteractionTests.cs index c1379f8..24abbba 100644 --- a/ytLive.Tests/RoundClipInteractionTests.cs +++ b/ytLive.Tests/RoundClipInteractionTests.cs @@ -5,6 +5,7 @@ using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Threading; +using Microsoft.Data.Sqlite; using Xunit; using ytLive.Models; using ytLive.ViewModels; @@ -47,6 +48,11 @@ public sealed class RoundClipInteractionTests var app = new App(); app.InitializeComponent(); + // Never let the real MainWindow read/write the user's actual layout DB — + // Shutdown() saves the layout, which would persist these test sources + // over the real ones. Point it at a throwaway temp DB instead. + var tempDb = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"ytLlive-test-{Guid.NewGuid():N}.db"); + MainViewModel.LayoutPathOverride = tempDb; var window = new MainWindow(); var vm = (MainViewModel)window.DataContext; try @@ -103,6 +109,9 @@ public sealed class RoundClipInteractionTests finally { window.Close(); + MainViewModel.LayoutPathOverride = null; + SqliteConnection.ClearAllPools(); + try { System.IO.File.Delete(tempDb); } catch { /* best-effort cleanup */ } } } diff --git a/ytLive.csproj b/ytLive.csproj index 6949ebd..1ed50ea 100644 --- a/ytLive.csproj +++ b/ytLive.csproj @@ -24,6 +24,12 @@ + + + <_Parameter1>ytLive.Tests + + +