| 1 |
package clnkrapp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"errors" |
| 7 |
"io" |
| 8 |
"reflect" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/clnkr-ai/clnkr" |
| 14 |
) |
| 15 |
|
| 16 |
func TestDecodeJSONLCommand(t *testing.T) { |
| 17 |
tests := []struct { |
| 18 |
name string |
| 19 |
line string |
| 20 |
want JSONLCommand |
| 21 |
wantErr string |
| 22 |
}{ |
| 23 |
{ |
| 24 |
"prompt command", |
| 25 |
`{"type":"prompt","text":"inspect","mode":"approval"}`, |
| 26 |
JSONLCommand{Type: "prompt", Text: "inspect", Mode: "approval"}, |
| 27 |
"", |
| 28 |
}, |
| 29 |
{ |
| 30 |
"full send prompt command", |
| 31 |
`{"type":"prompt","text":"ship it","mode":"full_send"}`, |
| 32 |
JSONLCommand{Type: "prompt", Text: "ship it", Mode: "full_send"}, |
| 33 |
"", |
| 34 |
}, |
| 35 |
{ |
| 36 |
"reply command", |
| 37 |
`{"type":"reply","text":"y"}`, |
| 38 |
JSONLCommand{Type: "reply", Text: "y"}, |
| 39 |
"", |
| 40 |
}, |
| 41 |
{ |
| 42 |
"compact command", |
| 43 |
`{"type":"compact"}`, |
| 44 |
JSONLCommand{Type: "compact"}, |
| 45 |
"", |
| 46 |
}, |
| 47 |
{"shutdown command", `{"type":"shutdown"}`, JSONLCommand{Type: "shutdown"}, ""}, |
| 48 |
{ |
| 49 |
"unknown command", |
| 50 |
`{"type":"bogus"}`, |
| 51 |
JSONLCommand{}, |
| 52 |
`unknown JSONL command type "bogus"`, |
| 53 |
}, |
| 54 |
{ |
| 55 |
"unknown prompt mode", |
| 56 |
`{"type":"prompt","text":"inspect","mode":"manual"}`, |
| 57 |
JSONLCommand{}, |
| 58 |
`unknown JSONL prompt mode "manual"`, |
| 59 |
}, |
| 60 |
{ |
| 61 |
"missing prompt mode", |
| 62 |
`{"type":"prompt","text":"inspect"}`, |
| 63 |
JSONLCommand{}, |
| 64 |
`unknown JSONL prompt mode ""`, |
| 65 |
}, |
| 66 |
{ |
| 67 |
"compact with extra field instructions", |
| 68 |
`{"type":"compact","instructions":"focus tests"}`, |
| 69 |
JSONLCommand{}, |
| 70 |
"JSONL compact supports only {\"type\":\"compact\"}", |
| 71 |
}, |
| 72 |
{ |
| 73 |
"compact with extra field foo", |
| 74 |
`{"type":"compact","foo":"bar"}`, |
| 75 |
JSONLCommand{}, |
| 76 |
"JSONL compact supports only {\"type\":\"compact\"}", |
| 77 |
}, |
| 78 |
{ |
| 79 |
"compact with extra field text", |
| 80 |
`{"type":"compact","text":"anything"}`, |
| 81 |
JSONLCommand{}, |
| 82 |
"JSONL compact supports only {\"type\":\"compact\"}", |
| 83 |
}, |
| 84 |
{ |
| 85 |
"compact with extra field mode", |
| 86 |
`{"type":"compact","mode":"approval"}`, |
| 87 |
JSONLCommand{}, |
| 88 |
"JSONL compact supports only {\"type\":\"compact\"}", |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
for _, tt := range tests { |
| 93 |
t.Run(tt.name, func(t *testing.T) { |
| 94 |
got, err := DecodeJSONLCommand([]byte(tt.line)) |
| 95 |
if tt.wantErr != "" { |
| 96 |
if err == nil || !strings.Contains(err.Error(), tt.wantErr) { |
| 97 |
t.Fatalf("DecodeJSONLCommand() error = %v, want %q", err, tt.wantErr) |
| 98 |
} |
| 99 |
return |
| 100 |
} |
| 101 |
if err != nil { |
| 102 |
t.Fatalf("DecodeJSONLCommand(): %v", err) |
| 103 |
} |
| 104 |
if got != tt.want { |
| 105 |
t.Fatalf("DecodeJSONLCommand() = %#v, want %#v", got, tt.want) |
| 106 |
} |
| 107 |
}) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestWriteJSONL(t *testing.T) { |
| 112 |
tests := []struct { |
| 113 |
name string |
| 114 |
event any |
| 115 |
typ string |
| 116 |
}{ |
| 117 |
{ |
| 118 |
name: "core response event uses event log encoding", |
| 119 |
event: clnkr.EventResponse{ |
| 120 |
Turn: verifiedDone("done"), |
| 121 |
Usage: clnkr.Usage{InputTokens: 1, OutputTokens: 2}, |
| 122 |
}, |
| 123 |
typ: "response", |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "core command start event uses event log encoding", |
| 127 |
event: clnkr.EventCommandStart{Command: "pwd", Dir: "/tmp"}, |
| 128 |
typ: "command_start", |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "core command done event uses event log encoding", |
| 132 |
event: clnkr.EventCommandDone{Command: "echo hi", Stdout: "hi\n", ExitCode: 0}, |
| 133 |
typ: "command_done", |
| 134 |
}, |
| 135 |
{ |
| 136 |
name: "context backstop debug event", |
| 137 |
event: clnkr.EventDebug{Message: clnkr.ContextLengthBackstopRetryingDebug}, |
| 138 |
typ: "debug", |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "clarification request", |
| 142 |
event: EventClarificationRequest{Question: "Which repo?"}, |
| 143 |
typ: "clarify", |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "approval request", |
| 147 |
event: EventApprovalRequest{ |
| 148 |
Prompt: "1. echo hi", |
| 149 |
Commands: []clnkr.BashAction{ |
| 150 |
{ID: "call_1", Command: "echo hi"}, |
| 151 |
{Command: "pwd", Workdir: "/tmp"}, |
| 152 |
}, |
| 153 |
}, |
| 154 |
typ: "approval_request", |
| 155 |
}, |
| 156 |
{name: "done event", event: EventDone{Summary: "done"}, typ: "done"}, |
| 157 |
{ |
| 158 |
name: "compacted event", |
| 159 |
event: EventCompacted{Stats: clnkr.CompactStats{CompactedMessages: 4, KeptMessages: 2}}, |
| 160 |
typ: "compacted", |
| 161 |
}, |
| 162 |
{name: "error event", event: EventError{Err: errors.New("boom")}, typ: "error"}, |
| 163 |
} |
| 164 |
|
| 165 |
var b bytes.Buffer |
| 166 |
for _, tt := range tests { |
| 167 |
if err := WriteJSONL(&b, tt.event); err != nil { |
| 168 |
t.Fatalf("WriteJSONL(%s): %v", tt.name, err) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
decoder := json.NewDecoder(&b) |
| 173 |
for i, tt := range tests { |
| 174 |
var got struct { |
| 175 |
TS string `json:"ts"` |
| 176 |
Type string `json:"type"` |
| 177 |
Payload map[string]any `json:"payload"` |
| 178 |
} |
| 179 |
if err := decoder.Decode(&got); err != nil { |
| 180 |
t.Fatalf("decode event %d: %v", i, err) |
| 181 |
} |
| 182 |
ts, err := time.Parse(time.RFC3339Nano, got.TS) |
| 183 |
if err != nil { |
| 184 |
t.Fatalf("event %d ts = %q, want RFC3339Nano timestamp: %v", i, got.TS, err) |
| 185 |
} |
| 186 |
if ts.Location() != time.UTC { |
| 187 |
t.Fatalf("event %d ts location = %v, want UTC", i, ts.Location()) |
| 188 |
} |
| 189 |
if got.Type != tt.typ { |
| 190 |
t.Fatalf("event %d type = %q, want %q", i, got.Type, tt.typ) |
| 191 |
} |
| 192 |
|
| 193 |
switch tt.typ { |
| 194 |
case "clarify": |
| 195 |
if got.Payload["question"] != "Which repo?" { |
| 196 |
t.Fatalf("clarify payload = %#v, want question", got.Payload) |
| 197 |
} |
| 198 |
case "approval_request": |
| 199 |
if got.Payload["prompt"] != "1. echo hi" { |
| 200 |
t.Fatalf("approval payload = %#v, want prompt", got.Payload) |
| 201 |
} |
| 202 |
commands := got.Payload["commands"].([]any) |
| 203 |
if !reflect.DeepEqual(commands[0], map[string]any{"command": "echo hi"}) { |
| 204 |
t.Fatalf("first approval command = %#v, want command only", commands[0]) |
| 205 |
} |
| 206 |
if !reflect.DeepEqual( |
| 207 |
commands[1], |
| 208 |
map[string]any{"command": "pwd", "workdir": "/tmp"}, |
| 209 |
) { |
| 210 |
t.Fatalf("second approval command = %#v, want command and workdir", commands[1]) |
| 211 |
} |
| 212 |
case "compacted": |
| 213 |
if got.Payload["compacted_messages"] != float64(4) || |
| 214 |
got.Payload["kept_messages"] != float64(2) { |
| 215 |
t.Fatalf("compacted payload = %#v, want explicit stats", got.Payload) |
| 216 |
} |
| 217 |
case "error": |
| 218 |
if got.Payload["message"] != "boom" { |
| 219 |
t.Fatalf("error payload = %#v, want message", got.Payload) |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
if err := decoder.Decode(&struct{}{}); err != io.EOF { |
| 224 |
t.Fatalf("extra JSONL event decode error = %v, want EOF", err) |
| 225 |
} |
| 226 |
} |