app/comments_test.go
93 lines · 2811 bytes
1package app
2
3import (
4 "net/http/httptest"
5 "net/url"
6 "strings"
7 "testing"
8
9 "github.com/krazywarez/devianter"
10)
11
12// withCommentSeams scripts the deviation and comment fetches and counts the
13// comment fetches, which is the call the link is meant to save.
14func withCommentSeams(t *testing.T, total int) *int {
15 t.Helper()
16 origDev, origCom := fetchDeviation, fetchComments
17 comments := 0
18 fetchDeviation = func(string, string) (devianter.Post, devianter.Error) {
19 var p devianter.Post
20 p.Deviation.Title = "T"
21 p.Deviation.Author.Username = "alice"
22 p.Comments.Total = total
23 return p, devianter.Error{}
24 }
25 fetchComments = func(string, string, int, int) (devianter.Comments, devianter.Error) {
26 comments++
27 return devianter.Comments{Total: total}, devianter.Error{}
28 }
29 t.Cleanup(func() { fetchDeviation, fetchComments = origDev, origCom })
30 return &comments
31}
32
33func post(args url.Values) *httptest.ResponseRecorder {
34 loadTemplates()
35 rec := httptest.NewRecorder()
36 s := skunkyart{Writer: rec, Host: "http://localhost", BasePath: "/", Args: args, _pth: "/post/alice/t-1"}
37 s.Deviation("alice", "t-1")
38 return rec
39}
40
41func TestPostShowsACommentsLinkWithoutFetching(t *testing.T) {
42 nsfw := CFG.Nsfw
43 CFG.Nsfw = true
44 defer func() { CFG.Nsfw = nsfw }()
45 fetches := withCommentSeams(t, 7)
46
47 body := post(url.Values{}).Body.String()
48
49 if *fetches != 0 {
50 t.Errorf("comments fetched %d times on a plain post view, want 0", *fetches)
51 }
52 if !strings.Contains(body, `href="/post/alice/t-1?comments=1"`) || !strings.Contains(body, "Comments (7)") {
53 t.Errorf("post lacks the comments link with its count:\n%s", body)
54 }
55}
56
57func TestPostFetchesCommentsWhenAsked(t *testing.T) {
58 nsfw := CFG.Nsfw
59 CFG.Nsfw = true
60 defer func() { CFG.Nsfw = nsfw }()
61 fetches := withCommentSeams(t, 7)
62
63 body := post(url.Values{"comments": {"1"}}).Body.String()
64
65 if *fetches != 1 {
66 t.Errorf("comments fetched %d times with ?comments=1, want 1", *fetches)
67 }
68 if !strings.Contains(body, "<details><summary>Comments: <b>7</b>") {
69 t.Errorf("thread not rendered:\n%s", body)
70 }
71}
72
73func TestNavBaseKeepsTheCommentsParameter(t *testing.T) {
74 s := skunkyart{_pth: "/post/alice/t-1", Args: url.Values{"comments": {"1"}}, Page: 1}
75 out := s.NavBase(DeviationList{More: true})
76 if !strings.Contains(out, "?p=2&comments=1") {
77 t.Errorf("next link drops comments=1:\n%s", out)
78 }
79}
80
81func TestGroupSearchURLPagesByTen(t *testing.T) {
82 cases := map[int]string{
83 0: "https://www.deviantart.com/groups/?q=cats",
84 1: "https://www.deviantart.com/groups/?q=cats",
85 2: "https://www.deviantart.com/groups/?q=cats&offset=10",
86 3: "https://www.deviantart.com/groups/?q=cats&offset=20",
87 }
88 for page, want := range cases {
89 if got := groupSearchURL("cats", page); got != want {
90 t.Errorf("page %d: %s, want %s", page, got, want)
91 }
92 }
93}