Something went wrong. Try again.
An ATProto Powered Reputation Network
Something went wrong. Try again.
5.2 kB · 126 lines
Go
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127package microcosm
import ( "context" "encoding/json" "errors" "github.com/coder/websocket" "net/http" "net/http/httptest" "testing"
"provenance/internal/atproto" "provenance/internal/domain")
func TestSoftEvents(t *testing.T) { for _, source := range []string{domain.FollowCollection, domain.BlockCollection} { record := atproto.SoftRecord{Type: source, Subject: subject, CreatedAt: "2026-10-09T00:00:00Z"} server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Query().Get("collection") != source { t.Error("wrong hydration source") } json.NewEncoder(w).Encode(map[string]any{"uri": "at://" + author + "/" + source + "/record", "cid": recordCID, "value": record}) })) for _, mode := range []domain.SoftMode{domain.SoftDisabled, domain.SoftParticipants, domain.SoftAllAuthors} { client, err := NewWithSoft("ws://127.0.0.1:1/subscribe", server.URL, mode) if err != nil { t.Fatal(err) } for _, operation := range []Operation{Create, Update, Delete} { for _, payload := range []any{record, nil} { data, err := json.Marshal(map[string]any{"did": author, "time_us": 100, "kind": domain.RecordChange, "commit": map[string]any{"operation": operation, "collection": source, "rkey": "record", "cid": recordCID, "record": payload}}) if err != nil { t.Fatal(err) } change, err := client.decode(context.Background(), data, nil) if err != nil { t.Fatal(err) } if mode == domain.SoftDisabled { if change.Kind != domain.IdentityChange || change.Vouch != nil { t.Fatal("disabled soft ingestion") } continue } if change.Source != source || change.RecordKey != "record" { t.Fatalf("source lost: %+v", change) } if operation == Delete { if change.Vouch != nil { t.Fatal("delete kept assertion") } continue } if change.Vouch == nil || change.Vouch.Score != domain.SourceScore(source) { t.Fatalf("soft event: %+v", change) } } } data, _ := json.Marshal(map[string]any{"did": author, "time_us": 200, "kind": domain.RecordChange, "commit": map[string]any{"operation": Update, "collection": source, "rkey": "record", "record": atproto.SoftRecord{Type: source, Subject: "invalid", CreatedAt: record.CreatedAt}}}) change, err := client.decode(context.Background(), data, nil) if err != nil || change.Vouch != nil { t.Fatalf("invalid soft update: %+v %v", change, err) } } server.Close() }}
func TestSoftEventScope(t *testing.T) { data, _ := json.Marshal(map[string]any{"did": author, "time_us": 100, "kind": domain.RecordChange, "commit": map[string]any{"operation": Create, "collection": domain.FollowCollection, "rkey": "record", "record": atproto.SoftRecord{Type: domain.FollowCollection, Subject: subject, CreatedAt: "2026-10-09T00:00:00Z"}}}) for _, mode := range []domain.SoftMode{domain.SoftParticipants, domain.SoftAllAuthors} { client, err := NewWithSoft("ws://127.0.0.1:1/subscribe", "https://records.example.com", mode) if err != nil { t.Fatal(err) } change, err := client.decode(context.Background(), data, func(context.Context, string, int64) (bool, error) { return false, nil }) if err != nil || (change.Vouch != nil) != (mode == domain.SoftAllAuthors) { t.Fatalf("scope: %+v %v", change, err) } change, err = client.decode(context.Background(), commit(t, Create, validRecord()), func(context.Context, string, int64) (bool, error) { return false, nil }) if err != nil || change.Vouch != nil { t.Fatal("all-author flag enabled explicit records") } }}
func TestSoftSubscriptions(t *testing.T) { for _, mode := range []domain.SoftMode{domain.SoftDisabled, domain.SoftParticipants, domain.SoftAllAuthors, domain.SoftParticipantLists, domain.SoftAllAuthorLists} { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { collections := r.URL.Query()["wantedCollections"] want := 1 if mode != domain.SoftDisabled { want = 3 } if domain.SoftLists(mode) { want = 6 } if len(collections) != want || collections[0] != domain.Collection { t.Errorf("subscriptions: %v", collections) } if want == 3 && (collections[1] != domain.FollowCollection || collections[2] != domain.BlockCollection) { t.Errorf("soft subscriptions: %v", collections) } if want == 6 && (collections[3] != domain.ListBlockCollection || collections[4] != domain.ListCollection || collections[5] != domain.ListItemCollection) { t.Errorf("list subscriptions: %v", collections) } conn, err := websocket.Accept(w, r, nil) if err != nil { t.Error(err) return } defer conn.CloseNow() conn.Write(r.Context(), websocket.MessageText, commit(t, Create, validRecord())) })) client, err := NewWithSoft("ws"+server.URL[len("http"):], server.URL, mode) if err != nil { t.Fatal(err) } stop := errors.New("complete") if err := client.Consume(context.Background(), 100, nil, func(context.Context, domain.Change) error { return stop }); !errors.Is(err, stop) { t.Fatal(err) } server.Close() }}