Files
poli0981andClaude Fable 5.2 dca740e94b feat(input): a session can store what it was played with - schema 0024, the .partial file's chunk 11, both tiers (D160)
Nine columns on sessions (the time in front and its four classes, the switches of method, the sources that answered,
the controllers as JSON) and three on games (what a store lists of controller support), ADD COLUMN only. The .partial
file gains chunk 11, so a session recovered after the Agent died keeps its readings. SessionFinalizer.Build stores
them before the tier branch: hooked, unhooked and recovered sessions alike. The input series are sensor_blobs rows
named input.*, which the sensors' read now leaves out and FindInputSensorsAsync returns.

Nothing produces a reading yet. A ledger this build has opened cannot be opened by 0.1.0-beta.49 or earlier.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-14 21:54:13 +07:00

149 lines
7.9 KiB
C#

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using FrameLedger.Application.Persistence;
namespace FrameLedger.Application.Tests.Recording;
internal sealed class FakeSessionRepository : ISessionRepository
{
public List<FinalizedSession> Stored { get; } = [];
public List<(long GameId, int Keep)> Sweeps { get; } = [];
public HashSet<Guid> PreExisting { get; } = [];
public int SweptPerCall { get; set; }
/// <summary>When set, <see cref="InsertFinalizedAsync"/> throws it instead of storing (a write that fails).</summary>
public Exception? InsertFailure { get; set; }
public ValueTask<long> InsertFinalizedAsync(FinalizedSession session, CancellationToken ct = default)
{
if (InsertFailure is { } failure)
{
throw failure;
}
if (PreExisting.Contains(session.Row.SessionGuid) || Stored.Any(s => s.Row.SessionGuid == session.Row.SessionGuid))
{
throw new InvalidOperationException("session_guid already stored");
}
Stored.Add(session);
return ValueTask.FromResult((long)Stored.Count);
}
public ValueTask<bool> ExistsAsync(Guid sessionGuid, CancellationToken ct = default) =>
ValueTask.FromResult(PreExisting.Contains(sessionGuid) || Stored.Any(s => s.Row.SessionGuid == sessionGuid));
/// <summary>Who owns a finishing session: by default the id it started under, as if nothing had been removed.</summary>
public Func<long, string?, DateTimeOffset, long?> Owner { get; set; } = static (gameId, _, _) => gameId;
/// <summary>Every owner lookup, in order.</summary>
public List<(long GameId, string? ExePath)> OwnerLookups { get; } = [];
public ValueTask<long?> ResolveOwnerAsync(long gameId, string? exePath, DateTimeOffset startedAt, CancellationToken ct = default)
{
OwnerLookups.Add((gameId, exePath));
return ValueTask.FromResult(Owner(gameId, exePath, startedAt));
}
public ValueTask<SessionRow?> FindAsync(Guid sessionGuid, CancellationToken ct = default) =>
ValueTask.FromResult(Stored.Select(s => s.Row).FirstOrDefault(r => r.SessionGuid == sessionGuid));
public ValueTask<IReadOnlyList<SessionRow>> ListAdvancedAsync(int count, CancellationToken ct = default) =>
ValueTask.FromResult<IReadOnlyList<SessionRow>>([.. Stored.Where(static s => s.Advanced is not null).Select(static s => s.Row).TakeLast(count).Reverse()]);
public ValueTask<IReadOnlyList<SessionRow>> ListRecentAsync(int count, CancellationToken ct = default) =>
ValueTask.FromResult<IReadOnlyList<SessionRow>>([.. Stored.Select(s => s.Row).TakeLast(count).Reverse()]);
public ValueTask<int> SweepRetentionAsync(long gameId, int keep, CancellationToken ct = default)
{
Sweeps.Add((gameId, keep));
return ValueTask.FromResult(SweptPerCall);
}
/// <summary>Every keep an all-games sweep was asked for (P4 PR-7), and the result it answers.</summary>
public List<int> SweepAlls { get; } = [];
public RetentionSweepResult SweepAllResult { get; set; } = new(0, 0);
public ValueTask<RetentionSweepResult> SweepRetentionAllAsync(int keep, CancellationToken ct = default)
{
SweepAlls.Add(keep);
return ValueTask.FromResult(SweepAllResult);
}
public ValueTask<FrameBlobs?> FindFramesAsync(long sessionId, CancellationToken ct = default) =>
ValueTask.FromResult(sessionId >= 1 && sessionId <= Stored.Count ? Stored[(int)sessionId - 1].Frames : null);
public ValueTask<SessionRow?> FindByIdAsync(long sessionId, CancellationToken ct = default) =>
ValueTask.FromResult(sessionId >= 1 && sessionId <= Stored.Count ? Stored[(int)sessionId - 1].Row with { Id = sessionId } : null);
public ValueTask<IReadOnlyList<SessionRow>> ListByGameAsync(long gameId, int limit, CancellationToken ct = default) =>
ValueTask.FromResult<IReadOnlyList<SessionRow>>([.. Stored.Select(s => s.Row).Where(r => r.GameId == gameId).OrderByDescending(r => r.StartedAt).Take(limit)]);
public ValueTask<IReadOnlyList<GameSessionSummary>> SummariseByGameAsync(CancellationToken ct = default) =>
ValueTask.FromResult<IReadOnlyList<GameSessionSummary>>([.. Stored.GroupBy(s => s.Row.GameId).Select(g => new GameSessionSummary
{
GameId = g.Key,
SessionCount = g.Count(),
HookedCount = g.Count(s => s.Row.Tier == Domain.Sessions.CaptureTier.Hooked),
TotalSeconds = g.Sum(s => s.Row.DurationSeconds),
LastPlayedAt = g.Max(s => s.Row.EndedAt),
})]);
public ValueTask<long> CountSinceAsync(DateTimeOffset since, CancellationToken ct = default) =>
ValueTask.FromResult((long)Stored.Count(s => s.Row.StartedAt >= since));
/// <summary>beta.11 (D40): every session of the game, or of every game, leaves the store.</summary>
public ValueTask<RetentionSweepResult> DeleteSessionsAsync(long? gameId, CancellationToken ct = default)
{
List<FinalizedSession> gone = [.. Stored.Where(s => gameId is null || s.Row.GameId == gameId)];
int games = gone.Select(static s => s.Row.GameId).Distinct().Count();
foreach (FinalizedSession session in gone)
{
Stored.Remove(session);
}
return ValueTask.FromResult(new RetentionSweepResult(games, gone.Count));
}
/// <summary>D38: each game's successful sessions under its grant in force, as the tests say; a game not named has none.</summary>
public Dictionary<long, int> SuccessfulUnderGrant { get; } = [];
/// <summary>D38: the count for a game the dictionary does not name (the recorder's own row ids are not the test's).</summary>
public int UnderGrantDefault { get; set; }
/// <summary>D38: every count asked for, in order — the family and grant time it was asked about.</summary>
public List<(long GameId, string Family, DateTimeOffset GrantedAt)> GrantCounts { get; } = [];
public ValueTask<int> CountSuccessfulUnderGrantAsync(long gameId, string family, DateTimeOffset grantedAt, CancellationToken ct = default)
{
GrantCounts.Add((gameId, family, grantedAt));
return ValueTask.FromResult(SuccessfulUnderGrant.GetValueOrDefault(gameId, UnderGrantDefault));
}
public ValueTask<IReadOnlyList<SegmentRow>> FindSegmentsAsync(long sessionId, CancellationToken ct = default) =>
ValueTask.FromResult(sessionId >= 1 && sessionId <= Stored.Count ? Stored[(int)sessionId - 1].Segments : []);
public ValueTask<IReadOnlyList<SensorBlob>> FindSensorsAsync(long sessionId, CancellationToken ct = default) =>
ValueTask.FromResult<IReadOnlyList<SensorBlob>>(sessionId >= 1 && sessionId <= Stored.Count
? [.. Stored[(int)sessionId - 1].Sensors.Where(static s => !s.Series.StartsWith("adv.", StringComparison.Ordinal) && !s.Series.StartsWith("input.", StringComparison.Ordinal))]
: []);
public ValueTask<IReadOnlyList<SensorBlob>> FindInputSensorsAsync(long sessionId, CancellationToken ct = default) =>
ValueTask.FromResult<IReadOnlyList<SensorBlob>>(sessionId >= 1 && sessionId <= Stored.Count
? [.. Stored[(int)sessionId - 1].Sensors.Where(static s => string.Equals(s.Series, "t_ms", StringComparison.Ordinal) || s.Series.StartsWith("input.", StringComparison.Ordinal))]
: []);
public ValueTask<IReadOnlyList<SensorBlob>> FindAdvancedSensorsAsync(long sessionId, CancellationToken ct = default) =>
ValueTask.FromResult<IReadOnlyList<SensorBlob>>(sessionId >= 1 && sessionId <= Stored.Count
? [.. Stored[(int)sessionId - 1].Sensors.Where(static s => string.Equals(s.Series, "t_ms", StringComparison.Ordinal) || s.Series.StartsWith("adv.", StringComparison.Ordinal))]
: []);
public ValueTask<AdvancedSessionRow?> FindAdvancedAsync(long sessionId, CancellationToken ct = default) =>
ValueTask.FromResult(sessionId >= 1 && sessionId <= Stored.Count ? Stored[(int)sessionId - 1].Advanced : null);
}