diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 5ab0882..5560c02 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -3,6 +3,8 @@ name: Build-Test-Publish Flow on: push: branches: [ "master" ] + pull_request: + branches: [ "master" ] jobs: build_test: @@ -10,11 +12,11 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v5 - name: Setup .NET - uses: actions/setup-dotnet@v4 + uses: actions/setup-dotnet@v5 with: - dotnet-version: 9.0.x + dotnet-version: 10.0.x - name: Restore dependencies run: dotnet restore - name: Build @@ -25,6 +27,7 @@ jobs: build_docker_iamge: name: Build and Push Docker Image needs: build_test + if: github.event_name == 'push' runs-on: ubuntu-latest permissions: packages: write @@ -34,7 +37,7 @@ jobs: echo "DOCKER_IMAGE_POSTFIX=$(date +%Y-%m-%d_%s)" >> "$GITHUB_ENV" echo "DOCKER_IMAGE_NAME=ghcr.io/rumkit/telegram-torrserver-bot" >> "$GITHUB_ENV" - name: Checkout the repo - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Log in to the Container registry uses: docker/login-action@v3 diff --git a/TTSBot.Tests/Commands/GetPlaylistCommandHandlerTests.cs b/TTSBot.Tests/Commands/GetPlaylistCommandHandlerTests.cs index bce2038..edba2ad 100644 --- a/TTSBot.Tests/Commands/GetPlaylistCommandHandlerTests.cs +++ b/TTSBot.Tests/Commands/GetPlaylistCommandHandlerTests.cs @@ -46,6 +46,6 @@ public async Task Handle_WhenPlaylistProvided_ShouldReturnSuccess() var result = await handler.TryHandleAsync("hash"); await Assert.That(result.IsSuccess).IsTrue(); - await Assert.That(result.Result).HasCount(3); + await Assert.That(result.Result).Count().IsEqualTo(3); } } \ No newline at end of file diff --git a/TTSBot.Tests/Commands/GetPlaylistCommandProcessorTests.cs b/TTSBot.Tests/Commands/GetPlaylistCommandProcessorTests.cs index 9a4e645..628bfb2 100644 --- a/TTSBot.Tests/Commands/GetPlaylistCommandProcessorTests.cs +++ b/TTSBot.Tests/Commands/GetPlaylistCommandProcessorTests.cs @@ -26,13 +26,13 @@ public async Task Process_WhenSuccess_ShouldReturnMessageResultAndCompleteCallba var keyboard = result.GetKeyboard() as InlineKeyboardMarkup; await Assert.That(keyboard).IsNotNull(); // two buttons in total - await Assert.That(keyboard.InlineKeyboard).HasCount(2); + await Assert.That(keyboard.InlineKeyboard).Count().IsEqualTo(2); // first row has one button - await Assert.That(keyboard.InlineKeyboard.First()).HasCount(1); + await Assert.That(keyboard.InlineKeyboard.First()).Count().IsEqualTo(1); // button has correct url await Assert.That(keyboard.InlineKeyboard.First().Single().Url).IsEqualTo("http://example.com/info1"); // second row has one button - await Assert.That(keyboard.InlineKeyboard.Last()).HasCount(1); + await Assert.That(keyboard.InlineKeyboard.Last()).Count().IsEqualTo(1); // button has correct url await Assert.That(keyboard.InlineKeyboard.Last().Single().Url).IsEqualTo("http://example.com/info2"); diff --git a/TTSBot.Tests/Commands/GetPlaylistRewriteDecoratorTests.cs b/TTSBot.Tests/Commands/GetPlaylistRewriteDecoratorTests.cs index f0938de..b8905aa 100644 --- a/TTSBot.Tests/Commands/GetPlaylistRewriteDecoratorTests.cs +++ b/TTSBot.Tests/Commands/GetPlaylistRewriteDecoratorTests.cs @@ -53,7 +53,7 @@ public async Task Handle_WhenRewriteUrlIsSet_ShouldReturnNewList() await Assert.That(testHandler.Hash).IsEqualTo("hash"); await Assert.That(result.IsSuccess).IsTrue(); - await Assert.That(result.Result).HasCount(3); + await Assert.That(result.Result).Count().IsEqualTo(3); var names = result.Result.Select(x => x.Name).ToArray(); await Assert.That(names).IsEquivalentTo(LocalList); await Assert.That(result.Result.Select(r => r.Length)).All().Satisfy(x => x.IsEqualTo(42)); diff --git a/TTSBot.Tests/Commands/M3uParserTests.cs b/TTSBot.Tests/Commands/M3uParserTests.cs index 670411c..add6c10 100644 --- a/TTSBot.Tests/Commands/M3uParserTests.cs +++ b/TTSBot.Tests/Commands/M3uParserTests.cs @@ -19,7 +19,7 @@ public class M3uParserTests public async Task Parse_WhenStringHasValidFormat_ShouldReturnListOfFiles() { var result = M3uParser.Parse(ValidContent); - await Assert.That(result).IsNotNull().And.HasCount(3); + await Assert.That(result).IsNotNull().And.Count().IsEqualTo(3); await Assert.That(result[0].Name).IsEqualTo("name0"); await Assert.That(result[1].Name).IsEqualTo(string.Empty); await Assert.That(result[2].Name).IsEqualTo("name2"); diff --git a/TTSBot.Tests/Extensions/BotRequestContextExtensionsTests.cs b/TTSBot.Tests/Extensions/BotRequestContextExtensionsTests.cs index d9f9e4b..e6218c3 100644 --- a/TTSBot.Tests/Extensions/BotRequestContextExtensionsTests.cs +++ b/TTSBot.Tests/Extensions/BotRequestContextExtensionsTests.cs @@ -1,7 +1,6 @@ using Telegram.Bot.Types; using TTSBot.Extensions; using TTSBot.Tests.TestUtils; -using TUnit.Assertions.AssertConditions.Throws; namespace TTSBot.Tests.Extensions; diff --git a/TTSBot.Tests/TTSBot.Tests.csproj b/TTSBot.Tests/TTSBot.Tests.csproj index 690b4d4..447fa59 100644 --- a/TTSBot.Tests/TTSBot.Tests.csproj +++ b/TTSBot.Tests/TTSBot.Tests.csproj @@ -4,12 +4,12 @@ enable annotations Exe - net9.0 + net10.0 default - + diff --git a/TTSBot.Tests/TestUtils/Assertions/AssertionExtensions.cs b/TTSBot.Tests/TestUtils/Assertions/AssertionExtensions.cs index aa826d2..077f9d9 100644 --- a/TTSBot.Tests/TestUtils/Assertions/AssertionExtensions.cs +++ b/TTSBot.Tests/TestUtils/Assertions/AssertionExtensions.cs @@ -1,28 +1,60 @@ -using MinimalTelegramBot.Results; -using TUnit.Assertions.AssertConditions.Interfaces; -using TUnit.Assertions.AssertionBuilders; +using System.Runtime.CompilerServices; +using MinimalTelegramBot.Results; +using TUnit.Assertions.Core; namespace TTSBot.Tests.TestUtils.Assertions; public static class AssertionExtensions { - public static InvokableValueAssertionBuilder IsMessage(this IValueSource valueSource) + public static ResultAssertion IsMessage(this IAssertionSource source) { - return valueSource.RegisterAssertion(new MessageResultKindAssertCondition(MessageResult.Message), []); + source.Context.ExpressionBuilder.Append(".IsMessage()"); + return new ResultAssertion(source.Context, r => CheckMessageKind(r, MessageResult.Message), + $"to be MessageResult of kind {MessageResult.Message}"); } - - public static InvokableValueAssertionBuilder IsReply(this IValueSource valueSource) + + public static ResultAssertion IsReply(this IAssertionSource source) { - return valueSource.RegisterAssertion(new MessageResultKindAssertCondition(MessageResult.Reply), []); + source.Context.ExpressionBuilder.Append(".IsReply()"); + return new ResultAssertion(source.Context, r => CheckMessageKind(r, MessageResult.Reply), + $"to be MessageResult of kind {MessageResult.Reply}"); } - - public static InvokableValueAssertionBuilder IsEmpty(this IValueSource valueSource) + + public static ResultAssertion IsEmpty(this IAssertionSource source) { - return valueSource.RegisterAssertion(new SpecificInternalTypeAssertCondition("EmptyResult"), []); + source.Context.ExpressionBuilder.Append(".IsEmpty()"); + return new ResultAssertion(source.Context, r => CheckTypeName(r, "EmptyResult"), + "to be of type EmptyResult"); } - - public static InvokableValueAssertionBuilder HasMessage(this IValueSource valueSource, string message) + + public static ResultAssertion HasMessage(this IAssertionSource source, string message, + [CallerArgumentExpression(nameof(message))] string? expression = null) { - return valueSource.RegisterAssertion(new MessageResultTextAssertCondition(message), []); + source.Context.ExpressionBuilder.Append($".HasMessage({expression})"); + return new ResultAssertion(source.Context, r => + { + var typeFailure = CheckTypeName(r, "MessageResult"); + if (typeFailure is not null) + return typeFailure; + + var actualText = r!.GetMessageText(); + return actualText == message ? null : $"MessageResult has text: {actualText}"; + }, $"to have message set to: {message}"); + } + + private static string? CheckTypeName(IResult? result, string expectedTypeName) + { + var actualTypeName = result?.GetType().Name; + return actualTypeName == expectedTypeName ? null : $"Has actual type {actualTypeName}"; + } + + private static string? CheckMessageKind(IResult? result, MessageResult expectedKind) + { + var typeFailure = CheckTypeName(result, "MessageResult"); + if (typeFailure is not null) + return typeFailure; + + var kind = result!.GetMessageKind(); + return kind == expectedKind ? null : $"MessageResult is of kind {kind}"; } -} \ No newline at end of file +} diff --git a/TTSBot.Tests/TestUtils/Assertions/MessageResultKindAssertCondition.cs b/TTSBot.Tests/TestUtils/Assertions/MessageResultKindAssertCondition.cs deleted file mode 100644 index bcdd044..0000000 --- a/TTSBot.Tests/TestUtils/Assertions/MessageResultKindAssertCondition.cs +++ /dev/null @@ -1,22 +0,0 @@ -using MinimalTelegramBot.Results; -using TUnit.Assertions.AssertConditions; - -namespace TTSBot.Tests.TestUtils.Assertions; - -public class MessageResultKindAssertCondition(MessageResult expectedMessageResultKind) : ValueAssertCondition -{ - protected override AssertionResult Passes(IResult? actualValue) - { - var actualTypeName = actualValue?.GetType().Name; - if(actualTypeName != "MessageResult") - return AssertionResult.Fail($"Has actual type {actualTypeName}"); - - var kind = actualValue.GetMessageKind(); - if(kind != expectedMessageResultKind) - return AssertionResult.Fail($"MessageResult is of kind {kind}"); - - return AssertionResult.Passed; - } - - protected override string GetFailureMessage(IResult? actualValue) => $"to be MessageResult of kind {expectedMessageResultKind}"; -} \ No newline at end of file diff --git a/TTSBot.Tests/TestUtils/Assertions/MessageResultTextAssertCondition.cs b/TTSBot.Tests/TestUtils/Assertions/MessageResultTextAssertCondition.cs deleted file mode 100644 index facdffe..0000000 --- a/TTSBot.Tests/TestUtils/Assertions/MessageResultTextAssertCondition.cs +++ /dev/null @@ -1,22 +0,0 @@ -using MinimalTelegramBot.Results; -using TUnit.Assertions.AssertConditions; - -namespace TTSBot.Tests.TestUtils.Assertions; - -public class MessageResultTextAssertCondition(string expectedMessageText) : ValueAssertCondition -{ - protected override AssertionResult Passes(IResult? actualValue) - { - var actualTypeName = actualValue?.GetType().Name; - if(actualTypeName != "MessageResult") - return AssertionResult.Fail($"Has actual type {actualTypeName}"); - - var actualMessageText = actualValue.GetMessageText(); - if(actualMessageText != expectedMessageText) - return AssertionResult.Fail($"MessageResult has text: {actualMessageText}"); - - return AssertionResult.Passed; - } - - protected override string GetFailureMessage(IResult? actualValue) => $"to have message set to: {expectedMessageText}"; -} \ No newline at end of file diff --git a/TTSBot.Tests/TestUtils/Assertions/ResultAssertion.cs b/TTSBot.Tests/TestUtils/Assertions/ResultAssertion.cs new file mode 100644 index 0000000..30b1eff --- /dev/null +++ b/TTSBot.Tests/TestUtils/Assertions/ResultAssertion.cs @@ -0,0 +1,23 @@ +using MinimalTelegramBot.Results; +using TUnit.Assertions.Core; + +namespace TTSBot.Tests.TestUtils.Assertions; + +/// +/// Assertion on a bot . The check returns null when it passes, +/// or the failure description when it doesn't. +/// +public class ResultAssertion(AssertionContext context, Func check, string expectation) + : Assertion(context) +{ + protected override Task CheckAsync(EvaluationMetadata metadata) + { + if (metadata.Exception is not null) + return Task.FromResult(AssertionResult.Failed($"threw {metadata.Exception.GetType().Name}")); + + var failure = check(metadata.Value); + return Task.FromResult(failure is null ? AssertionResult.Passed : AssertionResult.Failed(failure)); + } + + protected override string GetExpectation() => expectation; +} diff --git a/TTSBot.Tests/TestUtils/Assertions/SpecificInternalTypeAssertCondition.cs b/TTSBot.Tests/TestUtils/Assertions/SpecificInternalTypeAssertCondition.cs deleted file mode 100644 index fb7bf31..0000000 --- a/TTSBot.Tests/TestUtils/Assertions/SpecificInternalTypeAssertCondition.cs +++ /dev/null @@ -1,17 +0,0 @@ -using MinimalTelegramBot.Results; -using TUnit.Assertions.AssertConditions; - -namespace TTSBot.Tests.TestUtils.Assertions; - -public class SpecificInternalTypeAssertCondition(string expectedTypeName) : ValueAssertCondition -{ - protected override AssertionResult Passes(IResult? actualValue) - { - var actualTypeName = actualValue?.GetType().Name; - return actualTypeName != expectedTypeName ? - AssertionResult.Fail($"Has actual type {actualTypeName}") : - AssertionResult.Passed; - } - - protected override string GetFailureMessage(IResult? actualValue) => $"to be of type {expectedTypeName}"; -} \ No newline at end of file diff --git a/TTSBot.Tests/TestUtils/MockTelegramClient.cs b/TTSBot.Tests/TestUtils/MockTelegramClient.cs index 03a2d64..cf1015e 100644 --- a/TTSBot.Tests/TestUtils/MockTelegramClient.cs +++ b/TTSBot.Tests/TestUtils/MockTelegramClient.cs @@ -2,6 +2,7 @@ using Telegram.Bot.Args; using Telegram.Bot.Exceptions; using Telegram.Bot.Requests.Abstractions; +using Telegram.Bot.Types; namespace TTSBot.Tests.TestUtils; @@ -34,6 +35,11 @@ public class MockTelegramClient : ITelegramBotClient throw new NotImplementedException(); } + public Task DownloadFile(TGFile file, Stream destination, CancellationToken cancellationToken = new CancellationToken()) + { + throw new NotImplementedException(); + } + public bool LocalBotServer { get; } public long BotId { get; } public TimeSpan Timeout { get; set; } diff --git a/TTSBot.Tests/TestUtils/ResultExtensions.cs b/TTSBot.Tests/TestUtils/ResultExtensions.cs index 7bf1da3..acfe3dc 100644 --- a/TTSBot.Tests/TestUtils/ResultExtensions.cs +++ b/TTSBot.Tests/TestUtils/ResultExtensions.cs @@ -33,14 +33,14 @@ public static MessageResult GetMessageKind(this IResult result) return text?.ToString(); } - public static IReplyMarkup? GetKeyboard(this IResult result) + public static ReplyMarkup? GetKeyboard(this IResult result) { var type = result.GetType(); if (type.Name != "MessageResult") throw new InvalidOperationException("The result is not a MessageResult"); var keyboard = type.GetField("_replyMarkup", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(result); - return keyboard as IReplyMarkup; + return keyboard as ReplyMarkup; } } diff --git a/TTSBot/Dockerfile b/TTSBot/Dockerfile index 4d0a8e3..a32e717 100644 --- a/TTSBot/Dockerfile +++ b/TTSBot/Dockerfile @@ -1,8 +1,8 @@ -FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base +FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base USER $APP_UID WORKDIR /app -FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["TTSBot/TTSBot.csproj", "TTSBot/"] diff --git a/TTSBot/TTSBot.csproj b/TTSBot/TTSBot.csproj index bddd41e..c352e6e 100644 --- a/TTSBot/TTSBot.csproj +++ b/TTSBot/TTSBot.csproj @@ -2,7 +2,7 @@ Exe - net9.0 + net10.0 enable enable Linux @@ -16,8 +16,8 @@ - - + + diff --git a/global.json b/global.json new file mode 100644 index 0000000..3140116 --- /dev/null +++ b/global.json @@ -0,0 +1,5 @@ +{ + "test": { + "runner": "Microsoft.Testing.Platform" + } +}