Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bbd58142db | |||
| 956ec01583 | |||
| 5014ca5c58 | |||
| efd86bca0a | |||
| 2241568bac | |||
| 37ed697696 | |||
| 320ec18ab0 | |||
| 4424d8faad | |||
| 1f3fb6e89e | |||
| e3e6e841b8 | |||
| a0a84965b3 | |||
| 67e8d5b558 | |||
| 593f8a62fb | |||
| aee0ac1e6c | |||
| 68945d931f |
@@ -6,7 +6,7 @@ on:
|
|||||||
- main
|
- main
|
||||||
|
|
||||||
env:
|
env:
|
||||||
VERSION: 3.9.3
|
VERSION: 3.9.9
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
|
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<Project>
|
<Project>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>3.9.3</Version>
|
<Version>3.9.9</Version>
|
||||||
<TargetFramework>net10.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<LangVersion>preview</LangVersion>
|
<LangVersion>preview</LangVersion>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
|||||||
+3
-3
@@ -49,7 +49,7 @@ services:
|
|||||||
crond -f
|
crond -f
|
||||||
|
|
||||||
bot:
|
bot:
|
||||||
image: git.codeanddice.ru/toutsu/gmrelay-bot:3.9.3
|
image: git.codeanddice.ru/toutsu/gmrelay-bot:3.9.9
|
||||||
restart: always
|
restart: always
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
@@ -67,7 +67,7 @@ services:
|
|||||||
retries: 3
|
retries: 3
|
||||||
|
|
||||||
discord:
|
discord:
|
||||||
image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:3.9.3
|
image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:3.9.9
|
||||||
restart: always
|
restart: always
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
@@ -86,7 +86,7 @@ services:
|
|||||||
retries: 3
|
retries: 3
|
||||||
|
|
||||||
web:
|
web:
|
||||||
image: git.codeanddice.ru/toutsu/gmrelay-web:3.9.3
|
image: git.codeanddice.ru/toutsu/gmrelay-web:3.9.9
|
||||||
restart: always
|
restart: always
|
||||||
depends_on:
|
depends_on:
|
||||||
db:
|
db:
|
||||||
|
|||||||
@@ -30,8 +30,10 @@ RUN dotnet publish "GmRelay.Bot.csproj" -c Release -a $TARGETARCH -o /app/publis
|
|||||||
FROM mcr.microsoft.com/dotnet/runtime-deps:10.0-noble AS final
|
FROM mcr.microsoft.com/dotnet/runtime-deps:10.0-noble AS final
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Устанавливаем wget для healthcheck
|
# Устанавливаем wget для healthcheck и libgssapi-krb5-2 для Npgsql GSS/SSPI
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends wget \
|
# и HTTPS-handshake Telegram.Bot (без неё long-polling падает на первом запросе).
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
wget libgssapi-krb5-2 \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Копируем только AOT-результаты из билда
|
# Копируем только AOT-результаты из билда
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ using System.Linq;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Dapper;
|
||||||
using GmRelay.Shared.Domain;
|
using GmRelay.Shared.Domain;
|
||||||
using GmRelay.Shared.Features.Sessions.CreateSession;
|
using GmRelay.Shared.Features.Sessions.CreateSession;
|
||||||
using GmRelay.Shared.Features.Sessions.CreateSession.Wizard;
|
using GmRelay.Shared.Features.Sessions.CreateSession.Wizard;
|
||||||
using GmRelay.Shared.Platform;
|
using GmRelay.Shared.Platform;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Npgsql;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using SharedCreateSessionHandler = GmRelay.Shared.Features.Sessions.CreateSession.CreateSessionHandler;
|
using SharedCreateSessionHandler = GmRelay.Shared.Features.Sessions.CreateSession.CreateSessionHandler;
|
||||||
|
|
||||||
@@ -31,17 +33,23 @@ public sealed class CreateSessionHandler
|
|||||||
private readonly SharedCreateSessionHandler _shared;
|
private readonly SharedCreateSessionHandler _shared;
|
||||||
private readonly IWizardMessenger _messenger;
|
private readonly IWizardMessenger _messenger;
|
||||||
private readonly ILogger<CreateSessionHandler> _log;
|
private readonly ILogger<CreateSessionHandler> _log;
|
||||||
|
private readonly IPlatformMessenger? _platformMessenger;
|
||||||
|
private readonly NpgsqlDataSource? _dataSource;
|
||||||
|
|
||||||
public CreateSessionHandler(
|
public CreateSessionHandler(
|
||||||
IWizardDraftRepository drafts,
|
IWizardDraftRepository drafts,
|
||||||
SharedCreateSessionHandler shared,
|
SharedCreateSessionHandler shared,
|
||||||
IWizardMessenger messenger,
|
IWizardMessenger messenger,
|
||||||
ILogger<CreateSessionHandler> log)
|
ILogger<CreateSessionHandler> log,
|
||||||
|
IPlatformMessenger? platformMessenger = null,
|
||||||
|
NpgsqlDataSource? dataSource = null)
|
||||||
{
|
{
|
||||||
_drafts = drafts;
|
_drafts = drafts;
|
||||||
_shared = shared;
|
_shared = shared;
|
||||||
_messenger = messenger;
|
_messenger = messenger;
|
||||||
_log = log;
|
_log = log;
|
||||||
|
_platformMessenger = platformMessenger;
|
||||||
|
_dataSource = dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -106,19 +114,24 @@ public sealed class CreateSessionHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
var commands = BuildCommands(draft, payload);
|
var commands = BuildCommands(draft, payload);
|
||||||
|
var created = new List<(CreateSessionCommand Command, CreateSessionResult Result)>();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
foreach (var cmd in commands)
|
foreach (var cmd in commands)
|
||||||
{
|
{
|
||||||
await _shared.HandleAsync(cmd, ct);
|
var result = await _shared.HandleAsync(cmd, ct);
|
||||||
|
if (!result.Success)
|
||||||
|
{
|
||||||
|
await _messenger.EditDraftMessageAsync(
|
||||||
|
draft,
|
||||||
|
result.ErrorMessage ?? "❌ Не удалось создать сессию.",
|
||||||
|
Array.Empty<WizardAction>(),
|
||||||
|
ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
created.Add((cmd, result));
|
||||||
}
|
}
|
||||||
var totalSessions = commands.Sum(c => c.ScheduledTimes.Count);
|
|
||||||
await _messenger.EditDraftMessageAsync(
|
|
||||||
draft,
|
|
||||||
$"✅ Создано: {totalSessions} {(totalSessions == 1 ? "сессия" : "сессий")}",
|
|
||||||
Array.Empty<WizardAction>(),
|
|
||||||
ct);
|
|
||||||
await _drafts.DeleteAsync(draft.Id, ct);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -142,9 +155,89 @@ public sealed class CreateSessionHandler
|
|||||||
$"💥 Ошибка: {ex.Message}\nПопытка {payload.RetryCount}/{MaxRetries}.",
|
$"💥 Ошибка: {ex.Message}\nПопытка {payload.RetryCount}/{MaxRetries}.",
|
||||||
RetryCancelActions(),
|
RetryCancelActions(),
|
||||||
ct);
|
ct);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var totalSessions = created.Sum(c => c.Command.ScheduledTimes.Count);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
foreach (var item in created)
|
||||||
|
{
|
||||||
|
await PublishCreatedSessionAsync(item.Command, item.Result, ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_log.LogError(ex, "SubmitDraftAsync created draft {DraftId} but failed to publish schedule", draft.Id);
|
||||||
|
await _messenger.EditDraftMessageAsync(
|
||||||
|
draft,
|
||||||
|
$"✅ Создано: {totalSessions} {(totalSessions == 1 ? "сессия" : "сессий")}, но не удалось опубликовать сообщение для записи: {ex.Message}",
|
||||||
|
Array.Empty<WizardAction>(),
|
||||||
|
ct);
|
||||||
|
await _drafts.DeleteAsync(draft.Id, ct);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _messenger.EditDraftMessageAsync(
|
||||||
|
draft,
|
||||||
|
$"✅ Создано: {totalSessions} {(totalSessions == 1 ? "сессия" : "сессий")}",
|
||||||
|
Array.Empty<WizardAction>(),
|
||||||
|
ct);
|
||||||
|
await _drafts.DeleteAsync(draft.Id, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task PublishCreatedSessionAsync(CreateSessionCommand command, CreateSessionResult result, CancellationToken ct)
|
||||||
|
{
|
||||||
|
if (_platformMessenger is null || _dataSource is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Session publication dependencies are not configured.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.View is null || result.BatchId is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Created session result does not contain publication data.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var group = command.Group;
|
||||||
|
var topicCreatedByBot = false;
|
||||||
|
if (string.IsNullOrWhiteSpace(group.ExternalThreadId))
|
||||||
|
{
|
||||||
|
var thread = await _platformMessenger.CreateThreadAsync(group, command.Title, ct);
|
||||||
|
group = group with { ExternalThreadId = thread.ExternalThreadId };
|
||||||
|
topicCreatedByBot = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var scheduleMessage = await _platformMessenger.SendScheduleAsync(
|
||||||
|
new PlatformScheduleMessage(group, result.View, ExistingMessage: null, command.ImageReference),
|
||||||
|
ct);
|
||||||
|
|
||||||
|
await using var connection = await _dataSource.OpenConnectionAsync(ct);
|
||||||
|
await connection.ExecuteAsync(
|
||||||
|
"""
|
||||||
|
UPDATE sessions
|
||||||
|
SET thread_id = @ThreadId,
|
||||||
|
batch_message_id = @BatchMessageId,
|
||||||
|
topic_created_by_bot = @TopicCreatedByBot,
|
||||||
|
updated_at = now()
|
||||||
|
WHERE batch_id = @BatchId
|
||||||
|
""",
|
||||||
|
new
|
||||||
|
{
|
||||||
|
result.BatchId,
|
||||||
|
ThreadId = ParseNullableInt(group.ExternalThreadId),
|
||||||
|
BatchMessageId = ParseInt(scheduleMessage.ExternalMessageId),
|
||||||
|
TopicCreatedByBot = topicCreatedByBot
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int ParseInt(string value) =>
|
||||||
|
int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
private static int? ParseNullableInt(string? value) =>
|
||||||
|
string.IsNullOrWhiteSpace(value)
|
||||||
|
? null
|
||||||
|
: int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
// ── Build shared commands ────────────────────────────────────────
|
// ── Build shared commands ────────────────────────────────────────
|
||||||
// The shared handler creates one session per scheduled time in a
|
// The shared handler creates one session per scheduled time in a
|
||||||
// single transaction and assigns the same batch_id to all of them.
|
// single transaction and assigns the same batch_id to all of them.
|
||||||
@@ -229,7 +322,8 @@ public sealed class CreateSessionHandler
|
|||||||
if (p.Type == WizardCreationType.Single)
|
if (p.Type == WizardCreationType.Single)
|
||||||
{
|
{
|
||||||
if (p.Single?.ScheduledAt is null) missingFields.Add("дата/время");
|
if (p.Single?.ScheduledAt is null) missingFields.Add("дата/время");
|
||||||
if (p.Single?.MaxPlayers is null) missingFields.Add("лимит мест");
|
// MaxPlayers = null is a valid "♾ Без лимита" choice
|
||||||
|
// (see GameCreationWizard.ApplyCapacityChoice "no_limit").
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -82,7 +82,13 @@ public sealed class CreateSessionHandler(
|
|||||||
AND p.external_user_id = @ExternalGmId
|
AND p.external_user_id = @ExternalGmId
|
||||||
ON CONFLICT (group_id, player_id) DO NOTHING
|
ON CONFLICT (group_id, player_id) DO NOTHING
|
||||||
""",
|
""",
|
||||||
new { GroupId = groupId, ExternalGmId = externalUserId, OwnerRole = GroupManagerRoleExtensions.OwnerValue },
|
new
|
||||||
|
{
|
||||||
|
GroupId = groupId,
|
||||||
|
Platform = platform,
|
||||||
|
ExternalGmId = externalUserId,
|
||||||
|
OwnerRole = GroupManagerRoleExtensions.OwnerValue
|
||||||
|
},
|
||||||
transaction);
|
transaction);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -298,13 +298,25 @@ public sealed class GameCreationWizard
|
|||||||
: (null, "Неверная длительность"),
|
: (null, "Неверная длительность"),
|
||||||
};
|
};
|
||||||
|
|
||||||
private static (string?, string?) ApplyCapacityChoice(WizardPayload p, string choice) => choice switch
|
private static (string?, string?) ApplyCapacityChoice(WizardPayload p, string choice)
|
||||||
{
|
{
|
||||||
"waitlist:on" => (WizardStepNames.Visibility, SetWaitlist(p, true)),
|
if (choice is "no_limit")
|
||||||
"waitlist:off" => (WizardStepNames.Visibility, SetWaitlist(p, false)),
|
{
|
||||||
"no_limit" => (WizardStepNames.Visibility, SetMaxPlayers(p, null)),
|
return (WizardStepNames.Visibility, SetMaxPlayers(p, null));
|
||||||
_ => (null, "Неизвестный выбор"),
|
}
|
||||||
};
|
|
||||||
|
if (choice is "waitlist:on" or "waitlist:off" && p.Single?.MaxPlayers is null)
|
||||||
|
{
|
||||||
|
return (null, "Сначала введите лимит мест или нажмите «♾ Без лимита»");
|
||||||
|
}
|
||||||
|
|
||||||
|
return choice switch
|
||||||
|
{
|
||||||
|
"waitlist:on" => (WizardStepNames.Visibility, SetWaitlist(p, true)),
|
||||||
|
"waitlist:off" => (WizardStepNames.Visibility, SetWaitlist(p, false)),
|
||||||
|
_ => (null, "Неизвестный выбор"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private static (string?, string?) ApplyVisibilityChoice(WizardPayload p, string choice) => choice switch
|
private static (string?, string?) ApplyVisibilityChoice(WizardPayload p, string choice) => choice switch
|
||||||
{
|
{
|
||||||
@@ -316,9 +328,15 @@ public sealed class GameCreationWizard
|
|||||||
};
|
};
|
||||||
|
|
||||||
private static (string?, string?) ApplyPickClubChoice(WizardPayload p, string choice)
|
private static (string?, string?) ApplyPickClubChoice(WizardPayload p, string choice)
|
||||||
=> Guid.TryParse(choice, out var id)
|
{
|
||||||
? (NextAfterVisibility(p), SetClubId(p, id))
|
if (!Guid.TryParse(choice, out var id))
|
||||||
: (null, "Неверный идентификатор клуба");
|
{
|
||||||
|
return (null, "Неверный идентификатор клуба");
|
||||||
|
}
|
||||||
|
|
||||||
|
var error = SetClubId(p, id);
|
||||||
|
return (NextAfterVisibility(p), error);
|
||||||
|
}
|
||||||
|
|
||||||
private static (string?, string?) ApplyPublishChoice(WizardPayload p, string choice) => choice switch
|
private static (string?, string?) ApplyPublishChoice(WizardPayload p, string choice) => choice switch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -82,7 +82,7 @@
|
|||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<div class="nav-version">v3.9.3</div>
|
<div class="nav-version">v3.9.9</div>
|
||||||
</div>
|
</div>
|
||||||
</Authorized>
|
</Authorized>
|
||||||
<NotAuthorized>
|
<NotAuthorized>
|
||||||
|
|||||||
@@ -33,16 +33,18 @@ public sealed class DiscordWizardStepCapacityRenderTests
|
|||||||
Assert.Contains(labels, l => l.Contains("Без лимита", System.StringComparison.Ordinal));
|
Assert.Contains(labels, l => l.Contains("Без лимита", System.StringComparison.Ordinal));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public void RenderCapacity_NoLimitButton_HasChoiceCustomIdForNoLimit()
|
[InlineData(WizardStepNames.Capacity, "wizard:btn:choice:Capacity:no_limit")]
|
||||||
|
[InlineData(WizardStepNames.PoolSlotCapacity, "wizard:btn:choice:PoolSlotCapacity:no_limit")]
|
||||||
|
public void Render_NoLimitButton_HasChoiceCustomIdForNoLimit(string step, string expectedCustomIdPrefix)
|
||||||
{
|
{
|
||||||
var draft = new WizardDraft { Step = WizardStepNames.Capacity };
|
var draft = new WizardDraft { Step = step };
|
||||||
var render = DiscordWizardStep.Render(draft, new WizardPayload());
|
var render = DiscordWizardStep.Render(draft, new WizardPayload());
|
||||||
|
|
||||||
var buttons = ExtractButtons(render);
|
var buttons = ExtractButtons(render);
|
||||||
var noLimit = buttons.SingleOrDefault(b => b.Label?.Contains("Без лимита", System.StringComparison.Ordinal) == true);
|
var noLimit = buttons.SingleOrDefault(b => b.Label?.Contains("Без лимита", System.StringComparison.Ordinal) == true);
|
||||||
Assert.NotNull(noLimit);
|
Assert.NotNull(noLimit);
|
||||||
Assert.StartsWith("wizard:btn:choice:Capacity:no_limit", noLimit!.CustomId);
|
Assert.StartsWith(expectedCustomIdPrefix, noLimit!.CustomId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static System.Collections.Generic.List<string> ExtractButtonLabels(
|
private static System.Collections.Generic.List<string> ExtractButtonLabels(
|
||||||
|
|||||||
+130
@@ -0,0 +1,130 @@
|
|||||||
|
using GmRelay.Shared.Domain;
|
||||||
|
using GmRelay.Shared.Features.Sessions.CreateSession;
|
||||||
|
using GmRelay.Shared.Platform;
|
||||||
|
using Npgsql;
|
||||||
|
using Testcontainers.PostgreSql;
|
||||||
|
|
||||||
|
namespace GmRelay.Bot.Tests.Features.Sessions.CreateSession;
|
||||||
|
|
||||||
|
[CollectionDefinition(Name)]
|
||||||
|
public sealed class CreateSessionHandlerPostgresCollection : ICollectionFixture<CreateSessionHandlerPostgresFixture>
|
||||||
|
{
|
||||||
|
public const string Name = "Create session handler PostgreSQL";
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class CreateSessionHandlerPostgresFixture : IAsyncLifetime
|
||||||
|
{
|
||||||
|
private static readonly TimeSpan ContainerTimeout = TimeSpan.FromMinutes(2);
|
||||||
|
private readonly PostgreSqlContainer container = new PostgreSqlBuilder("postgres:17-alpine").Build();
|
||||||
|
|
||||||
|
public Task InitializeAsync()
|
||||||
|
{
|
||||||
|
return container.StartAsync().WaitAsync(ContainerTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task DisposeAsync()
|
||||||
|
{
|
||||||
|
return container.DisposeAsync().AsTask().WaitAsync(ContainerTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> CreateMigratedDatabaseAsync()
|
||||||
|
{
|
||||||
|
var databaseName = $"create_session_{Guid.NewGuid():N}";
|
||||||
|
|
||||||
|
await using (var adminConnection = new NpgsqlConnection(container.GetConnectionString()))
|
||||||
|
{
|
||||||
|
await adminConnection.OpenAsync().WaitAsync(ContainerTimeout);
|
||||||
|
await using var createDatabase = new NpgsqlCommand($"CREATE DATABASE \"{databaseName}\"", adminConnection);
|
||||||
|
await createDatabase.ExecuteNonQueryAsync().WaitAsync(ContainerTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
var connectionString = new NpgsqlConnectionStringBuilder(container.GetConnectionString())
|
||||||
|
{
|
||||||
|
Database = databaseName,
|
||||||
|
Timeout = 10,
|
||||||
|
CommandTimeout = 30
|
||||||
|
}.ConnectionString;
|
||||||
|
|
||||||
|
await using var connection = new NpgsqlConnection(connectionString);
|
||||||
|
await connection.OpenAsync().WaitAsync(ContainerTimeout);
|
||||||
|
|
||||||
|
foreach (var migration in GetMigrationPaths())
|
||||||
|
{
|
||||||
|
await using var command = new NpgsqlCommand(await File.ReadAllTextAsync(migration), connection)
|
||||||
|
{
|
||||||
|
CommandTimeout = 30
|
||||||
|
};
|
||||||
|
await command.ExecuteNonQueryAsync().WaitAsync(ContainerTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
return connectionString;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IReadOnlyList<string> GetMigrationPaths()
|
||||||
|
{
|
||||||
|
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
||||||
|
while (directory is not null)
|
||||||
|
{
|
||||||
|
var migrationsDirectory = Path.Combine(directory.FullName, "src", "GmRelay.Bot", "Migrations");
|
||||||
|
if (Directory.Exists(migrationsDirectory))
|
||||||
|
{
|
||||||
|
return Directory.GetFiles(migrationsDirectory, "V*.sql")
|
||||||
|
.OrderBy(path => Path.GetFileName(path), StringComparer.Ordinal)
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
directory = directory.Parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new DirectoryNotFoundException("Could not locate the bot migrations directory.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Collection(CreateSessionHandlerPostgresCollection.Name)]
|
||||||
|
public sealed class CreateSessionHandlerIntegrationTests(CreateSessionHandlerPostgresFixture fixture)
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task HandleAsync_NewPlatformGroup_AddsOwnerAndPersistsSession()
|
||||||
|
{
|
||||||
|
var connectionString = await fixture.CreateMigratedDatabaseAsync();
|
||||||
|
await using var dataSource = NpgsqlDataSource.Create(connectionString);
|
||||||
|
var sut = new CreateSessionHandler(dataSource);
|
||||||
|
|
||||||
|
var result = await sut.HandleAsync(
|
||||||
|
new CreateSessionCommand(
|
||||||
|
new PlatformUser(PlatformKind.Telegram, "111111111", "Test GM", "test_gm"),
|
||||||
|
new PlatformGroup(PlatformKind.Telegram, "222222222", "Test Group"),
|
||||||
|
"Test Adventure",
|
||||||
|
string.Empty,
|
||||||
|
[DateTimeOffset.UtcNow.AddDays(1)],
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
GameSystem.Dnd5e,
|
||||||
|
"Integration regression test",
|
||||||
|
"Online",
|
||||||
|
240,
|
||||||
|
true),
|
||||||
|
CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.True(result.Success, result.ErrorMessage);
|
||||||
|
Assert.NotNull(result.BatchId);
|
||||||
|
Assert.NotNull(result.GroupId);
|
||||||
|
|
||||||
|
await using var connection = await dataSource.OpenConnectionAsync();
|
||||||
|
await using var command = new NpgsqlCommand(
|
||||||
|
"""
|
||||||
|
SELECT count(*)
|
||||||
|
FROM group_managers gm
|
||||||
|
JOIN players p ON p.id = gm.player_id
|
||||||
|
WHERE gm.group_id = @group_id
|
||||||
|
AND gm.role = 'Owner'
|
||||||
|
AND p.platform = 'Telegram'
|
||||||
|
AND p.external_user_id = '111111111'
|
||||||
|
""",
|
||||||
|
connection);
|
||||||
|
command.Parameters.AddWithValue("group_id", result.GroupId.Value);
|
||||||
|
|
||||||
|
var ownerCount = (long)(await command.ExecuteScalarAsync() ?? 0L);
|
||||||
|
Assert.Equal(1, ownerCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
+100
-14
@@ -1,19 +1,105 @@
|
|||||||
using System;
|
using GmRelay.Bot.Features.Sessions.CreateSession;
|
||||||
using Xunit;
|
using GmRelay.Shared.Features.Sessions.CreateSession.Wizard;
|
||||||
|
using GmRelay.Shared.Platform;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using Npgsql;
|
||||||
|
using static GmRelay.Bot.Tests.Features.Sessions.CreateSession.Wizard.WizardTestFakes;
|
||||||
|
|
||||||
namespace GmRelay.Bot.Tests.Features.Sessions.CreateSession.Wizard;
|
namespace GmRelay.Bot.Tests.Features.Sessions.CreateSession.Wizard;
|
||||||
|
|
||||||
/// <summary>
|
[Collection(CreateSessionHandlerPostgresCollection.Name)]
|
||||||
/// Happy-path coverage for <see cref="Features.Sessions.CreateSession.CreateSessionHandler.SubmitDraftAsync"/>
|
public sealed class CreateSessionHandlerSubmitSingleDraftTests(CreateSessionHandlerPostgresFixture fixture)
|
||||||
/// on a single-game wizard payload. The success path calls the shared
|
|
||||||
/// <c>CreateSessionHandler.HandleAsync</c>, which needs a real
|
|
||||||
/// <c>NpgsqlDataSource</c> (it runs SQL against game_groups, players,
|
|
||||||
/// sessions, and related tables). The missing-fields and validation
|
|
||||||
/// branches are covered by the dedicated tests in this folder.
|
|
||||||
/// </summary>
|
|
||||||
public sealed class CreateSessionHandlerSubmitSingleDraftTests
|
|
||||||
{
|
{
|
||||||
[Fact(Skip = "Happy-path SubmitDraftAsync needs a Testcontainers-backed PostgreSQL with the production schema; see file-level summary.")]
|
[Fact]
|
||||||
public void SubmitDraftAsync_CompleteSinglePayload_CreatesOneSession() =>
|
public async Task SubmitDraftAsync_CompleteSinglePayload_PublishesScheduleAndStoresMessageRefs()
|
||||||
throw new NotImplementedException("See Skip reason above.");
|
{
|
||||||
|
var connectionString = await fixture.CreateMigratedDatabaseAsync();
|
||||||
|
await using var dataSource = NpgsqlDataSource.Create(connectionString);
|
||||||
|
var drafts = new FakeWizardDraftRepository();
|
||||||
|
var wizardMessenger = new FakeWizardMessenger();
|
||||||
|
var platformMessenger = new FakePlatformMessenger();
|
||||||
|
|
||||||
|
var sut = new CreateSessionHandler(
|
||||||
|
drafts,
|
||||||
|
new GmRelay.Shared.Features.Sessions.CreateSession.CreateSessionHandler(dataSource),
|
||||||
|
wizardMessenger,
|
||||||
|
NullLogger<CreateSessionHandler>.Instance,
|
||||||
|
platformMessenger,
|
||||||
|
dataSource);
|
||||||
|
|
||||||
|
var payload = new WizardPayload
|
||||||
|
{
|
||||||
|
Type = WizardCreationType.Single,
|
||||||
|
Title = "Тест публикации",
|
||||||
|
System = "Dnd5e",
|
||||||
|
DurationMinutes = 240,
|
||||||
|
Visibility = WizardVisibility.Public,
|
||||||
|
Single = new WizardSingleInput
|
||||||
|
{
|
||||||
|
ScheduledAt = DateTimeOffset.UtcNow.AddDays(7),
|
||||||
|
MaxPlayers = null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
var draft = NewDraft(WizardStepNames.Confirm, payload, ownerId: 111111111);
|
||||||
|
draft.ChatId = "-1003916537960";
|
||||||
|
draft.DraftMessageId = "7";
|
||||||
|
drafts.Seed(draft);
|
||||||
|
|
||||||
|
await sut.SubmitDraftAsync(draft, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.Single(platformMessenger.CreatedThreads);
|
||||||
|
Assert.Equal("Тест публикации", platformMessenger.CreatedThreads[0].Title);
|
||||||
|
Assert.Single(platformMessenger.SentSchedules);
|
||||||
|
Assert.Equal("456", platformMessenger.SentSchedules[0].Group.ExternalThreadId);
|
||||||
|
Assert.Contains(draft.Id, drafts.DeletedIds);
|
||||||
|
Assert.Contains(wizardMessenger.Edits, edit => edit.Text.Contains("✅ Создано: 1 сессия", StringComparison.Ordinal));
|
||||||
|
|
||||||
|
await using var connection = await dataSource.OpenConnectionAsync();
|
||||||
|
await using var command = new NpgsqlCommand(
|
||||||
|
"""
|
||||||
|
SELECT thread_id, batch_message_id, topic_created_by_bot
|
||||||
|
FROM sessions
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
LIMIT 1
|
||||||
|
""",
|
||||||
|
connection);
|
||||||
|
await using var reader = await command.ExecuteReaderAsync();
|
||||||
|
Assert.True(await reader.ReadAsync());
|
||||||
|
Assert.Equal(456, reader.GetInt32(0));
|
||||||
|
Assert.Equal(789, reader.GetInt32(1));
|
||||||
|
Assert.True(reader.GetBoolean(2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class FakePlatformMessenger : IPlatformMessenger
|
||||||
|
{
|
||||||
|
public List<(PlatformGroup Group, string Title)> CreatedThreads { get; } = new();
|
||||||
|
|
||||||
|
public List<PlatformScheduleMessage> SentSchedules { get; } = new();
|
||||||
|
|
||||||
|
public Task<PlatformMessageRef> CreateThreadAsync(PlatformGroup group, string title, CancellationToken ct)
|
||||||
|
{
|
||||||
|
CreatedThreads.Add((group, title));
|
||||||
|
return Task.FromResult(new PlatformMessageRef(group.Platform, group.ExternalGroupId, "456", string.Empty));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<PlatformMessageRef> SendScheduleAsync(PlatformScheduleMessage message, CancellationToken ct)
|
||||||
|
{
|
||||||
|
SentSchedules.Add(message);
|
||||||
|
return Task.FromResult(new PlatformMessageRef(
|
||||||
|
message.Group.Platform,
|
||||||
|
message.Group.ExternalGroupId,
|
||||||
|
message.Group.ExternalThreadId,
|
||||||
|
"789"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task UpdateScheduleAsync(PlatformScheduleMessage message, CancellationToken ct) => Task.CompletedTask;
|
||||||
|
|
||||||
|
public Task SendGroupMessageAsync(PlatformGroup group, string htmlText, CancellationToken ct) => Task.CompletedTask;
|
||||||
|
|
||||||
|
public Task SendPrivateMessageAsync(PlatformPrivateMessage message, CancellationToken ct) => Task.CompletedTask;
|
||||||
|
|
||||||
|
public Task AnswerInteractionAsync(PlatformInteractionReply reply, CancellationToken ct) => Task.CompletedTask;
|
||||||
|
|
||||||
|
public Task SendCalendarFileAsync(PlatformCalendarFile file, CancellationToken ct) => Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|||||||
+43
@@ -146,4 +146,47 @@ public sealed class CreateSessionHandlerSubmitValidationTests
|
|||||||
Assert.Single(messenger.Edits);
|
Assert.Single(messenger.Edits);
|
||||||
Assert.Contains("слоты", messenger.Edits[0].Text, StringComparison.OrdinalIgnoreCase);
|
Assert.Contains("слоты", messenger.Edits[0].Text, StringComparison.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task SubmitDraftAsync_SingleWithNoLimit_DoesNotReportMaxPlayersAsMissing()
|
||||||
|
{
|
||||||
|
// Regression for #131: pressing "♾ Без лимита" sets MaxPlayers = null.
|
||||||
|
// IsComplete must NOT flag that as a missing field; null means
|
||||||
|
// "no player limit" and is a valid final state.
|
||||||
|
var drafts = new FakeWizardDraftRepository();
|
||||||
|
var messenger = new FakeWizardMessenger();
|
||||||
|
|
||||||
|
var sut = new CreateSessionHandler(
|
||||||
|
drafts,
|
||||||
|
shared: null!,
|
||||||
|
messenger,
|
||||||
|
NullLogger<CreateSessionHandler>.Instance);
|
||||||
|
|
||||||
|
var payload = new WizardPayload
|
||||||
|
{
|
||||||
|
Type = WizardCreationType.Single,
|
||||||
|
Title = "T",
|
||||||
|
System = "Dnd5e",
|
||||||
|
DurationMinutes = 240,
|
||||||
|
Visibility = WizardVisibility.Public,
|
||||||
|
Single = new WizardSingleInput
|
||||||
|
{
|
||||||
|
ScheduledAt = DateTimeOffset.UtcNow.AddDays(7),
|
||||||
|
MaxPlayers = null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
var draft = NewDraft(WizardStepNames.Confirm, payload);
|
||||||
|
drafts.Seed(draft);
|
||||||
|
|
||||||
|
await sut.SubmitDraftAsync(draft, CancellationToken.None);
|
||||||
|
|
||||||
|
// Validation must let the no-limit payload through. The shared
|
||||||
|
// handler is null, so anything that reached the database call would
|
||||||
|
// throw a NullReferenceException — that is caught by the retry
|
||||||
|
// path and reported as a "💥 Ошибка:" edit, not a missing-fields
|
||||||
|
// edit. Therefore we assert that NO edit mentions a missing field.
|
||||||
|
Assert.NotEmpty(messenger.Edits);
|
||||||
|
var lastEdit = messenger.Edits[^1].Text;
|
||||||
|
Assert.DoesNotContain("Не заполнены", lastEdit, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-18
@@ -20,9 +20,7 @@ public sealed class GameCreationWizardStepTransitionsTests
|
|||||||
[InlineData(WizardStepNames.System, "Dnd5e", WizardStepNames.Duration)]
|
[InlineData(WizardStepNames.System, "Dnd5e", WizardStepNames.Duration)]
|
||||||
// Duration → DateTime (single, no maxPlayers yet)
|
// Duration → DateTime (single, no maxPlayers yet)
|
||||||
[InlineData(WizardStepNames.Duration, "240", WizardStepNames.DateTime)]
|
[InlineData(WizardStepNames.Duration, "240", WizardStepNames.DateTime)]
|
||||||
// Capacity → Visibility
|
// Capacity → Visibility (only explicit no-limit can skip numeric capacity)
|
||||||
[InlineData(WizardStepNames.Capacity, "waitlist:on", WizardStepNames.Visibility)]
|
|
||||||
[InlineData(WizardStepNames.Capacity, "waitlist:off", WizardStepNames.Visibility)]
|
|
||||||
[InlineData(WizardStepNames.Capacity, "no_limit", WizardStepNames.Visibility)]
|
[InlineData(WizardStepNames.Capacity, "no_limit", WizardStepNames.Visibility)]
|
||||||
// Visibility → Publish (public, no club)
|
// Visibility → Publish (public, no club)
|
||||||
[InlineData(WizardStepNames.Visibility, "public", WizardStepNames.Publish)]
|
[InlineData(WizardStepNames.Visibility, "public", WizardStepNames.Publish)]
|
||||||
@@ -98,13 +96,11 @@ public sealed class GameCreationWizardStepTransitionsTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ChoiceCallback_FromMismatchedStep_AdvancesBasedOnCallbackStep()
|
public async Task StaleCapacityWaitlistCallback_WithoutCapacity_StaysOnCurrentStep()
|
||||||
{
|
{
|
||||||
// The wizard's callback parser uses the step encoded in the callback
|
// A stale waitlist button from Capacity must not move a draft forward
|
||||||
// (not the draft's current step) to drive transitions. So a stale
|
// unless MaxPlayers is already set. Otherwise users can reach Confirm
|
||||||
// "Capacity" button pressed while the user is on System will in fact
|
// with a missing capacity and get "Не заполнены поля: лимит мест".
|
||||||
// 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 wizard = BuildWizard(out var drafts, out _);
|
||||||
var draft = NewDraft(WizardStepNames.System);
|
var draft = NewDraft(WizardStepNames.System);
|
||||||
drafts.Seed(draft);
|
drafts.Seed(draft);
|
||||||
@@ -112,17 +108,13 @@ public sealed class GameCreationWizardStepTransitionsTests
|
|||||||
var data = WizardCallbackData.Choice(WizardStepNames.Capacity, "waitlist:on");
|
var data = WizardCallbackData.Choice(WizardStepNames.Capacity, "waitlist:on");
|
||||||
await wizard.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), draft, CancellationToken.None);
|
await wizard.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), draft, CancellationToken.None);
|
||||||
|
|
||||||
Assert.Equal(WizardStepNames.Visibility, draft.Step);
|
Assert.Equal(WizardStepNames.System, draft.Step);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task PickClub_ValidGuid_ReachesStableStep()
|
public async Task PickClub_ValidGuid_AdvancesToPublishOnFirstClick()
|
||||||
{
|
{
|
||||||
// The wizard has a quirk: NextAfterVisibility is evaluated before
|
var wizard = BuildWizard(out var drafts, out _);
|
||||||
// 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 clubId = Guid.NewGuid();
|
||||||
var payload = new WizardPayload
|
var payload = new WizardPayload
|
||||||
{
|
{
|
||||||
@@ -138,8 +130,11 @@ public sealed class GameCreationWizardStepTransitionsTests
|
|||||||
var data = WizardCallbackData.Choice(WizardStepNames.PickClub, clubId.ToString());
|
var data = WizardCallbackData.Choice(WizardStepNames.PickClub, clubId.ToString());
|
||||||
await wizard.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), draft, CancellationToken.None);
|
await wizard.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), draft, CancellationToken.None);
|
||||||
|
|
||||||
// Wizard acknowledged the callback and re-rendered the (still PickClub) step.
|
Assert.Equal(WizardStepNames.Publish, draft.Step);
|
||||||
Assert.NotEmpty(messenger.Edits);
|
using var doc = JsonDocument.Parse(draft.PayloadJson);
|
||||||
|
var root = doc.RootElement;
|
||||||
|
Assert.True(root.TryGetProperty("clubId", out var clubIdJson));
|
||||||
|
Assert.Equal(clubId, clubIdJson.GetGuid());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
+23
@@ -136,6 +136,29 @@ public sealed class GameCreationWizardValidationTests
|
|||||||
Assert.Equal(WizardStepNames.Capacity, draft.Step);
|
Assert.Equal(WizardStepNames.Capacity, draft.Step);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("waitlist:on")]
|
||||||
|
[InlineData("waitlist:off")]
|
||||||
|
public async Task WaitlistChoiceWithoutCapacity_StaysOnCapacityStep(string choice)
|
||||||
|
{
|
||||||
|
var wizard = BuildWizard(out var drafts, out _);
|
||||||
|
var draft = NewDraft(WizardStepNames.Capacity,
|
||||||
|
new WizardPayload
|
||||||
|
{
|
||||||
|
Type = WizardCreationType.Single,
|
||||||
|
Title = "T",
|
||||||
|
System = "Dnd5e",
|
||||||
|
DurationMinutes = 240,
|
||||||
|
Single = new WizardSingleInput { ScheduledAt = DateTimeOffset.UtcNow.AddDays(1) },
|
||||||
|
});
|
||||||
|
drafts.Seed(draft);
|
||||||
|
|
||||||
|
var data = WizardCallbackData.Choice(WizardStepNames.Capacity, choice);
|
||||||
|
await wizard.HandleInteractionAsync(CallbackInteraction(data, ownerId: draft.OwnerId), draft, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.Equal(WizardStepNames.Capacity, draft.Step);
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("0")]
|
[InlineData("0")]
|
||||||
[InlineData("13")]
|
[InlineData("13")]
|
||||||
|
|||||||
@@ -14,8 +14,16 @@ public sealed class CampaignTemplatesNavigationTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task NavMenu_ShouldExposeCurrentProjectVersion()
|
public async Task NavMenu_ShouldExposeCurrentProjectVersion()
|
||||||
{
|
{
|
||||||
|
// Read the version from Directory.Build.props (the canonical source of
|
||||||
|
// truth) so the test doesn't need to be hand-edited on every version
|
||||||
|
// bump. Asserting the rendered NavMenu matches the canonical version
|
||||||
|
// catches real bugs (e.g. someone bumps Directory.Build.props but
|
||||||
|
// forgets to update NavMenu.razor) without false alarms from a stale
|
||||||
|
// hard-coded literal.
|
||||||
|
var propsPath = FindRepositoryFile("Directory.Build.props");
|
||||||
|
var version = ReadVersionFromProps(propsPath);
|
||||||
var navMenu = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Components/Layout/NavMenu.razor"));
|
var navMenu = await File.ReadAllTextAsync(FindRepositoryFile("src/GmRelay.Web/Components/Layout/NavMenu.razor"));
|
||||||
Assert.Contains("v3.9.3", navMenu, StringComparison.Ordinal);
|
Assert.Contains($"v{version}", navMenu, StringComparison.Ordinal);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -68,4 +76,23 @@ public sealed class CampaignTemplatesNavigationTests
|
|||||||
|
|
||||||
throw new FileNotFoundException($"Could not locate repository file '{relativePath}'.");
|
throw new FileNotFoundException($"Could not locate repository file '{relativePath}'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parse the <c><Version>...</Version></c> element from
|
||||||
|
/// <c>Directory.Build.props</c>. Tolerant of whitespace, comments and
|
||||||
|
/// attribute shuffling — the MSBuild schema for <c>Version</c> is just
|
||||||
|
/// a plain element with a string body.
|
||||||
|
/// </summary>
|
||||||
|
private static string ReadVersionFromProps(string propsPath)
|
||||||
|
{
|
||||||
|
var doc = System.Xml.Linq.XDocument.Load(propsPath);
|
||||||
|
var versionElement = doc.Descendants()
|
||||||
|
.FirstOrDefault(e => e.Name.LocalName == "Version");
|
||||||
|
Assert.NotNull(versionElement);
|
||||||
|
var version = versionElement!.Value.Trim();
|
||||||
|
Assert.False(
|
||||||
|
string.IsNullOrEmpty(version),
|
||||||
|
$"<Version> in {propsPath} is empty");
|
||||||
|
return version;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user