15040eb954
In the session creation wizard (Telegram + Discord), the Capacity step only exposed waitlist on/off buttons. The 'no waitlist' button silently advanced to the next step without setting MaxPlayers, so users who tried to create a session with no player cap were blocked with 'Не заполнены поля: лимит мест'. The DB contract and CreateSessionCommand already supported null MaxPlayers (int?, ck_sessions_max_players check in V006), and the web form already exposes 'Без лимита' as an empty InputNumber — only the wizard flow was broken. Changes: - Add '♾ Без лимита' choice button to Capacity in shared WizardStepViewBuilder.BuildCapacity (Telegram) and to RenderCapacity / RenderPoolSlotCapacity in DiscordWizardStep (Discord). - Add 'no_limit' branch to GameCreationWizard.ApplyCapacityChoice that sets MaxPlayers to null and advances to Visibility. - Change GameCreationWizard.SetMaxPlayers signature from int to int? so the 'no limit' branch compiles. - Change CreateSessionCommand builder in both Telegram and Discord submitters to take int? maxPlayers and drop the '?? 0' that would have turned null into 0 (violating the DB CHECK and the 'no limit' contract). - In Discord BuildConfirmDescription, render '👥 Без лимита, waitlist вкл/выкл' when MaxPlayers is null (the previous code silently omitted the line). - Expose BuildCommand as internal in both submitters and add InternalsVisibleTo('GmRelay.Bot.Tests') to the DiscordBot assembly for unit-test access. Tests (9 new): - WizardStepRenderTests.CapacityStep_HasWaitlistButtons — asserts the 'Без лимита' button is present. - GameCreationWizardStepTransitionsTests.NoLimitCapacityButton_… — asserts the choice advances to Visibility and leaves MaxPlayers null in the JSON draft. - GameCreationWizardStepTransitionsTests.ChoiceCallback_AdvancesToExpectedStep — new Theory row for Capacity/no_limit. - CreateSessionHandlerBuildCommandTests (new) — null/value propagation through the Telegram submitter's BuildCommand. - DiscordWizardStepCapacityRenderTests (new) — 'Без лимита' button is rendered for both Capacity and PoolSlotCapacity, with the expected custom-id shape. - DiscordWizardSubmitterBuildCommandTests (new) — null/value propagation through the Discord submitter's BuildCommand. Closes #123
218 lines
9.4 KiB
C#
218 lines
9.4 KiB
C#
using System;
|
|
using System.Text.Json;
|
|
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;
|
|
|
|
/// <summary>
|
|
/// Verifies the wizard's state machine: clicking each Choice callback should
|
|
/// advance the draft to the expected next step and persist it.
|
|
/// </summary>
|
|
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)]
|
|
[InlineData(WizardStepNames.Capacity, "no_limit", 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.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), 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.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), 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 NoLimitCapacityButton_AdvancesToVisibility_AndLeavesMaxPlayersNull()
|
|
{
|
|
var wizard = BuildWizard(out var drafts, out _);
|
|
var draft = NewDraft(WizardStepNames.Capacity, PayloadForStep(WizardStepNames.Capacity));
|
|
drafts.Seed(draft);
|
|
|
|
var data = WizardCallbackData.Choice(WizardStepNames.Capacity, "no_limit");
|
|
await wizard.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), draft, CancellationToken.None);
|
|
|
|
Assert.Equal(WizardStepNames.Visibility, draft.Step);
|
|
using var doc = JsonDocument.Parse(draft.PayloadJson);
|
|
var root = doc.RootElement;
|
|
Assert.True(root.TryGetProperty("single", out var single));
|
|
// WizardPayloadJsonContext имеет DefaultIgnoreCondition=WhenWritingNull,
|
|
// поэтому null-MaxPlayers просто не пишется. Оба варианта
|
|
// (отсутствует / JsonValueKind.Null) десериализуются обратно в null
|
|
// и уйдут в БД как NULL — то есть «без лимита».
|
|
if (single.TryGetProperty("maxPlayers", out var maxPlayers))
|
|
{
|
|
Assert.True(
|
|
maxPlayers.ValueKind == JsonValueKind.Null,
|
|
$"expected maxPlayers to be null (no limit), got {maxPlayers.ValueKind}");
|
|
}
|
|
}
|
|
|
|
[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.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), 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.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), 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.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), draft, CancellationToken.None);
|
|
|
|
Assert.Equal(WizardStepNames.PickClub, draft.Step);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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(),
|
|
};
|
|
}
|