Files
frameledger/tests/FrameLedger.Infrastructure.Tests/Telemetry/NvapiTelemetrySourceTests.cs
T
poli0981andClaude Fable 5.1 222dd10083 feat(telemetry): the NVAPI bridge -- FrameLedger.NvapiBridge.dll, L3 NvapiTelemetrySource, and the NGX probe in-process (P2 PR-E2) (#149)
A new native target, src/native/FrameLedger.NvapiBridge/, is the C ABI the
managed side reaches NVAPI through: refcounted FlNvInit/FlNvShutdown,
FlNvReadSample into a fixed struct with a present bit per field, FlNvNgxState
for one pid, FlNvDriverVersion, and size / ABI-version / build-id queries so a
drifted mirror is refused (FL_NV_BAD_SIZE) rather than read. It is the only
shipped binary that links nvapi64.lib, read-only by construction, loaded by the
Agent into its own process by absolute path (FrameLedger.NvapiBridge.targets)
and never into a game.

Infrastructure.Telemetry: NativeNvapiBridge (the second P/Invoke facade after
the guard's, same absolute-path rule), NvapiTelemetrySource (L3: a clear bit is
null, never 0; an Init that does not answer 0 disables the layer with zero
faults; two throws disable it for the session; VRAM is not read because the
vendored nvapi.h declares no non-deprecated memory-info call, so L1's PDH
counter keeps it), and NvapiNgxStateProbe, the INgxDriverProbe in-process.
The capture host composes L1 + L2 + L3 under the composite and no longer
spawns fl-probe-nvapi.exe; the probe is no longer staged beside it.

Infrastructure.Native.BesideThisAssembly is the assembly's one DllImport
resolver. The runtime allows one per assembly and the guard's static
constructor already set it, which the first real-hardware run of the mirror
test found (TypeInitializationException: a resolver is already set). The
guard claims its name as required (absent throws); the bridge as optional
(absent is a missing layer that reports itself).

Tests: ctest fl_nvapi_bridge (every export, BRANCH: line required);
NvapiBridgeMirrorTests (both mirrors' sizes against the DLL's answers, fails
rather than skips when the DLL is not staged); NvapiTelemetrySourceTests and
NvapiNgxStateProbeTests over a scripted bridge. versioninfo-check.ps1
requires the new DLL's version block.

Measured on the dev box (RTX 5080, 616.64 r616_41), written into
18_GPU_VENDOR_APIS §L3: core 35 °C, GPU domain 0 %, 2670 / 15001 MHz,
throttle mask 0, PCIe x16; memory-temperature and fan bits clear at an idle
desktop.

Docs: 18_GPU_VENDOR_APIS (§Abstraction, §L3 built note, function table,
measured matrix), 12_BUILD (§Targets, the .targets list), 03_METRICS and
20_OPEN_QUESTIONS §H5 (probe in-process), CLAUDE.md (pinned stack, the two
facades), legal/THIRD_PARTY_NOTICES (NVAPI row), CHANGELOG.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-10 08:18:23 +07:00

100 lines
4.0 KiB
C#

using FluentAssertions;
using FrameLedger.Application.Telemetry;
using FrameLedger.Infrastructure.Telemetry;
namespace FrameLedger.Infrastructure.Tests.Telemetry;
/// <summary>L3's contract through a scripted bridge: unavailable is not zero, a clear bit is null, two throws disable.</summary>
public sealed class NvapiTelemetrySourceTests
{
private sealed class ManualTimeProvider : TimeProvider
{
public override DateTimeOffset GetUtcNow() => new(2026, 9, 10, 12, 0, 0, TimeSpan.Zero);
}
[Fact]
public void AFullSampleMapsEveryFieldAndTheLayerIsNvapi()
{
using var bridge = new FakeNvapiBridge { Sample = FakeNvapiBridge.Full };
using var source = new NvapiTelemetrySource(bridge, new ManualTimeProvider());
source.TryRead(out GpuSample? s).Should().BeTrue();
s!.Layer.Should().Be(TelemetryLayer.Nvapi);
s.AdapterName.Should().Be("NVIDIA GeForce RTX 5080");
s.TempCoreC.Should().Be(62);
s.TempMemoryC.Should().BeNull("the bit was clear");
s.LoadPct.Should().Be(57);
s.VramAdapterMb.Should().BeNull("this build reads no memory info");
s.CoreClockMhz.Should().Be(2610);
s.MemClockMhz.Should().Be(15001);
s.FanRpm.Should().Be(1200);
s.ThrottleReasons.Should().Be(0x2u);
s.PcieWidth.Should().Be(16);
s.PcieGen.Should().BeNull();
source.Capabilities.Should().Be(GpuCapabilities.TempCore | GpuCapabilities.Load | GpuCapabilities.CoreClock
| GpuCapabilities.MemClock | GpuCapabilities.Fan | GpuCapabilities.ThrottleReasons | GpuCapabilities.Pcie);
source.DriverVersion.Should().Be("616.64 (r616_00)");
bridge.Inits.Should().Be(1);
}
[Theory]
[InlineData(NativeNvapiBridge.Unavailable)]
[InlineData(-6)]
[InlineData(NativeNvapiBridge.NoGpu)]
public void AnInitThatDoesNotAnswerZeroDisablesTheLayerWithoutAFault(int init)
{
using var bridge = new FakeNvapiBridge { InitResult = init, Sample = FakeNvapiBridge.Full };
using var source = new NvapiTelemetrySource(bridge, new ManualTimeProvider());
source.Start();
source.IsDisabled.Should().BeTrue();
source.Capabilities.Should().Be(GpuCapabilities.None, "unavailable is N/A, never zero");
source.Faults.Should().Be(0, "a machine without the driver is a normal condition");
source.LastFault.Should().NotBeNull();
source.TryRead(out _).Should().BeFalse();
bridge.Reads.Should().Be(0);
}
[Fact]
public void ABridgeThatThrowsTwiceIsDisabledAndAReadTheDriverRefusesIsNot()
{
int calls = 0;
using var bridge = new FakeNvapiBridge
{
Sample = () => ++calls is 2 or 4 ? throw new InvalidOperationException("driver went away") : FakeNvapiBridge.Full(),
};
using var source = new NvapiTelemetrySource(bridge, new ManualTimeProvider());
source.TryRead(out _).Should().BeTrue();
source.TryRead(out _).Should().BeFalse();
source.Faults.Should().Be(1);
source.IsDisabled.Should().BeFalse();
source.TryRead(out _).Should().BeTrue();
source.TryRead(out _).Should().BeFalse();
source.IsDisabled.Should().BeTrue();
source.Capabilities.Should().Be(GpuCapabilities.None);
source.TryRead(out _).Should().BeFalse("never retried");
}
[Fact]
public void DisposeShutsTheBridgeDownOnceAndOnlyIfItWasInitialised()
{
using var live = new FakeNvapiBridge { Sample = FakeNvapiBridge.Full };
var source = new NvapiTelemetrySource(live, new ManualTimeProvider());
source.Start();
source.Dispose();
source.Dispose();
live.Shutdowns.Should().Be(1);
live.Disposed.Should().BeTrue();
using var dead = new FakeNvapiBridge { InitResult = -6 };
var disabled = new NvapiTelemetrySource(dead, new ManualTimeProvider());
disabled.Start();
disabled.Dispose();
dead.Shutdowns.Should().Be(0, "nothing was initialised");
dead.Disposed.Should().BeTrue();
}
}