internal/store/pending_test.go

32a5f76e5b270097b63a5bba9a43557cf50ad63d
gitbay/internal/store/pending_test.go history · blame · raw

120 lines · 3552 bytes

  1package store
  2
  3import "testing"
  4
  5func pendingFixture(t *testing.T) (*Store, int64, int64, int64) {
  6	t.Helper()
  7	s := open(t)
  8	if err := s.MigrateUp(); err != nil {
  9		t.Fatal(err)
 10	}
 11	author, err := s.CreateUser("cmc", true)
 12	if err != nil {
 13		t.Fatal(err)
 14	}
 15	other, err := s.CreateUser("kim", false)
 16	if err != nil {
 17		t.Fatal(err)
 18	}
 19	repoID, err := s.CreateRepo("user", author, "lib", "public")
 20	if err != nil {
 21		t.Fatal(err)
 22	}
 23	n, err := s.CreateMR(repoID, author, repoID, "feature", "main", "t", "", "abc123", "md", false)
 24	if err != nil {
 25		t.Fatal(err)
 26	}
 27	mr, err := s.MRByNumber(repoID, n)
 28	if err != nil {
 29		t.Fatal(err)
 30	}
 31	return s, mr.ID, author, other
 32}
 33
 34// A pending comment belongs to the reviewer composing it and to nobody
 35// else, until they submit.
 36func TestPendingCommentsArePrivate(t *testing.T) {
 37	s, mrID, author, other := pendingFixture(t)
 38	if _, err := s.AddDiffComment(mrID, other, "abc123", "a.go", "new", 3, "half a thought", 0, true); err != nil {
 39		t.Fatal(err)
 40	}
 41	if _, err := s.AddDiffComment(mrID, other, "abc123", "a.go", "new", 9, "said out loud", 0, false); err != nil {
 42		t.Fatal(err)
 43	}
 44
 45	mine, err := s.ListDiffComments(mrID, other)
 46	if err != nil {
 47		t.Fatal(err)
 48	}
 49	if len(mine) != 2 {
 50		t.Fatalf("author of the pending comment sees %d, want both", len(mine))
 51	}
 52	theirs, _ := s.ListDiffComments(mrID, author)
 53	if len(theirs) != 1 || theirs[0].Body != "said out loud" {
 54		t.Fatalf("someone else sees %+v", theirs)
 55	}
 56	anon, _ := s.ListDiffComments(mrID, 0)
 57	if len(anon) != 1 {
 58		t.Fatalf("anonymous reader sees %d, want the published one only", len(anon))
 59	}
 60	if n := s.CountPendingComments(mrID, other); n != 1 {
 61		t.Fatalf("pending count = %d", n)
 62	}
 63}
 64
 65// An unsubmitted thread must not gate a merge: nobody else can see it,
 66// so nobody else could resolve it.
 67func TestPendingThreadsDoNotBlockMerges(t *testing.T) {
 68	s, mrID, _, other := pendingFixture(t)
 69	if _, err := s.AddDiffComment(mrID, other, "abc123", "a.go", "new", 3, "pending", 0, true); err != nil {
 70		t.Fatal(err)
 71	}
 72	n, err := s.UnresolvedThreadCount(mrID)
 73	if err != nil {
 74		t.Fatal(err)
 75	}
 76	if n != 0 {
 77		t.Fatalf("pending thread counted against the merge gate (%d)", n)
 78	}
 79
 80	if _, err := s.PublishPendingComments(mrID, other); err != nil {
 81		t.Fatal(err)
 82	}
 83	if n, _ := s.UnresolvedThreadCount(mrID); n != 1 {
 84		t.Fatalf("published thread does not gate (%d)", n)
 85	}
 86}
 87
 88func TestPublishAndDiscardPending(t *testing.T) {
 89	s, mrID, author, other := pendingFixture(t)
 90	for i := 0; i < 3; i++ {
 91		if _, err := s.AddDiffComment(mrID, other, "abc123", "a.go", "new", int64(i+1), "note", 0, true); err != nil {
 92			t.Fatal(err)
 93		}
 94	}
 95	// Another reviewer's batch is untouched by either operation.
 96	if _, err := s.AddDiffComment(mrID, author, "abc123", "b.go", "new", 1, "mine", 0, true); err != nil {
 97		t.Fatal(err)
 98	}
 99
100	n, err := s.PublishPendingComments(mrID, other)
101	if err != nil || n != 3 {
102		t.Fatalf("published %d (%v), want 3", n, err)
103	}
104	if got := s.CountPendingComments(mrID, author); got != 1 {
105		t.Fatalf("the other reviewer's batch was published too (%d left)", got)
106	}
107	// Publishing again is a no-op, not a double publish.
108	if n, _ := s.PublishPendingComments(mrID, other); n != 0 {
109		t.Fatalf("second publish moved %d rows", n)
110	}
111
112	// Discard removes only what is still pending.
113	if n, _ := s.DiscardPendingComments(mrID, author); n != 1 {
114		t.Fatalf("discarded %d, want 1", n)
115	}
116	all, _ := s.ListDiffComments(mrID, other)
117	if len(all) != 3 {
118		t.Fatalf("discard took published comments with it: %d remain", len(all))
119	}
120}