| 1 |
package clnkr_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
clnkr "github.com/clnkr-ai/clnkr" |
| 9 |
) |
| 10 |
|
| 11 |
func TestParseTurnAcceptsCanonicalTurns(t *testing.T) { |
| 12 |
tests := []struct { |
| 13 |
name string |
| 14 |
input string |
| 15 |
check func(*testing.T, clnkr.Turn) |
| 16 |
}{ |
| 17 |
{ |
| 18 |
name: "act", |
| 19 |
input: `{"type":"act","bash":{"commands":[{"command":"ls -la","workdir":null}]},"reasoning":"inspect files"}`, |
| 20 |
}, |
| 21 |
{ |
| 22 |
name: "act with more than three commands", |
| 23 |
input: `{"type":"act","bash":{"commands":[{"command":"one","workdir":null},{"command":"two","workdir":null},{"command":"three","workdir":null},{"command":"four","workdir":null}]},"reasoning":"batch"}`, |
| 24 |
check: func(t *testing.T, turn clnkr.Turn) { |
| 25 |
act, ok := turn.(*clnkr.ActTurn) |
| 26 |
if !ok { |
| 27 |
t.Fatalf("turn = %T, want *ActTurn", turn) |
| 28 |
} |
| 29 |
if got := len(act.Bash.Commands); got != 4 { |
| 30 |
t.Fatalf("commands = %d, want 4", got) |
| 31 |
} |
| 32 |
}, |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: "clarify", |
| 36 |
input: `{"type":"clarify","question":"Which directory?"}`, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "done verified", |
| 40 |
input: `{"type":"done","summary":"Finished the task.","verification":{"status":"verified","checks":[{"command":"go test ./...","outcome":"passed","evidence":"test suite passed"}]},"known_risks":[]}`, |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "done partially verified with risks", |
| 44 |
input: `{"type":"done","summary":"Implemented the parser change.","verification":{"status":"partially_verified","checks":[{"command":"go test ./...","outcome":"failed","evidence":"one unrelated fixture failed"}]},"known_risks":["full suite still has an unrelated fixture failure"]}`, |
| 45 |
check: func(t *testing.T, turn clnkr.Turn) { |
| 46 |
done, ok := turn.(*clnkr.DoneTurn) |
| 47 |
if !ok { |
| 48 |
t.Fatalf("turn = %T, want *DoneTurn", turn) |
| 49 |
} |
| 50 |
if done.Verification.Status != clnkr.VerificationPartiallyVerified { |
| 51 |
t.Fatalf( |
| 52 |
"status = %q, want %q", |
| 53 |
done.Verification.Status, |
| 54 |
clnkr.VerificationPartiallyVerified, |
| 55 |
) |
| 56 |
} |
| 57 |
if len(done.KnownRisks) != 1 { |
| 58 |
t.Fatalf("known risks = %#v, want one risk", done.KnownRisks) |
| 59 |
} |
| 60 |
}, |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "done not verified with risks and no checks", |
| 64 |
input: `{"type":"done","summary":"Could not verify because the command budget was exhausted.","verification":{"status":"not_verified","checks":[]},"known_risks":["command budget exhausted before verification"]}`, |
| 65 |
check: func(t *testing.T, turn clnkr.Turn) { |
| 66 |
done, ok := turn.(*clnkr.DoneTurn) |
| 67 |
if !ok { |
| 68 |
t.Fatalf("turn = %T, want *DoneTurn", turn) |
| 69 |
} |
| 70 |
if done.Verification.Status != clnkr.VerificationNotVerified { |
| 71 |
t.Fatalf( |
| 72 |
"status = %q, want %q", |
| 73 |
done.Verification.Status, |
| 74 |
clnkr.VerificationNotVerified, |
| 75 |
) |
| 76 |
} |
| 77 |
}, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
for _, tt := range tests { |
| 82 |
t.Run(tt.name, func(t *testing.T) { |
| 83 |
turn, err := clnkr.ParseTurn(tt.input) |
| 84 |
if err != nil { |
| 85 |
t.Fatalf("ParseTurn(%q) error = %v", tt.input, err) |
| 86 |
} |
| 87 |
if turn == nil { |
| 88 |
t.Fatalf("ParseTurn(%q) returned nil turn", tt.input) |
| 89 |
} |
| 90 |
if tt.check != nil { |
| 91 |
tt.check(t, turn) |
| 92 |
} |
| 93 |
}) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestParseTurnRejectsInvalidTurns(t *testing.T) { |
| 98 |
tests := []struct { |
| 99 |
name string |
| 100 |
input string |
| 101 |
want error |
| 102 |
detail string |
| 103 |
}{ |
| 104 |
{ |
| 105 |
name: "prose wrapped json", |
| 106 |
input: "Here is the turn:\n{\"type\":\"act\",\"bash\":{\"commands\":[{\"command\":\"pwd\",\"workdir\":null}]}}\nThanks.", |
| 107 |
want: clnkr.ErrInvalidJSON, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "wrapped provider turn so wrapper stays provider-owned", |
| 111 |
input: `{"turn":{"type":"act","bash":{"commands":[{"command":"pwd","workdir":null}]},"question":null,"summary":null,"reasoning":null}}`, |
| 112 |
want: clnkr.ErrInvalidJSON, |
| 113 |
}, |
| 114 |
{ |
| 115 |
name: "unknown fields", |
| 116 |
input: `{"type":"act","bash":{"commands":[{"command":"pwd","workdir":null}]},"extra":"nope"}`, |
| 117 |
want: clnkr.ErrInvalidJSON, |
| 118 |
detail: `unknown field "extra"`, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "wrong turn specific fields", |
| 122 |
input: `{"type":"act","bash":{"commands":[{"command":"pwd","workdir":null}]},"question":""}`, |
| 123 |
want: clnkr.ErrInvalidJSON, |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "provider-shaped act siblings", |
| 127 |
input: `{"type":"act","bash":{"commands":[{"command":"pwd","workdir":null}]},"question":null,"summary":null,"reasoning":null}`, |
| 128 |
want: clnkr.ErrInvalidJSON, |
| 129 |
detail: "act turn only allows question when it is omitted", |
| 130 |
}, |
| 131 |
{ |
| 132 |
name: "missing command workdir in canonical act", |
| 133 |
input: `{"type":"act","bash":{"commands":[{"command":"pwd"}]}}`, |
| 134 |
want: clnkr.ErrInvalidJSON, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "whitespace only clarify question", |
| 138 |
input: `{"type":"clarify","question":" "}`, |
| 139 |
want: clnkr.ErrEmptyClarify, |
| 140 |
}, |
| 141 |
{ |
| 142 |
name: "whitespace only done summary", |
| 143 |
input: `{"type":"done","summary":"\n\t"}`, |
| 144 |
want: clnkr.ErrEmptySummary, |
| 145 |
}, |
| 146 |
{ |
| 147 |
name: "summary-only done", |
| 148 |
input: `{"type":"done","summary":"Finished the task."}`, |
| 149 |
want: clnkr.ErrInvalidJSON, |
| 150 |
detail: `missing required field "verification"`, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "done with bad verification status", |
| 154 |
input: `{"type":"done","summary":"Finished.","verification":{"status":"sure","checks":[{"command":"go test ./...","outcome":"passed","evidence":"tests passed"}]},"known_risks":[]}`, |
| 155 |
want: clnkr.ErrInvalidJSON, |
| 156 |
detail: `invalid verification status "sure"`, |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "verified done without checks", |
| 160 |
input: `{"type":"done","summary":"Finished.","verification":{"status":"verified","checks":[]},"known_risks":[]}`, |
| 161 |
want: clnkr.ErrInvalidJSON, |
| 162 |
detail: "verified done requires at least one verification check", |
| 163 |
}, |
| 164 |
{ |
| 165 |
name: "done with null known risks", |
| 166 |
input: `{"type":"done","summary":"Could not verify.","verification":{"status":"not_verified","checks":[]},"known_risks":null}`, |
| 167 |
want: clnkr.ErrInvalidJSON, |
| 168 |
detail: `known_risks must be an array`, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "done with null verification checks", |
| 172 |
input: `{"type":"done","summary":"Could not verify.","verification":{"status":"not_verified","checks":null},"known_risks":[]}`, |
| 173 |
want: clnkr.ErrInvalidJSON, |
| 174 |
detail: `verification.checks must be an array`, |
| 175 |
}, |
| 176 |
{ |
| 177 |
name: "done without known risks", |
| 178 |
input: `{"type":"done","summary":"Finished.","verification":{"status":"verified","checks":[{"command":"go test ./...","outcome":"passed","evidence":"tests passed"}]}}`, |
| 179 |
want: clnkr.ErrInvalidJSON, |
| 180 |
detail: `missing required field "known_risks"`, |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "done without verification checks", |
| 184 |
input: `{"type":"done","summary":"Could not verify.","verification":{"status":"not_verified"},"known_risks":["not run"]}`, |
| 185 |
want: clnkr.ErrInvalidJSON, |
| 186 |
detail: `missing required field "verification.checks"`, |
| 187 |
}, |
| 188 |
{ |
| 189 |
name: "done with non-object verification", |
| 190 |
input: `{"type":"done","summary":"Could not verify.","verification":[],"known_risks":["not run"]}`, |
| 191 |
want: clnkr.ErrInvalidJSON, |
| 192 |
detail: `cannot unmarshal array`, |
| 193 |
}, |
| 194 |
} |
| 195 |
|
| 196 |
for _, tt := range tests { |
| 197 |
t.Run(tt.name, func(t *testing.T) { |
| 198 |
_, err := clnkr.ParseTurn(tt.input) |
| 199 |
if !errors.Is(err, tt.want) { |
| 200 |
t.Fatalf("ParseTurn(%q) error = %v, want %v", tt.input, err, tt.want) |
| 201 |
} |
| 202 |
if tt.detail != "" && !strings.Contains(err.Error(), tt.detail) { |
| 203 |
t.Fatalf("ParseTurn(%q) error = %v, want detail %q", tt.input, err, tt.detail) |
| 204 |
} |
| 205 |
}) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func TestParseTurnNormalizesEscapedClarifyText(t *testing.T) { |
| 210 |
raw := `{"type":"clarify","question":"Here are the skills I found:\\n- **citations-agent**: Verify claims.\\\\- **humanizer**: Sound more natural.\\nWhat would you like to work on?","reasoning":"Need to list the available skills.\\\\nThen I can ask a follow-up."}` |
| 211 |
turn, err := clnkr.ParseTurn(raw) |
| 212 |
if err != nil { |
| 213 |
t.Fatalf("ParseTurn(%q) error = %v", raw, err) |
| 214 |
} |
| 215 |
cl, ok := turn.(*clnkr.ClarifyTurn) |
| 216 |
if !ok { |
| 217 |
t.Fatalf("turn = %T, want *ClarifyTurn", turn) |
| 218 |
} |
| 219 |
wantQuestion := "Here are the skills I found:\n- **citations-agent**: Verify claims.\n- **humanizer**: Sound more natural.\nWhat would you like to work on?" |
| 220 |
if cl.Question != wantQuestion { |
| 221 |
t.Fatalf("question = %q, want %q", cl.Question, wantQuestion) |
| 222 |
} |
| 223 |
wantReasoning := "Need to list the available skills.\nThen I can ask a follow-up." |
| 224 |
if cl.Reasoning != wantReasoning { |
| 225 |
t.Fatalf("reasoning = %q, want %q", cl.Reasoning, wantReasoning) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func TestCanonicalRoundTrip(t *testing.T) { |
| 230 |
turns := []clnkr.Turn{ |
| 231 |
&clnkr.ActTurn{ |
| 232 |
Bash: clnkr.BashBatch{Commands: []clnkr.BashAction{{Command: "ls -la"}}}, |
| 233 |
Reasoning: "inspect files", |
| 234 |
}, |
| 235 |
&clnkr.ClarifyTurn{Question: "Which directory?"}, |
| 236 |
&clnkr.DoneTurn{ |
| 237 |
Summary: "Finished the task.", |
| 238 |
Verification: clnkr.CompletionVerification{ |
| 239 |
Status: clnkr.VerificationVerified, |
| 240 |
Checks: []clnkr.VerificationCheck{{ |
| 241 |
Command: "go test ./...", |
| 242 |
Outcome: "passed", |
| 243 |
Evidence: "test suite passed", |
| 244 |
}}, |
| 245 |
}, |
| 246 |
KnownRisks: []string{}, |
| 247 |
}, |
| 248 |
} |
| 249 |
|
| 250 |
for _, original := range turns { |
| 251 |
raw, err := clnkr.CanonicalTurnJSON(original) |
| 252 |
if err != nil { |
| 253 |
t.Fatalf("CanonicalTurnJSON(%T) error = %v", original, err) |
| 254 |
} |
| 255 |
parsed, err := clnkr.ParseTurn(raw) |
| 256 |
if err != nil { |
| 257 |
t.Fatalf("ParseTurn(%q) error = %v", raw, err) |
| 258 |
} |
| 259 |
roundTrip, err := clnkr.CanonicalTurnJSON(parsed) |
| 260 |
if err != nil { |
| 261 |
t.Fatalf("CanonicalTurnJSON(parsed) error = %v", err) |
| 262 |
} |
| 263 |
if roundTrip != raw { |
| 264 |
t.Fatalf("round trip = %q, want %q", roundTrip, raw) |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
func TestCanonicalTurnJSONAllowsMoreThanThreeCommands(t *testing.T) { |
| 270 |
raw, err := clnkr.CanonicalTurnJSON(&clnkr.ActTurn{ |
| 271 |
Bash: clnkr.BashBatch{Commands: []clnkr.BashAction{ |
| 272 |
{Command: "one"}, |
| 273 |
{Command: "two"}, |
| 274 |
{Command: "three"}, |
| 275 |
{Command: "four"}, |
| 276 |
}}, |
| 277 |
Reasoning: "batch", |
| 278 |
}) |
| 279 |
if err != nil { |
| 280 |
t.Fatalf("CanonicalTurnJSON error = %v", err) |
| 281 |
} |
| 282 |
parsed, err := clnkr.ParseTurn(raw) |
| 283 |
if err != nil { |
| 284 |
t.Fatalf("ParseTurn(%q) error = %v", raw, err) |
| 285 |
} |
| 286 |
act := parsed.(*clnkr.ActTurn) |
| 287 |
if got := len(act.Bash.Commands); got != 4 { |
| 288 |
t.Fatalf("commands = %d, want 4", got) |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
func TestCanonicalTurnJSONUsesEmptyArraysForNilDoneSlices(t *testing.T) { |
| 293 |
raw, err := clnkr.CanonicalTurnJSON(&clnkr.DoneTurn{ |
| 294 |
Summary: "Could not verify.", |
| 295 |
Verification: clnkr.CompletionVerification{ |
| 296 |
Status: clnkr.VerificationNotVerified, |
| 297 |
}, |
| 298 |
}) |
| 299 |
if err != nil { |
| 300 |
t.Fatalf("CanonicalTurnJSON error = %v", err) |
| 301 |
} |
| 302 |
if !strings.Contains(raw, `"checks":[]`) { |
| 303 |
t.Fatalf("canonical JSON = %q, want empty checks array", raw) |
| 304 |
} |
| 305 |
if !strings.Contains(raw, `"known_risks":[]`) { |
| 306 |
t.Fatalf("canonical JSON = %q, want empty known_risks array", raw) |
| 307 |
} |
| 308 |
if _, err := clnkr.ParseTurn(raw); err != nil { |
| 309 |
t.Fatalf("ParseTurn(%q) error = %v", raw, err) |
| 310 |
} |
| 311 |
} |