using System.Text;
using XFEWarframe.Client.Runtime;
using Xunit;
namespace XFEWarframe.Client.Tests;
public class GameTestProtocolTests
{
[Theory]
[InlineData("{\"slot\":4,\"pressed\":true}")]
[InlineData("{\"slot\":-1,\"pressed\":false}")]
[InlineData("{\"slot\":0,\"pressed\":1}")]
[InlineData("{\"slot\":0,\"pressed\":true,\"address\":1}")]
[InlineData("{\"slot\":0,\"pressed\":true,\"exclusive\":1}")]
public void SkillInputRejectsInvalidOrExtraFields(string body) =>
Assert.ThrowsAny<ArgumentException>(() => GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes(body)));
[Fact] public void SkillInputAcceptsOnlyFourNormalSlots()
{
foreach (var slot in Enumerable.Range(0, 4))
Assert.Equal(slot, GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes($$"""{"slot":{{slot}},"pressed":true}"""))["slot"]!.GetValue<int>());
}
[Fact] public void DiagnosticInputIsolationIsExplicit()
{
Assert.True(GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes("""{"slot":0,"pressed":true,"exclusive":true}"""))["exclusive"]!.GetValue<bool>());
}
[Fact] public void EngineCallsCanOnlyReferenceEarlierResults()
{
var accepted = GameTestProtocol.Parse("POST lua", Encoding.UTF8.GetBytes("""
{"steps":[{"global":"gRegion"},{"from":0,"field":"GetLocalPlayerAvatar"},{"call":1,"args":[{"ref":0}]}]}
"""));
Assert.Equal(3, accepted["steps"]!.AsArray().Count);
}
[Theory]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"from\":2,\"field\":\"GetLocalPlayerAvatar\"}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\",\"call\":0}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[{\"ref\":1}]}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[{\"ref\":0,\"address\":123}]}]}")]
[InlineData("{\"steps\":[{\"global\":\"0xBAD\"}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\",\"inspect\":1}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[1e100]}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[\"a\\u0000b\"]}]}")]
[InlineData("{\"steps\":[]}")]
public void RejectsMalformedCommandsBeforeAnyNativeCalls(string body) =>
Assert.ThrowsAny<ArgumentException>(() => GameTestProtocol.Parse("POST lua", Encoding.UTF8.GetBytes(body)));
[Fact] public void LoginRequiresAHashAndDoesNotAcceptPlaintextOrExtraOptions()
{
var hash = new string('a', 128);
Assert.Equal(hash, GameTestProtocol.Parse("POST login", Encoding.UTF8.GetBytes($$"""{"email":"test@example.invalid","passwordHash":"{{hash}}"}"""))["passwordHash"]!.GetValue<string>());
foreach (var json in new[] { "{\"email\":\"test@example.invalid\",\"password\":\"secret\"}",
$$"""{"email":"test@example.invalid","passwordHash":"{{hash}}","force":true}""",
"{\"email\":\"test@example.invalid\",\"passwordHash\":\"short\"}" })
Assert.Throws<ArgumentException>(() => GameTestProtocol.Parse("POST login", Encoding.UTF8.GetBytes(json)));
Assert.Throws<ArgumentException>(() => GameTestProtocol.Parse("POST anything", []));
}
}
using System.Text;
using XFEWarframe.Client.Runtime;
using Xunit;
namespace XFEWarframe.Client.Tests;
public class GameTestProtocolTests
{
[Theory]
[InlineData("{\"slot\":4,\"pressed\":true}")]
[InlineData("{\"slot\":-1,\"pressed\":false}")]
[InlineData("{\"slot\":0,\"pressed\":1}")]
[InlineData("{\"slot\":0,\"pressed\":true,\"address\":1}")]
[InlineData("{\"slot\":0,\"pressed\":true,\"exclusive\":1}")]
public void SkillInputRejectsInvalidOrExtraFields(string body) =>
Assert.ThrowsAny<ArgumentException>(() => GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes(body)));
[Fact] public void SkillInputAcceptsOnlyFourNormalSlots()
{
foreach (var slot in Enumerable.Range(0, 4))
Assert.Equal(slot, GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes($$"""{"slot":{{slot}},"pressed":true}"""))["slot"]!.GetValue<int>());
}
[Fact] public void DiagnosticInputIsolationIsExplicit()
{
Assert.True(GameTestProtocol.Parse("POST ability-input", Encoding.UTF8.GetBytes("""{"slot":0,"pressed":true,"exclusive":true}"""))["exclusive"]!.GetValue<bool>());
}
[Fact] public void EngineCallsCanOnlyReferenceEarlierResults()
{
var accepted = GameTestProtocol.Parse("POST lua", Encoding.UTF8.GetBytes("""
{"steps":[{"global":"gRegion"},{"from":0,"field":"GetLocalPlayerAvatar"},{"call":1,"args":[{"ref":0}]}]}
"""));
Assert.Equal(3, accepted["steps"]!.AsArray().Count);
}
[Theory]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"from\":2,\"field\":\"GetLocalPlayerAvatar\"}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\",\"call\":0}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[{\"ref\":1}]}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[{\"ref\":0,\"address\":123}]}]}")]
[InlineData("{\"steps\":[{\"global\":\"0xBAD\"}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\",\"inspect\":1}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[1e100]}]}")]
[InlineData("{\"steps\":[{\"global\":\"gRegion\"},{\"call\":0,\"args\":[\"a\\u0000b\"]}]}")]
[InlineData("{\"steps\":[]}")]
public void RejectsMalformedCommandsBeforeAnyNativeCalls(string body) =>
Assert.ThrowsAny<ArgumentException>(() => GameTestProtocol.Parse("POST lua", Encoding.UTF8.GetBytes(body)));
[Fact] public void LoginRequiresAHashAndDoesNotAcceptPlaintextOrExtraOptions()
{
var hash = new string('a', 128);
Assert.Equal(hash, GameTestProtocol.Parse("POST login", Encoding.UTF8.GetBytes($$"""{"email":"test@example.invalid","passwordHash":"{{hash}}"}"""))["passwordHash"]!.GetValue<string>());
foreach (var json in new[] { "{\"email\":\"test@example.invalid\",\"password\":\"secret\"}",
$$"""{"email":"test@example.invalid","passwordHash":"{{hash}}","force":true}""",
"{\"email\":\"test@example.invalid\",\"passwordHash\":\"short\"}" })
Assert.Throws<ArgumentException>(() => GameTestProtocol.Parse("POST login", Encoding.UTF8.GetBytes(json)));
Assert.Throws<ArgumentException>(() => GameTestProtocol.Parse("POST anything", []));
}
}