Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/agent/planexec/template_step_test.go
3434 views
1
package planexec_test
2
3
import (
4
"github.com/kardolus/chatgpt-cli/agent/planexec"
5
"github.com/kardolus/chatgpt-cli/agent/types"
6
"testing"
7
8
. "github.com/onsi/gomega"
9
"github.com/sclevine/spec"
10
"github.com/sclevine/spec/report"
11
)
12
13
func TestUnitTemplateStep(t *testing.T) {
14
spec.Run(t, "Testing step templating", testTemplateStep, spec.Report(report.Terminal{}))
15
}
16
17
func testTemplateStep(t *testing.T, when spec.G, it spec.S) {
18
it.Before(func() {
19
RegisterTestingT(t)
20
})
21
22
when("ApplyTemplate()", func() {
23
it("leaves fields unchanged when there are no template markers", func() {
24
step := types.Step{
25
Type: types.ToolShell,
26
Description: "plain desc",
27
Command: "echo",
28
Args: []string{"hi"},
29
}
30
31
ctx := types.ExecContext{Goal: "g", Plan: types.Plan{}, Results: nil}
32
33
out, err := planexec.ApplyTemplate(step, ctx)
34
Expect(err).NotTo(HaveOccurred())
35
Expect(out).To(Equal(step))
36
})
37
38
it("renders Description for any step type", func() {
39
step := types.Step{
40
Type: types.ToolKind("wat"),
41
Description: "goal={{.Goal}}",
42
}
43
44
ctx := types.ExecContext{Goal: "ship it"}
45
out, err := planexec.ApplyTemplate(step, ctx)
46
47
Expect(err).NotTo(HaveOccurred())
48
Expect(out.Description).To(Equal("goal=ship it"))
49
Expect(out.Type).To(Equal(types.ToolKind("wat")))
50
})
51
52
it("renders shell Command and Args", func() {
53
step := types.Step{
54
Type: types.ToolShell,
55
Description: "do {{.Goal}}",
56
Command: "echo {{.Goal}}",
57
Args: []string{"a={{.Goal}}", "b"},
58
}
59
60
ctx := types.ExecContext{Goal: "hello"}
61
out, err := planexec.ApplyTemplate(step, ctx)
62
63
Expect(err).NotTo(HaveOccurred())
64
Expect(out.Description).To(Equal("do hello"))
65
Expect(out.Command).To(Equal("echo hello"))
66
Expect(out.Args).To(Equal([]string{"a=hello", "b"}))
67
})
68
69
it("renders llm Prompt", func() {
70
step := types.Step{
71
Type: types.ToolLLM,
72
Description: "ask",
73
Prompt: "say {{.Goal}}",
74
}
75
76
ctx := types.ExecContext{Goal: "hola"}
77
out, err := planexec.ApplyTemplate(step, ctx)
78
79
Expect(err).NotTo(HaveOccurred())
80
Expect(out.Prompt).To(Equal("say hola"))
81
})
82
83
it("renders file Op/Path/Data", func() {
84
step := types.Step{
85
Type: types.ToolFiles,
86
Description: "write",
87
Op: "write",
88
Path: "/tmp/{{.Goal}}.txt",
89
Data: "payload={{.Goal}}",
90
}
91
92
ctx := types.ExecContext{Goal: "x"}
93
out, err := planexec.ApplyTemplate(step, ctx)
94
95
Expect(err).NotTo(HaveOccurred())
96
Expect(out.Path).To(Equal("/tmp/x.txt"))
97
Expect(out.Data).To(Equal("payload=x"))
98
})
99
100
it("errors when a referenced key is missing (missingkey=error)", func() {
101
step := types.Step{
102
Type: types.ToolLLM,
103
Description: "desc {{.Nope}}",
104
Prompt: "hi",
105
}
106
107
ctx := types.ExecContext{Goal: "g"}
108
_, err := planexec.ApplyTemplate(step, ctx)
109
110
Expect(err).To(HaveOccurred())
111
Expect(err.Error()).To(ContainSubstring("render Description"))
112
})
113
114
it("errors on invalid template syntax and wraps field name", func() {
115
step := types.Step{
116
Type: types.ToolShell,
117
Description: "ok",
118
Command: "{{ .Goal", // missing closing braces
119
Args: []string{"x"},
120
}
121
122
ctx := types.ExecContext{Goal: "g"}
123
_, err := planexec.ApplyTemplate(step, ctx)
124
125
Expect(err).To(HaveOccurred())
126
Expect(err.Error()).To(ContainSubstring("render Command"))
127
})
128
129
it("wraps args index in error message (Args[i])", func() {
130
step := types.Step{
131
Type: types.ToolShell,
132
Description: "ok",
133
Command: "echo",
134
Args: []string{"{{ .Goal", "ok"}, // first arg invalid template
135
}
136
137
ctx := types.ExecContext{Goal: "g"}
138
_, err := planexec.ApplyTemplate(step, ctx)
139
140
Expect(err).To(HaveOccurred())
141
Expect(err.Error()).To(ContainSubstring("render Args[0]"))
142
})
143
})
144
}
145
146