GitLabgitlab-org
cli
main
1//go:build !integration
3package tokenduration
5import (
6 "testing"
7 "time"
8)
10func TestParseDuration(t *testing.T) {
11 tests := []struct {
12 name string
13 input string
14 want time.Duration
15 wantErr bool
16 }{
17 {
18 name: "1 day with d suffix",
19 input: "1d",
20 want: 24 * time.Hour,
21 wantErr: false,
22 },
23 {
24 name: "7 days with d suffix",
25 input: "7d",
26 want: 7 * 24 * time.Hour,
27 wantErr: false,
28 },
29 {
30 name: "30 days with d suffix",
31 input: "30d",
32 want: 30 * 24 * time.Hour,
33 wantErr: false,
34 },
35 {
36 name: "365 days with d suffix",
37 input: "365d",
38 want: 365 * 24 * time.Hour,
39 wantErr: false,
40 },
41 {
42 name: "1 week with w suffix",
43 input: "1w",
44 want: 7 * 24 * time.Hour,
45 wantErr: false,
46 },
47 {
48 name: "2 weeks with w suffix",
49 input: "2w",
50 want: 14 * 24 * time.Hour,
51 wantErr: false,
52 },
53 {
54 name: "52 weeks with w suffix",
55 input: "52w",
56 want: 52 * 7 * 24 * time.Hour,
57 wantErr: false,
58 },
59 {
60 name: "24 hours with h suffix (backward compat)",
61 input: "24h",
62 want: 24 * time.Hour,
63 wantErr: false,
64 },
65 {
66 name: "168 hours with h suffix (1 week)",
67 input: "168h",
68 want: 168 * time.Hour,
69 wantErr: false,
70 },
71 {
72 name: "720 hours with h suffix (30 days)",
73 input: "720h",
74 want: 720 * time.Hour,
75 wantErr: false,
76 },
77 {
78 name: "invalid: just a number",
79 input: "30",
80 want: 0,
81 wantErr: true,
82 },
83 {
84 name: "invalid: fractional days",
85 input: "1.5d",
86 want: 0,
87 wantErr: true,
88 },
89 {
90 name: "invalid: unsupported unit",
91 input: "1y",
92 want: 0,
93 wantErr: true,
94 },
95 {
96 name: "invalid: month suffix",
97 input: "1M",
98 want: 0,
99 wantErr: true,
100 },
101 {
102 name: "invalid: empty string",
103 input: "",
104 want: 0,
105 wantErr: true,
106 },
107 {
108 name: "invalid: negative duration",
109 input: "-1d",
110 want: 0,
111 wantErr: true,
112 },
113 {
114 name: "invalid: not multiple of 24h",
115 input: "33h",
116 want: 0,
117 wantErr: true,
118 },
119 {
120 name: "invalid: 0 days",
121 input: "0d",
122 want: 0,
123 wantErr: true,
124 },
125 {
126 name: "invalid: over 365 days",
127 input: "400d",
128 want: 0,
129 wantErr: true,
130 },
131 {
132 name: "invalid: value exceeds 365-day limit",
133 input: "281474976710657d",
134 want: 0,
135 wantErr: true,
136 },
137 {
138 name: "invalid: 366 days",
139 input: "366d",
140 want: 0,
141 wantErr: true,
142 },
143 }
145 for _, tt := range tests {
146 t.Run(tt.name, func(t *testing.T) {
147 got, err := ParseDuration(tt.input)
148 if (err != nil) != tt.wantErr {
149 t.Errorf("ParseDuration(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
150 return
151 }
152 if !tt.wantErr && time.Duration(got) != tt.want {
153 t.Errorf("ParseDuration(%q) = %v, want %v", tt.input, time.Duration(got), tt.want)
154 }
155 })
156 }
157}
159func TestTokenDuration_String(t *testing.T) {
160 tests := []struct {
161 name string
162 duration TokenDuration
163 want string
164 }{
165 {
166 name: "1 day",
167 duration: TokenDuration(24 * time.Hour),
168 want: "1d",
169 },
170 {
171 name: "7 days (1 week)",
172 duration: TokenDuration(7 * 24 * time.Hour),
173 want: "1w",
174 },
175 {
176 name: "14 days (2 weeks)",
177 duration: TokenDuration(14 * 24 * time.Hour),
178 want: "2w",
179 },
180 {
181 name: "30 days",
182 duration: TokenDuration(30 * 24 * time.Hour),
183 want: "30d",
184 },
185 {
186 name: "365 days (52 weeks + 1 day, should display as days)",
187 duration: TokenDuration(365 * 24 * time.Hour),
188 want: "365d",
189 },
190 {
191 name: "12 hours",
192 duration: TokenDuration(12 * time.Hour),
193 want: "12h0m0s",
194 },
195 }
197 for _, tt := range tests {
198 t.Run(tt.name, func(t *testing.T) {
199 got := tt.duration.String()
200 if got != tt.want {
201 t.Errorf("TokenDuration.String() = %v, want %v", got, tt.want)
202 }
203 })
204 }
205}
207func TestTokenDuration_Set(t *testing.T) {
208 tests := []struct {
209 name string
210 value string
211 want TokenDuration
212 wantErr bool
213 }{
214 {
215 name: "set 30d",
216 value: "30d",
217 want: TokenDuration(30 * 24 * time.Hour),
218 wantErr: false,
219 },
220 {
221 name: "set 2w",
222 value: "2w",
223 want: TokenDuration(14 * 24 * time.Hour),
224 wantErr: false,
225 },
226 {
227 name: "set 24h",
228 value: "24h",
229 want: TokenDuration(24 * time.Hour),
230 wantErr: false,
231 },
232 {
233 name: "set invalid",
234 value: "invalid",
235 want: TokenDuration(0),
236 wantErr: true,
237 },
238 }
240 for _, tt := range tests {
241 t.Run(tt.name, func(t *testing.T) {
242 var d TokenDuration
243 err := d.Set(tt.value)
244 if (err != nil) != tt.wantErr {
245 t.Errorf("TokenDuration.Set(%q) error = %v, wantErr %v", tt.value, err, tt.wantErr)
246 return
247 }
248 if !tt.wantErr && d != tt.want {
249 t.Errorf("TokenDuration.Set(%q) = %v, want %v", tt.value, d, tt.want)
250 }
251 })
252 }
253}
255func TestTokenDuration_Type(t *testing.T) {
256 var d TokenDuration
257 if got := d.Type(); got != "duration" {
258 t.Errorf("TokenDuration.Type() = %v, want %v", got, "duration")
259 }
260}
262func TestTokenDuration_Duration(t *testing.T) {
263 td := TokenDuration(30 * 24 * time.Hour)
264 want := 30 * 24 * time.Hour
265 if got := td.Duration(); got != want {
266 t.Errorf("TokenDuration.Duration() = %v, want %v", got, want)
267 }
268}
270func TestTokenDuration_CalculateExpirationDate(t *testing.T) {
271 tests := []struct {
272 name string
273 duration TokenDuration
274 wantDays int // Number of days from today
275 }{
276 {
277 name: "1 day",
278 duration: TokenDuration(24 * time.Hour),
279 wantDays: 1,
280 },
281 {
282 name: "7 days",
283 duration: TokenDuration(7 * 24 * time.Hour),
284 wantDays: 7,
285 },
286 {
287 name: "30 days",
288 duration: TokenDuration(30 * 24 * time.Hour),
289 wantDays: 30,
290 },
291 }
293 for _, tt := range tests {
294 t.Run(tt.name, func(t *testing.T) {
295 got := tt.duration.CalculateExpirationDate()
297 // Verify it's at midnight UTC
298 if got.Hour() != 0 || got.Minute() != 0 || got.Second() != 0 {
299 t.Errorf("CalculateExpirationDate() not at midnight UTC: %v", got)
300 }
302 // Verify it's in UTC
303 if got.Location() != time.UTC {
304 t.Errorf("CalculateExpirationDate() not in UTC: %v", got.Location())
305 }
307 // Verify it's the correct number of days from today
308 now := time.Now()
309 today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
310 expected := today.AddDate(0, 0, tt.wantDays)
312 if !got.Equal(expected) {
313 t.Errorf("CalculateExpirationDate() = %v, want %v", got, expected)
314 }
315 })
316 }
317}
Diagnostics7 git calls · 348 ms
CallStatusSizeTime0348 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=4 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
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=2 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: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
fetchtree:02001.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=2 git.response.bytes=1238 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c10f4b8b760c3fec7ed8ca85e428d5bc523f8e17 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=1238 git.pkt.payload.max=1211 git.sideband.band1.bytes=1211 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1211 git.pack.trailer=7373aa350f2a611db7d4116eb1450c92f18a27caOpen pack map
fetchtree:0200145 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=145 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=b006b6b79422a89e770cae04101ddbce13384b2d 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=145 git.pkt.payload.max=118 git.sideband.band1.bytes=118 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=118 git.pack.trailer=384d4bed6dc05c4fcd238270ac20de656bfa0f7eOpen pack map
fetch2001.4 KiB337 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=337 git.response.bytes=1398 git.response.cache=MISS git.response.duration_ms=337 git.fetch.wants=28ad87a3681fe97ef4c585ff21f5bb626673d12f 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=1398 git.pkt.payload.max=1371 git.sideband.band1.bytes=1371 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
- a5e297103e672407
- Request
- f026d2a9…
- 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