operations-dal/tests/Operations.DAL.UnitTests/InMemoryRestaurantLifecycleRepositoryTests.cs
2026-03-31 18:06:29 -06:00

73 lines
3.0 KiB
C#

using Operations.DAL.Contracts;
using Operations.DAL.Repositories;
namespace Operations.DAL.UnitTests;
public sealed class InMemoryRestaurantLifecycleRepositoryTests
{
private readonly InMemoryRestaurantLifecycleRepository repository = new();
[Fact]
public async Task UpsertOrderAsync_PersistsAndReturnsSharedRecord()
{
var record = CreateRecord(orderId: "ORD-3001", orderState: "Accepted");
await repository.UpsertOrderAsync(record, CancellationToken.None);
var persisted = await repository.GetOrderAsync(record.ContextId, record.OrderId, CancellationToken.None);
Assert.NotNull(persisted);
Assert.Equal("CHK-3001", persisted!.CheckId);
Assert.Equal("Accepted", persisted.OrderState);
}
[Fact]
public async Task ListPayableOrdersAsync_ReturnsOnlyServedOpenBalanceOrders()
{
await repository.UpsertOrderAsync(CreateRecord(orderId: "ORD-3001", orderState: "Served", outstandingBalance: 18m), CancellationToken.None);
await repository.UpsertOrderAsync(CreateRecord(orderId: "ORD-3002", orderState: "Preparing", outstandingBalance: 18m), CancellationToken.None);
await repository.UpsertOrderAsync(CreateRecord(orderId: "ORD-3003", orderState: "Served", checkState: "Paid", outstandingBalance: 18m), CancellationToken.None);
var payable = await repository.ListPayableOrdersAsync("demo-context", CancellationToken.None);
Assert.Single(payable);
Assert.Equal("ORD-3001", payable.Single().OrderId);
}
[Fact]
public async Task AppendEventAsync_StoresOrderedHistoryForSharedOrder()
{
var first = new RestaurantLifecycleEventRecord("demo-context", "ORD-3001", "EVT-1", "Submitted", "Customer submitted the order.", DateTime.UtcNow.AddMinutes(-3));
var second = new RestaurantLifecycleEventRecord("demo-context", "ORD-3001", "EVT-2", "Accepted", "Operations accepted the order.", DateTime.UtcNow.AddMinutes(-1));
await repository.AppendEventAsync(first, CancellationToken.None);
await repository.AppendEventAsync(second, CancellationToken.None);
var events = await repository.ListEventsAsync("demo-context", "ORD-3001", CancellationToken.None);
Assert.Equal(2, events.Count);
Assert.Equal("EVT-2", events.First().EventId);
}
private static RestaurantLifecycleRecord CreateRecord(
string orderId,
string orderState,
string checkState = "Open",
decimal outstandingBalance = 24m)
{
return new RestaurantLifecycleRecord(
ContextId: "demo-context",
OrderId: orderId,
CheckId: "CHK-3001",
TableId: "T-11",
OrderState: orderState,
CheckState: checkState,
GuestCount: 4,
HasKitchenTicket: orderState is "InKitchen" or "Preparing" or "Ready" or "Served",
OutstandingBalance: outstandingBalance,
Currency: "USD",
Source: "customer-orders",
ItemIds: new[] { "ITEM-100", "ITEM-200" },
UpdatedAtUtc: DateTime.UtcNow);
}
}