Project · Production companions

Check full message equivalence and local effects

Run ten independent policy checks without pretending an in-memory owner is a durable inbox.

All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.

Program.cs

InboxChecks.Run();

Domain/InboxPolicy.cs

public sealed record Message(string Id,string Payload,int Amount);
public sealed class InboxPolicy {
    private readonly Dictionary<string,(string Payload,int Amount)> seen=new(StringComparer.Ordinal);
    public int Count=>seen.Count; public int Total {get;private set;}
    public string Apply(Message message) {
        if(string.IsNullOrWhiteSpace(message.Id)||string.IsNullOrWhiteSpace(message.Payload)||message.Amount is <1 or >10)return "invalid";
        var value=(message.Payload,message.Amount);
        if(seen.TryGetValue(message.Id,out var prior))return prior==value?"replay":"conflict";
        int next=checked(Total+message.Amount); seen.Add(message.Id,value); Total=next; return "created";
    }
}

Checks/InboxChecks.cs

public static class InboxChecks {
    public static void Run() {
        var owner=new InboxPolicy(); int checks=0; void Check(bool value){if(!value)throw new Exception("Independent inbox expectation failed");checks++;}
        Check(owner.Apply(new("one","rune",3))=="created");
        Check(owner.Apply(new("one","rune",3))=="replay");
        Check(owner.Apply(new("one","rune",4))=="conflict");
        Check(owner.Apply(new("one","glyph",3))=="conflict");
        Check(owner.Count==1&&owner.Total==3);
        Check(owner.Apply(new("two","glyph",2))=="created");
        Check(owner.Count==2&&owner.Total==5);
        Check(owner.Apply(new("","rune",1))=="invalid");
        Check(owner.Apply(new("three","rune",0))=="invalid");
        Check(owner.Count==2&&owner.Total==5);
        Console.WriteLine("inbox checks:"+checks); Console.WriteLine("owner:memory only");
    }
}

InboxPolicy.csproj

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable></PropertyGroup></Project>