An unofficial terminal client for Tangled, optimized for humans and agents. tgcli.wisp.place
cli atproto go tangled
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184package tangled
import ( "context" "errors" "reflect" "testing")
func TestListOptsParams(t *testing.T) { tests := []struct { name string opts ListOpts subject string cursor string want map[string]any }{ { name: "defaults", subject: "did:plc:repo", want: map[string]any{"subject": "did:plc:repo", "limit": int64(50)}, }, { name: "all options with cursor", opts: ListOpts{Author: "did:plc:alice", State: "open", Limit: 10, Order: "asc"}, subject: "did:plc:repo", cursor: "cursor1", want: map[string]any{ "subject": "did:plc:repo", "author": "did:plc:alice", "state": "open", "limit": int64(10), "order": "asc", "cursor": "cursor1", }, }, { name: "empty values omitted", opts: ListOpts{State: "closed"}, subject: "did:plc:repo", want: map[string]any{ "subject": "did:plc:repo", "state": "closed", "limit": int64(50), }, }, { name: "maximum results reduces page size", opts: ListOpts{Limit: 100, MaxItems: 20}, subject: "did:plc:repo", want: map[string]any{ "subject": "did:plc:repo", "limit": int64(20), }, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.opts.params(tt.subject, tt.cursor); !reflect.DeepEqual(got, tt.want) { t.Fatalf("got %v, want %v", got, tt.want) } }) }}
func TestFetchAllPages(t *testing.T) { strptr := func(s string) *string { return &s }
t.Run("single page", func(t *testing.T) { calls := 0 items, err := fetchAllPages(context.Background(), 0, func(_ context.Context, cursor string) ([]string, *string, error) { calls++ if cursor != "" { t.Errorf("first fetch got cursor %q, want empty", cursor) } return []string{"a", "b"}, nil, nil }) if err != nil { t.Fatalf("error = %v", err) } if !reflect.DeepEqual(items, []string{"a", "b"}) { t.Fatalf("got %v, want [a b]", items) } if calls != 1 { t.Fatalf("calls = %d, want 1", calls) } })
t.Run("multiple pages", func(t *testing.T) { pages := map[string][]string{ "": {"a", "b"}, "cursor1": {"c"}, "cursor2": {"d"}, } next := map[string]*string{"": strptr("cursor1"), "cursor1": strptr("cursor2"), "cursor2": nil} var gotCursors []string
items, err := fetchAllPages(context.Background(), 0, func(_ context.Context, cursor string) ([]string, *string, error) { gotCursors = append(gotCursors, cursor) pageItems, ok := pages[cursor] if !ok { t.Errorf("unexpected cursor %q", cursor) return nil, nil, nil } return pageItems, next[cursor], nil }) if err != nil { t.Fatalf("error = %v", err) } if !reflect.DeepEqual(items, []string{"a", "b", "c", "d"}) { t.Fatalf("got %v, want [a b c d]", items) } if !reflect.DeepEqual(gotCursors, []string{"", "cursor1", "cursor2"}) { t.Fatalf("cursors = %v, want [ cursor1 cursor2]", gotCursors) } })
t.Run("empty first page", func(t *testing.T) { items, err := fetchAllPages(context.Background(), 0, func(_ context.Context, _ string) ([]string, *string, error) { return nil, nil, nil }) if err != nil { t.Fatalf("error = %v", err) } if len(items) != 0 { t.Fatalf("got %v, want no items", items) } })
t.Run("error propagates", func(t *testing.T) { errFetch := errors.New("fetch failed") items, err := fetchAllPages(context.Background(), 0, func(_ context.Context, cursor string) ([]string, *string, error) { if cursor != "" { return nil, nil, errFetch } return []string{"a"}, strptr("cursor1"), nil }) if !errors.Is(err, errFetch) { t.Fatalf("error = %v, want %v", err, errFetch) } if items != nil { t.Fatalf("got %v, want nil items on error", items) } })
t.Run("runaway cursor", func(t *testing.T) { calls := 0 _, err := fetchAllPages(context.Background(), 0, func(_ context.Context, _ string) ([]string, *string, error) { calls++ return []string{"a"}, strptr("next"), nil }) if err == nil { t.Fatal("expected an error after the cursor repeats") } if calls != 2 { t.Fatalf("calls = %d, want 2", calls) } })
t.Run("maximum items", func(t *testing.T) { pages := map[string][]string{ "": {"a", "b"}, "cursor1": {"c", "d"}, } next := map[string]*string{"": strptr("cursor1"), "cursor1": strptr("cursor2")} var gotCursors []string
items, err := fetchAllPages(context.Background(), 3, func(_ context.Context, cursor string) ([]string, *string, error) { gotCursors = append(gotCursors, cursor) return pages[cursor], next[cursor], nil }) if err != nil { t.Fatalf("error = %v", err) } if !reflect.DeepEqual(items, []string{"a", "b", "c"}) { t.Fatalf("got %v, want [a b c]", items) } if !reflect.DeepEqual(gotCursors, []string{"", "cursor1"}) { t.Fatalf("cursors = %v, want [ cursor1]", gotCursors) } })}