using Newtonsoft.Json.Linq; using XFEWarframe.Core; using XFEWarframe.Core.Data; using XFEWarframe.Core.Services; using Xunit; namespace XFEWarframe.Tests; public sealed partial class EngineTests { [Fact] public async Task InboxClaimsOnlyOnceUnderConcurrentReadsAndNeverFromAnotherAccount() { var first = await Login(); var second = await Login("second@test.local"); var response = await Call($"/control/v1/accounts/{first["accountId"]}/message", new { sndr = "Test", sub = "Reward", msg = "Test delivery", RegularCredits = 250, countedAtt = new[] { new { ItemType = "/Lotus/Types/Items/MiscItems/Ferrite", ItemCount = 20 } } }, admin: true); Assert.Equal(201, response.Status); var id = Wire.Id(JObject.Parse(response.Body)["messageId"]); first["messageId"] = id; second["messageId"] = id; Assert.Equal(200, (await Call("/api/inbox.php", query: second)).Status); Assert.Equal(0, (await InventoryOf(second)).Value("RegularCredits")); var reads = await Task.WhenAll(Enumerable.Range(0, 8).Select(_ => Call("/api/inbox.php", query: first))); Assert.All(reads, x => Assert.Equal(200, x.Status)); Assert.Single(reads, x => x.Body.Contains("InventoryChanges", StringComparison.Ordinal)); var inv = await InventoryOf(first); Assert.Equal(250, inv.Value("RegularCredits")); Assert.Equal(20, inv["MiscItems"]!.Single()!.Value("ItemCount")); first.Remove("messageId"); first["lastMessage"] = id; Assert.Equal("no-new", (await Call("/api/inbox.php", query: first)).Body); } [Fact] public async Task InvalidGiftAttachmentsLeaveMailUnreadAndDoNotAwardCurrency() { var query = await Login(); var message = await Call($"/control/v1/accounts/{query["accountId"]}/message", new { sndr = "T", sub = "T", msg = "T", RegularCredits = 250, gifts = new[] { new { GiftType = "bundle" } } }, admin: true); query["messageId"] = Wire.Id(JObject.Parse(message.Body)["messageId"]); Assert.Equal(400, (await Call("/api/inbox.php", query: query)).Status); query.Remove("messageId"); Assert.False(JObject.Parse((await Call("/api/inbox.php", query: query)).Body)["Inbox"]![0]!.Value("r")); Assert.Equal(0, (await InventoryOf(query)).Value("RegularCredits")); } [Fact] public async Task MasteryHasCooldownAndDeliversTrophyViaInbox() { var query = await Login(); var result = await Call("/api/trainingResult.php", new { numLevelsGained = 1 }, query); Assert.Equal(200, result.Status); Assert.Equal(1, JObject.Parse(result.Body).Value("NewLevel")); Assert.Equal(409, (await Call("/api/trainingResult.php", new { numLevelsGained = 1 }, query)).Status); var mail = JObject.Parse((await Call("/api/inbox.php", query: query)).Body)["Inbox"]!.Single()!; query["messageId"] = Wire.Id(mail["messageId"]); Assert.Equal(200, (await Call("/api/inbox.php", query: query)).Status); Assert.Contains((await InventoryOf(query))["ShipDecorations"]!, x => x.Value("ItemType")!.EndsWith("Rank01Trophy", StringComparison.Ordinal)); } [Fact] public async Task StatsCaptureSubtractionTimersMaximaAndPublicProfileAreConsistent() { var query = await Login(); var body = new { add = new { KILL_ENEMY = new { Grineer = 5 }, CAPTURE_ENEMY = new { Grineer = 2 }, MISSION_COMPLETE = new { GS_SUCCESS = 1 } }, timers = new { CURRENT_MISSION_TIME = 4.5, MISSION_TIME = new { Venus = 400 } }, max = new { MISSION_SCORE = new { SolNode1 = 150 }, WEAPON_XP = new { Braton = 100 } } }; Assert.Equal(200, (await Call("/stats/upload.php", body, query)).Status); Assert.Equal(200, (await Call("/stats/upload.php", new { max = new { MISSION_SCORE = new { SolNode1 = 100 }, WEAPON_XP = new { Braton = 90 } } }, query)).Status); var stats = JObject.Parse((await Call("/stats/view.php", query: new() { ["id"] = query["accountId"] })).Body); Assert.Equal(3, stats["Enemies"]![0]!.Value("kills")); Assert.Equal(2, stats["Enemies"]![0]!.Value("captures")); Assert.Equal(4.5, stats.Value("TimePlayedSec")); Assert.Equal(100, stats["Weapons"]![0]!.Value("xp")); var leaderboard = await Call("/stats/leaderboardCurrent.php", query: new() { ["field"] = "weekly.accounts.SolNode1", ["before"] = "0", ["after"] = "10" }); Assert.Equal(150, JObject.Parse(leaderboard.Body)["results"]![0]!.Value("s")); var profile = await Call("/dynamic/getProfileViewingData.php", query: new() { ["playerId"] = query["accountId"] }); Assert.Equal(200, profile.Status); Assert.DoesNotContain("email", profile.Body); Assert.DoesNotContain("credential", profile.Body); Assert.DoesNotContain("Nonce", profile.Body); } [Fact] public async Task PowerSuitAcquisitionIncludesExaltedItemsAndPurchasedSlot() { var query = await Login(); var result = await Call($"/control/v1/accounts/{query["accountId"]}/item", new { type = "/Lotus/Powersuits/Excalibur/Excalibur", count = 1 }, admin: true); Assert.Equal(200, result.Status); var inv = await InventoryOf(query); Assert.Single((JArray)inv["Suits"]!); Assert.Contains(inv["SpecialItems"]!, x => x.Value("ItemType") == "/Lotus/Powersuits/Excalibur/DoomSword"); Assert.Equal(1, JObject.Parse(result.Body)["SuitBin"]!.Value("Extra")); } [Fact] public async Task FoundryCannotClaimEarlyAndDuplicateClaimRollsBackEntireBatch() { var query = await Login(); const string blueprint = "/Lotus/Types/Recipes/Components/FormaBlueprint"; var recipe = ((JObject)Assets.Json("export/ExportRecipes.json"))[blueprint]!; var inventory = new JObject { ["RegularCredits"] = 100_000, ["Recipes"] = new JArray(new JObject { ["ItemType"] = blueprint, ["ItemCount"] = 1 }), ["MiscItems"] = recipe["ingredients"]!.DeepClone() }; await Call($"/control/v1/accounts/{query["accountId"]}/import", new { inventory }, admin: true); var started = await Call("/api/startRecipe.php", new { RecipeName = blueprint, Ids = Array.Empty() }, query); Assert.Equal(200, started.Status); var id = JObject.Parse(started.Body)["RecipeId"]!; Assert.Equal(409, (await Call("/api/claimCompletedRecipe.php", new { RecipeIds = new[] { id } }, query)).Status); await store.Update(s => { s.Obj("accounts").Obj(query["accountId"]).Obj("Inventory").Array("PendingRecipes")[0]!["CompletionDate"] = Wire.Date(0); return 0; }); Assert.Equal(404, (await Call("/api/claimCompletedRecipe.php", new { RecipeIds = new[] { id, id } }, query)).Status); Assert.Single((JArray)(await InventoryOf(query))["PendingRecipes"]!); Assert.Equal(200, (await Call("/api/claimCompletedRecipe.php", new { RecipeIds = new[] { id } }, query)).Status); var inv = await InventoryOf(query); Assert.Empty((JArray)inv["Recipes"]!); Assert.Empty((JArray)inv["PendingRecipes"]!); Assert.Equal("/Lotus/Types/Items/MiscItems/Forma", inv["MiscItems"]!.Single()!.Value("ItemType")); } [Fact] public async Task CancellingSeveralRecipesAddsTheirRefunds() { var query = await Login(); var recipes = new[] { "/Lotus/Types/Recipes/Components/FormaBlueprint", "/Lotus/Types/Recipes/Components/UmbraFormaBlueprint" }; var pending = new JArray(recipes.Select(x => new JObject { ["ItemType"] = x, ["ItemId"] = Wire.Oid(Wire.NewId()), ["CompletionDate"] = Wire.Date(0) })); await Call($"/control/v1/accounts/{query["accountId"]}/import", new { inventory = new JObject { ["PendingRecipes"] = pending } }, admin: true); query["cancel"] = "1"; var cancelled = await Call("/api/claimCompletedRecipe.php", new { RecipeIds = pending.Select(x => x["ItemId"]).ToArray() }, query); Assert.Equal(200, cancelled.Status); Assert.Equal(70_000, JObject.Parse(cancelled.Body).Value("RegularCredits")); Assert.Equal(70_000, (await InventoryOf(query)).Value("RegularCredits")); } }