//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.Persistence.Initialization.Tests;
using Microsoft.Extensions.Logging.Abstractions;
using MUnique.OpenMU.Interfaces;
using MUnique.OpenMU.Persistence.EntityFramework;
using MUnique.OpenMU.Persistence.EntityFramework.Json;
using MUnique.OpenMU.Persistence.Initialization.Updates;
using MUnique.OpenMU.PlugIns;
///
/// A manual tool, not a test: applies the pending configuration updates to the database configured in
/// ConnectionSettings.xml - the same thing the admin panel's "Updates" page does, for when there is
/// no browser at hand. It is so it never runs in a normal test pass.
///
[TestFixture]
[Explicit("Writes to the configured database. Run it deliberately, not as part of the test suite.")]
internal class ApplyPendingUpdatesTool
{
///
/// Applies every configuration update which is not installed yet.
///
[Test]
public async Task ApplyPendingUpdatesAsync()
{
// The server registers these at startup; without them the configuration JSON cannot be read back.
JsonConverterRegistry.RegisterConverter(new LocalizedStringJsonConverter());
JsonConverterRegistry.RegisterConverter(new BinaryAsHexJsonConverter());
var loggerFactory = new NullLoggerFactory();
var contextProvider = new PersistenceContextProvider(loggerFactory, null);
var plugInManager = new PlugInManager(null, loggerFactory, null, null);
plugInManager.DiscoverAndRegisterPlugIns();
var service = new DataUpdateService(contextProvider, plugInManager);
var pending = (await service.DetermineAvailableUpdatesAsync().ConfigureAwait(false)).ToList();
TestContext.Out.WriteLine($"Pending updates: {pending.Count}");
foreach (var update in pending)
{
TestContext.Out.WriteLine($" {(int)update.Version} - {update.Name}");
}
if (pending.Count == 0)
{
return;
}
var progress = new Progress<(UpdateVersion CurrentUpdatingVersion, bool IsCompleted)>(
p => TestContext.Out.WriteLine($" applying {(int)p.CurrentUpdatingVersion} completed={p.IsCompleted}"));
await service.ApplyUpdatesAsync(pending, progress).ConfigureAwait(false);
var left = await service.DetermineAvailableUpdatesAsync().ConfigureAwait(false);
Assert.That(left, Is.Empty, "all updates should be installed now");
}
}