Repository navigation
Expand file tree
/
Copy pathagent_runner_test.go
More file actions
148 lines (117 loc) · 4.25 KB
/
Copy pathagent_runner_test.go
File metadata and controls
148 lines (117 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2026 Benjamin Borbe All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lib_test
import (
"context"
"errors"
"github.com/bborbe/collection"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
lib "github.com/bborbe/agent"
"github.com/bborbe/agent/mocks"
)
var _ = Describe("StepRunner", func() {
var ctx context.Context
BeforeEach(func() {
ctx = context.Background()
})
Describe("Run", func() {
It("calls deliverer when step succeeds", func() {
deliverer := &mocks.AgentResultDeliverer{}
step := &mocks.AgentStep{}
step.NameReturns("test-step")
step.ShouldRunReturns(true, nil)
step.RunReturns(&lib.Result{Status: lib.AgentStatusDone, NextPhase: "next"}, nil)
md := &lib.Markdown{}
runner := lib.NewStepRunner(deliverer, step)
result, err := runner.Run(ctx, md)
Expect(err).To(BeNil())
Expect(result).NotTo(BeNil())
Expect(result.Status).To(Equal(lib.AgentStatusDone))
Expect(deliverer.DeliverResultCallCount()).To(Equal(1))
})
It("forwards ContinueToNext to the deliverer", func() {
deliverer := &mocks.AgentResultDeliverer{}
step := &mocks.AgentStep{}
step.NameReturns("preflight-step")
step.ShouldRunReturns(true, nil)
step.RunReturns(&lib.Result{
Status: lib.AgentStatusDone,
ContinueToNext: true,
}, nil)
md := &lib.Markdown{}
runner := lib.NewStepRunner(deliverer, step)
_, err := runner.Run(ctx, md)
Expect(err).To(BeNil())
Expect(deliverer.DeliverResultCallCount()).To(Equal(1))
_, info := deliverer.DeliverResultArgsForCall(0)
Expect(info.Status).To(Equal(lib.AgentStatusDone))
Expect(info.NextPhase).To(Equal(""))
Expect(info.ContinueToNext).To(BeTrue(),
"deliverer must see ContinueToNext so Done+empty NextPhase preflight saves are distinguishable")
})
It("forwards the run's observed counts to the deliverer", func() {
deliverer := &mocks.AgentResultDeliverer{}
step := &mocks.AgentStep{}
step.NameReturns("counted-step")
step.ShouldRunReturns(true, nil)
step.RunReturns(&lib.Result{
Status: lib.AgentStatusDone,
AgentTurns: collection.Ptr(int64(7)),
InteractionCount: collection.Ptr(int64(0)),
}, nil)
md := &lib.Markdown{}
runner := lib.NewStepRunner(deliverer, step)
_, err := runner.Run(ctx, md)
Expect(err).To(BeNil())
Expect(deliverer.DeliverResultCallCount()).To(Equal(1))
_, info := deliverer.DeliverResultArgsForCall(0)
Expect(info.AgentTurns).NotTo(BeNil())
Expect(*info.AgentTurns).To(Equal(int64(7)))
Expect(info.InteractionCount).NotTo(BeNil())
Expect(*info.InteractionCount).To(Equal(int64(0)))
})
It("forwards absence as absence", func() {
deliverer := &mocks.AgentResultDeliverer{}
step := &mocks.AgentStep{}
step.NameReturns("uncounted-step")
step.ShouldRunReturns(true, nil)
step.RunReturns(&lib.Result{Status: lib.AgentStatusDone}, nil)
md := &lib.Markdown{}
runner := lib.NewStepRunner(deliverer, step)
_, err := runner.Run(ctx, md)
Expect(err).To(BeNil())
Expect(deliverer.DeliverResultCallCount()).To(Equal(1))
_, info := deliverer.DeliverResultArgsForCall(0)
Expect(info.AgentTurns).To(BeNil())
Expect(info.InteractionCount).To(BeNil())
})
It("returns error when step.Run returns error", func() {
deliverer := &mocks.AgentResultDeliverer{}
step := &mocks.AgentStep{}
step.NameReturns("failing-step")
step.ShouldRunReturns(true, nil)
step.RunReturns(nil, errors.New("step failed"))
md := &lib.Markdown{}
runner := lib.NewStepRunner(deliverer, step)
_, err := runner.Run(ctx, md)
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("failing-step"))
Expect(err.Error()).To(ContainSubstring("step failed"))
})
It("returns early when ctx.Done fires", func() {
deliverer := &mocks.AgentResultDeliverer{}
cancelCtx, cancel := context.WithCancel(context.Background())
cancel()
step := &mocks.AgentStep{}
step.NameReturns("test-step")
step.ShouldRunReturns(true, nil)
md := &lib.Markdown{}
runner := lib.NewStepRunner(deliverer, step)
_, err := runner.Run(cancelCtx, md)
Expect(err).NotTo(BeNil())
Expect(errors.Is(err, context.Canceled)).To(BeTrue())
})
})
})