From f5447484512d726f4403f0d7725777d0a95601fb Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Tue, 19 Sep 2023 00:16:36 +0200 Subject: Add more stuff, add unit tests --- Tests/TestDataGenerator/Bot/DataFetcher.cs | 69 ++++++++++++++++++++++ .../Bot/DataFetcherConfiguration.cs | 9 +++ Tests/TestDataGenerator/Program.cs | 31 ++++++++++ .../Properties/launchSettings.json | 26 ++++++++ Tests/TestDataGenerator/TestDataGenerator.csproj | 32 ++++++++++ .../TestDataGenerator/appsettings.Development.json | 18 ++++++ Tests/TestDataGenerator/appsettings.json | 9 +++ 7 files changed, 194 insertions(+) create mode 100644 Tests/TestDataGenerator/Bot/DataFetcher.cs create mode 100644 Tests/TestDataGenerator/Bot/DataFetcherConfiguration.cs create mode 100644 Tests/TestDataGenerator/Program.cs create mode 100644 Tests/TestDataGenerator/Properties/launchSettings.json create mode 100644 Tests/TestDataGenerator/TestDataGenerator.csproj create mode 100644 Tests/TestDataGenerator/appsettings.Development.json create mode 100644 Tests/TestDataGenerator/appsettings.json (limited to 'Tests/TestDataGenerator') diff --git a/Tests/TestDataGenerator/Bot/DataFetcher.cs b/Tests/TestDataGenerator/Bot/DataFetcher.cs new file mode 100644 index 0000000..b1f8402 --- /dev/null +++ b/Tests/TestDataGenerator/Bot/DataFetcher.cs @@ -0,0 +1,69 @@ +using System.Text; +using System.Threading.Channels; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Helpers; +using LibMatrix.Homeservers; +using LibMatrix.Interfaces; +using LibMatrix.RoomTypes; +using LibMatrix.Services; +using LibMatrix.Tests; +using LibMatrix.Utilities.Bot; +using LibMatrix.Utilities.Bot.Interfaces; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace PluralContactBotPoC.Bot; + +public class DataFetcher(AuthenticatedHomeserverGeneric hs, ILogger logger, LibMatrixBotConfiguration botConfiguration, + // DataFetcherConfiguration configuration, + HomeserverResolverService hsResolver) : IHostedService { + private Task _listenerTask; + + private GenericRoom? _logRoom; + + /// Triggered when the application host is ready to start the service. + /// Indicates that the start process has been aborted. + public async Task StartAsync(CancellationToken cancellationToken) { + _listenerTask = Run(cancellationToken); + logger.LogInformation("Bot started!"); + } + + private async Task Run(CancellationToken cancellationToken) { + Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete); + _logRoom = hs.GetRoom(botConfiguration.LogRoom); + + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: "Test data collector started!")); + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: "Fetching rooms...")); + + var rooms = await hs.GetJoinedRooms(); + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: $"Fetched {rooms.Count} rooms!")); + + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: "Fetching room data...")); + + Config cfg = new Config(); + + var roomAliasTasks = rooms.Select(room => room.GetCanonicalAliasAsync()).ToAsyncEnumerable(); + List> aliasResolutionTasks = new(); + await foreach (var @event in roomAliasTasks) { + if (@event?.Alias != null) { + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: $"Fetched room alias {(@event).Alias}!")); + aliasResolutionTasks.Add(Task<(string, string)>.Run(async () => { + var alias = await hs.ResolveRoomAliasAsync(@event.Alias); + return (@event.Alias, @alias.RoomId); + }, cancellationToken)); + } + } + var aliasResolutionTaskEnumerator = aliasResolutionTasks.ToAsyncEnumerable(); + await foreach (var result in aliasResolutionTaskEnumerator) { + await _logRoom.SendMessageEventAsync(new RoomMessageEventContent(body: $"Resolved room alias {result.Item1} to {result.Item2}!")); + } + } + + /// Triggered when the application host is performing a graceful shutdown. + /// Indicates that the shutdown process should no longer be graceful. + public async Task StopAsync(CancellationToken cancellationToken) { + logger.LogInformation("Shutting down bot!"); + } +} diff --git a/Tests/TestDataGenerator/Bot/DataFetcherConfiguration.cs b/Tests/TestDataGenerator/Bot/DataFetcherConfiguration.cs new file mode 100644 index 0000000..06df0eb --- /dev/null +++ b/Tests/TestDataGenerator/Bot/DataFetcherConfiguration.cs @@ -0,0 +1,9 @@ +using Microsoft.Extensions.Configuration; + +namespace PluralContactBotPoC.Bot; + +public class DataFetcherConfiguration { + public DataFetcherConfiguration(IConfiguration config) => config.GetRequiredSection("DataFetcher").Bind(this); + + // public string +} diff --git a/Tests/TestDataGenerator/Program.cs b/Tests/TestDataGenerator/Program.cs new file mode 100644 index 0000000..9bd091b --- /dev/null +++ b/Tests/TestDataGenerator/Program.cs @@ -0,0 +1,31 @@ +// See https://aka.ms/new-console-template for more information + +using System.Text.Json; +using System.Text.Json.Serialization; +using ArcaneLibs.Extensions; +using LibMatrix.Services; +using LibMatrix.Utilities.Bot; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using PluralContactBotPoC; +using PluralContactBotPoC.Bot; + +Console.WriteLine("Hello, World!"); + +var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) => { + services.AddScoped(x => + new TieredStorageService( + cacheStorageProvider: new FileStorageProvider("bot_data/cache/"), + dataStorageProvider: new FileStorageProvider("bot_data/data/") + ) + ); + // services.AddSingleton(); + services.AddSingleton(); + + services.AddRoryLibMatrixServices(); + services.AddBot(withCommands: false); + + services.AddHostedService(); +}).UseConsoleLifetime().Build(); + +await host.RunAsync(); diff --git a/Tests/TestDataGenerator/Properties/launchSettings.json b/Tests/TestDataGenerator/Properties/launchSettings.json new file mode 100644 index 0000000..997e294 --- /dev/null +++ b/Tests/TestDataGenerator/Properties/launchSettings.json @@ -0,0 +1,26 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "Default": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + + } + }, + "Development": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + }, + "Local config": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Local" + } + } + } +} diff --git a/Tests/TestDataGenerator/TestDataGenerator.csproj b/Tests/TestDataGenerator/TestDataGenerator.csproj new file mode 100644 index 0000000..1b5a4a9 --- /dev/null +++ b/Tests/TestDataGenerator/TestDataGenerator.csproj @@ -0,0 +1,32 @@ + + + + Exe + net7.0 + preview + enable + enable + false + true + + + + + + + + + + + + + + Always + + + + + + + + diff --git a/Tests/TestDataGenerator/appsettings.Development.json b/Tests/TestDataGenerator/appsettings.Development.json new file mode 100644 index 0000000..38c45c4 --- /dev/null +++ b/Tests/TestDataGenerator/appsettings.Development.json @@ -0,0 +1,18 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + }, + "LibMatrixBot": { + // The homeserver to connect to + "Homeserver": "rory.gay", + // The access token to use + "AccessToken": "syt_xxxxxxxxxxxxxxxxx", + // The command prefix + "Prefix": "?", + "LogRoom": "!xxxxxxxxxxxxxxxxxxxxxx:example.com" + } +} diff --git a/Tests/TestDataGenerator/appsettings.json b/Tests/TestDataGenerator/appsettings.json new file mode 100644 index 0000000..6ba02f3 --- /dev/null +++ b/Tests/TestDataGenerator/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} -- cgit 1.4.1