Files
frameledger/tests/FrameLedger.Infrastructure.Tests/Telemetry/CompositeTelemetrySourceTests.cs
poli0981andClaude Opus 5.5 e77a130fc9 fix: a refusal for a driver or service says "on this PC", the unhook notice promises nothing untrue, the GPU sensors follow the game's adapter, and the language-changing tests run alone (beta.13 PR-1) (#260)
The four follow-ups the beta.12 documentation audit left as chips; three of
them here, the fourth (Velopack deltas) rides the release PR.

S23-3. The guard's checks 2 and 2b are machine-wide: while Easy Anti-Cheat's
service runs for another title, every game refuses with BlockedService, and
the notice read "Easy Anti-Cheat was detected in this game". CaptureRefused
and SafetyUnhook gain an optional guardReason (additive; reason stays the
session's end reason), and BlockedService / BlockedDriver read
Safety_Refused_MachineWide_Format, Safety_Unhooked_MachineWide_Format and
Summary_Tier2_Why_MachineWide_Format (en/vi; ja as an English copy awaiting
the human safety review, like its siblings). Safety_Unhooked_Format loses
"will not inject into this game again until you enable it again", untrue both
ways since 2026-09-22. Formats.NamedRefusal is the one place a named refusal
is worded; SafetyNotices and HookingConsentResult both use it.

L1's adapter. SelectAdapter had no caller, and fixing that alone would have
changed little: the composite merges L3 > L2 > L1 per field, and L2 and L3
read their own first GPU. CaptureProgress carries the handshake's AdapterLuid;
the recorder hands it to the poller once (noted in the .partial);
CompositeTelemetrySource.SelectAdapter points L1 at it (IGpuAdapterSelector)
and tells the other layers its vendor (IGpuVendorFollower): L2 maps that
vendor's LHM node, L3 contributes nothing for a non-NVIDIA adapter. What stays
first-found is written in 18_GPU_VENDOR_APIS and LIMITATIONS.

Culture races. The strings-culture collection serialized its own members but
ran beside every other class; it now has a definition with
DisableParallelization, and StringsTests asserts xUnit bound it.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 09:11:38 +07:00

245 lines
9.5 KiB
C#

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using System.Diagnostics.CodeAnalysis;
using FluentAssertions;
using FrameLedger.Application.Telemetry;
using FrameLedger.Infrastructure.Telemetry;
namespace FrameLedger.Infrastructure.Tests.Telemetry;
/// <summary>
/// <c>18_GPU_VENDOR_APIS</c> §Abstraction: <i>"merges them with a fixed precedence per field
/// (L3 &gt; L2 &gt; L1) and records which layer supplied each value"</i>, and the descriptor
/// <c>sessions.telemetry_source</c> stores.
/// </summary>
public sealed class CompositeTelemetrySourceTests
{
private static readonly DateTimeOffset _t0 = new(2026, 9, 9, 12, 0, 0, TimeSpan.Zero);
[Fact]
public void AHigherLayerWinsAFieldAndALowerOneFillsWhatItLeftNull()
{
using var l1 = new FakeLayer(TelemetryLayer.Baseline);
using var l2 = new FakeLayer(TelemetryLayer.Lhm);
using var l3 = new FakeLayer(TelemetryLayer.Nvapi);
l1.Publish(l1.Sample(_t0, vram: 3_000, adapter: "dxgi name"));
l2.Publish(l2.Sample(_t0.AddMilliseconds(-300), load: 55, vram: 2_990, temp: 61, power: 210, adapter: "lhm name"));
l3.Publish(l3.Sample(_t0.AddMilliseconds(-100), load: 57, temp: 62));
using var composite = new CompositeTelemetrySource([l1, l3, l2]);
composite.TryRead(out GpuSample? merged).Should().BeTrue();
merged!.LoadPct.Should().Be(57, "L3 over L2");
merged.TempCoreC.Should().Be(62);
merged.PowerW.Should().Be(210, "L3 had none; L2 fills");
merged.VramAdapterMb.Should().Be(2_990, "L2 over L1");
merged.AdapterName.Should().Be("lhm name", "identity follows the same precedence");
merged.Layer.Should().Be(TelemetryLayer.Nvapi, "the highest layer that contributed");
merged.TakenAt.Should().Be(_t0.AddMilliseconds(-100));
composite.LayerOf(GpuCapabilities.Load).Should().Be(TelemetryLayer.Nvapi);
composite.LayerOf(GpuCapabilities.Power).Should().Be(TelemetryLayer.Lhm);
composite.LayerOf(GpuCapabilities.VramAdapter).Should().Be(TelemetryLayer.Lhm);
composite.LayerOf(GpuCapabilities.Fan).Should().Be(TelemetryLayer.None, "nobody supplied it");
composite.Capabilities.Should().Be(GpuCapabilities.Load | GpuCapabilities.VramAdapter | GpuCapabilities.TempCore | GpuCapabilities.Power);
}
[Fact]
public void TheDescriptorListsStandingLayersLowestFirstWhateverOrderTheyWereGivenIn()
{
using var l1 = new FakeLayer(TelemetryLayer.Baseline);
using var l2 = new FakeLayer(TelemetryLayer.Lhm);
using var composite = new CompositeTelemetrySource([l2, l1]);
composite.Descriptor.Should().Be("l1+lhm");
composite.Layer.Should().Be(TelemetryLayer.None, "the composite is not a layer; the descriptor is its identity");
l2.IsDisabled = true;
composite.Descriptor.Should().Be("l1", "a layer disabled mid-session drops out");
composite.IsDisabled.Should().BeFalse();
l1.IsDisabled = true;
composite.Descriptor.Should().BeEmpty();
composite.IsDisabled.Should().BeTrue();
composite.TryRead(out _).Should().BeFalse();
}
[Fact]
public void ALayerWhoseTryReadThrowsIsExcludedOnTheSecondThrowAndTheOthersAreUnaffected()
{
using var l1 = new FakeLayer(TelemetryLayer.Baseline).Publish(null);
l1.Publish(l1.Sample(_t0, vram: 100));
using var l2 = new FakeLayer(TelemetryLayer.Lhm).Script(() => throw new InvalidOperationException("driver said no"));
using var composite = new CompositeTelemetrySource([l1, l2]);
composite.TryRead(out GpuSample? first).Should().BeTrue();
first!.VramAdapterMb.Should().Be(100, "L1 still answers while L2 throws");
composite.Descriptor.Should().Be("l1+lhm", "one throw is tolerated");
composite.LastFaultOf(TelemetryLayer.Lhm).Should().Contain("driver said no");
composite.TryRead(out _).Should().BeTrue();
composite.Descriptor.Should().Be("l1", "the second throw excludes the layer for the session");
l2.Reads.Should().Be(CompositeTelemetrySource.MaxFaults);
composite.TryRead(out _).Should().BeTrue();
l2.Reads.Should().Be(CompositeTelemetrySource.MaxFaults, "never retried");
composite.LastFaultOf(TelemetryLayer.Nvapi).Should().BeNull("no such layer here");
}
[Fact]
public void ADisabledLayersCapabilitiesAreNotTheCompositesAndItsSampleIsNotRead()
{
using var l1 = new FakeLayer(TelemetryLayer.Baseline);
l1.Publish(l1.Sample(_t0, vram: 100));
using var l2 = new FakeLayer(TelemetryLayer.Lhm);
l2.Publish(l2.Sample(_t0, load: 50));
l2.IsDisabled = true;
l2.Capabilities = GpuCapabilities.None;
using var composite = new CompositeTelemetrySource([l1, l2]);
composite.TryRead(out GpuSample? merged).Should().BeTrue();
merged!.LoadPct.Should().BeNull();
l2.Reads.Should().Be(0);
composite.Capabilities.Should().Be(GpuCapabilities.VramAdapter);
}
[Fact]
public void TwoSourcesClaimingOneLayerOrNoLayerAreRefused()
{
using var a = new FakeLayer(TelemetryLayer.Lhm);
using var b = new FakeLayer(TelemetryLayer.Lhm);
using var unnamed = new FakeLayer(TelemetryLayer.None);
Action twice = () => new CompositeTelemetrySource([a, b]).Dispose();
Action none = () => new CompositeTelemetrySource([unnamed]).Dispose();
twice.Should().Throw<ArgumentException>();
none.Should().Throw<ArgumentException>();
}
[Fact]
public void LayerOfTakesExactlyOneBit()
{
using var l1 = new FakeLayer(TelemetryLayer.Baseline);
using var composite = new CompositeTelemetrySource([l1]);
Action two = () => composite.LayerOf(GpuCapabilities.Load | GpuCapabilities.Fan);
Action zero = () => composite.LayerOf(GpuCapabilities.None);
two.Should().Throw<ArgumentOutOfRangeException>();
zero.Should().Throw<ArgumentOutOfRangeException>();
}
[Fact]
public void DisposingTheCompositeDisposesItsLayers()
{
using var l1 = new FakeLayer(TelemetryLayer.Baseline);
using var l2 = new FakeLayer(TelemetryLayer.Lhm);
var composite = new CompositeTelemetrySource([l1, l2]);
composite.Dispose();
composite.Dispose();
l1.Disposed.Should().BeTrue();
l2.Disposed.Should().BeTrue();
}
/// <summary>
/// beta.13: the game's adapter, by the LUID the Overlay published — L1 switches to it and the layers that read their own
/// GPU are told its vendor; a LUID no layer listed changes nothing and tells nobody.
/// </summary>
[Fact]
public void AnAdapterTheGamePresentsOnIsSelectedInL1AndItsVendorReachesTheOtherLayers()
{
using var l1 = new AdapterLayer(listedLuid: 0x0000_0001_0000_0042, vendor: 0x1002);
using var l3 = new VendorLayer(TelemetryLayer.Nvapi);
using var composite = new CompositeTelemetrySource([l1, l3]);
composite.SelectAdapter(0x99).Should().BeFalse("no layer listed it");
l3.Vendors.Should().BeEmpty();
composite.Selected.Should().BeNull();
composite.SelectAdapter(0x0000_0001_0000_0042).Should().BeTrue();
composite.Selected!.VendorId.Should().Be(0x1002u);
l3.Vendors.Should().Equal(0x1002u);
}
[Fact]
public void TheNamesAreTheOnesTheSchemaExampleUses()
{
TelemetryLayerNames.Describe([TelemetryLayer.Nvapi, TelemetryLayer.Baseline, TelemetryLayer.Lhm, TelemetryLayer.Lhm])
.Should().Be("l1+lhm+nvapi");
Action none = () => TelemetryLayerNames.Of(TelemetryLayer.None);
none.Should().Throw<ArgumentOutOfRangeException>();
}
/// <summary>L1 as the composite sees it: one adapter, by LUID.</summary>
private sealed class AdapterLayer(ulong listedLuid, uint vendor) : IGpuTelemetrySource, IGpuAdapterSelector
{
public TelemetryLayer Layer => TelemetryLayer.Baseline;
public GpuCapabilities Capabilities => GpuCapabilities.None;
public bool IsDisabled => false;
public GpuAdapterIdentity? Selected { get; private set; }
public bool SelectAdapter(ulong luid)
{
if (luid != listedLuid)
{
return false;
}
Selected = new GpuAdapterIdentity
{
Name = "fake",
Luid = luid,
VendorId = vendor,
DeviceId = 1,
SubSysId = 0,
Revision = 0,
DedicatedVideoMemoryMb = 8192,
SharedSystemMemoryMb = 16384,
IsSoftware = false,
};
return true;
}
public bool TryRead([NotNullWhen(true)] out GpuSample? sample)
{
sample = null;
return false;
}
public void Dispose()
{
}
}
/// <summary>A layer that reads its own GPU and is told the vendor.</summary>
private sealed class VendorLayer(TelemetryLayer layer) : IGpuTelemetrySource, IGpuVendorFollower
{
public List<uint> Vendors { get; } = [];
public TelemetryLayer Layer => layer;
public GpuCapabilities Capabilities => GpuCapabilities.None;
public bool IsDisabled => false;
public void FollowVendor(uint vendorId) => Vendors.Add(vendorId);
public bool TryRead([NotNullWhen(true)] out GpuSample? sample)
{
sample = null;
return false;
}
public void Dispose()
{
}
}
}