Debian Codegraph @

cmd/internal/clnkrapp/test_helpers_test.go

raw
1 package clnkrapp
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7
8 "github.com/clnkr-ai/clnkr"
9 )
10
11 func actJSON(command string) string {
12 return fmt.Sprintf(
13 `{"type":"act","bash":{"commands":[{"command":%q,"workdir":null}]}}`,
14 command,
15 )
16 }
17
18 func mustTurn(raw string) clnkr.Turn {
19 turn, err := clnkr.ParseTurn(raw)
20 if err == nil {
21 return turn
22 }
23 var env struct {
24 Type string `json:"type"`
25 Summary string `json:"summary"`
26 }
27 if json.Unmarshal([]byte(raw), &env) == nil && env.Type == "done" {
28 return verifiedDone(env.Summary)
29 }
30 panic(err)
31 }
32
33 func mustResponse(raw string) clnkr.Response {
34 return clnkr.Response{Turn: mustTurn(raw), Raw: raw}
35 }
36
37 func verifiedDone(summary string) *clnkr.DoneTurn {
38 return &clnkr.DoneTurn{
39 Summary: summary,
40 Verification: clnkr.CompletionVerification{
41 Status: clnkr.VerificationVerified,
42 Checks: []clnkr.VerificationCheck{
43 {
44 Command: "go test ./...",
45 Outcome: "passed",
46 Evidence: "go test ./... passed and ls output showed current directory entries for completion",
47 },
48 },
49 },
50 KnownRisks: []string{},
51 }
52 }
53
54 type fakeModel struct {
55 responses []clnkr.Response
56 errs []error
57 calls int
58 responseCalls int
59 queries [][]clnkr.Message
60 }
61
62 func (m *fakeModel) Query(_ context.Context, messages []clnkr.Message) (clnkr.Response, error) {
63 m.queries = append(m.queries, append([]clnkr.Message{}, messages...))
64 call := m.calls
65 m.calls++
66 if call < len(m.errs) && m.errs[call] != nil {
67 return clnkr.Response{}, m.errs[call]
68 }
69 if m.responseCalls >= len(m.responses) {
70 return clnkr.Response{}, fmt.Errorf("no more responses")
71 }
72 resp := m.responses[m.responseCalls]
73 m.responseCalls++
74 return resp, nil
75 }
76
77 type fakeExecutor struct {
78 results []clnkr.CommandResult
79 errs []error
80 calls int
81 gotCmds []string
82 }
83
84 func (e *fakeExecutor) Execute(_ context.Context, command, _ string) (clnkr.CommandResult, error) {
85 e.gotCmds = append(e.gotCmds, command)
86 if e.calls >= len(e.results) {
87 return clnkr.CommandResult{}, fmt.Errorf("no more results")
88 }
89 result := e.results[e.calls]
90 if result.Command == "" {
91 result.Command = command
92 }
93 var err error
94 if e.calls < len(e.errs) {
95 err = e.errs[e.calls]
96 }
97 e.calls++
98 return result, err
99 }
100
101 func (e *fakeExecutor) SetEnv(map[string]string) {}
102
103 type fakeCompactor struct {
104 summary string
105 err error
106 calls int
107 messages []clnkr.Message
108 }
109
110 func (c *fakeCompactor) Summarize(_ context.Context, messages []clnkr.Message) (string, error) {
111 c.calls++
112 c.messages = append([]clnkr.Message{}, messages...)
113 if c.err != nil {
114 return "", c.err
115 }
116 return c.summary, nil
117 }
118
119 func compactableMessages() []clnkr.Message {
120 return []clnkr.Message{
121 {Role: "user", Content: "first task"},
122 {Role: "assistant", Content: `{"type":"done","summary":"done"}`},
123 {Role: "user", Content: "second task"},
124 {Role: "assistant", Content: `{"type":"done","summary":"done again"}`},
125 {Role: "user", Content: "third task"},
126 {Role: "assistant", Content: `{"type":"done","summary":"done third"}`},
127 }
128 }
129
130 func hasUserMessage(msgs []clnkr.Message, want string) bool {
131 for _, msg := range msgs {
132 if msg.Role == "user" && msg.Content == want {
133 return true
134 }
135 }
136 return false
137 }
138
139 func countUserMessages(msgs []clnkr.Message, want string) int {
140 count := 0
141 for _, msg := range msgs {
142 if msg.Role == "user" && msg.Content == want {
143 count++
144 }
145 }
146 return count
147 }