| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "sync/atomic" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider/claude" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // An Engineering step that commits nothing ends no_changes, unless Equipo |
| 14 | // refused tool requests the step made: then it ends tools_refused, so |
| 15 | // the person reads why nothing changed. |
| 16 | func TestEmptyStepReason(t *testing.T) { |
| 17 | if got := emptyStepReason(stepResult{}); got != "no_changes" { |
| 18 | t.Fatalf("no refusals: %q", got) |
| 19 | } |
| 20 | if got := emptyStepReason(stepResult{refused: 2}); got != "tools_refused" { |
| 21 | t.Fatalf("refusals: %q", got) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Claude's permission callback counts each request it denies (here a |
| 26 | // shell command under a policy that keeps none in the sandbox, and a |
| 27 | // request with a field the step does not accept), and never one it |
| 28 | // allows. |
| 29 | func TestClaudeStepCountsRefusals(t *testing.T) { |
| 30 | var n atomic.Int64 |
| 31 | answer := countRefusals(claude.StepPermission, &n) |
| 32 | policy := provider.Policy{} |
| 33 | for _, raw := range []string{ |
| 34 | `{"tool_name":"Bash","input":{"command":"go test ./..."}}`, |
| 35 | `{"tool_name":"Read","input":{"file_path":"/workspace/README.md"},"extra":1}`, |
| 36 | } { |
| 37 | if out := answer(json.RawMessage(raw), policy); !denied(out) { |
| 38 | t.Fatalf("%s allowed: %s", raw, out) |
| 39 | } |
| 40 | } |
| 41 | if n.Load() != 2 { |
| 42 | t.Fatalf("counted %d refusals, want 2", n.Load()) |
| 43 | } |
| 44 | allow := countRefusals(func(json.RawMessage, provider.Policy) json.RawMessage { |
| 45 | return json.RawMessage(`{"behavior":"allow","updatedInput":{"file_path":"/workspace/README.md"}}`) |
| 46 | }, &n) |
| 47 | if out := allow(json.RawMessage(`{"tool_name":"Read","input":{"file_path":"/workspace/README.md"}}`), policy); denied(out) { |
| 48 | t.Fatalf("allowed read counted as refused: %s", out) |
| 49 | } |
| 50 | if n.Load() != 2 { |
| 51 | t.Fatalf("an allowed request counted: %d", n.Load()) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Pi asks Equipo about a call its extension does not allow; the step |
| 56 | // refuses it. An Engineering step that then commits nothing ends |
| 57 | // tools_refused; one that simply changed nothing ends no_changes. |
| 58 | func TestPiEngineeringRefusedToolsReason(t *testing.T) { |
| 59 | r := newPiRig(t, fakePiInstall(t), bigBudget) |
| 60 | r.urgent() |
| 61 | if rep := r.engineering("tk1", "e1", "read:/etc/hostname"); rep.State != wire.StepFailed || rep.Reason != "tools_refused" { |
| 62 | t.Fatalf("refused: %+v", rep) |
| 63 | } |
| 64 | if rep := r.engineering("tk2", "e2", "nothing to do"); rep.State != wire.StepFailed || rep.Reason != "no_changes" { |
| 65 | t.Fatalf("no changes: %+v", rep) |
| 66 | } |
| 67 | } |
| 68 | |