diff --git a/SVSim.EmulatedEntrypoint/Controllers/MyPageController.cs b/SVSim.EmulatedEntrypoint/Controllers/MyPageController.cs index a94d4491..8c61c683 100644 --- a/SVSim.EmulatedEntrypoint/Controllers/MyPageController.cs +++ b/SVSim.EmulatedEntrypoint/Controllers/MyPageController.cs @@ -30,16 +30,18 @@ public class MyPageController : SVSimController private readonly IGameConfigService _config; private readonly IArenaTwoPickRunRepository _arenaTwoPickRuns; private readonly IHomeDialogSessionTracker _homeDialogTracker; + private readonly ILoginBonusService _loginBonus; public MyPageController(IViewerRepository viewerRepository, IGlobalsRepository globalsRepository, IGameConfigService config, IArenaTwoPickRunRepository arenaTwoPickRuns, - IHomeDialogSessionTracker homeDialogTracker) + IHomeDialogSessionTracker homeDialogTracker, ILoginBonusService loginBonus) { _viewerRepository = viewerRepository; _globalsRepository = globalsRepository; _config = config; _arenaTwoPickRuns = arenaTwoPickRuns; _homeDialogTracker = homeDialogTracker; + _loginBonus = loginBonus; } [HttpPost("index")] @@ -83,6 +85,7 @@ public class MyPageController : SVSimController return new MyPageIndexResponse { UserInfo = new UserInfo(deviceType, viewer), + CanGiveDailyLoginBonus = _loginBonus.IsDue(viewer), UnreceivedMissionRewardCount = 0, // TODO(mypage-stub): viewer mission progress ReceiveFriendApplyCount = 0, // TODO(mypage-stub): viewer friend-request inbox UnreadPresentCount = 0, // TODO(mypage-stub): viewer presents/mail diff --git a/SVSim.UnitTests/Controllers/MyPageControllerTests.cs b/SVSim.UnitTests/Controllers/MyPageControllerTests.cs new file mode 100644 index 00000000..a3fb9733 --- /dev/null +++ b/SVSim.UnitTests/Controllers/MyPageControllerTests.cs @@ -0,0 +1,57 @@ +using System.Net; +using System.Text; +using System.Text.Json; +using NUnit.Framework; +using SVSim.UnitTests.Infrastructure; + +namespace SVSim.UnitTests.Controllers; + +/// +/// Coverage for /mypage/index. Focused on fields computed from viewer state +/// that are easy to regress (can_give_daily_login_bonus). +/// +public class MyPageControllerTests +{ + // MyPageIndexRequest extends BaseRequest, so viewer_id + steam_session_ticket are required. + private const string MyPageRequestJson = + """{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","carrier":"steam"}"""; + + private const string LoadIndexRequestJson = + """{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","carrier":"steam","card_master_hash":""}"""; + + [Test] + public async Task MyPage_can_give_daily_login_bonus_is_true_for_fresh_viewer() + { + using var factory = new SVSimTestFactory(); + long viewerId = await factory.SeedViewerAsync(); + using var client = factory.CreateAuthenticatedClient(viewerId); + + var resp = await client.PostAsync("/mypage/index", + new StringContent(MyPageRequestJson, Encoding.UTF8, "application/json")); + Assert.That(resp.StatusCode, Is.EqualTo(HttpStatusCode.OK), await resp.Content.ReadAsStringAsync()); + + var root = JsonDocument.Parse(await resp.Content.ReadAsStringAsync()).RootElement; + Assert.That(root.GetProperty("can_give_daily_login_bonus").GetBoolean(), Is.True); + } + + [Test] + public async Task MyPage_can_give_daily_login_bonus_is_false_after_load_index_claim() + { + using var factory = new SVSimTestFactory(); + long viewerId = await factory.SeedViewerAsync(); + using var client = factory.CreateAuthenticatedClient(viewerId); + + // /load/index claims today's bonus + var loadResp = await client.PostAsync("/load/index", + new StringContent(LoadIndexRequestJson, Encoding.UTF8, "application/json")); + Assert.That(loadResp.StatusCode, Is.EqualTo(HttpStatusCode.OK), await loadResp.Content.ReadAsStringAsync()); + + // /mypage/index must report flag = false after the claim + var resp = await client.PostAsync("/mypage/index", + new StringContent(MyPageRequestJson, Encoding.UTF8, "application/json")); + Assert.That(resp.StatusCode, Is.EqualTo(HttpStatusCode.OK), await resp.Content.ReadAsStringAsync()); + + var root = JsonDocument.Parse(await resp.Content.ReadAsStringAsync()).RootElement; + Assert.That(root.GetProperty("can_give_daily_login_bonus").GetBoolean(), Is.False); + } +}