package thin import ( "encoding/json" "sync/atomic" "testing" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runner/provider/claude" "amadan.net/oficina/equipo/internal/wire" ) // An Engineering step that commits nothing ends no_changes, unless Equipo // refused tool requests the step made: then it ends tools_refused, so // the person reads why nothing changed. func TestEmptyStepReason(t *testing.T) { if got := emptyStepReason(stepResult{}); got != "no_changes" { t.Fatalf("no refusals: %q", got) } if got := emptyStepReason(stepResult{refused: 2}); got != "tools_refused" { t.Fatalf("refusals: %q", got) } } // Claude's permission callback counts each request it denies (here a // shell command under a policy that keeps none in the sandbox, and a // request with a field the step does not accept), and never one it // allows. func TestClaudeStepCountsRefusals(t *testing.T) { var n atomic.Int64 answer := countRefusals(claude.StepPermission, &n) policy := provider.Policy{} for _, raw := range []string{ `{"tool_name":"Bash","input":{"command":"go test ./..."}}`, `{"tool_name":"Read","input":{"file_path":"/workspace/README.md"},"extra":1}`, } { if out := answer(json.RawMessage(raw), policy); !denied(out) { t.Fatalf("%s allowed: %s", raw, out) } } if n.Load() != 2 { t.Fatalf("counted %d refusals, want 2", n.Load()) } allow := countRefusals(func(json.RawMessage, provider.Policy) json.RawMessage { return json.RawMessage(`{"behavior":"allow","updatedInput":{"file_path":"/workspace/README.md"}}`) }, &n) if out := allow(json.RawMessage(`{"tool_name":"Read","input":{"file_path":"/workspace/README.md"}}`), policy); denied(out) { t.Fatalf("allowed read counted as refused: %s", out) } if n.Load() != 2 { t.Fatalf("an allowed request counted: %d", n.Load()) } } // Pi asks Equipo about a call its extension does not allow; the step // refuses it. An Engineering step that then commits nothing ends // tools_refused; one that simply changed nothing ends no_changes. func TestPiEngineeringRefusedToolsReason(t *testing.T) { r := newPiRig(t, fakePiInstall(t), bigBudget) r.urgent() if rep := r.engineering("tk1", "e1", "read:/etc/hostname"); rep.State != wire.StepFailed || rep.Reason != "tools_refused" { t.Fatalf("refused: %+v", rep) } if rep := r.engineering("tk2", "e2", "nothing to do"); rep.State != wire.StepFailed || rep.Reason != "no_changes" { t.Fatalf("no changes: %+v", rep) } }