using System.Net.Http.Json; using System.Text.Json; using FluentAssertions; using foodmarket.IntegrationTests.Support; using Xunit; namespace foodmarket.IntegrationTests; /// Паритет п.7: акт сверки с поставщиком — начальное сальдо, движения /// периода, конечное сальдо. [Collection(ApiCollection.Name)] public class SupplierReconciliationTests { private readonly ApiFactory _factory; public SupplierReconciliationTests(ApiFactory factory) => _factory = factory; private static string RandomBarcode() => string.Concat(Enumerable.Range(0, 13).Select(_ => Random.Shared.Next(0, 10))); [Fact] public async Task Reconciliation_includes_supply_as_debit_and_closing_balance() { var api = new ApiActor(_factory.CreateClient()); await api.SignupAndLoginAsync($"recon-{Guid.NewGuid():N}"); var refs = await api.LoadRefsAsync(); var supplier = await api.CreateCounterpartyAsync($"S-{Guid.NewGuid():N}"); var product = await api.CreateProductAsync(refs, $"P-{Guid.NewGuid():N}", 500m, RandomBarcode()); // Приёмка 10×100 = 1000 → мы должны 1000. var (_, total) = await api.CreatePostedSupplyAsync(refs, supplier, product, 10m, 100m); var recon = await api.GetJsonAsync($"/api/money/reports/supplier-reconciliation?supplierId={supplier}"); recon.GetProperty("supplierId").GetString().Should().Be(supplier); var rows = recon.GetProperty("rows").EnumerateArray().ToList(); rows.Should().Contain(r => r.GetProperty("docType").GetString() == "Приёмка" && r.GetProperty("debit").GetDecimal() == total); recon.GetProperty("closingBalance").GetDecimal().Should().Be(total, "конечное сальдо = сумма приёмки (неоплачено)"); } }