package main import ( "encoding/json" "strings" "testing" "github.com/novox/mesh-host/internal/store" ) // A declaration carries no order, so a host cannot tell an older one from a newer (novox/hq // 04-ISSUES/107). Its only identity was the digest of its bytes: "not the last" could be said, // "older" could not. func keptWith(t *testing.T, sequence int64) store.Declared { t.Helper() body, err := json.Marshal(map[string]any{ "declaration": 1, "resources": []any{}, "owns_nothing": true, "sequence": sequence, }) if err != nil { t.Fatal(err) } return store.Declared{Declaration: body, Signature: []byte("x")} } func TestAnOlderDeclarationFromTheMeshIsRefused(t *testing.T) { err := refuseOlder(keptWith(t, 7), nil, 5) if err == nil { t.Fatal("sequence 5 was accepted over a kept 7") } if !strings.Contains(err.Error(), "older") || !strings.Contains(err.Error(), "nothing was applied") { t.Fatalf("the refusal does not say what it is: %v", err) } } func TestANewerOrEqualDeclarationIsNot(t *testing.T) { if err := refuseOlder(keptWith(t, 7), nil, 8); err != nil { t.Fatalf("sequence 8 was refused over a kept 7: %v", err) } // Equal is the same declaration again, which reconciling is for. if err := refuseOlder(keptWith(t, 7), nil, 7); err != nil { t.Fatalf("the same sequence was refused: %v", err) } } func TestNoOrderClaimedMeansNoOrderCompared(t *testing.T) { // An older controller sends none; a host that received before it understood them kept none. // Refusing on a guess would strand a node the moment the controller is older than the host. if err := refuseOlder(keptWith(t, 7), nil, 0); err != nil { t.Fatalf("a declaration claiming no order was refused: %v", err) } if err := refuseOlder(keptWith(t, 0), nil, 3); err != nil { t.Fatalf("a declaration was refused against a kept one that claimed no order: %v", err) } if err := refuseOlder(store.Declared{}, store.ErrNothingDeclared, 3); err != nil { t.Fatalf("a first declaration was refused: %v", err) } }