Something went wrong. Try again.
An ATProto Powered Reputation Network
Something went wrong. Try again.
2.8 kB · 76 lines
Go
at master
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677package appview
import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "time"
"provenance/internal/domain" "provenance/internal/trust" "provenance/internal/trust/algorithms")
func TestPathComments(t *testing.T) { for _, algorithm := range []trust.Algorithm{algorithms.Optimistic{}, algorithms.ShortestPath{}} { t.Run(algorithm.Describe().Name, func(t *testing.T) { service, _ := algorithmService(t, algorithm) ctx := context.Background() for _, blocked := range []bool{false, true} { edges := []domain.Vouch{ {Author: alice, Subject: carol, Score: -1, Comment: "Weak path."}, {Author: alice, Subject: dan, Score: 2, Comment: "Strong path."}, {Author: alice, Subject: dan, Tag: "work", Score: 2, Comment: "Topic reference."}, } if blocked { edges = append(edges, domain.Vouch{Author: alice, Subject: bob, Score: domain.Blocked, Comment: "Block reference."}) service.now = func() time.Time { return time.Now().Add(trust.DefaultCacheTTL) } } if err := service.store.Replace(ctx, alice, edges, time.Now()); err != nil { t.Fatal(err) } w := httptest.NewRecorder() Handler(service, serviceKey).ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/v1/trust?source="+alice+"&target="+bob+"&tag=work&mode=fallback&include_path=true", nil)) var got trust.Result if w.Code != http.StatusOK { t.Fatalf("response: %d %s", w.Code, w.Body) } if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { t.Fatal(err) } want := "Topic reference." if algorithm.Describe().Name == (algorithms.ShortestPath{}).Describe().Name { want = "Weak path." } if blocked { want = "Block reference." } if len(got.PathScores) < 2 || got.PathScores[0].Comment != "" || got.PathScores[1].Comment != want { t.Fatalf("comments: %+v", got) } for _, node := range got.PathScores[2:] { if node.Comment != "" { t.Fatalf("unexpected comment: %+v", node) } } } }) }}
func TestCommentResultCopy(t *testing.T) { score := 2.0 assertion := 2.0 original := trust.Result{Status: trust.CalculatedStatus, Score: &score, Path: []string{alice, bob}, PathScores: []trust.PathScore{ {DID: alice, Score: domain.SelfScore}, {DID: bob, Assertion: &assertion, Score: score}, }} got := pathComments(original, pathRecords([]domain.Vouch{{Author: alice, Subject: bob, Score: assertion, Comment: "Reference only."}})) if original.PathScores[1].Comment != "" { t.Fatal("changed an algorithm result buffer") } if got.Score != original.Score || got.PathScores[1].Score != score || got.PathScores[1].Assertion != &assertion || got.PathScores[1].Comment != "Reference only." { t.Fatalf("changed calculation: %+v", got) }}