Skip to content

Commit 69b05ba

Browse files
Replace DelayRequestByMs with SleepAfterMs
1 parent cb25859 commit 69b05ba

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

checks/checks.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ func runHTTPRequest(
8787
req.SetBasicAuth(requestStep.Request.BasicAuth.Username, requestStep.Request.BasicAuth.Password)
8888
}
8989

90-
if requestStep.Request.Actions.DelayRequestByMs != nil {
91-
time.Sleep(time.Duration(*requestStep.Request.Actions.DelayRequestByMs) * time.Millisecond)
92-
}
93-
9490
resp, err := client.Do(req)
9591
if err != nil {
9692
errString := fmt.Sprintf("Failed to fetch: %s", err.Error())
@@ -169,11 +165,13 @@ func CLIChecks(cliData api.CLIData, overrideBaseURL string, ch chan tea.Msg) (re
169165
results[i].CLICommandResult = &result
170166

171167
sendCLICommandResults(ch, *step.CLICommand, result, i)
168+
handleSleep(step.CLICommand, ch)
172169

173170
case step.HTTPRequest != nil:
174171
result := runHTTPRequest(client, baseURL, variables, *step.HTTPRequest)
175172
results[i].HTTPRequestResult = &result
176173
sendHTTPRequestResults(ch, *step.HTTPRequest, result, i)
174+
handleSleep(step.HTTPRequest, ch)
177175

178176
default:
179177
cobra.CheckErr("unable to run lesson: missing step")
@@ -415,3 +413,11 @@ func InterpolateVariables(template string, vars map[string]string) string {
415413
return m // return the original placeholder if no substitution found
416414
})
417415
}
416+
417+
func handleSleep(s api.Sleepable, ch chan tea.Msg) {
418+
sleepMs := s.GetSleepAfterMs()
419+
if sleepMs != nil && *sleepMs > 0 {
420+
ch <- messages.SleepMsg{DurationMs: *sleepMs}
421+
time.Sleep(time.Duration(*sleepMs) * time.Millisecond)
422+
}
423+
}

client/lessons.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ type CLIStep struct {
3232
}
3333

3434
type CLIStepCLICommand struct {
35-
Command string
36-
Tests []CLICommandTest
35+
Command string
36+
Tests []CLICommandTest
37+
SleepAfterMs *int
3738
}
3839

3940
type CLICommandTest struct {
@@ -47,6 +48,19 @@ type CLIStepHTTPRequest struct {
4748
ResponseVariables []HTTPRequestResponseVariable
4849
Tests []HTTPRequestTest
4950
Request HTTPRequest
51+
SleepAfterMs *int
52+
}
53+
54+
type Sleepable interface {
55+
GetSleepAfterMs() *int
56+
}
57+
58+
func (c *CLIStepCLICommand) GetSleepAfterMs() *int {
59+
return c.SleepAfterMs
60+
}
61+
62+
func (h *CLIStepHTTPRequest) GetSleepAfterMs() *int {
63+
return h.SleepAfterMs
5064
}
5165

5266
const BaseURLPlaceholder = "${baseURL}"
@@ -58,18 +72,13 @@ type HTTPRequest struct {
5872
BodyJSON map[string]any
5973

6074
BasicAuth *HTTPBasicAuth
61-
Actions HTTPActions
6275
}
6376

6477
type HTTPBasicAuth struct {
6578
Username string
6679
Password string
6780
}
6881

69-
type HTTPActions struct {
70-
DelayRequestByMs *int
71-
}
72-
7382
type HTTPRequestResponseVariable struct {
7483
Name string
7584
Path string

messages/messages.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ type ResolveStepMsg struct {
2828
Passed *bool
2929
Result *api.CLIStepResult
3030
}
31+
32+
type SleepMsg struct {
33+
DurationMs int
34+
}

render/render.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type stepModel struct {
9090
result *api.CLIStepResult
9191
finished bool
9292
tests []testModel
93+
sleepAfter string
9394
}
9495

9596
type rootModel struct {
@@ -141,6 +142,20 @@ func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
141142
})
142143
return m, nil
143144

145+
case messages.SleepMsg:
146+
if len(m.steps) > 0 {
147+
lastStepIdx := len(m.steps) - 1
148+
durationSec := float64(msg.DurationMs) / 1000.0
149+
sleepText := ""
150+
if durationSec >= 1.0 {
151+
sleepText = fmt.Sprintf("Waiting %.1fs...", durationSec)
152+
} else {
153+
sleepText = fmt.Sprintf("Waiting %dms...", msg.DurationMs)
154+
}
155+
m.steps[lastStepIdx].sleepAfter = sleepText
156+
}
157+
return m, nil
158+
144159
case messages.ResolveStepMsg:
145160
m.steps[msg.Index].passed = msg.Passed
146161
m.steps[msg.Index].finished = true
@@ -178,6 +193,12 @@ func (m rootModel) View() string {
178193
str += renderTestHeader(step.step, m.spinner, step.finished, m.isSubmit, step.passed)
179194
str += renderTests(step.tests, s)
180195
str += renderTestResponseVars(step.responseVariables)
196+
197+
if step.sleepAfter != "" && step.finished {
198+
sleepBox := borderBox.Render(fmt.Sprintf(" %s ", step.sleepAfter))
199+
str += sleepBox + "\n"
200+
}
201+
181202
if step.result == nil || !m.finalized {
182203
continue
183204
}
@@ -196,7 +217,6 @@ func (m rootModel) View() string {
196217
for _, s := range sliced {
197218
str += gray.Render(s) + "\n"
198219
}
199-
200220
}
201221

202222
if step.result.HTTPRequestResult != nil {

0 commit comments

Comments
 (0)