package site import ( "context" "net/http" "net/http/httptest" "strings" "testing" ) func TestReadFindsLinksAndFeeds(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } w.Header().Set("Content-Type", "text/html") w.Write([]byte(`Rival About X Mail`)) }) mux.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") w.Write([]byte(` Them`)) }) srv := httptest.NewServer(mux) defer srv.Close() s, err := Read(context.Background(), srv.URL, "test") if err != nil { t.Fatal(err) } if len(s.Pages) != 2 || !strings.Contains(s.PromptText(), "Title: Rival") { t.Errorf("pages: %+v", s.Pages) } if strings.Join(s.Feeds, " ") != srv.URL+"/feed.xml https://rival.example/atom" { t.Errorf("feeds: %q", s.Feeds) } for _, l := range s.Links { if strings.HasPrefix(l, "mailto:") { t.Errorf("mailto link kept: %s", l) } } // The home page's own account comes first. if a := FindAccounts(s.Links); a.X != "rivalhq" { t.Errorf("x = %q (links %q)", a.X, s.Links) } } func TestFindAccounts(t *testing.T) { a := FindAccounts([]string{ "https://twitter.com/intent/tweet?text=hi", "https://twitter.com/share", "https://x.com/acme", "https://www.linkedin.com/company/acme-inc/", "https://www.youtube.com/channel/UCabcdefghijklmnopqrstuv", "https://www.instagram.com/p/xyz", "https://instagram.com/acme.co", "https://www.reddit.com/r/selfhosted/", "https://reddit.com/r/SelfHosted", "https://old.reddit.com/r/homelab", }) if a.X != "acme" || a.LinkedIn != "https://www.linkedin.com/company/acme-inc" || a.YouTubeChannel != "UCabcdefghijklmnopqrstuv" || a.Instagram != "acme.co" || strings.Join(a.Subreddits, ",") != "selfhosted,homelab" { t.Errorf("accounts: %+v", a) } } func TestNormalize(t *testing.T) { if u, err := Normalize(" acme.example "); err != nil || u.String() != "https://acme.example" { t.Errorf("got %v %v", u, err) } if _, err := Normalize("ftp://acme.example"); err == nil { t.Error("ftp accepted") } } func TestPageInfo(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") w.Write([]byte(`Widget 2 & more`)) })) defer srv.Close() title, desc, err := PageInfo(context.Background(), srv.URL+"/w", "test") if err != nil || title != "Widget 2 & more" || desc != "Faster widgets." { t.Errorf("got %q %q %v", title, desc, err) } }