// SPDX-License-Identifier: GPL-3.0-only // Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE using System.Diagnostics; using System.Globalization; using System.Text.RegularExpressions; using FluentAssertions; using FrameLedger.Domain.Display; using FrameLedger.Infrastructure.Display; namespace FrameLedger.Infrastructure.Tests.Display; /// /// The window reader against a real window in another process (beta.10, 03_METRICS §Display mode): /// hook-harness --hold-presenting-hwnd makes a top-level window with a swap chain on it and prints the handle. Nothing /// is injected — the reader asks user32 about a window from outside, as the Agent does. /// /// /// The harness's window is never shown (a hosted runner has nothing to show it on), so the positive case goes through the /// NAMED handle, the path region 4 feeds; the largest-visible-window search is exercised by what it must not find. A /// machine with no window station or no monitor skips, and says so. /// public sealed partial class WindowGeometryProbeTests { private static string Harness => Path.Combine(AppContext.BaseDirectory, "hook-harness.exe"); [GeneratedRegex(@"hwnd=0x(?[0-9a-fA-F]+) width=(?\d+) height=(?\d+)", RegexOptions.None, 1000)] private static partial Regex FixtureLine(); [Fact] public async Task TheNamedWindowIsReadWhenItsOwnerIsAmongThePidsAndRefusedWhenNot() { CancellationToken ct = TestContext.Current.CancellationToken; File.Exists(Harness).Should().BeTrue("hook-harness.exe must be staged beside the test binary (FrameLedger.DrainFixtures.targets); run build.ps1 native first"); using Process harness = Process.Start(new ProcessStartInfo(Harness, "--hold-presenting-hwnd 15") { UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, })!; try { Match? fixture = null; while (fixture is null && await harness.StandardOutput.ReadLineAsync(ct).ConfigureAwait(true) is { } line) { Assert.SkipWhen(line.Contains("[SKIP]", StringComparison.Ordinal), "the harness could not make a window with a swap chain here: " + line.Trim()); Match m = FixtureLine().Match(line); fixture = m.Success ? m : null; } fixture.Should().NotBeNull("the harness prints the window it made before it starts presenting"); ulong hwnd = ulong.Parse(fixture.Groups["hwnd"].Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture); var probe = new WindowGeometryProbe(); WindowView? view = probe.Observe([harness.Id], hwnd); Assert.SkipWhen(view is null, "no monitor to place the window on in this session"); view.Value.Minimized.Should().BeFalse(); view.Value.Client.IsEmpty.Should().BeFalse("the harness's window has a client area"); view.Value.Monitor.IsEmpty.Should().BeFalse(); view.Value.CoversMonitor.Should().BeFalse("a 320×200 window covers no monitor"); DisplayModeClassifier.Classify(null, view).Mode.Should().Be(DisplayMode.Windowed); // The same handle asked about a process that does not own it: a handle is reused once its window is gone, so // one that belongs to someone else is not the game's — and this test process shows no window of its own. probe.Observe([Environment.ProcessId], hwnd).Should().BeNull(); probe.Observe([], hwnd).Should().BeNull("no process, no window"); // Advanced metrics (D101) asks the driver about the display the game's window is on: its GDI device name, from // Windows. The same rule for whose window it is - and a name, never an identity of the monitor. WindowGeometryProbe.MonitorDeviceOf([harness.Id], hwnd).Should().StartWith(@"\\.\DISPLAY", "the window is on a monitor, and Windows names the monitor's place in its list"); WindowGeometryProbe.MonitorDeviceOf([Environment.ProcessId], hwnd).Should().BeNull("a window another process owns is not this one's, and this test process shows none"); } finally { try { if (!harness.HasExited) { harness.Kill(entireProcessTree: true); } } catch (InvalidOperationException) { // Exited between the check and the kill. } } } [Fact] public void WithNoProcessOrNoWindowThereIsNoMonitorToName() { WindowGeometryProbe.MonitorDeviceOf([]).Should().BeNull("no process, no window"); WindowGeometryProbe.MonitorDeviceOf([Environment.ProcessId]).Should().BeNull("this test process shows no window"); WindowGeometryProbe.MonitorDeviceOf([Environment.ProcessId], 0xDEAD_0000_BEEF).Should().BeNull("a handle that is no window names no monitor"); } [Fact] public void AHandleThatIsNoWindowIsNotRead() => new WindowGeometryProbe().Observe([Environment.ProcessId], 0xDEAD_0000_BEEF).Should().BeNull(); }