package domain import "testing" func TestPatternValidation(t *testing.T) { p := Pattern{Title: "demo", Width: 2, Height: 1, Cells: []*string{str("R"), nil}, Colors: []Color{{Code: "R", Hex: "#ff0000"}}, Visibility: "private"} if err := p.Validate(); err != nil { t.Fatal(err) } p.Cells[0] = str("missing") if p.Validate() == nil { t.Fatal("unknown color accepted") } p.Cells = []*string{} if p.Validate() == nil { t.Fatal("wrong grid size accepted") } } func str(s string) *string { return &s } func TestTransitionAuthorization(t *testing.T) { cases := []struct { role, old, next string own bool ok bool }{{"guest", "pending", "cancelled", true, true}, {"guest", "accepted", "cancelled", true, false}, {"guest", "pending", "cancelled", false, false}, {"staff", "pending", "completed", false, false}, {"staff", "pending", "accepted", false, true}, {"admin", "accepted", "completed", false, true}} for _, c := range cases { if Transition(c.role, c.old, c.next, c.own) != c.ok { t.Fatalf("%+v", c) } } } func TestIdempotencyContent(t *testing.T) { if Fingerprint("help", "one", "") == Fingerprint("help", "two", "") { t.Fatal("different contents collide") } if Fingerprint("help", "one", "") != Fingerprint("help", "one", "") { t.Fatal("unstable fingerprint") } } func TestPrivateIsolation(t *testing.T) { p := Pattern{OwnerID: "owner", Visibility: "private", StoreID: "s1"} if CanRead(p, User{ID: "stranger", Role: "guest", StoreID: "s1"}) { t.Fatal("private pattern leaked") } if CanRead(p, User{ID: "admin", Role: "admin", StoreID: "s2"}) { t.Fatal("cross-store admin read") } if !CanRead(p, User{ID: "owner"}) { t.Fatal("owner denied") } }