Files
Febe/tests/Febe.Tests/Engine/Support/ScriptedRunner.cs
T
Claude aff81dfac9 Collegamento e motore: correzioni regole-1, regole-2, regole-6, websec-1, websec-5, websec-6, engine-1, engine-2, sched-1
Pagine aperte da Alberto non sono segnali (regole-1); cookie perso fuori sessione = disconnessione (regole-2); pagina a riposo quando Alberto lascia il browser (regole-6); la lease di «Accedi»/«Usa il browser» finisce da sola dopo 2 minuti senza vista remota né finestra (websec-1); ping con scadenza sulla vista remota (websec-5); aperture della vista remota serializzate (websec-6); orologio virtuale senza fine anticipata (engine-1); la sincronizzazione della coda non duplica il compito in corso (engine-2); raccolte in evidenza programmate (sched-1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
2026-10-14 00:21:13 +00:00

113 lines
3.6 KiB
C#

using Febe.Core.Abstractions;
using Febe.Core.Engine;
namespace Febe.Tests.Engine.Support;
/// <summary>A recipe run seen by <see cref="ScriptedRunner"/>.</summary>
internal sealed record RunRecord(DateTimeOffset At, RecipeRequest Request, long SessionId, RecipeStatus Status);
/// <summary>
/// A recipe runner for engine tests: asks the Governor for one counted action like the real recipes (stories:
/// none, as if no story was active), spends a little virtual time and completes, unless <see cref="Script"/> says
/// otherwise. Records every run.
/// </summary>
internal sealed class ScriptedRunner(IGovernor governor, IClock clock) : IRecipeRunner
{
private readonly Lock _gate = new();
private readonly List<RunRecord> _runs = [];
/// <summary>Forced outcome for a request (null: default behaviour). Runs before the Governor is asked.</summary>
public Func<RecipeRequest, RecipeOutcome?>? Script { get; set; }
/// <summary>Called after the counted action was granted (e.g. to report a signal).</summary>
public Action<RecipeRequest>? AfterGrant { get; set; }
/// <summary>
/// Awaited when a recipe starts, while its task is running (e.g. what the real recipe writes, or a command of
/// Alberto arriving meanwhile).
/// </summary>
public Func<RecipeRequest, Task>? Before { get; set; }
/// <summary>Accounts the stories bar flags in every sweep.</summary>
public IReadOnlyList<long> FlaggedStories { get; set; } = [];
public int Rests { get; private set; }
public IReadOnlyList<RunRecord> Runs
{
get
{
lock (_gate)
{
return [.. _runs];
}
}
}
public async Task<RecipeOutcome> RunAsync(RecipeRequest request, ISessionContext session, CancellationToken ct)
{
RecipeOutcome outcome;
try
{
if (Before is { } before)
{
await before(request);
}
outcome = Script?.Invoke(request) ?? await DefaultAsync(request, ct);
}
catch (OperationCanceledException)
{
lock (_gate)
{
_runs.Add(new RunRecord(clock.Now, request, session.SessionId, RecipeStatus.Cancelled));
}
throw;
}
lock (_gate)
{
_runs.Add(new RunRecord(clock.Now, request, session.SessionId, outcome.Status));
}
return outcome;
}
public Task RestAsync(CancellationToken ct)
{
Rests++;
return Task.CompletedTask;
}
private async Task<RecipeOutcome> DefaultAsync(RecipeRequest request, CancellationToken ct)
{
if (request.Kind == RecipeKind.AccountStories)
{
await clock.Delay(TimeSpan.FromSeconds(2), ct);
return new RecipeOutcome(RecipeStatus.Completed, 0, 0);
}
var kind = CheckPolicy.CategoryOf(request.Kind);
var pages = request.Kind == RecipeKind.Backfill ? Math.Max(1, request.MaxPages ?? 1) : 1;
var counted = 0;
for (var i = 0; i < pages; i++)
{
var permit = await governor.RequestAsync(kind, request.AccountId, ct);
if (!permit.Granted)
{
return new RecipeOutcome(counted > 0 ? RecipeStatus.Completed : RecipeStatus.Denied, counted, 0);
}
counted++;
AfterGrant?.Invoke(request);
await clock.Delay(TimeSpan.FromSeconds(5), ct);
}
return new RecipeOutcome(RecipeStatus.Completed, counted, 0)
{
AccountsWithStories = request.Kind == RecipeKind.FollowingSweep ? FlaggedStories : [],
};
}
}