Hermetic real-MainWindow tests: LayoutPathOverride temp-DB seam (InternalsVisibleTo); fix tests persisting fake webcam sources over the real layout DB; LayoutStore delete-roundtrip guard

This commit is contained in:
2026-08-06 15:16:58 -07:00
parent 7bc70872d9
commit f31ce9fdb7
5 changed files with 90 additions and 2 deletions
+6 -1
View File
@@ -481,7 +481,7 @@ public class MainViewModel : ViewModelBase
SaveLayoutAsCommand = new RelayCommand(_ => SaveLayoutAs()); SaveLayoutAsCommand = new RelayCommand(_ => SaveLayoutAs());
OpenLayoutCommand = new RelayCommand(_ => OpenLayoutFile()); OpenLayoutCommand = new RelayCommand(_ => OpenLayoutFile());
_layoutStore = new LayoutStore(DefaultLayoutPath); _layoutStore = new LayoutStore(LayoutPathOverride ?? DefaultLayoutPath);
_activeLayoutPath = _layoutStore.ActivePath; _activeLayoutPath = _layoutStore.ActivePath;
_cameraEnumerator = new MediaCaptureCameraEnumerator(); _cameraEnumerator = new MediaCaptureCameraEnumerator();
@@ -531,6 +531,11 @@ public class MainViewModel : ViewModelBase
"ytLlive", "ytLlive",
"ytLlive.db"); "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() private void LoadLayout()
{ {
_isLoading = true; _isLoading = true;
+19 -1
View File
@@ -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 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 + 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 ## Architecture
@@ -0,0 +1,50 @@
using System;
using System.IO;
using Microsoft.Data.Sqlite;
using Xunit;
using ytLive.Models;
using ytLive.Services;
namespace ytLive.Tests;
/// <summary>
/// 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.
/// </summary>
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 */ }
}
}
}
@@ -5,6 +5,7 @@ using System.Windows.Controls;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Windows.Threading; using System.Windows.Threading;
using Microsoft.Data.Sqlite;
using Xunit; using Xunit;
using ytLive.Models; using ytLive.Models;
using ytLive.ViewModels; using ytLive.ViewModels;
@@ -47,6 +48,11 @@ public sealed class RoundClipInteractionTests
var app = new App(); var app = new App();
app.InitializeComponent(); 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 window = new MainWindow();
var vm = (MainViewModel)window.DataContext; var vm = (MainViewModel)window.DataContext;
try try
@@ -103,6 +109,9 @@ public sealed class RoundClipInteractionTests
finally finally
{ {
window.Close(); window.Close();
MainViewModel.LayoutPathOverride = null;
SqliteConnection.ClearAllPools();
try { System.IO.File.Delete(tempDb); } catch { /* best-effort cleanup */ }
} }
} }
+6
View File
@@ -24,6 +24,12 @@
<Resource Include="Assets\llama-logo-icon.png"/> <Resource Include="Assets\llama-logo-icon.png"/>
</ItemGroup> </ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>ytLive.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.10"/> <PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.10"/>
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.12"/> <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.12"/>