Brokkr/brokkr-contracts.Tests/EditorScenarioTests.cs
Meta Vens 1e46483fe5 A3: brokkr-command, the caller CLI, with every intent field and a real CultLib root
Re-landed from the donor as a fresh file (read with git show, not merged).
Changes from the donor: the CultLibRoot default is ..\..\CultLib instead of a
directory that does not exist; every field of brokkr.unity.command_intent.v0
has a --kebab option (the donor mapped six of seventeen), and unknown options
are refused before anything is written; keys come from BrokkrCommandLedger;
exit is 0 accepted, 1 other receipt, 2 usage error or timeout; readHost opens
the store read-only and prints the agent-commands flag; a timeout says when
the editor reports the sink off. The intent gains the capture fields at keys
13-16, which the CLI needs and A4's executor will read.

Tests run the built CLI as a process against the editor probe: field mapping,
fresh id per invocation, exit code from the receipt, timeout leaves only the
intent, unknown option writes nothing, and every action name reaches the
editor once with one receipt.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-29 14:26:23 +02:00

46 lines
2 KiB
C#

using GameCult.Brokkr;
using GameCult.Caching;
namespace Brokkr.Contracts.Tests;
// The Imagination probe, kept: many writers, one editor, the real store. Scaled to run in seconds; the probe
// measured 24 commands at a 1000 ms tick and 90 at a 20 ms tick, three writers at a time, with no loss.
public sealed class EditorScenarioTests
{
[Theory]
[InlineData(24, 200)]
[InlineData(90, 20)]
public async Task ManyWritersOneEditorExecuteEveryIntentOnceAndAnswerEachId(int commands, int tickMilliseconds)
{
using var project = new ScratchProject();
using var editor = await EditorProbe.StartAsync(project.StorePath);
var ids = Enumerable.Range(0, commands).Select(index => $"scenario-{index}").ToArray();
using var stop = new CancellationTokenSource();
var editorLoop = Task.Run(async () =>
{
while (!stop.IsCancellationRequested)
{
await editor.TickAsync();
await Task.Delay(tickMilliseconds);
}
});
foreach (var wave in ids.Chunk(3))
await Task.WhenAll(wave.Select(id => project.WriteCommandAsync(id)));
var deadline = DateTime.UtcNow.AddSeconds(60);
while (DateTime.UtcNow < deadline && editor.Executed.Count < commands)
await Task.Delay(50);
// One more tick after the last receipt, so a duplicate execution would have had its chance.
await Task.Delay(tickMilliseconds * 3);
stop.Cancel();
await editorLoop;
Assert.Equal(commands, editor.Executed.Count);
Assert.Equal(ids.OrderBy(id => id), editor.Executed.Select(command => command.commandId).OrderBy(id => id));
using var audit = project.OpenCache();
var receipts = audit.AllStoredDocuments.Select(entry => entry.Document).OfType<BrokkrUnityCommandReceipt>().ToArray();
Assert.Equal(ids.OrderBy(id => id), receipts.Select(receipt => receipt.commandId).OrderBy(id => id));
}
}