GitLabgitlab-org
cli
main
1//go:build !integration
3package mrutils
5import (
6 "bytes"
7 "testing"
8 "time"
10 "github.com/acarl005/stripansi"
11 "github.com/stretchr/testify/assert"
13 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
15 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
16)
18func Test_PrintDiscussions(t *testing.T) {
19 t.Parallel()
21 tests := []struct {
22 name string
23 discussion *gitlab.Discussion
24 options PrintDiscussionsOptions
25 contains string
26 notContains string
27 }{
28 {
29 name: "enabled individual one-note discussion",
30 discussion: &gitlab.Discussion{
31 ID: "abc12345deadbeef1234567890abcdef12345678",
32 IndividualNote: true,
33 Notes: []*gitlab.Note{
34 {ID: 101, Body: "Individual note", Author: gitlab.NoteAuthor{Username: "alice"}},
35 },
36 },
37 options: PrintDiscussionsOptions{ShowSingleNoteDiscussionPrefix: true},
38 contains: "[note #101] [discussion: abc12345…]",
39 },
40 {
41 name: "enabled non-individual one-note discussion",
42 discussion: &gitlab.Discussion{
43 ID: "def67890deadbeef1234567890abcdef12345678",
44 IndividualNote: false,
45 Notes: []*gitlab.Note{
46 {ID: 102, Body: "Single-note thread", Author: gitlab.NoteAuthor{Username: "bob"}},
47 },
48 },
49 options: PrintDiscussionsOptions{ShowSingleNoteDiscussionPrefix: true},
50 contains: "[note #102] [discussion: def67890…]",
51 },
52 {
53 name: "disabled one-note discussion",
54 discussion: &gitlab.Discussion{
55 ID: "fedcba98deadbeef1234567890abcdef12345678",
56 IndividualNote: true,
57 Notes: []*gitlab.Note{
58 {ID: 103, Body: "Hidden prefix", Author: gitlab.NoteAuthor{Username: "carol"}},
59 },
60 },
61 contains: "[note #103]",
62 notContains: "[discussion:",
63 },
64 {
65 name: "system note remains label-free",
66 discussion: &gitlab.Discussion{
67 ID: "system12deadbeef1234567890abcdef12345678",
68 IndividualNote: true,
69 Notes: []*gitlab.Note{
70 {ID: 104, Body: "changed status", System: true, Author: gitlab.NoteAuthor{Username: "bot"}},
71 },
72 },
73 options: PrintDiscussionsOptions{ShowSystemLogs: true, ShowSingleNoteDiscussionPrefix: true},
74 contains: "@bot changed status",
75 notContains: "[discussion:",
76 },
77 {
78 name: "empty ID one-note discussion remains label-free",
79 discussion: &gitlab.Discussion{
80 IndividualNote: true,
81 Notes: []*gitlab.Note{
82 {ID: 105, Body: "No ID", Author: gitlab.NoteAuthor{Username: "dana"}},
83 },
84 },
85 options: PrintDiscussionsOptions{ShowSingleNoteDiscussionPrefix: true},
86 contains: "[note #105]",
87 notContains: "[discussion:",
88 },
89 {
90 name: "non-empty multi-note thread header is unchanged",
91 discussion: &gitlab.Discussion{
92 ID: "abc12345deadbeef1234567890abcdef12345678",
93 IndividualNote: false,
94 Notes: []*gitlab.Note{
95 {ID: 106, Body: "First", Author: gitlab.NoteAuthor{Username: "erin"}},
96 {ID: 107, Body: "Reply", Author: gitlab.NoteAuthor{Username: "frank"}},
97 },
98 },
99 contains: "Thread [discussion: abc12345…]",
100 },
101 {
102 name: "empty ID multi-note thread has no discussion label",
103 discussion: &gitlab.Discussion{
104 IndividualNote: false,
105 Notes: []*gitlab.Note{
106 {ID: 108, Body: "First", Author: gitlab.NoteAuthor{Username: "gina"}},
107 {ID: 109, Body: "Reply", Author: gitlab.NoteAuthor{Username: "hank"}},
108 },
109 },
110 contains: "Thread\n",
111 notContains: "[discussion:",
112 },
113 }
115 for _, tt := range tests {
116 t.Run(tt.name, func(t *testing.T) {
117 t.Parallel()
119 ioStr, _, _, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
120 var buf bytes.Buffer
122 PrintDiscussions(&buf, ioStr, []*gitlab.Discussion{tt.discussion}, tt.options)
124 out := stripansi.Strip(buf.String())
125 assert.Contains(t, out, tt.contains)
126 if tt.notContains != "" {
127 assert.NotContains(t, out, tt.notContains)
128 }
129 })
130 }
131}
133func Test_noteTimeAgo(t *testing.T) {
134 t.Parallel()
135 t.Run("combines relative and absolute time", func(t *testing.T) {
136 t.Parallel()
137 created := time.Now().Add(-24 * time.Hour)
138 got := noteTimeAgo(&gitlab.Note{CreatedAt: &created})
139 expected := "about 1 day ago (" + created.Format("2006-01-02 15:04:05") + ")"
140 assert.Equal(t, expected, got)
141 })
143 t.Run("empty when CreatedAt is nil", func(t *testing.T) {
144 t.Parallel()
145 assert.Empty(t, noteTimeAgo(&gitlab.Note{}))
146 })
147}
149func Test_PrintCommentFileContext(t *testing.T) {
150 // NOTE: we need to force disable colors, otherwise we'd need ANSI sequences in our test output assertions.
151 t.Setenv("NO_COLOR", "true")
153 ioStr, _, _, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(true))
154 c := ioStr.Color()
156 tests := []struct {
157 name string
158 note *gitlab.Note
159 expected string
160 }{
161 {
162 name: "single line comment on new file",
163 note: &gitlab.Note{
164 Position: &gitlab.NotePosition{
165 NewPath: "internal/commands/mr/view/mr_view.go",
166 NewLine: 42,
167 },
168 },
169 expected: " on internal/commands/mr/view/mr_view.go:42\n",
170 },
171 {
172 name: "single line comment on old file",
173 note: &gitlab.Note{
174 Position: &gitlab.NotePosition{
175 OldPath: "internal/commands/mr/view/mr_view.go",
176 OldLine: 35,
177 },
178 },
179 expected: " on internal/commands/mr/view/mr_view.go:35\n",
180 },
181 {
182 name: "multi-line comment",
183 note: &gitlab.Note{
184 Position: &gitlab.NotePosition{
185 NewPath: "internal/gateway/mcp/tools/get_coin_open_interest.go",
186 LineRange: &gitlab.LineRange{
187 StartRange: &gitlab.LinePosition{NewLine: 63},
188 EndRange: &gitlab.LinePosition{NewLine: 68},
189 },
190 },
191 },
192 expected: " on internal/gateway/mcp/tools/get_coin_open_interest.go:63-68\n",
193 },
194 {
195 name: "single line range (same start and end)",
196 note: &gitlab.Note{
197 Position: &gitlab.NotePosition{
198 NewPath: "main.go",
199 LineRange: &gitlab.LineRange{
200 StartRange: &gitlab.LinePosition{NewLine: 10},
201 EndRange: &gitlab.LinePosition{NewLine: 10},
202 },
203 },
204 },
205 expected: " on main.go:10\n",
206 },
207 {
208 name: "file-level comment has a path but no line",
209 note: &gitlab.Note{
210 Position: &gitlab.NotePosition{
211 NewPath: "file.go",
212 NewLine: 0,
213 },
214 },
215 expected: " on file.go\n",
216 },
217 {
218 name: "file-level comment on a deleted file",
219 note: &gitlab.Note{
220 Position: &gitlab.NotePosition{OldPath: "removed.go", PositionType: "file"},
221 },
222 expected: " on removed.go\n",
223 },
224 {
225 name: "empty position",
226 note: &gitlab.Note{Position: &gitlab.NotePosition{}},
227 expected: "",
228 },
229 }
231 for _, tt := range tests {
232 t.Run(tt.name, func(t *testing.T) {
233 var buf bytes.Buffer
234 PrintCommentFileContext(&buf, c, tt.note.Position)
235 got := buf.String()
236 assert.Equal(t, tt.expected, got)
237 })
238 }
239}
241func Test_PrintDraftNotes(t *testing.T) {
242 t.Parallel()
244 ios, _, out, _ := cmdtest.TestIOStreams(cmdtest.WithTestIOStreamsAsTTY(false))
246 PrintDraftNotes(ios, []*gitlab.DraftNote{
247 {ID: 501, Note: "General draft", Position: &gitlab.NotePosition{}},
248 {ID: 502, Note: "Diff draft", Position: &gitlab.NotePosition{NewPath: "main.go", NewLine: 42}},
249 {ID: 503, Note: "Reply draft", DiscussionID: "abcdef1234567890abcdef1234567890abcdef12"},
250 {ID: 504, Note: "Range draft", Position: &gitlab.NotePosition{
251 NewPath: "main.go",
252 NewLine: 12,
253 LineRange: &gitlab.LineRange{
254 StartRange: &gitlab.LinePosition{NewLine: 10},
255 EndRange: &gitlab.LinePosition{NewLine: 12},
256 },
257 }},
258 })
260 assert.Equal(t,
261 "[draft #501]\n General draft\n\n"+
262 "[draft #502] on main.go:42\n Diff draft\n\n"+
263 "[draft #503] (reply to abcdef12…)\n Reply draft\n\n"+
264 "[draft #504] on main.go:10-12\n Range draft\n\n",
265 stripansi.Strip(out.String()),
266 )
267}
Diagnostics8 git calls · 278 ms
CallStatusSizeTime0278 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200746 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=746 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=746 git.pkt.payload.max=719 git.sideband.band1.bytes=719 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=719 git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790Open pack map
fetchtree:0200469 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=266 git.response.bytes=469 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=6020159a68b7314d330ee4ac0f7529b651d7accc git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=469 git.pkt.payload.max=442 git.sideband.band1.bytes=442 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=442 git.pack.trailer=97b1fd715810f2e169ed93a75830c9ee2eff1fc0Open pack map
fetch2002.0 KiB266 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2009 git.response.cache=MISS git.response.duration_ms=266 git.fetch.wants=b5d75c8bead961385d58eaf5d2627d47df93f55e git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2009 git.pkt.payload.max=1982 git.sideband.band1.bytes=1982 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2f6bf5b353a24
- Request
- 83a11189…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format