|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/minicodemonkey/chief/internal/loop" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCursorProvider_Name(t *testing.T) { |
| 11 | + p := NewCursorProvider("") |
| 12 | + if p.Name() != "Cursor" { |
| 13 | + t.Errorf("Name() = %q, want Cursor", p.Name()) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func TestCursorProvider_CLIPath(t *testing.T) { |
| 18 | + p := NewCursorProvider("") |
| 19 | + if p.CLIPath() != "agent" { |
| 20 | + t.Errorf("CLIPath() empty arg = %q, want agent", p.CLIPath()) |
| 21 | + } |
| 22 | + p2 := NewCursorProvider("/usr/local/bin/agent") |
| 23 | + if p2.CLIPath() != "/usr/local/bin/agent" { |
| 24 | + t.Errorf("CLIPath() custom = %q, want /usr/local/bin/agent", p2.CLIPath()) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestCursorProvider_LogFileName(t *testing.T) { |
| 29 | + p := NewCursorProvider("") |
| 30 | + if p.LogFileName() != "cursor.log" { |
| 31 | + t.Errorf("LogFileName() = %q, want cursor.log", p.LogFileName()) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestCursorProvider_LoopCommand(t *testing.T) { |
| 36 | + ctx := context.Background() |
| 37 | + p := NewCursorProvider("/bin/agent") |
| 38 | + cmd := p.LoopCommand(ctx, "hello world", "/work/dir") |
| 39 | + |
| 40 | + if cmd.Path != "/bin/agent" { |
| 41 | + t.Errorf("LoopCommand Path = %q, want /bin/agent", cmd.Path) |
| 42 | + } |
| 43 | + wantArgs := []string{"/bin/agent", "-p", "--output-format", "stream-json", "--force", "--workspace", "/work/dir", "--trust"} |
| 44 | + if len(cmd.Args) != len(wantArgs) { |
| 45 | + t.Fatalf("LoopCommand Args len = %d, want %d: %v", len(cmd.Args), len(wantArgs), cmd.Args) |
| 46 | + } |
| 47 | + for i, w := range wantArgs { |
| 48 | + if cmd.Args[i] != w { |
| 49 | + t.Errorf("LoopCommand Args[%d] = %q, want %q", i, cmd.Args[i], w) |
| 50 | + } |
| 51 | + } |
| 52 | + if cmd.Dir != "/work/dir" { |
| 53 | + t.Errorf("LoopCommand Dir = %q, want /work/dir", cmd.Dir) |
| 54 | + } |
| 55 | + if cmd.Stdin == nil { |
| 56 | + t.Error("LoopCommand Stdin must be set (prompt via stdin)") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestCursorProvider_InteractiveCommand(t *testing.T) { |
| 61 | + p := NewCursorProvider("/bin/agent") |
| 62 | + cmd := p.InteractiveCommand("/work", "my prompt") |
| 63 | + if cmd.Dir != "/work" { |
| 64 | + t.Errorf("InteractiveCommand Dir = %q, want /work", cmd.Dir) |
| 65 | + } |
| 66 | + if len(cmd.Args) != 2 || cmd.Args[0] != "/bin/agent" || cmd.Args[1] != "my prompt" { |
| 67 | + t.Errorf("InteractiveCommand Args = %v, want [/bin/agent my prompt]", cmd.Args) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestCursorProvider_ParseLine(t *testing.T) { |
| 72 | + p := NewCursorProvider("") |
| 73 | + line := `{"type":"system","subtype":"init","session_id":"x"}` |
| 74 | + e := p.ParseLine(line) |
| 75 | + if e == nil { |
| 76 | + t.Fatal("ParseLine(system init) returned nil") |
| 77 | + } |
| 78 | + if e.Type != loop.EventIterationStart { |
| 79 | + t.Errorf("ParseLine(system init) Type = %v, want EventIterationStart", e.Type) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestCursorProvider_CleanOutput(t *testing.T) { |
| 84 | + p := NewCursorProvider("") |
| 85 | + // NDJSON: last result/success |
| 86 | + ndjson := `{"type":"system","subtype":"init"} |
| 87 | +{"type":"result","subtype":"success","result":"final answer","session_id":"x"}` |
| 88 | + if got := p.CleanOutput(ndjson); got != "final answer" { |
| 89 | + t.Errorf("CleanOutput(NDJSON) = %q, want final answer", got) |
| 90 | + } |
| 91 | + // Single JSON result |
| 92 | + single := `{"type":"result","subtype":"success","result":"single result","session_id":"x"}` |
| 93 | + if got := p.CleanOutput(single); got != "single result" { |
| 94 | + t.Errorf("CleanOutput(single JSON) = %q, want single result", got) |
| 95 | + } |
| 96 | + // No result: return as-is |
| 97 | + plain := "plain text" |
| 98 | + if got := p.CleanOutput(plain); got != plain { |
| 99 | + t.Errorf("CleanOutput(plain) = %q, want %q", got, plain) |
| 100 | + } |
| 101 | +} |
0 commit comments