// 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 Stored { get; } = []; public List<(long GameId, int Keep)> Sweeps { get; } = []; public HashSet PreExisting { get; } = []; public int SweptPerCall { get; set; } /// When set, throws it instead of storing (a write that fails). public Exception? InsertFailure { get; set; } public ValueTask 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 ExistsAsync(Guid sessionGuid, CancellationToken ct = default) => ValueTask.FromResult(PreExisting.Contains(sessionGuid) || Stored.Any(s => s.Row.SessionGuid == sessionGuid)); /// Who owns a finishing session: by default the id it started under, as if nothing had been removed. public Func Owner { get; set; } = static (gameId, _, _) => gameId; /// Every owner lookup, in order. public List<(long GameId, string? ExePath)> OwnerLookups { get; } = []; public ValueTask ResolveOwnerAsync(long gameId, string? exePath, DateTimeOffset startedAt, CancellationToken ct = default) { OwnerLookups.Add((gameId, exePath)); return ValueTask.FromResult(Owner(gameId, exePath, startedAt)); } public ValueTask FindAsync(Guid sessionGuid, CancellationToken ct = default) => ValueTask.FromResult(Stored.Select(s => s.Row).FirstOrDefault(r => r.SessionGuid == sessionGuid)); public ValueTask> ListAdvancedAsync(int count, CancellationToken ct = default) => ValueTask.FromResult>([.. Stored.Where(static s => s.Advanced is not null).Select(static s => s.Row).TakeLast(count).Reverse()]); public ValueTask> ListRecentAsync(int count, CancellationToken ct = default) => ValueTask.FromResult>([.. Stored.Select(s => s.Row).TakeLast(count).Reverse()]); public ValueTask SweepRetentionAsync(long gameId, int keep, CancellationToken ct = default) { Sweeps.Add((gameId, keep)); return ValueTask.FromResult(SweptPerCall); } /// Every keep an all-games sweep was asked for (P4 PR-7), and the result it answers. public List SweepAlls { get; } = []; public RetentionSweepResult SweepAllResult { get; set; } = new(0, 0); public ValueTask SweepRetentionAllAsync(int keep, CancellationToken ct = default) { SweepAlls.Add(keep); return ValueTask.FromResult(SweepAllResult); } public ValueTask FindFramesAsync(long sessionId, CancellationToken ct = default) => ValueTask.FromResult(sessionId >= 1 && sessionId <= Stored.Count ? Stored[(int)sessionId - 1].Frames : null); public ValueTask FindByIdAsync(long sessionId, CancellationToken ct = default) => ValueTask.FromResult(sessionId >= 1 && sessionId <= Stored.Count ? Stored[(int)sessionId - 1].Row with { Id = sessionId } : null); public ValueTask> ListByGameAsync(long gameId, int limit, CancellationToken ct = default) => ValueTask.FromResult>([.. Stored.Select(s => s.Row).Where(r => r.GameId == gameId).OrderByDescending(r => r.StartedAt).Take(limit)]); public ValueTask> SummariseByGameAsync(CancellationToken ct = default) => ValueTask.FromResult>([.. 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 CountSinceAsync(DateTimeOffset since, CancellationToken ct = default) => ValueTask.FromResult((long)Stored.Count(s => s.Row.StartedAt >= since)); /// beta.11 (D40): every session of the game, or of every game, leaves the store. public ValueTask DeleteSessionsAsync(long? gameId, CancellationToken ct = default) { List 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)); } /// D38: each game's successful sessions under its grant in force, as the tests say; a game not named has none. public Dictionary SuccessfulUnderGrant { get; } = []; /// D38: the count for a game the dictionary does not name (the recorder's own row ids are not the test's). public int UnderGrantDefault { get; set; } /// D38: every count asked for, in order — the family and grant time it was asked about. public List<(long GameId, string Family, DateTimeOffset GrantedAt)> GrantCounts { get; } = []; public ValueTask CountSuccessfulUnderGrantAsync(long gameId, string family, DateTimeOffset grantedAt, CancellationToken ct = default) { GrantCounts.Add((gameId, family, grantedAt)); return ValueTask.FromResult(SuccessfulUnderGrant.GetValueOrDefault(gameId, UnderGrantDefault)); } public ValueTask> FindSegmentsAsync(long sessionId, CancellationToken ct = default) => ValueTask.FromResult(sessionId >= 1 && sessionId <= Stored.Count ? Stored[(int)sessionId - 1].Segments : []); public ValueTask> FindSensorsAsync(long sessionId, CancellationToken ct = default) => ValueTask.FromResult>(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> FindInputSensorsAsync(long sessionId, CancellationToken ct = default) => ValueTask.FromResult>(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> FindAdvancedSensorsAsync(long sessionId, CancellationToken ct = default) => ValueTask.FromResult>(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 FindAdvancedAsync(long sessionId, CancellationToken ct = default) => ValueTask.FromResult(sessionId >= 1 && sessionId <= Stored.Count ? Stored[(int)sessionId - 1].Advanced : null); }