Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
102 lines
3.3 KiB
C#
102 lines
3.3 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>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
|
|
{
|
|
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 : [],
|
|
};
|
|
}
|
|
}
|