Files
Khaemhat/tests/Khaemhat.Web.Tests/Hosting/CompanyIsolationTests.cs
T
Alby96andClaude Opus 5.5 34e196424f feat: company tables, profile, fiscal years and base data (#38, #39, #45)
Adds the company tables of SPEC 11.1 behind one generic service so that every
table gets the same rules, history, cache and CSV import/export, plus the
company profile, fiscal years with monthly lock and the base-data seeder.

- 26 coded tables, exchange rates, company profile, fiscal years and periods:
  entities, maps and migrations 010-014
- TableService + TableCatalog: unique codes, per-table rules (VAT codes are
  checked by the fiscal pack), delete or disable when in use, single default,
  CSV export/import all-or-nothing, cache with cross-instance invalidation
- CompanyProfileService (Italian identifiers through the pack),
  FiscalYearService with the date check used by every dated record
- EcbRatesClient and the ECB rates system task; CsvReader
- ISeeder/SeedRunner with the data version in company settings; base tables
  from the pack (IBaseTablesTemplate) and an embedded ISO country list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 22:33:37 +01:00

87 lines
4.0 KiB
C#

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;
/// <summary>Two companies on one instance (SPEC §1.4, issue #23): each has its own database and no data crosses over.</summary>
[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<ILicenseActivationService>().ActivateAsync(root, TestLicenseCatalog.All);
Assert.IsTrue(activation.Succeeded, activation.Outcome);
var created = await services.GetRequiredService<CompanyService>().CreateAsync(Actor.System, new NewCompany(second, "Seconda S.r.l."));
Assert.AreEqual(CompanyStatus.Active, created.Status);
var factory = services.GetRequiredService<IUnitOfWorkFactory>();
await using (var unitOfWork = await factory.CreateAsync(DatabaseScope.Company(first)))
{
await unitOfWork.BeginAsync();
await unitOfWork.Repository<Role, long>().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<ICompanyRegistry>();
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<ICompanyRegistry>();
var roles = await CountAsync(services.GetRequiredService<IUnitOfWorkFactory>(), company, null);
await services.GetRequiredService<StartupOrchestrator>().RunAsync();
Assert.AreEqual(1, (await registry.ListAsync()).Count);
Assert.AreEqual(roles, await CountAsync(services.GetRequiredService<IUnitOfWorkFactory>(), company, null));
Assert.IsTrue(services.GetRequiredService<BootstrapState>().IsReady);
var scheduler = services.GetRequiredService<JobScheduler>();
await scheduler.EnsureSystemTasksAsync();
foreach (var task in JobScheduler.SystemTasks.Where(t => !t.UsesNetwork))
{
Assert.AreEqual(JobOutcome.Succeeded, await scheduler.RunAsync(task.Code), task.Code);
}
await app.StopAsync();
}
}
private static async Task<int> CountAsync(IUnitOfWorkFactory factory, string company, string? code)
{
await using var unitOfWork = await factory.CreateAsync(DatabaseScope.Company(company));
var query = code is null ? Query.For<Role>() : Query.For<Role>().Where(r => r.Code == code);
return (await unitOfWork.Repository<Role, long>().ListAsync(query)).Items.Count;
}
}