using System; using System.Text.Json; using GmRelay.Bot.Features.Sessions.CreateSession.Wizard; using GmRelay.Shared.Features.Sessions.CreateSession.Wizard; using static GmRelay.Bot.Tests.Features.Sessions.CreateSession.Wizard.WizardTestFakes; namespace GmRelay.Bot.Tests.Features.Sessions.CreateSession.Wizard; /// /// Verifies the wizard's state machine: clicking each Choice callback should /// advance the draft to the expected next step and persist it. /// public sealed class GameCreationWizardStepTransitionsTests { [Theory] // Type → Title (single game) [InlineData(WizardStepNames.Type, "single", WizardStepNames.Title)] // Type → Title (pool) [InlineData(WizardStepNames.Type, "pool", WizardStepNames.Title)] // System → Duration (a known system code) [InlineData(WizardStepNames.System, "Dnd5e", WizardStepNames.Duration)] // Duration → DateTime (single, no maxPlayers yet) [InlineData(WizardStepNames.Duration, "240", WizardStepNames.DateTime)] // Capacity → Visibility [InlineData(WizardStepNames.Capacity, "waitlist:on", WizardStepNames.Visibility)] [InlineData(WizardStepNames.Capacity, "waitlist:off", WizardStepNames.Visibility)] // Visibility → Publish (public, no club) [InlineData(WizardStepNames.Visibility, "public", WizardStepNames.Publish)] // Visibility → PickClub [InlineData(WizardStepNames.Visibility, "club", WizardStepNames.PickClub)] [InlineData(WizardStepNames.Visibility, "members", WizardStepNames.PickClub)] [InlineData(WizardStepNames.Visibility, "pickclub", WizardStepNames.PickClub)] // Publish → Confirm [InlineData(WizardStepNames.Publish, "yes", WizardStepNames.Confirm)] [InlineData(WizardStepNames.Publish, "no", WizardStepNames.Confirm)] public async Task ChoiceCallback_AdvancesToExpectedStep( string fromStep, string choice, string expectedStep) { var wizard = BuildWizard(out var drafts, out _); var draft = NewDraft(fromStep, PayloadForStep(fromStep)); drafts.Seed(draft); var data = WizardCallbackData.Choice(fromStep, choice); await wizard.HandleUpdateAsync(CallbackUpdate(data), draft, CancellationToken.None); Assert.Equal(expectedStep, draft.Step); Assert.NotEmpty(drafts.Upserts); // was persisted } [Fact] public async Task PoolSystemDuration_PreselectedButton_AdvancesToVisibility() { var wizard = BuildWizard(out var drafts, out _); var payload = new WizardPayload { Type = WizardCreationType.Pool, Title = "Pool", }; var draft = NewDraft(WizardStepNames.PoolSystemDuration, payload); drafts.Seed(draft); var data = WizardCallbackData.Choice(WizardStepNames.PoolSystemDuration, "Dnd5e:240"); await wizard.HandleUpdateAsync(CallbackUpdate(data), draft, CancellationToken.None); Assert.Equal(WizardStepNames.Visibility, draft.Step); using var doc = JsonDocument.Parse(draft.PayloadJson); var root = doc.RootElement; Assert.True(root.TryGetProperty("system", out var sys)); Assert.Equal("Dnd5e", sys.GetString()); Assert.True(root.TryGetProperty("durationMinutes", out var dur)); Assert.Equal(240, dur.GetInt32()); } [Fact] public async Task ChoiceCallback_FromMismatchedStep_AdvancesBasedOnCallbackStep() { // The wizard's callback parser uses the step encoded in the callback // (not the draft's current step) to drive transitions. So a stale // "Capacity" button pressed while the user is on System will in fact // move the draft forward as if they had pressed it on Capacity. We // lock that behaviour in. var wizard = BuildWizard(out var drafts, out _); var draft = NewDraft(WizardStepNames.System); drafts.Seed(draft); var data = WizardCallbackData.Choice(WizardStepNames.Capacity, "waitlist:on"); await wizard.HandleUpdateAsync(CallbackUpdate(data), draft, CancellationToken.None); Assert.Equal(WizardStepNames.Visibility, draft.Step); } [Fact] public async Task PickClub_ValidGuid_ReachesStableStep() { // The wizard has a quirk: NextAfterVisibility is evaluated before // SetClubId, so a single click leaves the draft still on PickClub. // We assert that the wizard does NOT throw and the messenger is asked // to re-render (i.e. the handler ran end-to-end). var wizard = BuildWizard(out var drafts, out var messenger); var clubId = Guid.NewGuid(); var payload = new WizardPayload { Type = WizardCreationType.Single, Title = "T", System = "Dnd5e", DurationMinutes = 240, Visibility = WizardVisibility.Club, }; var draft = NewDraft(WizardStepNames.PickClub, payload); drafts.Seed(draft); var data = WizardCallbackData.Choice(WizardStepNames.PickClub, clubId.ToString()); await wizard.HandleUpdateAsync(CallbackUpdate(data), draft, CancellationToken.None); // Wizard acknowledged the callback and re-rendered the (still PickClub) step. Assert.NotEmpty(messenger.Edits); } [Fact] public async Task PickClub_InvalidGuid_StaysOnPickClub() { var wizard = BuildWizard(out var drafts, out _); var payload = new WizardPayload { Type = WizardCreationType.Single, Title = "T", System = "Dnd5e", DurationMinutes = 240, Visibility = WizardVisibility.Club, }; var draft = NewDraft(WizardStepNames.PickClub, payload); drafts.Seed(draft); var data = WizardCallbackData.Choice(WizardStepNames.PickClub, "not-a-guid"); await wizard.HandleUpdateAsync(CallbackUpdate(data), draft, CancellationToken.None); Assert.Equal(WizardStepNames.PickClub, draft.Step); } /// /// Builds a payload that already contains the values the wizard expects to /// be set when the user is sitting on a given step. Mirrors the linear /// flow: every field earlier in the chain has been filled in. /// private static WizardPayload PayloadForStep(string step) => step switch { WizardStepNames.Type or WizardStepNames.Title => new WizardPayload(), WizardStepNames.System => new WizardPayload { Type = WizardCreationType.Single, Title = "T" }, WizardStepNames.Duration => new WizardPayload { Type = WizardCreationType.Single, Title = "T", System = "Dnd5e" }, WizardStepNames.Capacity => new WizardPayload { Type = WizardCreationType.Single, Title = "T", System = "Dnd5e", DurationMinutes = 240, Single = new WizardSingleInput { ScheduledAt = DateTimeOffset.UtcNow.AddDays(1) }, }, WizardStepNames.Visibility => new WizardPayload { Type = WizardCreationType.Single, Title = "T", System = "Dnd5e", DurationMinutes = 240, }, WizardStepNames.PickClub => new WizardPayload { Type = WizardCreationType.Single, Title = "T", System = "Dnd5e", DurationMinutes = 240, Visibility = WizardVisibility.Club, }, WizardStepNames.Publish => new WizardPayload { Type = WizardCreationType.Single, Title = "T", System = "Dnd5e", DurationMinutes = 240, Visibility = WizardVisibility.Public, }, WizardStepNames.Confirm => new WizardPayload { Type = WizardCreationType.Single, Title = "T", System = "Dnd5e", DurationMinutes = 240, Visibility = WizardVisibility.Public, }, _ => new WizardPayload(), }; }