71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
package cart
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
cart_messages "git.k6n.net/mats/go-cart-actor/proto/cart"
|
|
)
|
|
|
|
// Item identity is the catalog item id, not the (reference-only) SKU: two adds
|
|
// with the same ItemId but different SKU strings must merge into one line.
|
|
func TestAddItem_MergesByItemIdNotSku(t *testing.T) {
|
|
reg := NewCartMultationRegistry(NewCartMutationContext(nil))
|
|
g := NewCartGrain(1, time.Now())
|
|
ctx := context.Background()
|
|
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 144047, Sku: "A0162590", Quantity: 1, Price: 1000}); err != nil {
|
|
t.Fatalf("first add: %v", err)
|
|
}
|
|
// Same id, different (reference) sku string -> should merge, not create a line.
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 144047, Sku: "STALE-REF", Quantity: 2, Price: 1000}); err != nil {
|
|
t.Fatalf("second add: %v", err)
|
|
}
|
|
|
|
if len(g.Items) != 1 {
|
|
t.Fatalf("items = %d, want 1 (merged by id)", len(g.Items))
|
|
}
|
|
if g.Items[0].Quantity != 3 {
|
|
t.Errorf("quantity = %d, want 3", g.Items[0].Quantity)
|
|
}
|
|
|
|
// A different id is a distinct line.
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 170852, Sku: "A0190103", Quantity: 1, Price: 500}); err != nil {
|
|
t.Fatalf("third add: %v", err)
|
|
}
|
|
if len(g.Items) != 2 {
|
|
t.Fatalf("items = %d, want 2", len(g.Items))
|
|
}
|
|
}
|
|
|
|
// Re-adding the same parent (same ItemId + StoreId, no ParentId on the
|
|
// re-add itself) merges the quantity, and the proportional cascade resizes
|
|
// every direct child to match the new parent qty so a "+1 drill" leaves
|
|
// the bundle internally consistent.
|
|
func TestAddItem_MergeCascadesToChildren(t *testing.T) {
|
|
reg := NewCartMultationRegistry(NewCartMutationContext(nil))
|
|
g := NewCartGrain(1, time.Now())
|
|
ctx := context.Background()
|
|
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 100, Sku: "P", Quantity: 1, Price: 1000}); err != nil {
|
|
t.Fatalf("add parent: %v", err)
|
|
}
|
|
parentLine := g.Items[0].Id
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 101, Sku: "C1", Quantity: 1, Price: 100, ParentId: &parentLine}); err != nil {
|
|
t.Fatalf("add child: %v", err)
|
|
}
|
|
|
|
// Re-add the same parent — same ItemId, no ParentId on the re-add itself.
|
|
if _, err := reg.Apply(ctx, g, &cart_messages.AddItem{ItemId: 100, Sku: "P", Quantity: 1, Price: 1000}); err != nil {
|
|
t.Fatalf("re-add parent: %v", err)
|
|
}
|
|
|
|
// Parent qty 1 -> 2; child qty 1 -> 2 via cascade.
|
|
for _, it := range g.Items {
|
|
if it.Quantity != 2 {
|
|
t.Errorf("line %s qty = %d, want 2 (merge cascade)", it.Sku, it.Quantity)
|
|
}
|
|
}
|
|
}
|