naco-api/internal/server/searchactors_test.go

84 lines
2.5 KiB
Go

package server
import (
"testing"
"github.com/nacorid/naco-api/internal/actor"
)
func TestNormalizeHandle(t *testing.T) {
cases := []struct {
in string
want string
}{
{"", ""},
{" ", ""},
{"@alice", "alice"},
{"@", ""},
{"alice@mastodon.social", "alice"},
{"Alice@Mastodon.Social", "alice"},
{"@Alice@mastodon.social", "alice"},
{"alice.bsky.social", "alice.bsky.social"},
{" @bob.X ", "bob.x"},
}
for _, c := range cases {
if got := normalizeHandle(c.in); got != c.want {
t.Errorf("normalizeHandle(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestDedupeActors(t *testing.T) {
in := []*ActorProfile{
{Handle: "@alice", DisplayName: "Alice"},
{Handle: "alice@mastodon.social", DisplayName: "Alice"},
{Handle: "", DisplayName: ""},
{Handle: "bob", DisplayName: "Bob"},
}
// @alice and alice@mastodon.social dedupe by handle; the empty-handle AND
// empty-display-name is appended unconditionally; bob is another person.
out := dedupeActors(in)
if len(out) != 3 {
t.Fatalf("expected 3 deduped results, got %d: %+v", len(out), out)
}
}
func TestDedupeActorsByDisplayNameOnly(t *testing.T) {
// When handles are empty but display names collide, dedupe by display name.
in := []*ActorProfile{
{Handle: "", DisplayName: "Shared"},
{Handle: "", DisplayName: "Shared"},
{Handle: "", DisplayName: "Other"},
}
out := dedupeActors(in)
if len(out) != 2 {
t.Fatalf("expected 2 results after display-name dedup, got %d: %+v", len(out), out)
}
}
func TestSearchActorsTwitterExcluded(t *testing.T) {
// searchActorsAll only queries Bluesky, ActivityPub and Nostr. A Twitter
// result must never appear because the backend is not consulted.
tw := &fakeTwitter{actors: []*actor.Profile{{Platform: "Twitter", Handle: "t"}}, calls: 0}
bsky := &fakeBluesky{actors: []*actor.Profile{{Platform: "Bluesky", Handle: "a"}}}
ap := &fakeAP{actors: []*actor.Profile{{Platform: "ActivityPub", Handle: "b"}}}
nstr := &fakeNostr{actors: []*actor.Profile{{Platform: "Nostr", Handle: "c"}}}
s := newTestServer(bsky, ap, nstr, tw)
res, err := callTool(s, "searchActors", SearchActorsRequest{Query: "q"})
if err != nil {
t.Fatalf("searchActors error: %v", err)
}
var out SearchActorsResponse
if err := jsonUnmarshal(t, textOf(t, res), &out); err != nil {
t.Fatalf("unmarshal: %v", err)
}
for _, r := range out.Results {
if r.Platform == "Twitter" {
t.Fatalf("unexpected Twitter result: %+v", r)
}
}
if tw.calls != 0 {
t.Fatalf("expected Twitter backend untouched, got %d calls", tw.calls)
}
}