|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + iteragent "github.com/GrayCodeAI/iteragent" |
| 9 | +) |
| 10 | + |
| 11 | +// ── /run ────────────────────────────────────────────────────────────────────── |
| 12 | + |
| 13 | +func TestCmdRun_NoArgs(t *testing.T) { |
| 14 | + ctx := Context{Parts: []string{"/run"}} |
| 15 | + result := cmdRun(ctx) |
| 16 | + if !result.Handled { |
| 17 | + t.Error("expected handled") |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestCmdRun_CapturesOutput(t *testing.T) { |
| 22 | + lastRunMu.Lock() |
| 23 | + lastRunOutput = "" |
| 24 | + lastRunMu.Unlock() |
| 25 | + |
| 26 | + ctx := Context{ |
| 27 | + Parts: []string{"/run", "echo", "hello"}, |
| 28 | + RepoPath: t.TempDir(), |
| 29 | + } |
| 30 | + result := cmdRun(ctx) |
| 31 | + if !result.Handled { |
| 32 | + t.Error("expected handled") |
| 33 | + } |
| 34 | + out := LastRunOutput() |
| 35 | + if !strings.Contains(out, "hello") { |
| 36 | + t.Errorf("expected 'hello' in captured output, got %q", out) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestCmdRun_AskFlag_NoArg(t *testing.T) { |
| 41 | + ctx := Context{Parts: []string{"/run", "--ask"}} |
| 42 | + result := cmdRun(ctx) |
| 43 | + if !result.Handled { |
| 44 | + t.Error("expected handled") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestCmdRun_AskFlag_SendsPrompt(t *testing.T) { |
| 49 | + var gotPrompt string |
| 50 | + ctx := Context{ |
| 51 | + Parts: []string{"/run", "--ask", "echo", "world"}, |
| 52 | + RepoPath: t.TempDir(), |
| 53 | + Agent: &iteragent.Agent{}, |
| 54 | + REPL: REPLCallbacks{ |
| 55 | + StreamAndPrint: func(_ context.Context, _ *iteragent.Agent, prompt, _ string) { |
| 56 | + gotPrompt = prompt |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + lastRunMu.Lock() |
| 61 | + lastRunOutput = "" |
| 62 | + lastRunMu.Unlock() |
| 63 | + |
| 64 | + result := cmdRun(ctx) |
| 65 | + if !result.Handled { |
| 66 | + t.Error("expected handled") |
| 67 | + } |
| 68 | + if !strings.Contains(gotPrompt, "echo world") { |
| 69 | + t.Errorf("expected command in prompt, got %q", gotPrompt) |
| 70 | + } |
| 71 | + if !strings.Contains(gotPrompt, "world") { |
| 72 | + t.Errorf("expected output in prompt, got %q", gotPrompt) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// ── /explain-error ───────────────────────────────────────────────────────────── |
| 77 | + |
| 78 | +func TestCmdExplainError_NoArg_NoLastRun(t *testing.T) { |
| 79 | + lastRunMu.Lock() |
| 80 | + lastRunOutput = "" |
| 81 | + lastRunMu.Unlock() |
| 82 | + |
| 83 | + ctx := Context{ |
| 84 | + Parts: []string{"/explain-error"}, |
| 85 | + Line: "/explain-error", |
| 86 | + } |
| 87 | + result := cmdExplainError(ctx) |
| 88 | + if !result.Handled { |
| 89 | + t.Error("expected handled") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestCmdExplainError_NoArg_UsesLastRun(t *testing.T) { |
| 94 | + lastRunMu.Lock() |
| 95 | + lastRunOutput = "error: undefined: foo" |
| 96 | + lastRunMu.Unlock() |
| 97 | + defer func() { |
| 98 | + lastRunMu.Lock() |
| 99 | + lastRunOutput = "" |
| 100 | + lastRunMu.Unlock() |
| 101 | + }() |
| 102 | + |
| 103 | + var gotPrompt string |
| 104 | + ctx := Context{ |
| 105 | + Parts: []string{"/explain-error"}, |
| 106 | + Line: "/explain-error", |
| 107 | + Agent: &iteragent.Agent{}, |
| 108 | + REPL: REPLCallbacks{ |
| 109 | + StreamAndPrint: func(_ context.Context, _ *iteragent.Agent, prompt, _ string) { |
| 110 | + gotPrompt = prompt |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | + result := cmdExplainError(ctx) |
| 115 | + if !result.Handled { |
| 116 | + t.Error("expected handled") |
| 117 | + } |
| 118 | + if !strings.Contains(gotPrompt, "undefined: foo") { |
| 119 | + t.Errorf("expected last run output in prompt, got %q", gotPrompt) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestCmdExplainError_WithArg(t *testing.T) { |
| 124 | + var gotPrompt string |
| 125 | + ctx := Context{ |
| 126 | + Parts: []string{"/explain-error", "segfault"}, |
| 127 | + Line: "/explain-error segfault", |
| 128 | + Agent: &iteragent.Agent{}, |
| 129 | + REPL: REPLCallbacks{ |
| 130 | + StreamAndPrint: func(_ context.Context, _ *iteragent.Agent, prompt, _ string) { |
| 131 | + gotPrompt = prompt |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | + result := cmdExplainError(ctx) |
| 136 | + if !result.Handled { |
| 137 | + t.Error("expected handled") |
| 138 | + } |
| 139 | + if !strings.Contains(gotPrompt, "segfault") { |
| 140 | + t.Errorf("expected error text in prompt, got %q", gotPrompt) |
| 141 | + } |
| 142 | +} |
0 commit comments