Files
frameledger/tests/FrameLedger.App.Tests/BugBundleBuilderTests.cs
poli0981andClaude Fable 5.2 1342979353 ci: Qodana - JetBrains' code analysis from a workflow of its own, and what its first run reported settled (D93) (#373)
* ci: Qodana - a workflow of its own from JetBrains' template, Qodana Community for .NET over FrameLedger.slnx (D93)

The first run: the linter in its Linux container, EnableWindowsTargeting passed
to it, no quality gate yet - what it builds and what it reports decides the rest.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* ci: Qodana's gate is zero problems, and the inspections this project argues with are off in .editorconfig, each with its reason (D93)

Pushed before the fixes on purpose: the run this starts has the settings and
none of the fixes, so it shows the gate failing on what is left.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* fix: what Qodana's first run reported, settled - about 400 fixes in 181 files, none changing behaviour; the documents (D93)

Redundant qualifiers, casts, using directives, nullable suppressions and unsafe
modifiers removed from the report's own positions; var for built-in types and
one-line block bodies brought to .editorconfig's style by dotnet format; every
XAML item template names its DataType and a binding that reached its page through
DataContext.ViewModel names the page's type; two unread fields, two overwritten
initialisers and an unused parameter removed; SpokenTextAutomationPeer no longer
assumes the base peer's name is a string.

docs/13_CI_CD.md §Qodana, CLAUDE.md, the wiki's workflow table, HANDOFF's
decisions index (D93) and the changelog entry.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 16:13:04 +00:00

190 lines
10 KiB
C#

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using System.IO;
using System.IO.Compression;
using System.Text.Json;
using FluentAssertions;
using FrameLedger.App.Services;
using FrameLedger.Application.Settings;
using FrameLedger.Infrastructure.Persistence;
using FrameLedger.Shared.Ipc;
namespace FrameLedger.App.Tests;
/// <summary>
/// <c>10_LOGGING</c> §Bug report flow step 2: our logs of the last seven days, the last overlay logs, sysinfo and the
/// sanitized settings — and nothing that is not ours (no game's file, however it is named).
/// </summary>
public sealed class BugBundleBuilderTests : IDisposable
{
private static CancellationToken Ct => TestContext.Current.CancellationToken;
private readonly string _dir = Path.Combine(Path.GetTempPath(), "fl-bundle-" + Guid.NewGuid().ToString("N"));
private sealed class FixedClock(DateTimeOffset now) : TimeProvider
{
public override DateTimeOffset GetUtcNow() => now;
}
public BugBundleBuilderTests() => Directory.CreateDirectory(_dir);
public void Dispose() => Directory.Delete(_dir, recursive: true);
[Fact]
public async Task ShipsOurLogsOfSevenDaysTheLastOverlaysAndTheTwoJsonFiles()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var clock = new FixedClock(new DateTimeOffset(2026, 9, 14, 12, 0, 0, TimeSpan.Zero));
DateTime now = clock.GetUtcNow().UtcDateTime;
Write("ui-20260914.log", now);
Write("agent-20260914.log", now.AddHours(-1));
Write("ui-20260901.log", now.AddDays(-8));
Write("game-crash.log", now);
for (int i = 0; i < BugBundleBuilder.OverlayLogsKept + 2; i++)
{
Write($"overlay-{1000 + i}-20260914-12000{i}.log", now.AddMinutes(-i));
}
var settings = new RegisteredSettings(new SqliteSettingsStore(s.Db));
await settings.SetAsync(SettingsRegistry.HookingKillSwitch, true, Ct);
string zip = Path.Combine(_dir, "bundle.zip");
IReadOnlyList<string> written = await new BugBundleBuilder(_dir, settings, clock).WriteAsync(zip, agent: null, ct: Ct);
written.Should().Contain(["logs/ui-20260914.log", "logs/agent-20260914.log", "sysinfo.json", "settings.json"]);
written.Should().NotContain("logs/ui-20260901.log", "older than seven days");
written.Should().NotContain(static e => e.Contains("game-crash", StringComparison.Ordinal), "we ship our logs only");
written.Count(static e => e.StartsWith("overlay/", StringComparison.Ordinal)).Should().Be(BugBundleBuilder.OverlayLogsKept);
written.Should().Contain("overlay/overlay-1000-20260914-120000.log", "the newest overlay log");
written.Should().NotContain("overlay/overlay-1006-20260914-120006.log", "the oldest is past the cap");
Dictionary<string, string> values = ReadJson(zip, "settings.json");
values.Should().ContainKey("hooking.kill_switch").WhoseValue.Should().Be("1");
values.Keys.Should().BeEquivalentTo(SettingsRegistry.All.Select(static d => d.Key), "every registry key, and no path");
Dictionary<string, string> sysinfo = ReadJson(zip, "sysinfo.json");
sysinfo["agent_version"].Should().Be("not connected");
sysinfo["app_version"].Should().Be(UiIdentity.Version);
}
[Fact]
public async Task TheAgentsHelloFillsSysinfoWhenConnected()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
string zip = Path.Combine(_dir, "bundle.zip");
var hello = new HelloAck("1.2.3", IpcProtocol.Version, 4, true, "build-x", true, "l3", true, "consent-dialog/1");
await new BugBundleBuilder(_dir, new RegisteredSettings(new SqliteSettingsStore(s.Db))).WriteAsync(zip, hello, ct: Ct);
Dictionary<string, string> sysinfo = ReadJson(zip, "sysinfo.json");
sysinfo["agent_version"].Should().Be("1.2.3");
sysinfo["overlay_build_id"].Should().Be("build-x");
sysinfo["vulkan_layer_registered"].Should().Be("True");
sysinfo["telemetry_source"].Should().Be("l3");
}
[Fact]
public async Task TheLogCopiesCarryNoUserNameAndTheFilesOnDiskAreUntouched()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var clock = new FixedClock(new DateTimeOffset(2026, 9, 15, 12, 0, 0, TimeSpan.Zero));
DateTime now = clock.GetUtcNow().UtcDateTime;
string ui = Path.Combine(_dir, "ui-20260915.log");
await File.WriteAllTextAsync(ui, "[12:00:00.000 INF] ui: started (0.1.0), ledger C:\\Users\\tester\\AppData\\Local\\FrameLedger\\ledger.db {\"Path\":\"C:\\\\Users\\\\tester\\\\AppData\"}\r\n", Ct);
File.SetLastWriteTimeUtc(ui, now);
string overlay = Path.Combine(_dir, "overlay-4242-20260915-120000.log");
await File.WriteAllTextAsync(overlay, "# FrameLedger.Overlay build x pid 4242 layout v9 image C:\\Users\\tester\\Games\\title.exe\nSTOP\n", Ct);
File.SetLastWriteTimeUtc(overlay, now);
string zip = Path.Combine(_dir, "bundle.zip");
await new BugBundleBuilder(_dir, new RegisteredSettings(new SqliteSettingsStore(s.Db)), clock, redactor: new LogRedactor(@"C:\Users\tester")).WriteAsync(zip, agent: null, ct: Ct);
string uiCopy = ReadEntry(zip, "logs/ui-20260915.log");
string overlayCopy = ReadEntry(zip, "overlay/overlay-4242-20260915-120000.log");
uiCopy.Should().NotContain("tester").And.Contain(@"C:\Users\<user>\AppData\Local\FrameLedger\ledger.db").And.Contain(@"C:\\Users\\<user>\\AppData");
overlayCopy.Should().NotContain("tester").And.StartWith(@"# FrameLedger.Overlay build x pid 4242 layout v9 image C:\Users\<user>\Games\title.exe");
(await File.ReadAllTextAsync(ui, Ct)).Should().Contain("tester", "the redaction is the bundle's copy, never the log on disk");
}
[Fact]
public void TheSuggestedNameIsTheDocsFormat() => BugBundleBuilder.SuggestedName(new DateTimeOffset(2026, 9, 14, 12, 34, 0, TimeSpan.Zero).ToLocalTime()).Should().MatchRegex(@"^FrameLedger-bugreport-\d{8}-\d{4}\.zip$");
[Fact]
public async Task TheCrashDumpGoesInOnlyWhenPassedAndOnlyFromTheDumpDirectory()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var clock = new FixedClock(new DateTimeOffset(2026, 9, 15, 12, 0, 0, TimeSpan.Zero));
string dumps = Path.Combine(_dir, "crashdumps");
string dump = WriteDump(dumps, "ui-20260915-100000-42.dmp", clock.GetUtcNow().UtcDateTime.AddHours(-2), bytes: 3000);
var builder = new BugBundleBuilder(_dir, new RegisteredSettings(new SqliteSettingsStore(s.Db)), clock, dumps);
CrashDumpInfo latest = builder.LatestCrashDump()!;
latest.Path.Should().Be(dump);
latest.Bytes.Should().Be(3000);
IReadOnlyList<string> without = await builder.WriteAsync(Path.Combine(_dir, "without.zip"), agent: null, ct: Ct);
IReadOnlyList<string> with = await builder.WriteAsync(Path.Combine(_dir, "with.zip"), agent: null, latest, ct: Ct);
without.Should().NotContain(static e => e.StartsWith("crashdumps/", StringComparison.Ordinal), "a dump goes in only when the user ticked it");
with.Should().Contain("crashdumps/ui-20260915-100000-42.dmp");
string foreign = Path.Combine(_dir, "game.dmp");
await File.WriteAllTextAsync(foreign, "MDMP", Ct);
Func<Task> outside = () => builder.WriteAsync(Path.Combine(_dir, "foreign.zip"), agent: null, latest with { Path = foreign }, ct: Ct);
await outside.Should().ThrowAsync<ArgumentException>("the bundle never carries a file from outside the dump directory");
File.Exists(Path.Combine(_dir, "foreign.zip")).Should().BeFalse("refused before the zip is created");
}
[Fact]
public async Task TheOfferedDumpIsTheNewestOfTheLastSevenDays()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var settings = new RegisteredSettings(new SqliteSettingsStore(s.Db));
var clock = new FixedClock(new DateTimeOffset(2026, 9, 15, 12, 0, 0, TimeSpan.Zero));
DateTime now = clock.GetUtcNow().UtcDateTime;
string dumps = Path.Combine(_dir, "crashdumps");
new BugBundleBuilder(_dir, settings, clock).LatestCrashDump().Should().BeNull("no dump directory was given");
new BugBundleBuilder(_dir, settings, clock, dumps).LatestCrashDump().Should().BeNull("the directory does not exist yet");
WriteDump(dumps, "ui-old.dmp", now.AddDays(-8), bytes: 10);
new BugBundleBuilder(_dir, settings, clock, dumps).LatestCrashDump().Should().BeNull("older than seven days");
WriteDump(dumps, "agent-older.dmp", now.AddDays(-2), bytes: 20);
WriteDump(dumps, "ui-newest.dmp", now.AddHours(-1), bytes: 30);
WriteDump(dumps, "ui-notes.txt", now, bytes: 40);
CrashDumpInfo? latest = new BugBundleBuilder(_dir, settings, clock, dumps).LatestCrashDump();
latest.Should().NotBeNull();
Path.GetFileName(latest.Path).Should().Be("ui-newest.dmp", "the newest dump of either process; a .txt is not a dump");
latest.WrittenAt.Should().Be(new DateTimeOffset(now.AddHours(-1), TimeSpan.Zero));
latest.Bytes.Should().Be(30);
}
private static string WriteDump(string directory, string name, DateTime mtimeUtc, int bytes)
{
Directory.CreateDirectory(directory);
string path = Path.Combine(directory, name);
File.WriteAllBytes(path, new byte[bytes]);
File.SetLastWriteTimeUtc(path, mtimeUtc);
return path;
}
private static string ReadEntry(string zip, string entry)
{
using ZipArchive archive = ZipFile.OpenRead(zip);
using var reader = new StreamReader(archive.GetEntry(entry)!.Open());
return reader.ReadToEnd();
}
private static Dictionary<string, string> ReadJson(string zip, string entry)
{
using ZipArchive archive = ZipFile.OpenRead(zip);
using Stream stream = archive.GetEntry(entry)!.Open();
return JsonSerializer.Deserialize<Dictionary<string, string>>(stream)!;
}
private void Write(string name, DateTime mtimeUtc)
{
string path = Path.Combine(_dir, name);
File.WriteAllText(path, "[12:00:00.000 INF] " + name + "\n");
File.SetLastWriteTimeUtc(path, mtimeUtc);
}
}