72 lines
2.5 KiB
Go
72 lines
2.5 KiB
Go
package findings
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
)
|
|
|
|
// fakeKV is the two calls a findings state makes of its bucket.
|
|
type fakeKV struct {
|
|
jetstream.KeyValue
|
|
held map[string][]byte
|
|
}
|
|
|
|
func (f *fakeKV) Put(_ context.Context, key string, value []byte) (uint64, error) {
|
|
f.held[key] = value
|
|
return uint64(len(f.held)), nil
|
|
}
|
|
|
|
func (f *fakeKV) Delete(_ context.Context, key string, _ ...jetstream.KVDeleteOpt) error {
|
|
delete(f.held, key)
|
|
return nil
|
|
}
|
|
|
|
// A part's finding is written in the seat mesh-conditions' layout under the key the condition keeper reads, cleared by the
|
|
// same finding, and refused while the bucket is not bound (the part says that in its log).
|
|
func TestAFindingIsWrittenInTheSeatsLayoutAndClearedAndRefusedUnbound(t *testing.T) {
|
|
kv := &fakeKV{held: map[string][]byte{}}
|
|
var bound jetstream.KeyValue
|
|
w := Writer(context.Background(), func() jetstream.KeyValue { return bound })
|
|
f := views.Finding{Observer: "mesh-delivery-teller", Kind: views.UnansweredKind, ID: "mesh-delivery-teller.asks", Summary: "s", Headline: "h",
|
|
Explanation: "e", At: time.Date(2026, 11, 5, 15, 0, 0, 0, time.UTC)}
|
|
if err := w.Put(f); !errors.Is(err, ErrUnbound) {
|
|
t.Fatalf("an unbound bucket took the finding: %v", err)
|
|
}
|
|
bound = kv
|
|
if err := w.Put(f); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got struct{ Key, Kind, Observer, Source string }
|
|
for key, value := range kv.held {
|
|
if err := json.Unmarshal(value, &got); err != nil || got.Key != key || got.Kind != views.UnansweredKind ||
|
|
got.Observer != f.Observer || got.Source != f.Observer+"."+f.Kind {
|
|
t.Fatalf("%s holds %s (%v)", key, value, err)
|
|
}
|
|
}
|
|
if len(kv.held) != 1 {
|
|
t.Fatalf("the bucket holds %d findings, want 1", len(kv.held))
|
|
}
|
|
if err := w.Delete(f); err != nil || len(kv.held) != 0 {
|
|
t.Fatalf("the finding is not cleared: %v, %d left", err, len(kv.held))
|
|
}
|
|
// The same finding over two functions (a part whose findings also say its conditions).
|
|
held := map[string][]byte{}
|
|
o := WriterOver(func(k string, v []byte) error { held[k] = v; return nil }, func(k string) error { delete(held, k); return nil })
|
|
if err := o.Put(f); err != nil || len(held) != 1 {
|
|
t.Fatalf("over functions: %v, %d held", err, len(held))
|
|
}
|
|
if err := o.Delete(f); err != nil || len(held) != 0 {
|
|
t.Fatalf("over functions not cleared: %v, %d left", err, len(held))
|
|
}
|
|
if err := WriterOver(nil, nil).Put(f); !errors.Is(err, ErrUnbound) {
|
|
t.Fatalf("no functions took the finding: %v", err)
|
|
}
|
|
}
|