| 1 |
package actwire |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/clnkr-ai/clnkr" |
| 9 |
) |
| 10 |
|
| 11 |
type schemaAssert func(*testing.T, map[string]any) |
| 12 |
|
| 13 |
func TestSchemasExposeAllowedTurnShapes(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
name string |
| 16 |
schema map[string]any |
| 17 |
asserts []schemaAssert |
| 18 |
}{ |
| 19 |
{ |
| 20 |
name: "request", |
| 21 |
schema: RequestSchema(), |
| 22 |
asserts: []schemaAssert{ |
| 23 |
assertActTurnSchema, |
| 24 |
assertClarifyTurnSchema, |
| 25 |
assertDoneTurnSchema, |
| 26 |
}, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "unattended request", |
| 30 |
schema: UnattendedRequestSchema(), |
| 31 |
asserts: []schemaAssert{assertActTurnSchema, assertDoneTurnSchema}, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "final turn", |
| 35 |
schema: FinalTurnSchema(), |
| 36 |
asserts: []schemaAssert{assertClarifyTurnSchema, assertDoneTurnSchema}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "done only", |
| 40 |
schema: DoneOnlySchema(), |
| 41 |
asserts: []schemaAssert{assertDoneTurnSchema}, |
| 42 |
}, |
| 43 |
} |
| 44 |
|
| 45 |
for _, tt := range tests { |
| 46 |
t.Run(tt.name, func(t *testing.T) { |
| 47 |
choices := turnChoices(t, tt.schema) |
| 48 |
if len(choices) != len(tt.asserts) { |
| 49 |
t.Fatalf("turn choices = %d, want %d", len(choices), len(tt.asserts)) |
| 50 |
} |
| 51 |
for i, assert := range tt.asserts { |
| 52 |
assert(t, choices[i]) |
| 53 |
} |
| 54 |
}) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func TestParseProviderTurn(t *testing.T) { |
| 59 |
tests := []struct { |
| 60 |
name string |
| 61 |
raw string |
| 62 |
want clnkr.Turn |
| 63 |
wantErr error |
| 64 |
}{ |
| 65 |
{ |
| 66 |
name: "act", |
| 67 |
raw: `{"turn":{"type":"act","bash":{"commands":[{"command":"pwd","workdir":"/tmp"}]},"question":null,"summary":null,"reasoning":"inspect"}}`, |
| 68 |
want: &clnkr.ActTurn{ |
| 69 |
Bash: clnkr.BashBatch{ |
| 70 |
Commands: []clnkr.BashAction{{Command: "pwd", Workdir: "/tmp"}}, |
| 71 |
}, |
| 72 |
Reasoning: "inspect", |
| 73 |
}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "act with more than three commands", |
| 77 |
raw: `{"turn":{"type":"act","bash":{"commands":[{"command":"one","workdir":null},{"command":"two","workdir":null},{"command":"three","workdir":null},{"command":"four","workdir":null}]},"question":null,"summary":null,"reasoning":"batch"}}`, |
| 78 |
want: &clnkr.ActTurn{ |
| 79 |
Bash: clnkr.BashBatch{Commands: []clnkr.BashAction{ |
| 80 |
{Command: "one"}, |
| 81 |
{Command: "two"}, |
| 82 |
{Command: "three"}, |
| 83 |
{Command: "four"}, |
| 84 |
}}, |
| 85 |
Reasoning: "batch", |
| 86 |
}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "clarify", |
| 90 |
raw: `{"turn":{"type":"clarify","bash":null,"question":"Which repo?","summary":null,"reasoning":"need target"}}`, |
| 91 |
want: &clnkr.ClarifyTurn{Question: "Which repo?", Reasoning: "need target"}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "done", |
| 95 |
raw: `{"turn":{"type":"done","bash":null,"question":null,"summary":"complete","verification":{"status":"verified","checks":[{"command":"go test ./...","outcome":"passed","evidence":"test suite passed"}]},"known_risks":[],"reasoning":"tests passed"}}`, |
| 96 |
want: &clnkr.DoneTurn{ |
| 97 |
Summary: "complete", |
| 98 |
Verification: clnkr.CompletionVerification{ |
| 99 |
Status: clnkr.VerificationVerified, |
| 100 |
Checks: []clnkr.VerificationCheck{{ |
| 101 |
Command: "go test ./...", |
| 102 |
Outcome: "passed", |
| 103 |
Evidence: "test suite passed", |
| 104 |
}}, |
| 105 |
}, |
| 106 |
KnownRisks: []string{}, |
| 107 |
Reasoning: "tests passed", |
| 108 |
}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "done without verification", |
| 112 |
raw: `{"turn":{"type":"done","bash":null,"question":null,"summary":"complete","reasoning":null}}`, |
| 113 |
wantErr: clnkr.ErrInvalidJSON, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "done with null known risks", |
| 117 |
raw: `{"turn":{"type":"done","bash":null,"question":null,"summary":"complete","verification":{"status":"not_verified","checks":[]},"known_risks":null,"reasoning":null}}`, |
| 118 |
wantErr: clnkr.ErrInvalidJSON, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "done with null verification checks", |
| 122 |
raw: `{"turn":{"type":"done","bash":null,"question":null,"summary":"complete","verification":{"status":"not_verified","checks":null},"known_risks":[],"reasoning":null}}`, |
| 123 |
wantErr: clnkr.ErrInvalidJSON, |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "malformed wrapped act shape", |
| 127 |
raw: `{"turn":{"type":"act","bash":{"command":"pwd","workdir":null},"question":null,"summary":null,"reasoning":null}}`, |
| 128 |
wantErr: clnkr.ErrInvalidJSON, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "semantic invalid act", |
| 132 |
raw: `{"turn":{"type":"act","bash":{"commands":[{"command":"","workdir":null}]},"question":null,"summary":null,"reasoning":null}}`, |
| 133 |
wantErr: clnkr.ErrMissingCommand, |
| 134 |
}, |
| 135 |
} |
| 136 |
|
| 137 |
for _, tt := range tests { |
| 138 |
t.Run(tt.name, func(t *testing.T) { |
| 139 |
got, err := ParseProviderTurn(tt.raw) |
| 140 |
if tt.wantErr != nil { |
| 141 |
if !errors.Is(err, tt.wantErr) { |
| 142 |
t.Fatalf("error = %v, want %v", err, tt.wantErr) |
| 143 |
} |
| 144 |
return |
| 145 |
} |
| 146 |
if err != nil { |
| 147 |
t.Fatalf("unexpected error: %v", err) |
| 148 |
} |
| 149 |
if !reflect.DeepEqual(got, tt.want) { |
| 150 |
t.Fatalf("turn = %#v, want %#v", got, tt.want) |
| 151 |
} |
| 152 |
}) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func TestNormalizeMessagesForProvider(t *testing.T) { |
| 157 |
messages := []clnkr.Message{ |
| 158 |
{Role: "user", Content: `{"type":"done","summary":"leave users alone"}`}, |
| 159 |
{ |
| 160 |
Role: "assistant", |
| 161 |
Content: `{"type":"done","summary":"canonical","verification":{"status":"verified","checks":[{"command":"go test ./...","outcome":"passed","evidence":"go test ./... passed and ls output showed current directory entries for completion"}]},"known_risks":[],"reasoning":"ok"}`, |
| 162 |
}, |
| 163 |
{ |
| 164 |
Role: "assistant", |
| 165 |
Content: `{"turn":{"type":"clarify","bash":null,"question":"Which repo?","summary":null,"reasoning":null}}`, |
| 166 |
}, |
| 167 |
{Role: "assistant", Content: "plain text"}, |
| 168 |
} |
| 169 |
|
| 170 |
got := NormalizeMessagesForProvider(messages) |
| 171 |
want := []clnkr.Message{ |
| 172 |
messages[0], |
| 173 |
{ |
| 174 |
Role: "assistant", |
| 175 |
Content: `{"turn":{"type":"done","bash":null,"question":null,"summary":"canonical","verification":{"status":"verified","checks":[{"command":"go test ./...","outcome":"passed","evidence":"go test ./... passed and ls output showed current directory entries for completion"}]},"known_risks":[],"reasoning":"ok"}}`, |
| 176 |
}, |
| 177 |
messages[2], |
| 178 |
messages[3], |
| 179 |
} |
| 180 |
if !reflect.DeepEqual(got, want) { |
| 181 |
t.Fatalf("messages = %#v, want %#v", got, want) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func TestParseProviderTurnNormalizesEscapedHumanText(t *testing.T) { |
| 186 |
clarify, err := ParseProviderTurn( |
| 187 |
`{"turn":{"type":"clarify","bash":null,"question":"First\\n\\\\- escaped","summary":null,"reasoning":"Because\\n\\\\* escaped"}}`, |
| 188 |
) |
| 189 |
if err != nil { |
| 190 |
t.Fatalf("parse clarify: %v", err) |
| 191 |
} |
| 192 |
if got := clarify.(*clnkr.ClarifyTurn); got.Question != "First\n- escaped" || |
| 193 |
got.Reasoning != "Because\n* escaped" { |
| 194 |
t.Fatalf("clarify = %#v", got) |
| 195 |
} |
| 196 |
|
| 197 |
done, err := ParseProviderTurn( |
| 198 |
`{"turn":{"type":"done","bash":null,"question":null,"summary":"Fixed\\n\\\\- item","verification":{"status":"verified","checks":[{"command":"go test ./...","outcome":"passed","evidence":"go test ./... passed and ls output showed current directory entries for completion"}]},"known_risks":[],"reasoning":"Reason\\n\\\\* item"}}`, |
| 199 |
) |
| 200 |
if err != nil { |
| 201 |
t.Fatalf("parse done: %v", err) |
| 202 |
} |
| 203 |
if got := done.(*clnkr.DoneTurn); got.Summary != "Fixed\n- item" || |
| 204 |
got.Reasoning != "Reason\n* item" { |
| 205 |
t.Fatalf("done = %#v", got) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func turnChoices(t *testing.T, schema map[string]any) []map[string]any { |
| 210 |
t.Helper() |
| 211 |
|
| 212 |
if schema["type"] != "object" || schema["additionalProperties"] != false { |
| 213 |
t.Fatalf("schema envelope = %#v", schema) |
| 214 |
} |
| 215 |
if got := schema["required"]; !reflect.DeepEqual(got, []string{"turn"}) { |
| 216 |
t.Fatalf("schema required = %#v, want turn", got) |
| 217 |
} |
| 218 |
|
| 219 |
choices := schema["properties"].(map[string]any)["turn"].(map[string]any)["anyOf"].([]any) |
| 220 |
branches := make([]map[string]any, 0, len(choices)) |
| 221 |
for _, choice := range choices { |
| 222 |
branches = append(branches, choice.(map[string]any)) |
| 223 |
} |
| 224 |
return branches |
| 225 |
} |
| 226 |
|
| 227 |
func assertActTurnSchema(t *testing.T, schema map[string]any) { |
| 228 |
t.Helper() |
| 229 |
|
| 230 |
properties := assertTurnSchema( |
| 231 |
t, |
| 232 |
schema, |
| 233 |
"act", |
| 234 |
[]string{"type", "bash", "question", "summary", "reasoning"}, |
| 235 |
) |
| 236 |
assertNullSchema(t, properties, "question") |
| 237 |
assertNullSchema(t, properties, "summary") |
| 238 |
assertNullableStringSchema(t, properties["reasoning"]) |
| 239 |
|
| 240 |
bash := properties["bash"].(map[string]any) |
| 241 |
if bash["type"] != "object" || bash["additionalProperties"] != false { |
| 242 |
t.Fatalf("act bash schema = %#v", bash) |
| 243 |
} |
| 244 |
if got := bash["required"]; !reflect.DeepEqual(got, []string{"commands"}) { |
| 245 |
t.Fatalf("act bash required = %#v, want commands", got) |
| 246 |
} |
| 247 |
|
| 248 |
commands := bash["properties"].(map[string]any)["commands"].(map[string]any) |
| 249 |
if commands["type"] != "array" || commands["minItems"] != 1 { |
| 250 |
t.Fatalf("act commands schema = %#v", commands) |
| 251 |
} |
| 252 |
command := commands["items"].(map[string]any) |
| 253 |
if command["type"] != "object" || command["additionalProperties"] != false { |
| 254 |
t.Fatalf("act command item schema = %#v", command) |
| 255 |
} |
| 256 |
if got := command["required"]; !reflect.DeepEqual(got, []string{"command", "workdir"}) { |
| 257 |
t.Fatalf("act command required = %#v, want command and workdir", got) |
| 258 |
} |
| 259 |
commandProperties := command["properties"].(map[string]any) |
| 260 |
if got := commandProperties["command"]; !reflect.DeepEqual( |
| 261 |
got, |
| 262 |
map[string]any{"type": "string"}, |
| 263 |
) { |
| 264 |
t.Fatalf("act command property = %#v", got) |
| 265 |
} |
| 266 |
assertNullableStringSchema(t, commandProperties["workdir"]) |
| 267 |
} |
| 268 |
|
| 269 |
func assertClarifyTurnSchema(t *testing.T, schema map[string]any) { |
| 270 |
t.Helper() |
| 271 |
|
| 272 |
properties := assertTurnSchema( |
| 273 |
t, |
| 274 |
schema, |
| 275 |
"clarify", |
| 276 |
[]string{"type", "bash", "question", "summary", "reasoning"}, |
| 277 |
) |
| 278 |
assertNullSchema(t, properties, "bash") |
| 279 |
if got := properties["question"]; !reflect.DeepEqual(got, map[string]any{"type": "string"}) { |
| 280 |
t.Fatalf("clarify question schema = %#v", got) |
| 281 |
} |
| 282 |
assertNullSchema(t, properties, "summary") |
| 283 |
assertNullableStringSchema(t, properties["reasoning"]) |
| 284 |
} |
| 285 |
|
| 286 |
func assertDoneTurnSchema(t *testing.T, schema map[string]any) { |
| 287 |
t.Helper() |
| 288 |
|
| 289 |
properties := assertTurnSchema( |
| 290 |
t, |
| 291 |
schema, |
| 292 |
"done", |
| 293 |
[]string{"type", "bash", "question", "summary", "verification", "known_risks", "reasoning"}, |
| 294 |
) |
| 295 |
assertNullSchema(t, properties, "bash") |
| 296 |
assertNullSchema(t, properties, "question") |
| 297 |
if got := properties["summary"]; !reflect.DeepEqual(got, map[string]any{"type": "string"}) { |
| 298 |
t.Fatalf("done summary schema = %#v", got) |
| 299 |
} |
| 300 |
assertNullableStringSchema(t, properties["reasoning"]) |
| 301 |
|
| 302 |
verification := properties["verification"].(map[string]any) |
| 303 |
if verification["type"] != "object" || verification["additionalProperties"] != false { |
| 304 |
t.Fatalf("verification schema = %#v", verification) |
| 305 |
} |
| 306 |
if got := verification["required"]; !reflect.DeepEqual(got, []string{"status", "checks"}) { |
| 307 |
t.Fatalf("verification required = %#v, want status and checks", got) |
| 308 |
} |
| 309 |
verificationProperties := verification["properties"].(map[string]any) |
| 310 |
if got := verificationProperties["status"]; !reflect.DeepEqual( |
| 311 |
got, |
| 312 |
map[string]any{ |
| 313 |
"type": "string", |
| 314 |
"enum": []string{"verified", "partially_verified", "not_verified"}, |
| 315 |
}, |
| 316 |
) { |
| 317 |
t.Fatalf("verification status schema = %#v", got) |
| 318 |
} |
| 319 |
checks := verificationProperties["checks"].(map[string]any) |
| 320 |
if checks["type"] != "array" { |
| 321 |
t.Fatalf("verification checks schema = %#v", checks) |
| 322 |
} |
| 323 |
check := checks["items"].(map[string]any) |
| 324 |
if check["type"] != "object" || check["additionalProperties"] != false { |
| 325 |
t.Fatalf("verification check schema = %#v", check) |
| 326 |
} |
| 327 |
if got := check["required"]; !reflect.DeepEqual( |
| 328 |
got, |
| 329 |
[]string{"command", "outcome", "evidence"}, |
| 330 |
) { |
| 331 |
t.Fatalf("verification check required = %#v, want command, outcome, evidence", got) |
| 332 |
} |
| 333 |
checkProperties := check["properties"].(map[string]any) |
| 334 |
for _, name := range []string{"command", "outcome", "evidence"} { |
| 335 |
if got := checkProperties[name]; !reflect.DeepEqual(got, map[string]any{"type": "string"}) { |
| 336 |
t.Fatalf("verification check %s schema = %#v", name, got) |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
risks := properties["known_risks"].(map[string]any) |
| 341 |
if got := risks; !reflect.DeepEqual( |
| 342 |
got, |
| 343 |
map[string]any{"type": "array", "items": map[string]any{"type": "string"}}, |
| 344 |
) { |
| 345 |
t.Fatalf("known risks schema = %#v", got) |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
func assertTurnSchema( |
| 350 |
t *testing.T, |
| 351 |
schema map[string]any, |
| 352 |
typ string, |
| 353 |
required []string, |
| 354 |
) map[string]any { |
| 355 |
t.Helper() |
| 356 |
|
| 357 |
if schema["type"] != "object" || schema["additionalProperties"] != false { |
| 358 |
t.Fatalf("%s schema = %#v", typ, schema) |
| 359 |
} |
| 360 |
if got := schema["required"]; !reflect.DeepEqual(got, required) { |
| 361 |
t.Fatalf("%s required = %#v, want %#v", typ, got, required) |
| 362 |
} |
| 363 |
properties := schema["properties"].(map[string]any) |
| 364 |
if got := properties["type"]; !reflect.DeepEqual( |
| 365 |
got, |
| 366 |
map[string]any{"type": "string", "const": typ}, |
| 367 |
) { |
| 368 |
t.Fatalf("%s type schema = %#v", typ, got) |
| 369 |
} |
| 370 |
return properties |
| 371 |
} |
| 372 |
|
| 373 |
func assertNullSchema(t *testing.T, properties map[string]any, name string) { |
| 374 |
t.Helper() |
| 375 |
|
| 376 |
if got := properties[name]; !reflect.DeepEqual(got, map[string]any{"type": "null"}) { |
| 377 |
t.Fatalf("%s schema = %#v, want null", name, got) |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
func assertNullableStringSchema(t *testing.T, got any) { |
| 382 |
t.Helper() |
| 383 |
|
| 384 |
want := map[string]any{ |
| 385 |
"anyOf": []any{map[string]any{"type": "string"}, map[string]any{"type": "null"}}, |
| 386 |
} |
| 387 |
if !reflect.DeepEqual(got, want) { |
| 388 |
t.Fatalf("nullable string schema = %#v, want %#v", got, want) |
| 389 |
} |
| 390 |
} |