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 { private sealed class TestGoogleIdentity : IGoogleIdentityProvider { public Task Verify(string token) => token == "valid-google-token" ? Task.FromResult(new GoogleIdentity("google-subject-1", "android@test.local")) : throw new ApiException(400, "incorrect login data"); } [Fact] public async Task AndroidSignupRequiresVerifiedIdentityAndDoesNotCreateSharedPassword() { engine = new(store, options, googleIdentityProvider: new TestGoogleIdentity()); var payload = new { ClientType = "Android", GoogleTokenId = "valid-google-token", kick = true }; Assert.Equal(400, (await Call("/api/login.php", new { ClientType = "Android", GoogleTokenId = "forged" })).Status); var first = await Call("/api/login.php", payload); Assert.Equal(400, first.Status); Assert.Contains("noAndroidAccount", first.Body); Assert.Equal(400, (await Call("/api/signupAndroid.php", query: new() { ["googleTokenId"] = "forged", ["accountName"] = "MobileTenno" })).Status); Assert.Equal(200, (await Call("/api/signupAndroid.php", query: new() { ["googleTokenId"] = "valid-google-token", ["accountName"] = "MobileTenno" })).Status); var login = await Call("/api/login.php", payload); Assert.True(login.Status == 200, login.Body); Assert.Equal("MobileTenno", JObject.Parse(login.Body).Value("DisplayName")); Assert.Equal(400, (await Call("/api/login.php", new { ClientType = "Windows", email = "android@test.local", password = "android", kick = true })).Status); Assert.DoesNotContain("valid-google-token", File.ReadAllText(Path.Combine(root, "state.json"))); var invalid = await Assert.ThrowsAsync(() => new GoogleIdentityProvider(new ServerOptions { GoogleClientIds = ["client-id"] }).Verify("not-a-signed-google-token")); Assert.Equal(400, invalid.Status); } [Fact] public async Task ContentRoutesServeExactBytesAndBlockEscapingAssetRoot() { options.GameAssetRoot = Path.Combine(root, "assets"); Directory.CreateDirectory(Path.Combine(options.GameAssetRoot, "content", "Tools")); Directory.CreateDirectory(Path.Combine(options.GameAssetRoot, "PublicExport", "Lotus", "Interface")); byte[] data = [1, 2, 0, 255, 17]; await File.WriteAllBytesAsync(Path.Combine(options.GameAssetRoot, "H.Cache.bin"), data); await File.WriteAllBytesAsync(Path.Combine(options.GameAssetRoot, "content", "Tools", "test.bin"), data); await File.WriteAllBytesAsync(Path.Combine(options.GameAssetRoot, "PublicExport", "Lotus", "Interface", "test.png"), data); foreach (var path in new[] { "/origin/test/1/H.Cache.bin", "/1/H.Cache.bin", "/Tools/test.bin", "/PublicExport/Lotus/Interface/test.png!hash" }) { var result = await Call(path); Assert.Equal(200, result.Status); Assert.Equal(data, result.BinaryBody); } Assert.Equal(400, (await Call("/Tools/%2e%2e/%2e%2e/server.json")).Status); Assert.Equal(400, (await Call("/Tools/file:stream")).Status); Assert.Equal(404, (await Call("/Tools/missing.bin")).Status); Assert.False(File.Exists(Path.Combine(root, "state.json"))); } [Fact] public async Task ArbiterBalancesByRegionAndTracksDepartureWithoutNegativeCounts() { options.HubServers = [new() { Address = "asia.test:6952", Regions = ["AS"] }, new() { Address = "eu.test:6952", Regions = ["EU"] }]; var q = new Dictionary { ["level"] = "EarthHUB_1", ["regionId"] = "EU" }; var route = engine.Routes.All.First(x => x.Controller == "hubController").Path; Assert.Contains("eu.test:6952", (await Call(route, query: q)).Body); Assert.Contains("eu.test:6952", (await Call(route, query: q)).Body); for (var i = 0; i < 3; i++) Assert.Equal(200, (await Call("/custom/hubDropped", query: q)).Status); var stats = await Call(engine.Routes.All.First(x => x.Controller == "statsController").Path); Assert.Equal(0, JObject.Parse(stats.Body)["Levels"]!["EarthHUB_1"]!.Value("Players")); options.HubServers = []; Assert.Equal(503, (await Call(route, query: q)).Status); } [Fact] public async Task ActivityThresholdsDeliverOnlyOnceAndClanTrophiesUseClanTier() { var q = await Login(); await CreateClan(q, "ActivityClan"); var id = Wire.NewId(); await store.Update(state => { state.Obj("worldState")["Goals"] = new JArray(new JObject { ["_id"] = Wire.Oid(id), ["Tag"] = "JadeShadowsEvent", ["Personal"] = true, ["Best"] = true, ["Goal"] = 15, ["ClanGoal"] = 1, ["Reward"] = new JObject { ["credits"] = 123, ["countedItems"] = new JArray(new JObject { ["ItemType"] = "/Lotus/Types/Items/MiscItems/Ferrite", ["ItemCount"] = 7 }) } }); return 0; }); var progress = new { Progress = new[] { new { _id = Wire.Oid(id), Count = 15, Best = 15 } } }; var first = await Call("/api/goals.php", progress, q); Assert.True(first.Status == 200, first.Body); var inbox = await Call("/api/inbox.php", query: q); Assert.Contains("Ferrite", inbox.Body); Assert.Equal(200, (await Call("/api/goals.php", new { Progress = new[] { new { _id = Wire.Oid(id), Count = 0, Best = 15 } } }, q)).Status); Assert.Equal(inbox.Body, (await Call("/api/inbox.php", query: q)).Body); var guild = JObject.Parse((await Call("/custom/getGuild", query: q)).Body); Assert.Single(guild["Vault"]!["DecoRecipes"]!); var before = await InventoryOf(q); Assert.Equal(400, (await Call("/api/goals.php", new { Progress = new[] { new { _id = Wire.Oid(Wire.NewId()), Count = 1, Best = 1 } } }, q)).Status); Assert.True(JToken.DeepEquals(before, await InventoryOf(q))); } [Fact] public async Task CompletedInvasionPaysBattlePayOnceOnInventoryRefresh() { var time = new TestClock(new DateTimeOffset(2026, 9, 24, 12, 0, 0, TimeSpan.Zero)); engine = new(store, options, time); var q = await Login(); var invasion = WorldRotations.Invasion(WorldData.DayOf(time.Now.ToUnixTimeMilliseconds()), 0, time.Now.ToUnixTimeMilliseconds(), GameVersion.Parse(options.FallbackBuildLabel)); var id = Wire.Id(invasion["_id"]); for (var i = 0; i < 3; i++) { var response = await Call("/api/missionInventoryUpdate.php", new { EndOfMatchUpload = true, uploadSequence = i, InvasionProgress = new[] { new { _id = Wire.Oid(id), Delta = -1, AttackerScore = 0, DefenderScore = 1 } } }, q); Assert.True(response.Status == 200, response.Body); } var before = await InventoryOf(q); time.Now = time.Now.AddDays(1); var paid = await InventoryOf(q); var pay = (JArray)invasion["DefenderReward"]!["countedItems"]!; foreach (var item in pay) { var type = item.Value("ItemType")!; var category = new ItemCatalog().Get(type).Category; long Count(JObject inv) => (inv[category] as JArray ?? []).FirstOrDefault(x => x.Value("ItemType") == type)?.Value("ItemCount") ?? 0; Assert.Equal(Count(before) + item.Value("ItemCount"), Count(paid)); } Assert.True(JToken.DeepEquals(paid, await InventoryOf(q))); var mail = await Call("/api/inbox.php", query: q); await InventoryOf(q); Assert.Equal(mail.Body, (await Call("/api/inbox.php", query: q)).Body); } [Fact] public async Task StrippedDropsWaitForExtractionAndDoNotRepeatOnRetry() { var q = await Login(); var table = ((JObject)GameData.Table("Enemies")["droptables"]!).Properties().First(x => x.Name.Contains("TeralystCapturedDropTable", StringComparison.Ordinal)); var body = new JObject { ["BMI"] = true, ["StrippedItems"] = new JArray(new JObject { ["DropTable"] = table.Name, ["DROP_MOD"] = new JArray(1) }), ["RewardInfo"] = new JObject { ["node"] = "SolNode000" } }; var mid = await Call("/api/missionInventoryUpdate.php", body, q); Assert.True(mid.Status == 200, mid.Body); Assert.Empty(JObject.Parse(mid.Body)["MissionRewards"]!); body["EndOfMatchUpload"] = true; var final = await Call("/api/missionInventoryUpdate.php", body, q); Assert.True(final.Status == 200, final.Body); Assert.Single(JObject.Parse(final.Body)["MissionRewards"]!); var inv = await InventoryOf(q); Assert.Equal(final.Body, (await Call("/api/missionInventoryUpdate.php", body, q)).Body); Assert.True(JToken.DeepEquals(inv, await InventoryOf(q))); } [Fact] public async Task OllieMailAndGuildStatisticsAreIdempotentAndUseActualMembership() { var q = await Login(); await CreateClan(q, "ScoreClan"); var report = new { max = new { OlliesCrashCourseScore = 9991000 } }; var uploadPath = engine.Routes.All.First(x => x.Controller == "uploadController").Path; var result = await Call(uploadPath, report, q); Assert.True(result.Status == 200, result.Body); var inbox = await Call("/api/inbox.php", query: q); Assert.Contains("Ollie", inbox.Body); Assert.Equal(200, (await Call(uploadPath, report, q)).Status); Assert.Equal(inbox.Body, (await Call("/api/inbox.php", query: q)).Body); var inv = await InventoryOf(q); var profile = await Call("/dynamic/getProfileViewingData.php", query: new() { ["guildId"] = Wire.Id(inv["GuildId"]) }); Assert.True(profile.Status == 200, profile.Body); Assert.Equal(9991000, JObject.Parse(profile.Body)["Stats"]!.Value("OlliesCrashCourseScore")); var leaderboard = await Call(engine.Routes.All.First(x => x.Controller == "leaderboardPostController").Path, new { field = "weekly.guilds.OlliesCrashCourseScore", guildTier = 1, before = 0, after = 10 }); Assert.True(leaderboard.Status == 200, leaderboard.Body); Assert.Single(JObject.Parse(leaderboard.Body)["results"]!); } }