Files
frameledger/tests/FrameLedger.CaptureHost.Tests/CommandLineSurfaceTests.cs
poli0981andClaude Fable 5.2 c4318b69d5 feat(telemetry): the reader behind Advanced metrics - one for the Agent, on its own thread, only while held; LibreHardwareMonitor's card, processor, memory and board groups; measured before anything composes it (Advanced PR-3, D103/D107) (#389)
Nothing calls it yet: no Agent code composes the reader, so nothing a user
sees or records changes.

- Application.Advanced: the port IAdvancedHardware (Acquire, Latest,
  ReadSessionFacts), AdvancedSnapshot, AdvancedSources and the status of
  each source - Reading, NotPresent, NotElevated, PawnIoMissing, Faulted.
  A source that is not there is a state, never an exception and never a
  value in its place.
- Infrastructure: AdvancedHardwareHub - one reader for the whole Agent on
  a thread of its own (fl-advanced), started by the first holder and
  closed a linger after the last lets go (D107), so a PC idling with
  FrameLedger in the tray reads nothing for this. AdvancedHardwareReader -
  one tick of NVAPI and of LibreHardwareMonitor. LhmAdvancedReader over a
  Computer of its own: every sensor of the graphics card, and as
  administrator with PawnIO the processor, memory and motherboard groups
  (D103). LhmGroups has a member for those four and no other.
- Storage is not opened (D103's outcome, written before any run):
  LibreHardwareMonitor 0.9.6's StorageGroup lists every drive through
  DiskInfoToolkit with no filter by bus and follows drives plugged in
  later. lhm.storage.* is Deferred in the catalog; a drive's temperature
  will read N/A with that reason.
- A device that throws twice is left alone and the others go on; NVAPI
  answering with no field twice running, after it had answered, is
  released and initialised again once (20_OPEN_QUESTIONS M12).
- FrameLedger.CaptureHost probe-advanced (unshipped): runs that reader on
  the caller's own rights and prints what it read and what it cost. On the
  dev box (RTX 5080, unelevated): 35 facts, 58 readings a tick; one NVAPI
  live read 0.4 ms of processor time; one refresh of the sensor library's
  graphics-card group 14 ms (62 ms elapsed) - 1.4% of one core at one a
  second, above NFR-2. So NVAPI is read every tick and the sensor library
  every fifth (0.28%), with no value repeated between.
- The same refresh is what the L2 telemetry layer does once a second in
  every session. That cost is not this train's: recorded as
  20_OPEN_QUESTIONS M16, with the owner's choices.
- PrivilegeFootprintTests: opening every group this project can name
  enables no powerful privilege (the real thing on the elevated CI runner).
- docs: 18_GPU_VENDOR_APIS (the reader, D103 as found, L2's line),
  20_OPEN_QUESTIONS (M13 half answered, M16 new), HANDOFF, 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 08:58:39 +00:00

200 lines
10 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.Consent;
namespace FrameLedger.CaptureHost.Tests;
/// <summary>
/// What the host can be ASKED to do, pinned.
/// </summary>
/// <remarks>
/// §S27's gap was a user-named pid on a binary carrying no consent record. The
/// surface is the place that stays closed, so it is the place that gets an
/// assertion — a reviewer scanning a diff for a new flag is the mechanism this
/// replaces.
/// </remarks>
public sealed class CommandLineSurfaceTests
{
[Fact]
public void TheAcceptedOptionsAreExactlyThese() =>
// TWO, and adding the second was a deliberate, reviewed act -- this assertion is what
// made it one. `--seconds` bounds how long an already-consented, already-guarded
// session runs; it cannot widen WHAT is injected or skip a check, which is the
// property that separates it from the --pid / --payload / --force / --yes this host
// refuses. It exists because CaptureSession honoured MaxDuration and nothing could set
// it, so a bounded real-title measurement was impossible to take.
// THREE since P1 item 2: `--args` is the command line the launched GAME receives. It names
// nothing about injection -- payload, target and evidence are exactly what they were -- so it
// has the same property `--seconds` has: it cannot widen what is injected or skip a check.
CommandLine.AcceptedOptions.Should().BeEquivalentTo(["--exe", "--seconds", "--args"]);
[Fact]
public void LaunchTakesTheExecutableItsArgumentsAndABound()
{
CommandLine parsed = CommandLine.Parse(["launch", "--exe", @"C:\a\game.exe", "--args", "--real --hold 3", "--seconds", "9"]);
parsed.Error.Should().BeNull();
parsed.Verb.Should().Be(Verb.Launch);
parsed.ExePath.Should().Be(@"C:\a\game.exe");
parsed.Arguments.Should().Be("--real --hold 3", "handed to the game verbatim");
parsed.Seconds.Should().Be(9);
CommandLine.Parse(["launch"]).Error.Should().NotBeNull("--exe is required: there is nothing else to launch");
CommandLine.Parse(["launch", "--exe", "g.exe"]).Arguments.Should().BeEmpty();
}
[Fact]
public void ASecondsValueThatIsNotAPositiveNumberIsAnErrorAndNeverASilentZero()
{
// ZERO MEANS "UNTIL THE TARGET EXITS". So leniency here does not produce a shorter
// session than asked for — it produces an UNBOUNDED one, which is the opposite of what
// an operator typing --seconds wants. Every rejected form is listed, because "it parsed
// as 0" and "it was refused" are indistinguishable from the exit code alone.
foreach (string bad in new[] { "abc", "0", "-5", "", "1.5", "2s", "+9", " 9" })
{
CommandLine.Parse(["capture", "--exe", "game.exe", "--seconds", bad]).Error
.Should().NotBeNull($"'--seconds {bad}' must be refused rather than read as unbounded");
}
CommandLine parsed = CommandLine.Parse(["capture", "--exe", "game.exe", "--seconds", "45"]);
parsed.Error.Should().BeNull();
parsed.Seconds.Should().Be(45);
parsed.ExePath.Should().Be("game.exe", "--seconds must not be mistaken for the --exe value");
// Absent is the product default, and it is 0 = run until the target exits.
CommandLine.Parse(["capture", "--exe", "game.exe"]).Seconds.Should().Be(0);
}
[Fact]
public void NoVerbOrOptionNamesAPidAPayloadOrAnOverride()
{
// --pid is §S27's gap. --payload would defeat §S22, which compares the payload's directory
// against the guard's own by file id. --force and --yes are the "I understand, continue anyway"
// button CLAUDE.md rule 2 forbids. --diag is already taken by the App (10_LOGGING).
string[] forbidden = ["pid", "payload", "force", "yes", "diag", "override"];
IEnumerable<string> surface = [.. CommandLine.AcceptedOptions, .. Enum.GetNames<Verb>()];
foreach (string token in surface)
{
foreach (string bad in forbidden)
{
token.Should().NotContainEquivalentOf(bad);
}
}
}
[Theory]
[InlineData("--pid")]
[InlineData("--force")]
[InlineData("--payload")]
public void AnUnknownOptionIsRefusedRatherThanIgnored(string option) => CommandLine.Parse(["capture", option, "1234"]).Error.Should().NotBeNull();
[Fact]
public void CaptureNeedsAnExecutable()
{
CommandLine.Parse(["capture"]).Error.Should().NotBeNull();
CommandLine.Parse(["capture", "--exe", @"C:\a\game.exe"]).Should().BeEquivalentTo(
new CommandLine { Verb = Verb.Capture, ExePath = @"C:\a\game.exe" });
}
[Fact]
public void ProbeLhmNamesNoGameAndNeedsNoConsentRecord()
{
// §M5's instrument opens a sensor library in THIS process. There is nothing to inject
// into and nothing to consent to, so --exe is not required — and it is the only verb
// besides `consent list` for which that is true. The seconds bound is shared.
CommandLine.Parse(["probe-lhm"]).Should().BeEquivalentTo(new CommandLine { Verb = Verb.ProbeLhm });
CommandLine.Parse(["probe-lhm", "--seconds", "12"]).Seconds.Should().Be(12);
CommandLine.Parse(["probe-lhm", "--seconds", "0"]).Error.Should().NotBeNull();
CommandLine.Parse(["probe-lhm", "--pid", "1"]).Error.Should().NotBeNull();
}
[Fact]
public void ProbeAdvancedNamesNoGameAndNeedsNoConsentRecord()
{
// The instrument of 20_OPEN_QUESTIONS M12 and M13 runs the advanced reader in THIS process: like probe-lhm it
// has nothing to inject into and nothing to consent to, and it shares the seconds bound.
CommandLine.Parse(["probe-advanced"]).Should().BeEquivalentTo(new CommandLine { Verb = Verb.ProbeAdvanced });
CommandLine.Parse(["probe-advanced", "--seconds", "30"]).Seconds.Should().Be(30);
CommandLine.Parse(["probe-advanced", "--seconds", "x"]).Error.Should().NotBeNull();
CommandLine.Parse(["probe-advanced", "--storage", "1"]).Error.Should().NotBeNull("no option opens a group the reader does not open");
}
[Fact]
public void AnEmptyCommandLineIsAUsageError()
{
CommandLine.Parse([]).Error.Should().NotBeNull();
CommandLine.Parse(["nonsense"]).Error.Should().NotBeNull();
}
[Fact]
public void RedirectedInputCannotAcknowledgeAnything()
{
// An acknowledgement a script can supply is not the explicit human action HANDOFF licenses as
// "not synthesis". Passing null is exactly what Program does when Console.IsInputRedirected.
using var output = new StringWriter();
OperatorDisclosure.Confirm(output, input: null).Should().BeFalse();
output.ToString().Should().Contain("stdin is redirected");
}
[Fact]
public void OnlyTheExactPhraseAcknowledges()
{
using var output = new StringWriter();
using var wrongCase = new StringWriter();
using var exact = new StringWriter();
OperatorDisclosure.Confirm(output, new StringReader("yes")).Should().BeFalse();
OperatorDisclosure.Confirm(wrongCase, new StringReader("i accept the injection risk"))
.Should().BeFalse("the comparison is ordinal, so case is part of the phrase");
OperatorDisclosure.Confirm(exact, new StringReader("I ACCEPT THE INJECTION RISK"))
.Should().BeTrue();
}
[Fact]
public void TheDisclosureSaysWhatItIsNotBeforeItSaysAnythingElse()
{
// 19_SAFETY §User-facing consent's four statements, plus the one this text has to make that a
// real consent dialog does not: that it is NOT that dialog.
using var output = new StringWriter();
OperatorDisclosure.Confirm(output, input: null);
string text = output.ToString();
text.Should().Contain("DEVELOPER TOOL");
text.Should().Contain("NOT the per-game consent dialog");
text.Should().Contain("may flag or ban accounts");
text.Should().Contain("CANNOT GUARANTEE IT KNOWS EVERY ANTI-CHEAT");
text.Should().Contain("terms of service");
// THE FOURTH STATEMENT, AND THIS ASSERTION USED TO BE UNABLE TO FAIL FOR IT.
// It was `Contain("Tier-2")`. When the ladder collapsed to two rungs on 2026-08-28 the
// claim behind that token changed completely -- Tier 2 stopped being a no-injection
// MEASUREMENT and became "nothing was measured, and here is why" -- and any rewrite
// keeping the literal token would still have passed. A gate that cannot fail for the one
// thing that changed is this project's own recurring defect.
//
// So pin the SUBSTANCE: that declining is stated, and stated in terms of what it yields.
text.Should().Contain("N/A", "the fourth statement is what a session yields WITHOUT hooking");
text.Should().Contain("Tier 2");
text.Should().NotContain("Tier 3", "the ladder has two rungs");
// The two sentences this text carried until 2026-08-28, both false by then. The first told
// an operator that a no-injection mode would capture for them; the second told them the
// injection risk was optional, when hooking is now the only frame-time path there is.
text.Should().NotContainEquivalentOf("required to measure");
text.Should().NotContainEquivalentOf("is the default for anything the guard");
// 19_SAFETY: "Consent and disclaimer wording must say 'within 30 seconds', never 'immediately'."
//
// The word is banned OUTRIGHT rather than only in a promising sense, and the first draft of
// this text found out why: it read "stops WITHIN 30 SECONDS — not immediately", which is
// honest, denies the promise, and still puts the word in front of a reader who may take away
// only the emphasised part. A rule a test can enforce beats one that needs a reader to parse a
// negation, and the sentence loses nothing by saying "of it being detected" instead.
text.Should().Contain("WITHIN 30 SECONDS");
text.Should().NotContainEquivalentOf("immediately");
}
}