using Khaemhat.Application.Instance; using Khaemhat.Application.Jobs; using Khaemhat.Application.Licensing; using Khaemhat.Application.Security; using Khaemhat.Core.Instance; using Khaemhat.Core.Security; using Khaemhat.Data; using Khaemhat.Data.Companies; using Khaemhat.Data.Querying; using Microsoft.Extensions.DependencyInjection; namespace Khaemhat.Web.Tests.Hosting; /// Two companies on one instance (SPEC ยง1.4, issue #23): each has its own database and no data crosses over. [TestClass] public sealed class CompanyIsolationTests { [TestMethod] public async Task ASecondCompany_HasItsOwnDatabase_AndSeesNothingOfTheFirst() { var (app, _, first) = await TestInstance.StartAsync(CancellationToken.None); await using (app) { var services = app.Services; var second = "S" + first[1..]; // One company is free; more need the MULTI module (here the test code, accepted only in test mode). var root = new Actor(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null); var activation = await services.GetRequiredService().ActivateAsync(root, TestLicenseCatalog.All); Assert.IsTrue(activation.Succeeded, activation.Outcome); var created = await services.GetRequiredService().CreateAsync(Actor.System, new NewCompany(second, "Seconda S.r.l.")); Assert.AreEqual(CompanyStatus.Active, created.Status); var factory = services.GetRequiredService(); await using (var unitOfWork = await factory.CreateAsync(DatabaseScope.Company(first))) { await unitOfWork.BeginAsync(); await unitOfWork.Repository().InsertAsync(new Role { Code = "SOLO_PRIMA", Name = "Solo nella prima" }); await unitOfWork.CommitAsync(); } Assert.AreEqual(1, await CountAsync(factory, first, "SOLO_PRIMA")); Assert.AreEqual(0, await CountAsync(factory, second, "SOLO_PRIMA")); // Both companies got the predefined roles from their own seeding. Assert.IsTrue(await CountAsync(factory, second, null) > 0); var registry = services.GetRequiredService(); Assert.AreNotEqual((await registry.ResolveAsync(first)).DatabaseName, (await registry.ResolveAsync(second)).DatabaseName); await app.StopAsync(); } } [TestMethod] public async Task TheStartSequence_RunTwice_ChangesNothing_AndSystemJobsRun() { var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None); await using (app) { var services = app.Services; var registry = services.GetRequiredService(); var roles = await CountAsync(services.GetRequiredService(), company, null); await services.GetRequiredService().RunAsync(); Assert.AreEqual(1, (await registry.ListAsync()).Count); Assert.AreEqual(roles, await CountAsync(services.GetRequiredService(), company, null)); Assert.IsTrue(services.GetRequiredService().IsReady); var scheduler = services.GetRequiredService(); await scheduler.EnsureSystemTasksAsync(); foreach (var task in JobScheduler.SystemTasks) { Assert.AreEqual(JobOutcome.Succeeded, await scheduler.RunAsync(task.Code), task.Code); } await app.StopAsync(); } } private static async Task CountAsync(IUnitOfWorkFactory factory, string company, string? code) { await using var unitOfWork = await factory.CreateAsync(DatabaseScope.Company(company)); var query = code is null ? Query.For() : Query.For().Where(r => r.Code == code); return (await unitOfWork.Repository().ListAsync(query)).Items.Count; } }