Files
frameledger/tests/FrameLedger.Infrastructure.Tests/Telemetry/TelemetryPollerAdvancedTests.cs
poli0981andClaude Fable 5.2 e0bfbe6edf feat(recording): the ledger can hold Advanced metrics - schema 0023 (two tables, no column on sessions), adv.* series with NaN for a gap, two .partial chunks, and an unknown chunk is skipped (Advanced PR-4, D102/D106) (#390)
Produced by nothing yet: no Agent code composes the advanced reader, so no
session stores any of it and nothing a user sees or records changes.

- Schema 0023, the first CREATE TABLE since 0001: machine_profiles (the
  PC's facts, one row per distinct set, by the SHA-256 of their JSON) and
  session_advanced (one row per session recorded with the option on - its
  facts, each source's state, its series' statistics and directory). No
  column on sessions: every read of that row, the JSON export and the bug
  bundle carry all of it. A session recorded with the option off has no
  row, and that absence is how the App will say so.
- The advanced series are rows of sensor_blobs under names that start with
  adv., with NaN - not -1 - for a tick with no reading: a -12 V rail is
  negative, and the sensor library is read every fifth tick. A second
  encoder, ISeriesCodec.EncodeSparseFloat32, allows NaN and still refuses
  an infinity; EncodeFloat32 still throws on a NaN. FindSensorsAsync, which
  every summary, chart and export reads, leaves the adv. rows out;
  FindAdvancedSensorsAsync and FindAdvancedAsync read them.
- Application.Advanced.AdvancedJson: the one stored form. Writing is
  strict, reading is tolerant, and only a fact whose catalog entry is read
  may be written (D104 held at the door of the ledger).
- AdvancedSessionBuilder, both tiers: one series per advanced reading,
  aligned to t_ms; at most 256 a session, the rest counted. With no reading
  and no context it builds nothing, and a session is byte for byte what it
  was.
- TelemetrySample.Advanced, and the poller's optional IAdvancedHardware:
  held from Start to Dispose, each of its ticks attached to one sample and
  never repeated on the next.
- The .partial: chunk 9 (the readings of the sensor chunk before it, with
  their own names) and chunk 10 (the session's context), written only when
  there is something to write; a recovered session keeps both. D106: a
  chunk of a type the reader does not know, whose length and checksum are
  right, is skipped where it ended the read - so a build one release older
  no longer loses everything after the first new chunk.
- LedgerDatabaseTests' rewind helper undoes a script's CREATE TABLE and
  CREATE INDEX as well as its ADD COLUMN: 0023 would otherwise have failed
  every test that rewinds a ledger.
- Measured: one hour of 58 series shaped like the dev box's reader is 816
  KiB raw and 189 KiB stored (NFR-5 allows 3 MB an hour).
- docs: 06_DATA_MODEL (schema 0023, the NaN rule, chunks 9 and 10, D106),
  18_GPU_VENDOR_APIS, CLAUDE.md, CHANGELOG.

Hook overhead: not applicable - nothing in code loaded into a game
changes.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 09:37:53 +00:00

159 lines
5.7 KiB
C#

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using FluentAssertions;
using FrameLedger.Application.Advanced;
using FrameLedger.Application.Telemetry;
using FrameLedger.Infrastructure.Telemetry;
namespace FrameLedger.Infrastructure.Tests.Telemetry;
/// <summary>
/// The poller and the shared advanced reader (owner decision D101): it holds the reader while it runs, attaches each
/// of the reader's ticks to one sample and to no other, and with no reader composed a sample is what it always was.
/// </summary>
public sealed class TelemetryPollerAdvancedTests
{
private static readonly DateTimeOffset _t0 = new(2026, 11, 11, 9, 0, 0, TimeSpan.Zero);
private sealed class Clock : TimeProvider
{
public long Ticks { get; set; } = 1_000_000;
public override DateTimeOffset GetUtcNow() => _t0;
public override long GetTimestamp() => Ticks;
}
private sealed class FakeHub : IAdvancedHardware
{
public int Acquired { get; private set; }
public int Released { get; private set; }
public AdvancedSnapshot? Latest { get; set; }
public IDisposable Acquire()
{
Acquired++;
return new Hold(this);
}
public IReadOnlyList<AdvancedFact> ReadSessionFacts(AdvancedSessionTarget target) => [];
public void Publish(params AdvancedReading[] readings)
=> Latest = new AdvancedSnapshot(_t0, readings, [], [], AdvancedSources.Unknown);
private sealed class Hold(FakeHub hub) : IDisposable
{
public void Dispose() => hub.Released++;
}
}
private static List<TelemetrySample> Drained(TelemetryPoller poller)
{
var samples = new List<TelemetrySample>();
poller.Drain(samples);
return samples;
}
[Fact]
public void EachTickOfTheReaderIsAttachedToOneSampleAndIsNotRepeatedOnTheNext()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
layer.Publish(layer.Sample(_t0, load: 50));
var hub = new FakeHub();
using var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new Clock(), advanced: hub);
hub.Publish(new AdvancedReading("adv.nvapi.pstate", 0), new AdvancedReading("adv.lhm.board.voltage.lpc_0_7", -11.97));
poller.PollOnce();
poller.PollOnce();
hub.Publish(new AdvancedReading("adv.nvapi.pstate", 8));
poller.PollOnce();
List<TelemetrySample> samples = Drained(poller);
samples.Should().HaveCount(3);
samples[0].Advanced.Should().Equal(new AdvancedReading("adv.nvapi.pstate", 0), new AdvancedReading("adv.lhm.board.voltage.lpc_0_7", -11.97));
samples[1].Advanced.Should().BeNull("nothing newer arrived: a reading is of one tick, and the last one's is not repeated");
samples[2].Advanced.Should().Equal(new AdvancedReading("adv.nvapi.pstate", 8));
samples.Should().OnlyContain(static s => s.Sample.LoadPct == 50, "the fixed fields are untouched");
}
[Fact]
public void ATickTheReaderPublishedWithNoReadingAttachesNothing()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
layer.Publish(layer.Sample(_t0, load: 50));
var hub = new FakeHub();
using var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new Clock(), advanced: hub);
hub.Publish();
poller.PollOnce();
Drained(poller).Should().ContainSingle().Which.Advanced.Should().BeNull();
}
[Fact]
public void ATickOnlyTheAdvancedReaderAnsweredIsStillASample()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
var hub = new FakeHub();
using var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new Clock { Ticks = 77_000 }, advanced: hub);
poller.PollOnce();
Drained(poller).Should().BeEmpty("nothing answered at all");
hub.Publish(new AdvancedReading("adv.nvapi.pstate", 0));
poller.PollOnce();
TelemetrySample only = Drained(poller).Should().ContainSingle().Subject;
only.QpcTicks.Should().Be(77_000);
only.Sample.Layer.Should().Be(TelemetryLayer.None, "the placeholder names no layer and no field");
only.Advanced.Should().ContainSingle();
}
[Fact]
public void WithNoReaderComposedASampleIsWhatItAlwaysWas()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
layer.Publish(layer.Sample(_t0, load: 50));
using var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new Clock());
poller.PollOnce();
Drained(poller).Should().ContainSingle().Which.Advanced.Should().BeNull();
}
[Fact]
public void TheReaderIsHeldFromStartToDisposeAndNeverOwned()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
var hub = new FakeHub();
var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new Clock(), ownsSource: false, advanced: hub);
hub.Acquired.Should().Be(0, "a poller that was never started holds nothing");
poller.Start();
poller.Start();
hub.Acquired.Should().Be(1);
hub.Released.Should().Be(0);
poller.Dispose();
poller.Dispose();
hub.Released.Should().Be(1, "some seconds after the last holder lets go the shared reader closes (D107)");
}
[Fact]
public void APollerThatWasNeverStartedReleasesNothing()
{
using var layer = new FakeLayer(TelemetryLayer.Lhm);
var hub = new FakeHub();
var poller = new TelemetryPoller(layer, new TelemetryPollerOptions(), new Clock(), advanced: hub);
poller.Dispose();
hub.Acquired.Should().Be(0);
hub.Released.Should().Be(0);
}
}