| 1 | package agents |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func one(t *testing.T, line string) Act { |
| 6 | t.Helper() |
| 7 | acts := Translate([]byte(line)) |
| 8 | if len(acts) != 1 { |
| 9 | t.Fatalf("Translate(%s) = %+v, want one act", line, acts) |
| 10 | } |
| 11 | return acts[0] |
| 12 | } |
| 13 | |
| 14 | func TestTranslateInit(t *testing.T) { |
| 15 | a := one(t, `{"type":"system","subtype":"init","session_id":"cs-123","model":"x"}`) |
| 16 | if a.Kind != "init" || a.ClaudeSessionID != "cs-123" { |
| 17 | t.Errorf("init: %+v", a) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestTranslateToolUse(t *testing.T) { |
| 22 | a := one(t, `{"type":"assistant","message":{"content":[{"type":"text","text":"Let me check."},{"type":"tool_use","name":"Bash","input":{"command":"go test ./internal/bot"}}]}}`) |
| 23 | if a.Kind != "tool" || a.Tool != "Ran go test ./internal/bot" { |
| 24 | t.Errorf("tool: %+v", a) |
| 25 | } |
| 26 | a = one(t, `{"type":"assistant","message":{"content":[{"type":"tool_use","name":"Read","input":{"file_path":"/x/y.go"}}]}}`) |
| 27 | if a.Kind != "tool" || a.Tool != "Read /x/y.go" { |
| 28 | t.Errorf("tool: %+v", a) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestTranslateResultTurn(t *testing.T) { |
| 33 | a := one(t, `{"type":"result","subtype":"success","result":"Fixed — the ticker fired early.","is_error":false,"session_id":"cs-123"}`) |
| 34 | if a.Kind != "turn" || a.Text != "Fixed — the ticker fired early." { |
| 35 | t.Errorf("turn: %+v", a) |
| 36 | } |
| 37 | a = one(t, `{"type":"result","subtype":"error_during_execution","result":"boom","is_error":true}`) |
| 38 | if a.Kind != "error" || a.Text != "boom" { |
| 39 | t.Errorf("error: %+v", a) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestTranslateIgnoresNoise(t *testing.T) { |
| 44 | for _, line := range []string{ |
| 45 | "not json", |
| 46 | `{"type":"user","message":{"content":[{"type":"tool_result"}]}}`, |
| 47 | `{"type":"stream_event","event":{"type":"content_block_delta"}}`, |
| 48 | `{"type":"result","result":"","is_error":false}`, |
| 49 | `{"type":"system","subtype":"compact"}`, |
| 50 | } { |
| 51 | if acts := Translate([]byte(line)); len(acts) != 0 { |
| 52 | t.Errorf("Translate(%s) = %+v, want none", line, acts) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestClampLineFlattens(t *testing.T) { |
| 58 | if got := clampLine("a\n b\tc", 100); got != "a b c" { |
| 59 | t.Errorf("clampLine: %q", got) |
| 60 | } |
| 61 | } |
| 62 | |