| 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 |
type eventLogEntry struct { |
| 17 |
TS string `json:"ts"` |
| 18 |
Type string `json:"type"` |
| 19 |
Payload json.RawMessage `json:"payload"` |
| 20 |
} |
| 21 |
|
| 22 |
func TestWriteEventLogIncludesTimestamp(t *testing.T) { |
| 23 |
entry := writeEventLogEntry(t, clnkr.EventDebug{Message: "querying model..."}) |
| 24 |
|
| 25 |
if entry.TS == "" { |
| 26 |
t.Fatal("ts is empty, want RFC3339Nano UTC timestamp") |
| 27 |
} |
| 28 |
|
| 29 |
ts, err := time.Parse(time.RFC3339Nano, entry.TS) |
| 30 |
if err != nil { |
| 31 |
t.Fatalf("parse ts %q: %v", entry.TS, err) |
| 32 |
} |
| 33 |
if ts.Location() != time.UTC { |
| 34 |
t.Fatalf("ts location = %v, want UTC", ts.Location()) |
| 35 |
} |
| 36 |
if entry.Type != "debug" { |
| 37 |
t.Fatalf("type = %q, want debug", entry.Type) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestWriteEventLogResponsePayload(t *testing.T) { |
| 42 |
entry := writeEventLogEntry(t, clnkr.EventResponse{ |
| 43 |
Turn: verifiedDone("done"), |
| 44 |
Usage: clnkr.Usage{InputTokens: 3, OutputTokens: 4}, |
| 45 |
Raw: "raw response", |
| 46 |
}) |
| 47 |
if entry.Type != "response" { |
| 48 |
t.Fatalf("type = %q, want response", entry.Type) |
| 49 |
} |
| 50 |
|
| 51 |
var payload struct { |
| 52 |
Turn json.RawMessage `json:"turn"` |
| 53 |
Usage map[string]json.RawMessage `json:"usage"` |
| 54 |
Raw string `json:"raw"` |
| 55 |
} |
| 56 |
decodePayload(t, entry, &payload) |
| 57 |
|
| 58 |
wantTurn := `{"type":"done","summary":"done","verification":{"status":"verified","checks":[{"command":"go test ./...","outcome":"passed","evidence":"go test ./... passed and ls output showed current directory entries for completion"}]},"known_risks":[]}` |
| 59 |
if string(payload.Turn) != wantTurn { |
| 60 |
t.Fatalf("turn = %s, want canonical done turn", payload.Turn) |
| 61 |
} |
| 62 |
if _, ok := payload.Usage["input_tokens"]; !ok { |
| 63 |
t.Fatalf("usage = %#v, want input_tokens", payload.Usage) |
| 64 |
} |
| 65 |
if _, ok := payload.Usage["InputTokens"]; ok { |
| 66 |
t.Fatalf("usage has Go field name: %#v", payload.Usage) |
| 67 |
} |
| 68 |
if payload.Raw != "raw response" { |
| 69 |
t.Fatalf("raw = %q, want raw response", payload.Raw) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestWriteEventLogSnakeCasePayloads(t *testing.T) { |
| 74 |
tests := []struct { |
| 75 |
name string |
| 76 |
event clnkr.Event |
| 77 |
wantType string |
| 78 |
wantKey string |
| 79 |
rejectKey string |
| 80 |
wantFields map[string]any |
| 81 |
}{ |
| 82 |
{ |
| 83 |
name: "command start", |
| 84 |
event: clnkr.EventCommandStart{Command: "pwd", Dir: "/tmp"}, |
| 85 |
wantType: "command_start", |
| 86 |
wantKey: "command", |
| 87 |
rejectKey: "Command", |
| 88 |
wantFields: map[string]any{ |
| 89 |
"command": "pwd", |
| 90 |
"dir": "/tmp", |
| 91 |
}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "debug", |
| 95 |
event: clnkr.EventDebug{Message: "querying model..."}, |
| 96 |
wantType: "debug", |
| 97 |
wantKey: "message", |
| 98 |
rejectKey: "Message", |
| 99 |
wantFields: map[string]any{ |
| 100 |
"message": "querying model...", |
| 101 |
}, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "context backstop debug", |
| 105 |
event: clnkr.EventDebug{Message: clnkr.ContextLengthBackstopCompactingDebug}, |
| 106 |
wantType: "debug", |
| 107 |
wantKey: "message", |
| 108 |
rejectKey: "Message", |
| 109 |
wantFields: map[string]any{ |
| 110 |
"message": clnkr.ContextLengthBackstopCompactingDebug, |
| 111 |
}, |
| 112 |
}, |
| 113 |
{ |
| 114 |
name: "protocol failure", |
| 115 |
event: clnkr.EventProtocolFailure{Reason: "bad json", Raw: "nope"}, |
| 116 |
wantType: "protocol_failure", |
| 117 |
wantKey: "reason", |
| 118 |
rejectKey: "Reason", |
| 119 |
wantFields: map[string]any{ |
| 120 |
"reason": "bad json", |
| 121 |
"raw": "nope", |
| 122 |
}, |
| 123 |
}, |
| 124 |
{ |
| 125 |
name: "completion gate", |
| 126 |
event: clnkr.EventCompletionGate{ |
| 127 |
Decision: clnkr.CompletionChallenge, |
| 128 |
Reasons: []string{"artifact_claim_without_check"}, |
| 129 |
Summary: "created result.txt", |
| 130 |
}, |
| 131 |
wantType: "completion_gate", |
| 132 |
wantKey: "decision", |
| 133 |
rejectKey: "Decision", |
| 134 |
wantFields: map[string]any{ |
| 135 |
"decision": "challenge", |
| 136 |
"reasons": []any{"artifact_claim_without_check"}, |
| 137 |
"summary": "created result.txt", |
| 138 |
}, |
| 139 |
}, |
| 140 |
} |
| 141 |
|
| 142 |
for _, tt := range tests { |
| 143 |
t.Run(tt.name, func(t *testing.T) { |
| 144 |
entry := writeEventLogEntry(t, tt.event) |
| 145 |
if entry.Type != tt.wantType { |
| 146 |
t.Fatalf("type = %q, want %q", entry.Type, tt.wantType) |
| 147 |
} |
| 148 |
|
| 149 |
payload := payloadObject(t, entry) |
| 150 |
if _, ok := payload[tt.wantKey]; !ok { |
| 151 |
t.Fatalf("payload = %#v, want %q", payload, tt.wantKey) |
| 152 |
} |
| 153 |
if _, ok := payload[tt.rejectKey]; ok { |
| 154 |
t.Fatalf("payload has Go field name %q: %#v", tt.rejectKey, payload) |
| 155 |
} |
| 156 |
for key, want := range tt.wantFields { |
| 157 |
if !reflect.DeepEqual(payload[key], want) { |
| 158 |
t.Fatalf("payload[%q] = %#v, want %#v", key, payload[key], want) |
| 159 |
} |
| 160 |
} |
| 161 |
}) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func TestWriteEventLogCommandDonePayload(t *testing.T) { |
| 166 |
tests := []struct { |
| 167 |
name string |
| 168 |
event clnkr.EventCommandDone |
| 169 |
wantErr any |
| 170 |
wantErrKey bool |
| 171 |
}{ |
| 172 |
{ |
| 173 |
name: "feedback included and nil err omitted", |
| 174 |
event: clnkr.EventCommandDone{ |
| 175 |
Command: "touch note.txt", |
| 176 |
ExitCode: 0, |
| 177 |
Feedback: clnkr.CommandFeedback{ChangedFiles: []string{"note.txt"}}, |
| 178 |
}, |
| 179 |
wantErr: nil, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "err included when present", |
| 183 |
event: clnkr.EventCommandDone{ |
| 184 |
Command: "false", |
| 185 |
ExitCode: 1, |
| 186 |
Err: errors.New("boom"), |
| 187 |
}, |
| 188 |
wantErr: "boom", |
| 189 |
wantErrKey: true, |
| 190 |
}, |
| 191 |
} |
| 192 |
|
| 193 |
for _, tt := range tests { |
| 194 |
t.Run(tt.name, func(t *testing.T) { |
| 195 |
entry := writeEventLogEntry(t, tt.event) |
| 196 |
if entry.Type != "command_done" { |
| 197 |
t.Fatalf("type = %q, want command_done", entry.Type) |
| 198 |
} |
| 199 |
|
| 200 |
payload := payloadObject(t, entry) |
| 201 |
if payload["Command"] != nil || payload["ExitCode"] != nil { |
| 202 |
t.Fatalf("payload has Go field names: %#v", payload) |
| 203 |
} |
| 204 |
if got, ok := payload["err"]; ok != tt.wantErrKey || |
| 205 |
!reflect.DeepEqual(got, tt.wantErr) { |
| 206 |
t.Fatalf( |
| 207 |
"err = %#v, present = %v; want %#v, present = %v", |
| 208 |
got, |
| 209 |
ok, |
| 210 |
tt.wantErr, |
| 211 |
tt.wantErrKey, |
| 212 |
) |
| 213 |
} |
| 214 |
if len(tt.event.Feedback.ChangedFiles) > 0 { |
| 215 |
feedback := payload["feedback"].(map[string]any) |
| 216 |
if !reflect.DeepEqual(feedback["changed_files"], []any{"note.txt"}) { |
| 217 |
t.Fatalf("feedback = %#v, want note.txt", feedback) |
| 218 |
} |
| 219 |
} |
| 220 |
}) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
func TestWriteEventLogPreservesRawCommandOutput(t *testing.T) { |
| 225 |
stdout := "stdout-head\n" + strings.Repeat( |
| 226 |
"raw stdout\n", |
| 227 |
80*1024/len("raw stdout\n"), |
| 228 |
) + "stdout-tail\n" |
| 229 |
stderr := "stderr-head\n" + strings.Repeat( |
| 230 |
"raw stderr\n", |
| 231 |
80*1024/len("raw stderr\n"), |
| 232 |
) + "stderr-tail\n" |
| 233 |
|
| 234 |
entry := writeEventLogEntry(t, clnkr.EventCommandDone{ |
| 235 |
Command: "make test", |
| 236 |
Stdout: stdout, |
| 237 |
Stderr: stderr, |
| 238 |
ExitCode: 1, |
| 239 |
}) |
| 240 |
if entry.Type != "command_done" { |
| 241 |
t.Fatalf("event type = %q, want command_done", entry.Type) |
| 242 |
} |
| 243 |
|
| 244 |
var payload struct { |
| 245 |
Stdout string `json:"stdout"` |
| 246 |
Stderr string `json:"stderr"` |
| 247 |
} |
| 248 |
decodePayload(t, entry, &payload) |
| 249 |
if payload.Stdout != stdout { |
| 250 |
t.Fatalf("stdout length = %d, want raw %d", len(payload.Stdout), len(stdout)) |
| 251 |
} |
| 252 |
if payload.Stderr != stderr { |
| 253 |
t.Fatalf("stderr length = %d, want raw %d", len(payload.Stderr), len(stderr)) |
| 254 |
} |
| 255 |
if strings.Contains(payload.Stdout, "compressed") || |
| 256 |
strings.Contains(payload.Stderr, "compressed") { |
| 257 |
t.Fatalf("event log should not contain model-facing compression markers") |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func writeEventLogEntry(t *testing.T, event clnkr.Event) eventLogEntry { |
| 262 |
t.Helper() |
| 263 |
|
| 264 |
var buf bytes.Buffer |
| 265 |
if err := WriteEventLog(&buf, event); err != nil { |
| 266 |
t.Fatalf("WriteEventLog: %v", err) |
| 267 |
} |
| 268 |
|
| 269 |
var entry eventLogEntry |
| 270 |
decoder := json.NewDecoder(&buf) |
| 271 |
if err := decoder.Decode(&entry); err != nil { |
| 272 |
t.Fatalf("decode event log: %v", err) |
| 273 |
} |
| 274 |
if err := decoder.Decode(&struct{}{}); err != io.EOF { |
| 275 |
t.Fatalf("extra event decode error = %v, want EOF", err) |
| 276 |
} |
| 277 |
return entry |
| 278 |
} |
| 279 |
|
| 280 |
func payloadObject(t *testing.T, entry eventLogEntry) map[string]any { |
| 281 |
t.Helper() |
| 282 |
|
| 283 |
var payload map[string]any |
| 284 |
decodePayload(t, entry, &payload) |
| 285 |
return payload |
| 286 |
} |
| 287 |
|
| 288 |
func decodePayload(t *testing.T, entry eventLogEntry, v any) { |
| 289 |
t.Helper() |
| 290 |
|
| 291 |
if err := json.Unmarshal(entry.Payload, v); err != nil { |
| 292 |
t.Fatalf("decode %s payload: %v", entry.Type, err) |
| 293 |
} |
| 294 |
} |