chore: begin porting code from playground
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<Project>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AnalysisLevel>latest</AnalysisLevel>
|
||||
<AnalysisMode>All</AnalysisMode>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
|
||||
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,4 @@
|
||||
[*.cs]
|
||||
dotnet_diagnostic.CA1515.severity = none
|
||||
dotnet_diagnostic.CA1707.severity = none
|
||||
dotnet_diagnostic.CA2201.severity = none
|
||||
@@ -1,21 +1,41 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.msbuild" Version="6.0.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="7.2.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="moq" Version="4.20.72" />
|
||||
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<CollectCoverage>true</CollectCoverage>
|
||||
<CoverletOutput>./TestResults/Coverage/</CoverletOutput>
|
||||
<CoverletOutputFormat>cobertura</CoverletOutputFormat>
|
||||
<Include>[StevesBot.Worker]*</Include>
|
||||
<ExcludeByFile>**/Program.cs,**/Worker.cs</ExcludeByFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<Target Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResultAfterTest">
|
||||
<Exec Command="reportgenerator -reports:./TestResults/Coverage/*.xml -targetdir:./TestResults/Coverage/Report/ -reporttypes:Html_Dark" />
|
||||
</Target>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
<Using Include="FluentAssertions" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace StevesBot.Worker.Tests.Unit;
|
||||
|
||||
public class DiscordClientExceptionTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithoutParameters_ItShouldCreateInstance()
|
||||
{
|
||||
var exception = new DiscordClientException();
|
||||
|
||||
exception.Should().NotBeNull();
|
||||
exception.Should().BeOfType<DiscordClientException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithMessage_ItShouldCreateInstance()
|
||||
{
|
||||
var message = "Test message";
|
||||
var exception = new DiscordClientException(message);
|
||||
|
||||
exception.Should().NotBeNull();
|
||||
exception.Should().BeOfType<DiscordClientException>();
|
||||
exception.Message.Should().Be(message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithMessageAndInnerException_ItShouldCreateInstance()
|
||||
{
|
||||
var message = "Test message";
|
||||
var innerException = new Exception("Inner exception");
|
||||
var exception = new DiscordClientException(message, innerException);
|
||||
|
||||
exception.Should().NotBeNull();
|
||||
exception.Should().BeOfType<DiscordClientException>();
|
||||
exception.Message.Should().Be(message);
|
||||
exception.InnerException.Should().Be(innerException);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace StevesBot.Worker.Tests.Unit;
|
||||
|
||||
public class DiscordClientOptionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithoutParameters_ItShouldCreateInstance()
|
||||
{
|
||||
var options = new DiscordClientOptions();
|
||||
|
||||
options.Should().NotBeNull();
|
||||
options.Should().BeOfType<DiscordClientOptions>();
|
||||
options.ApiUrl.Should().Be(string.Empty);
|
||||
options.AppToken.Should().Be(string.Empty);
|
||||
options.Intents.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithParameters_ItShouldCreateInstance()
|
||||
{
|
||||
var apiUrl = "https://api.example.com";
|
||||
var appToken = "test-token";
|
||||
var intents = 123;
|
||||
|
||||
var options = new DiscordClientOptions
|
||||
{
|
||||
ApiUrl = apiUrl,
|
||||
AppToken = appToken,
|
||||
Intents = intents
|
||||
};
|
||||
|
||||
options.Should().NotBeNull();
|
||||
options.Should().BeOfType<DiscordClientOptions>();
|
||||
options.ApiUrl.Should().Be(apiUrl);
|
||||
options.AppToken.Should().Be(appToken);
|
||||
options.Intents.Should().Be(intents);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace StevesBot.Worker.Tests;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
global using StevesBot.Worker.Discord;
|
||||
@@ -0,0 +1,2 @@
|
||||
[*.cs]
|
||||
dotnet_diagnostic.CA2007.severity = none
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace StevesBot.Worker.Discord;
|
||||
|
||||
internal class DiscordClientException : Exception
|
||||
{
|
||||
public DiscordClientException()
|
||||
{
|
||||
}
|
||||
|
||||
public DiscordClientException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public DiscordClientException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace StevesBot.Worker.Discord;
|
||||
|
||||
internal class DiscordClientOptions
|
||||
{
|
||||
public string ApiUrl { get; init; } = string.Empty;
|
||||
public string AppToken { get; init; } = string.Empty;
|
||||
public int Intents { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal record DiscordEvent
|
||||
{
|
||||
[JsonPropertyName("op")]
|
||||
public int Op { get; init; }
|
||||
|
||||
[JsonPropertyName("s")]
|
||||
public int? Sequence { get; init; }
|
||||
|
||||
[JsonPropertyName("t")]
|
||||
public string? Type { get; init; }
|
||||
|
||||
[JsonPropertyName("d")]
|
||||
public object? Data { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal class DiscordEventConverter : JsonConverter<DiscordEvent>
|
||||
{
|
||||
private const string OpPropertyName = "op";
|
||||
|
||||
public override DiscordEvent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
using var jsonDoc = JsonDocument.ParseValue(ref reader);
|
||||
var root = jsonDoc.RootElement;
|
||||
var op = root.GetProperty(OpPropertyName).GetInt32();
|
||||
|
||||
return op switch
|
||||
{
|
||||
DiscordOpCodes.Hello => JsonSerializer.Deserialize<HelloDiscordEvent>(root.GetRawText(), options),
|
||||
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), options)
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DiscordEvent value, JsonSerializerOptions options)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal static class DiscordOpCodes
|
||||
{
|
||||
public const int Hello = 10;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace StevesBot.Worker.Discord.Events;
|
||||
|
||||
internal record HelloDiscordEvent : DiscordEvent
|
||||
{
|
||||
[JsonPropertyName("d")]
|
||||
public new HelloData? Data { get; init; }
|
||||
}
|
||||
|
||||
internal record HelloData
|
||||
{
|
||||
[JsonPropertyName("heartbeat_interval")]
|
||||
public int HeartbeatInterval { get; init; }
|
||||
}
|
||||
@@ -4,4 +4,4 @@ var builder = Host.CreateApplicationBuilder(args);
|
||||
builder.Services.AddHostedService<Worker>();
|
||||
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
await host.RunAsync();
|
||||
@@ -1,13 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Worker">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>dotnet-StevesBot.Worker-0b7a94ae-5cbd-406d-8422-1c791715ef7d</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
global using System.Text.Json;
|
||||
global using System.Text.Json.Serialization;
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace StevesBot.Worker;
|
||||
|
||||
public class Worker : BackgroundService
|
||||
internal class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
|
||||
@@ -15,8 +15,9 @@ public class Worker : BackgroundService
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
_logger.LogInformation("Worker running at: {Time}", DateTimeOffset.Now);
|
||||
}
|
||||
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"DiscordClientOptions": {
|
||||
"ApiUrl": "ApiUrl",
|
||||
"AppToken": "AppToken",
|
||||
"Intents": 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user