aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/src/Godosa.Core.Tests/Runs/Scripts/ActionInputTests.cs
blob: 1b539107b6f13dbce7450bbb31d165fe97cd0f5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Godosa.Core.Runs;
using Godosa.Core.Runs.Scripts;
using Xunit;
using static Godosa.Core.Tests.Runs.Scripts.ScriptRunnerTests;

namespace Godosa.Core.Tests.Runs.Scripts;

/// <summary>Multi-click verbs (walk-path, fight, travel-to): a poll may hand the runner more input; the plan plays from
/// the next frame (its frame 0), the action is not polled until it is sent.</summary>
public sealed class ActionInputTests
{
    [Fact]
    public void PollInput_PlaysFromTheNextFrame_ThenPollsAgain()
    {
        var g = new FakeGame();
        var polls = new List<int>();
        var follow = new InputPlan([new(0, new InputEvent(InputKind.KeyDown, Key: "B")), new(2, new InputEvent(InputKind.KeyUp, Key: "B"))]);
        g.ActionTable["walk"] = new FakeAction(VerbKind.Player,
            _ => new ActionStart(new InputPlan([new(0, new InputEvent(InputKind.KeyDown, Key: "A"))])),
            n =>
            {
                polls.Add(n);
                return n == 1 ? StepStatus.Input(follow) : StepStatus.Done;
            });
        var (r, output) = Run("format 1\nmode player\nwalk\nlog done", g);

        Assert.True(r.Outcome == RunOutcome.Passed, string.Join("\n", output));
        Assert.Equal(["A", "B", "B"], g.Sent.Select(s => s.Event.Key));
        var a = g.Sent[0].Frame;
        Assert.Equal(a + 1, g.Sent[1].Frame); // polled after frame a → B on the next frame
        Assert.Equal(a + 3, g.Sent[2].Frame); // its frame 2
        Assert.Equal([1, 2], polls);          // no poll while the follow-up plan is unsent
        Assert.Contains("done", output);
    }

    [Fact]
    public void PollInput_HalfSent_ReleasedOnFailure()
    {
        var g = new FakeGame();
        var follow = new InputPlan([new(0, new InputEvent(InputKind.KeyDown, Key: "B")), new(50, new InputEvent(InputKind.KeyUp, Key: "B"))]);
        g.ActionTable["walk"] = new FakeAction(VerbKind.Player, _ => new ActionStart(), n => StepStatus.Input(follow));
        var (r, _) = Run("format 1\nmode player\nwalk", g, new ScriptRunOptions { StepCap = 10 });

        Assert.Equal(RunOutcome.Failed, r.Outcome);
        Assert.Equal(InputKind.KeyUp, g.Sent[^1].Event.Kind); // the held key is let go
    }
}