package cart import ( "context" "testing" "time" cart_messages "git.k6n.net/go-cart-actor/proto/cart" ) func TestAddItem_StoresCustomFields(t *testing.T) { reg := NewCartMultationRegistry(NewCartMutationContext(nil)) g := NewCartGrain(1, time.Now()) mustApply(t, reg, g, &cart_messages.AddItem{ ItemId: 100, Sku: "P", Quantity: 1, Price: 1000, CustomFields: map[string]string{"engraving": "Happy Birthday", "color": "blue"}, }) if len(g.Items) != 1 { t.Fatalf("items = %d, want 1", len(g.Items)) } cf := g.Items[0].CustomFields if cf["engraving"] != "Happy Birthday" || cf["color"] != "blue" { t.Fatalf("custom fields = %v, want engraving+color", cf) } } func TestSetLineItemCustomFields_Merges(t *testing.T) { reg := NewCartMultationRegistry(NewCartMutationContext(nil)) g := NewCartGrain(1, time.Now()) mustApply(t, reg, g, &cart_messages.AddItem{ ItemId: 100, Sku: "P", Quantity: 1, Price: 1000, CustomFields: map[string]string{"engraving": "v1"}, }) line := g.Items[0].Id // Upsert: overwrite "engraving", add "note", leave others alone. mustApply(t, reg, g, &cart_messages.SetLineItemCustomFields{ Id: line, CustomFields: map[string]string{"engraving": "v2", "note": "gift wrap"}, }) cf := g.Items[0].CustomFields if cf["engraving"] != "v2" { t.Errorf("engraving = %q, want v2", cf["engraving"]) } if cf["note"] != "gift wrap" { t.Errorf("note = %q, want 'gift wrap'", cf["note"]) } } func TestSetLineItemCustomFields_UnknownItem(t *testing.T) { reg := NewCartMultationRegistry(NewCartMutationContext(nil)) g := NewCartGrain(1, time.Now()) // The handler error is reported per-mutation in the ApplyResult (the // registry only returns a top-level error for unregistered mutations). results, _ := reg.Apply(context.Background(), g, &cart_messages.SetLineItemCustomFields{ Id: 999, CustomFields: map[string]string{"x": "y"}, }) if len(results) != 1 || results[0].Error == nil { t.Errorf("expected per-mutation error for unknown item id, got %+v", results) } }