package inventory import ( "testing" "github.com/novox/mesh-controller/internal/catalogue" ) // Where a consumer of data was bound is kept, and a move keeps where it was (novox/hq ADR 0232). func TestABindingIsRecordedAndAMoveKeepsWhereItWas(t *testing.T) { inv := fresh(t) ctx := t.Context() for _, n := range []string{"home", "anchor"} { if _, err := inv.AddNode(ctx, n); err != nil { t.Fatal(err) } } home := catalogue.Chosen{Node: "home", Module: "store"} anchor := catalogue.Chosen{Node: "anchor", Module: "store"} if err := inv.RecordBindings(ctx, "home", []Binding{ {Consumer: "board", Provision: "postgres-database", Provider: home}, {Consumer: "game", Provision: "postgres-database", Provider: home}, }); err != nil { t.Fatal(err) } got, err := inv.BindingsFor(ctx, "home") if err != nil { t.Fatal(err) } if got["board"]["postgres-database"] != home || got["game"]["postgres-database"] != home { t.Fatalf("recorded %+v", got) } first, err := inv.Bindings(ctx) if err != nil || len(first) != 2 { t.Fatalf("%+v, %v", first, err) } // Sent again where it is: only marked sent. if err := inv.RecordBindings(ctx, "home", []Binding{{Consumer: "board", Provision: "postgres-database", Provider: home}}); err != nil { t.Fatal(err) } // Moved by a pin and sent: bound anew, with where it was. if err := inv.RecordBindings(ctx, "home", []Binding{{Consumer: "game", Provision: "postgres-database", Provider: anchor}}); err != nil { t.Fatal(err) } all, err := inv.Bindings(ctx) if err != nil { t.Fatal(err) } for _, b := range all { switch b.Consumer { case "board": if b.Provider != home || b.MovedFrom != "" || !b.BoundAt.Equal(first[0].BoundAt) || b.Machine != "home" { t.Errorf("a binding sent again where it is changed: %+v (was %+v)", b, first[0]) } case "game": if b.Provider != anchor || b.MovedFrom != "home/store" || !b.BoundAt.After(first[1].BoundAt) { t.Errorf("a moved binding: %+v", b) } } } // A provider machine leaving keeps the record of where the data is. if _, err := inv.store.Pool().Exec(ctx, `delete from node where name = 'anchor'`); err != nil { t.Fatal(err) } got, err = inv.BindingsFor(ctx, "home") if err != nil { t.Fatal(err) } if got["game"]["postgres-database"] != anchor { t.Fatalf("the record went with the provider's machine: %+v", got) } }