e2e/diffcomment_test.go
132 lines · 5460 bytes
1package e2e
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12func TestDiffComments(t *testing.T) {
13 inst := startInstance(t)
14 aliceKey := inst.newKey(t, "alice")
15 bobKey := inst.newKey(t, "bob")
16 eveKey := inst.newKey(t, "eve")
17 inst.admin(t, "admin", "user", "create", "alice",
18 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
19 inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
20 inst.admin(t, "admin", "user", "create", "eve", "--key", eveKey+".pub")
21
22 // MR with a real multi-line diff.
23 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/lib"); code != 0 {
24 t.Fatalf("repo create: %s", errOut)
25 }
26 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "access", "grant", "alice/lib", "bob", "write"); code != 0 {
27 t.Fatal("grant failed")
28 }
29 work := t.TempDir()
30 env := inst.gitEnv(aliceKey)
31 mustGit(t, work, env, "clone", inst.sshURL("alice/lib"), "w")
32 dir := filepath.Join(work, "w")
33 os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n\nfunc main() {\n}\n"), 0o644)
34 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
35 mustGit(t, dir, env, "add", ".")
36 mustGit(t, dir, env, "commit", "-q", "-m", "base")
37 mustGit(t, dir, env, "push", "-q", "origin", "main")
38 mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
39 os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"hi\")\n}\n"), 0o644)
40 mustGit(t, dir, env, "add", ".")
41 mustGit(t, dir, env, "commit", "-q", "-m", "add greeting")
42 mustGit(t, dir, env, "push", "-q", "origin", "feat")
43 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/lib",
44 "--source", "feat", "--target", "main", "--title", "'greeting'"); code != 0 {
45 t.Fatalf("mr create: %s", errOut)
46 }
47
48 // A thread on a real diff line; a path outside the diff is refused.
49 if _, errOut, code := inst.ssh(t, bobKey, "", "mr", "diff-comment", "alice/lib", "1",
50 "--path", "nope.go", "--line", "1", "--message", "'x'"); code != 2 || !strings.Contains(errOut, "not part of") {
51 t.Fatalf("off-diff path: exit %d, %s", code, errOut)
52 }
53 out, errOut, code := inst.ssh(t, bobKey, "", "mr", "diff-comment", "alice/lib", "1",
54 "--path", "main.go", "--line", "6", "--message", "'use log instead of fmt'", "--json")
55 if code != 0 {
56 t.Fatalf("diff-comment: %s", errOut)
57 }
58 var env2 struct {
59 Data struct {
60 Thread int64 `json:"thread"`
61 } `json:"data"`
62 }
63 json.Unmarshal([]byte(out), &env2)
64 thread := env2.Data.Thread
65
66 // Reply joins the thread; replying to a reply is refused.
67 out, _, code = inst.ssh(t, aliceKey, "", "mr", "diff-comment", "alice/lib", "1",
68 "--reply", fmt.Sprint(thread), "--message", "'will do'", "--json")
69 if code != 0 {
70 t.Fatalf("reply failed: %s", out)
71 }
72 var env3 struct {
73 Data struct {
74 ID int64 `json:"id"`
75 } `json:"data"`
76 }
77 json.Unmarshal([]byte(out), &env3)
78 if _, errOut, code = inst.ssh(t, bobKey, "", "mr", "diff-comment", "alice/lib", "1",
79 "--reply", fmt.Sprint(env3.Data.ID), "--message", "'nested'"); code != 2 || !strings.Contains(errOut, "thread root") {
80 t.Fatalf("nested reply: exit %d, %s", code, errOut)
81 }
82
83 // Threads listing shows the thread, both comments, fresh.
84 out, _, _ = inst.ssh(t, aliceKey, "", "mr", "threads", "alice/lib", "1", "--json")
85 if !strings.Contains(out, `"path":"main.go"`) || !strings.Contains(out, `"line":6`) ||
86 !strings.Contains(out, "will do") || strings.Contains(out, `"stale":true`) {
87 t.Fatalf("threads: %s", out)
88 }
89
90 // mr show counts the unresolved thread; the web renders it inline.
91 out, _, _ = inst.ssh(t, aliceKey, "", "mr", "show", "alice/lib", "1", "--json")
92 if !strings.Contains(out, `"unresolved_threads":1`) {
93 t.Fatalf("mr show count: %s", out)
94 }
95 status, body := inst.get(t, "/alice/lib/mrs/1")
96 if status != 200 || !strings.Contains(body, "use log instead of fmt") ||
97 !strings.Contains(body, `class="thread`) {
98 t.Fatalf("web thread: %d", status)
99 }
100 if strings.Contains(body, "threads on earlier revisions") {
101 t.Fatal("fresh thread rendered as detached")
102 }
103
104 // Resolution: eve (read-only outsider) cannot; the thread author can;
105 // count drops; unresolve restores it.
106 if _, _, code = inst.ssh(t, eveKey, "", "mr", "resolve", "alice/lib", "1", fmt.Sprint(thread)); code != 4 {
107 t.Fatal("outsider resolved a thread")
108 }
109 if _, errOut, code = inst.ssh(t, bobKey, "", "mr", "resolve", "alice/lib", "1", fmt.Sprint(thread)); code != 0 {
110 t.Fatalf("resolve: %s", errOut)
111 }
112 out, _, _ = inst.ssh(t, aliceKey, "", "mr", "show", "alice/lib", "1", "--json")
113 if strings.Contains(out, `"unresolved_threads"`) {
114 t.Fatalf("resolved thread still counted: %s", out)
115 }
116 if _, _, code = inst.ssh(t, bobKey, "", "mr", "unresolve", "alice/lib", "1", fmt.Sprint(thread)); code != 0 {
117 t.Fatal("unresolve failed")
118 }
119
120 // Force-push moves the head: the thread goes stale and the web moves it
121 // to the earlier-revisions section.
122 mustGit(t, dir, env, "commit", "-q", "--amend", "-m", "add greeting (amended)")
123 mustGit(t, dir, env, "push", "-q", "--force", "origin", "feat")
124 out, _, _ = inst.ssh(t, aliceKey, "", "mr", "threads", "alice/lib", "1", "--json")
125 if !strings.Contains(out, `"stale":true`) {
126 t.Fatalf("thread not stale after force-push: %s", out)
127 }
128 _, body = inst.get(t, "/alice/lib/mrs/1")
129 if !strings.Contains(body, "threads on earlier revisions") {
130 t.Fatal("stale thread not moved to detached section")
131 }
132}