| 1 |
package clnkr |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestProtocolCorrectionMessage(t *testing.T) { |
| 11 |
tests := []struct { |
| 12 |
name string |
| 13 |
err error |
| 14 |
protocol ActProtocol |
| 15 |
contains []string |
| 16 |
notContains []string |
| 17 |
}{ |
| 18 |
{ |
| 19 |
name: "states prior response was ignored", |
| 20 |
err: fmt.Errorf("%w: unexpected trailing JSON value", ErrInvalidJSON), |
| 21 |
protocol: ActProtocolClnkrInline, |
| 22 |
contains: []string{ |
| 23 |
"Your previous response was ignored and no command ran.", |
| 24 |
"If you intended to run commands, resend only that act turn.", |
| 25 |
"Do not jump to done unless prior command results in this conversation already prove the task is complete.", |
| 26 |
protocolActExample, |
| 27 |
"Do not emit multiple JSON objects in one response.", |
| 28 |
"Do not emit an act turn and a done turn together.", |
| 29 |
"Include reasoning in every response; use null if you have nothing to add.", |
| 30 |
`"verification"`, |
| 31 |
`"known_risks"`, |
| 32 |
}, |
| 33 |
notContains: []string{ |
| 34 |
`top-level "turn" field`, |
| 35 |
`{"type":"clarify"`, |
| 36 |
`{"type":"done"`, |
| 37 |
}, |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "mentions invalid pipe escape", |
| 41 |
err: fmt.Errorf("%w: invalid character '|' in string escape code", ErrInvalidJSON), |
| 42 |
protocol: ActProtocolClnkrInline, |
| 43 |
contains: []string{`\|`, `\\|`}, |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: "mentions invalid backtick escape", |
| 47 |
err: fmt.Errorf("%w: invalid character '`' in string escape code", ErrInvalidJSON), |
| 48 |
protocol: ActProtocolClnkrInline, |
| 49 |
contains: []string{"\\`", "\\\\"}, |
| 50 |
}, |
| 51 |
} |
| 52 |
|
| 53 |
for _, tt := range tests { |
| 54 |
t.Run(tt.name, func(t *testing.T) { |
| 55 |
msg := protocolCorrectionMessageFor(tt.err, tt.protocol) |
| 56 |
for _, want := range tt.contains { |
| 57 |
if !strings.Contains(msg, want) { |
| 58 |
t.Fatalf("message missing %q: %q", want, msg) |
| 59 |
} |
| 60 |
} |
| 61 |
for _, unwanted := range tt.notContains { |
| 62 |
if strings.Contains(msg, unwanted) { |
| 63 |
t.Fatalf("message contains %q: %q", unwanted, msg) |
| 64 |
} |
| 65 |
} |
| 66 |
}) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestTurnTypes(t *testing.T) { |
| 71 |
for _, turn := range []Turn{ |
| 72 |
ActTurn{Bash: BashBatch{Commands: []BashAction{{Command: "ls"}}}}, |
| 73 |
&ActTurn{Bash: BashBatch{Commands: []BashAction{{Command: "ls"}}}}, |
| 74 |
ClarifyTurn{Question: "which dir?"}, |
| 75 |
DoneTurn{Summary: "done"}, |
| 76 |
} { |
| 77 |
turn.turn() |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestErrorToReason(t *testing.T) { |
| 82 |
tests := []struct { |
| 83 |
err error |
| 84 |
want string |
| 85 |
}{ |
| 86 |
{ErrInvalidJSON, "invalid_json"}, |
| 87 |
{ErrMissingCommand, "missing_command"}, |
| 88 |
{ErrEmptyClarify, "empty_clarify"}, |
| 89 |
{ErrEmptySummary, "empty_summary"}, |
| 90 |
{ErrUnknownTurnType, "unknown_turn_type"}, |
| 91 |
{errors.New("something else"), "unknown"}, |
| 92 |
} |
| 93 |
for _, tt := range tests { |
| 94 |
if got := errorToReason(tt.err); got != tt.want { |
| 95 |
t.Errorf("errorToReason(%v) = %q, want %q", tt.err, got, tt.want) |
| 96 |
} |
| 97 |
} |
| 98 |
} |