using Khaemhat.Application.Items; using Khaemhat.Application.Querying; using Khaemhat.Application.Security; using Khaemhat.Application.Seeding; using Khaemhat.Core.Items; using Khaemhat.Core.Security; using Khaemhat.Core.Validation; using Khaemhat.Web.Tests.Hosting; using Microsoft.Extensions.DependencyInjection; namespace Khaemhat.Web.Tests.Items; /// The demo items (SPEC ยง14, issue #42): 120 per scale, valid bar codes, derived lists, and every item passes the checks. [TestClass] public sealed class DemoItemsTests { private static readonly Actor Root = new(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null); [TestMethod] public void Catalogue_HasTheItemsOfTheSpecification_WithValidBarCodes() { var catalogue = DemoItemsSeeder.Catalogue(); Assert.HasCount(DemoItemsSeeder.ItemsPerScale, catalogue); Assert.HasCount(catalogue.Count, catalogue.Select(i => i.Description).Distinct().ToList(), "No description twice."); Assert.IsTrue(catalogue.All(i => i.Kind != ItemKind.Goods || i.Price > 0)); Assert.IsTrue(catalogue.Any(i => i.Kind == ItemKind.Service) && catalogue.Any(i => i.Kind == ItemKind.Expense) && catalogue.Any(i => i.Kind == ItemKind.Text)); foreach (var n in new[] { 1, 99, 500_123 }) { Assert.IsTrue(EanValidator.IsValid(DemoItemsSeeder.Ean(n)), DemoItemsSeeder.Ean(n)); Assert.StartsWith("200", DemoItemsSeeder.Ean(n)); } } [TestMethod] public async Task DemoSeed_FillsAnEmptyCompanyOnce_WithItemsThatPassTheChecks() { var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None); await using (app) { var runner = app.Services.GetRequiredService(); var items = app.Services.GetRequiredService(); var prices = app.Services.GetRequiredService(); var first = await runner.RunAsync(company, SeedKind.Demo); Assert.AreEqual(DemoItemsSeeder.ItemsPerScale, first.Added["items"]); Assert.AreEqual(2, first.Added["price_lists"]); Assert.IsGreaterThan(0, first.Added["item_suppliers"]); Assert.IsGreaterThan(0, first.Added["party_item_codes"]); Assert.IsEmpty((await runner.RunAsync(company, SeedKind.Demo)).Added, "Only an empty company gets the demo data."); var lists = await prices.ListsAsync(Root, company); CollectionAssert.AreEqual(new[] { "BASE", "PUBB", "RIV" }, lists.Select(l => l.Code).ToArray()); var grid = await prices.GridAsync(Root, company, new ListQuery { Search = "sfera 1/2", Limit = 10 }, DateOnly.FromDateTime(DateTime.Today), null); var lever = grid.Page.Items[0]; Assert.IsTrue(lever.Prices[lists[1].Id].Derived, "The public list is derived from the base one."); Assert.IsNotNull(lever.AverageCost); var all = (await items.ListAsync(Root, company, new ListQuery { Limit = 200 })).Items; Assert.HasCount(DemoItemsSeeder.ItemsPerScale, all); foreach (var row in all) { var record = (await items.GetAsync(Root, company, row.Id))!; Assert.IsTrue(record.Item.IsDemo); var errors = (await items.ValidateAsync(Root, company, record)).Where(p => !p.IsWarning).Select(p => p.Field + ":" + p.Code).ToList(); Assert.IsEmpty(errors, row.Code + " " + row.Description + ": " + string.Join(", ", errors)); } await app.StopAsync(); } } [TestMethod] public async Task Scale_RepeatsTheCatalogue() { var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None); await using (app) { var report = await app.Services.GetRequiredService().RunAsync(company, SeedKind.Demo, 2); Assert.AreEqual(2 * DemoItemsSeeder.ItemsPerScale, report.Added["items"]); await app.StopAsync(); } } }