Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ name: Build-Test-Publish Flow
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build_test:
name: Build and Test
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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion TTSBot.Tests/Commands/GetPlaylistCommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions TTSBot.Tests/Commands/GetPlaylistCommandProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion TTSBot.Tests/Commands/GetPlaylistRewriteDecoratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion TTSBot.Tests/Commands/M3uParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Telegram.Bot.Types;
using TTSBot.Extensions;
using TTSBot.Tests.TestUtils;
using TUnit.Assertions.AssertConditions.Throws;

namespace TTSBot.Tests.Extensions;

Expand Down
4 changes: 2 additions & 2 deletions TTSBot.Tests/TTSBot.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>annotations</Nullable>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="TUnit" Version="0.24.0" />
<PackageReference Include="TUnit" Version="1.81.10" />
</ItemGroup>

<ItemGroup>
Expand Down
62 changes: 47 additions & 15 deletions TTSBot.Tests/TestUtils/Assertions/AssertionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<IResult> IsMessage(this IValueSource<IResult> valueSource)
public static ResultAssertion IsMessage(this IAssertionSource<IResult> 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<IResult> IsReply(this IValueSource<IResult> valueSource)

public static ResultAssertion IsReply(this IAssertionSource<IResult> 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<IResult> IsEmpty(this IValueSource<IResult> valueSource)

public static ResultAssertion IsEmpty(this IAssertionSource<IResult> 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<IResult> HasMessage(this IValueSource<IResult> valueSource, string message)

public static ResultAssertion HasMessage(this IAssertionSource<IResult> 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}";
}
}
}

This file was deleted.

This file was deleted.

23 changes: 23 additions & 0 deletions TTSBot.Tests/TestUtils/Assertions/ResultAssertion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using MinimalTelegramBot.Results;
using TUnit.Assertions.Core;

namespace TTSBot.Tests.TestUtils.Assertions;

/// <summary>
/// Assertion on a bot <see cref="IResult"/>. The check returns null when it passes,
/// or the failure description when it doesn't.
/// </summary>
public class ResultAssertion(AssertionContext<IResult> context, Func<IResult?, string?> check, string expectation)
: Assertion<IResult>(context)
{
protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<IResult> 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;
}

This file was deleted.

6 changes: 6 additions & 0 deletions TTSBot.Tests/TestUtils/MockTelegramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -34,9 +35,14 @@
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; }
public IExceptionParser ExceptionsParser { get; set; }
public event AsyncEventHandler<ApiRequestEventArgs>? OnMakingApiRequest;

Check warning on line 47 in TTSBot.Tests/TestUtils/MockTelegramClient.cs

View workflow job for this annotation

GitHub Actions / Build and Test

The event 'MockTelegramClient.OnMakingApiRequest' is never used

Check warning on line 47 in TTSBot.Tests/TestUtils/MockTelegramClient.cs

View workflow job for this annotation

GitHub Actions / Build and Test

The event 'MockTelegramClient.OnMakingApiRequest' is never used
public event AsyncEventHandler<ApiResponseEventArgs>? OnApiResponseReceived;

Check warning on line 48 in TTSBot.Tests/TestUtils/MockTelegramClient.cs

View workflow job for this annotation

GitHub Actions / Build and Test

The event 'MockTelegramClient.OnApiResponseReceived' is never used

Check warning on line 48 in TTSBot.Tests/TestUtils/MockTelegramClient.cs

View workflow job for this annotation

GitHub Actions / Build and Test

The event 'MockTelegramClient.OnApiResponseReceived' is never used
Expand Down
4 changes: 2 additions & 2 deletions TTSBot.Tests/TestUtils/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions TTSBot/Dockerfile
Original file line number Diff line number Diff line change
@@ -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/"]
Expand Down
6 changes: 3 additions & 3 deletions TTSBot/TTSBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
Expand All @@ -16,8 +16,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MinimalTelegramBot" Version="0.0.40" />
<PackageReference Include="Scrutor" Version="6.1.0" />
<PackageReference Include="MinimalTelegramBot" Version="0.1.0" />
<PackageReference Include="Scrutor" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"test": {
"runner": "Microsoft.Testing.Platform"
}
}
Loading