GitLabgitlab-org

cli

main
ply8 git calls66 ms99 KiB7 cachedprotocol v2
217 lines6.2 KiBRaw
1//go:build !integration
3package ask
5import (
6 "net/http"
7 "net/http/httptest"
8 "testing"
10 "git.sr.ht/~timofurrer/ugh"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 "gitlab.com/gitlab-org/cli/internal/api"
17 "gitlab.com/gitlab-org/cli/internal/glinstance"
18 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
19 "gitlab.com/gitlab-org/cli/test"
22func TestAskCmd(t *testing.T) {
23 t.Skip("flaky, hits 10m timeout")
25 initialAiResponse := "The appropriate ```git log --pretty=format:'%h'``` Git command ```non-git cmd``` for listing ```git show``` commit SHAs."
26 outputWithoutExecution := "Commands:\n" + `
27git log --pretty=format:'%h'
28non-git cmd
29git show
31Explanation:
33The appropriate git log --pretty=format:'%h' Git command non-git cmd for listing git show commit SHAs.
37 tests := []struct {
38 desc string
39 content string
40 withPrompt bool
41 withExecution bool
42 withGlInstanceHostname string
43 expectedResult []string
44 expectedGlInstanceHostname string
45 }{
46 {
47 desc: "agree to run commands",
48 content: initialAiResponse,
49 withGlInstanceHostname: "",
50 withPrompt: true,
51 withExecution: true,
52 expectedResult: []string{outputWithoutExecution, "git log executed", "git show executed"},
53 expectedGlInstanceHostname: glinstance.DefaultHostname,
54 },
55 {
56 desc: "disagree to run commands",
57 content: initialAiResponse,
58 withGlInstanceHostname: "example.com",
59 withPrompt: true,
60 withExecution: false,
61 expectedResult: []string{outputWithoutExecution},
62 expectedGlInstanceHostname: "example.com",
63 },
64 {
65 desc: "no commands",
66 content: "There are no Git commands related to the text.",
67 withGlInstanceHostname: "instance.example.com",
68 withPrompt: false,
69 withExecution: false,
70 expectedResult: []string{"Commands:\n\n\nExplanation:\n\nThere are no Git commands related to the text.\n\n"},
71 expectedGlInstanceHostname: "instance.example.com",
72 },
73 }
74 cmdLogResult := "git log executed"
75 cmdShowResult := "git show executed"
77 for _, tc := range tests {
78 t.Run(tc.desc, func(t *testing.T) {
79 body := `{"predictions": [{ "candidates": [ {"content": "` + tc.content + `"} ]}]}`
81 // Create test server for the AI endpoint
82 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
83 if r.Method == http.MethodPost && r.URL.Path == "/api/v4/ai/llm/git_command" {
84 w.WriteHeader(http.StatusOK)
85 _, _ = w.Write([]byte(body))
86 return
87 }
88 w.WriteHeader(http.StatusNotFound)
89 }))
90 defer testServer.Close()
92 if tc.withPrompt {
93 cs, restore := test.InitCmdStubber()
94 defer restore()
95 cs.Stub(cmdLogResult)
96 cs.Stub(cmdShowResult)
97 }
99 // Create a GitLab client with the test server's URL
100 gitlabClient, err := gitlab.NewClient("test-token", gitlab.WithBaseURL(testServer.URL+"/api/v4"))
101 require.NoError(t, err)
103 apiClient, err := api.NewClient(
104 func(*http.Client) (gitlab.AuthSource, error) {
105 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
106 },
107 api.WithGitLabClient(gitlabClient),
108 )
109 require.NoError(t, err)
111 opts := []cmdtest.FactoryOption{
112 func(f *cmdtest.Factory) {
113 f.ApiClientStub = func(repoHost string) (*api.Client, error) {
114 require.Equal(t, tc.expectedGlInstanceHostname, repoHost)
115 return apiClient, nil
116 }
117 },
118 cmdtest.WithBaseRepo("OWNER", "REPO", tc.withGlInstanceHostname),
119 }
121 // Set up prompt stub if needed
122 if tc.withPrompt {
123 c := ugh.New(t)
124 if !tc.withExecution {
125 c.Expect(ugh.Confirm(runCmdsQuestion)).
126 Do(ugh.Reject)
127 } else {
128 c.Expect(ugh.Confirm(runCmdsQuestion)).
129 Do(ugh.Affirm).
130 Expect(ugh.ConfirmRegexp(`Run ` + "`.*?`")).
131 Do(ugh.Affirm).
132 Expect(ugh.ConfirmRegexp(`Run ` + "`.*?`")).
133 Do(ugh.Affirm)
134 }
135 opts = append(opts, cmdtest.WithConsole(t, c))
136 }
138 exec := cmdtest.SetupCmdForTest(t, NewCmdAsk, false, opts...)
140 output, err := exec("git list 10 commits")
141 require.NoError(t, err)
143 stdout := output.String()
144 for _, r := range tc.expectedResult {
145 assert.Contains(t, stdout, r)
146 }
147 require.Empty(t, output.Stderr())
148 })
149 }
152func TestFailedHttpResponse(t *testing.T) {
153 t.Parallel()
155 tests := []struct {
156 desc string
157 code int
158 response string
159 expectedMsg string
160 }{
161 {
162 desc: "API error",
163 code: http.StatusNotFound,
164 response: `{"message": "Error message"}`,
165 expectedMsg: "404 Not Found",
166 },
167 {
168 desc: "Empty response",
169 code: http.StatusOK,
170 response: `{"choices": []}`,
171 expectedMsg: aiResponseErr,
172 },
173 {
174 desc: "Bad JSON",
175 code: http.StatusOK,
176 response: `{"choices": [{"message": {"content": "hello"}}]}`,
177 expectedMsg: aiResponseErr,
178 },
179 }
181 for _, tc := range tests {
182 t.Run(tc.desc, func(t *testing.T) {
183 t.Parallel()
185 // Create test server for the AI endpoint
186 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
187 if r.Method == http.MethodPost && r.URL.Path == "/api/v4/ai/llm/git_command" {
188 w.WriteHeader(tc.code)
189 _, _ = w.Write([]byte(tc.response))
190 return
191 }
192 w.WriteHeader(http.StatusNotFound)
193 }))
194 defer testServer.Close()
196 // Create a GitLab client with the test server's URL
197 gitlabClient, err := gitlab.NewClient("test-token", gitlab.WithBaseURL(testServer.URL+"/api/v4"))
198 require.NoError(t, err)
200 apiClient, err := api.NewClient(
201 func(*http.Client) (gitlab.AuthSource, error) {
202 return gitlab.AccessTokenAuthSource{Token: "test-token"}, nil
203 },
204 api.WithGitLabClient(gitlabClient),
205 )
206 require.NoError(t, err)
208 exec := cmdtest.SetupCmdForTest(t, NewCmdAsk, false,
209 cmdtest.WithApiClient(apiClient),
210 cmdtest.WithBaseRepo("OWNER", "REPO", ""),
211 )
213 _, err = exec("git list 10 commits")
214 require.EqualError(t, err, tc.expectedMsg)
215 })
216 }
Diagnostics8 git calls · 66 ms
CallStatusSizeTime066 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 KiB · hit2 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=HIT
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 · hit3 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=32926
git.response.cache=HIT
git.response.duration_ms=3
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 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=3
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=3
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200191 B · hit3 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=191
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c3602e112f33dfaff92a8559e2993279d5c2f1ac
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=191
git.pkt.payload.max=164
git.sideband.band1.bytes=164
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=164
git.pack.trailer=43874cd66e8aba9ccffcac699b3b9f515e08cf67
Open pack map
fetchtree:0200172 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=172
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=97a3b7ac4cdd1f4e2351ca134b012a56159b11b8
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=172
git.pkt.payload.max=145
git.sideband.band1.bytes=145
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=145
git.pack.trailer=2fbaff8815b3fcfce9ac505b9b02062f6ca8a3da
Open pack map
fetch2001.8 KiB · hit51 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=51
git.response.bytes=1879
git.response.cache=HIT
git.response.duration_ms=51
git.fetch.wants=b9c72f55e8123c07144eef9a8a087bfdcd856c65
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=1879
git.pkt.payload.max=1852
git.sideband.band1.bytes=1852
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
a5e2ca861d4f4b67
Request
05144d9d…
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