/ internal / tools / process_test.go
process_test.go
  1  package tools
  2  
  3  import (
  4  	"context"
  5  	"runtime"
  6  	"testing"
  7  )
  8  
  9  func TestProcess_Info(t *testing.T) {
 10  	tool := &ProcessTool{}
 11  	info := tool.Info()
 12  	if info.Name != "process" {
 13  		t.Errorf("expected name 'process', got %q", info.Name)
 14  	}
 15  	if len(info.Required) != 1 || info.Required[0] != "action" {
 16  		t.Errorf("expected required [action], got %v", info.Required)
 17  	}
 18  }
 19  
 20  func TestProcess_InvalidArgs(t *testing.T) {
 21  	tool := &ProcessTool{}
 22  	result, err := tool.Run(context.Background(), `not valid json`)
 23  	if err != nil {
 24  		t.Fatalf("unexpected error: %v", err)
 25  	}
 26  	if !result.IsError {
 27  		t.Error("expected error result for invalid JSON")
 28  	}
 29  }
 30  
 31  func TestProcess_UnknownAction(t *testing.T) {
 32  	tool := &ProcessTool{}
 33  	result, err := tool.Run(context.Background(), `{"action": "restart"}`)
 34  	if err != nil {
 35  		t.Fatalf("unexpected error: %v", err)
 36  	}
 37  	if !result.IsError {
 38  		t.Error("expected error result for unknown action")
 39  	}
 40  	if !contains(result.Content, "unknown action") {
 41  		t.Errorf("expected 'unknown action' in error, got: %s", result.Content)
 42  	}
 43  }
 44  
 45  func TestProcess_KillNoPID(t *testing.T) {
 46  	tool := &ProcessTool{}
 47  	result, err := tool.Run(context.Background(), `{"action": "kill"}`)
 48  	if err != nil {
 49  		t.Fatalf("unexpected error: %v", err)
 50  	}
 51  	if !result.IsError {
 52  		t.Error("expected error result for kill without PID")
 53  	}
 54  	if !contains(result.Content, "pid is required") {
 55  		t.Errorf("expected 'pid is required' in error, got: %s", result.Content)
 56  	}
 57  }
 58  
 59  func TestProcess_List(t *testing.T) {
 60  	if runtime.GOOS == "windows" {
 61  		t.Skip("process list test not supported on Windows")
 62  	}
 63  	tool := &ProcessTool{}
 64  	result, err := tool.Run(context.Background(), `{"action": "list"}`)
 65  	if err != nil {
 66  		t.Fatalf("unexpected error: %v", err)
 67  	}
 68  	if result.IsError {
 69  		t.Fatalf("unexpected error result: %s", result.Content)
 70  	}
 71  	if !contains(result.Content, "PID") {
 72  		t.Errorf("expected PID header in ps output, got: %s", result.Content)
 73  	}
 74  }
 75  
 76  func TestProcess_RequiresApproval(t *testing.T) {
 77  	tool := &ProcessTool{}
 78  	if !tool.RequiresApproval() {
 79  		t.Error("expected RequiresApproval to return true")
 80  	}
 81  }
 82  
 83  func TestProcess_IsSafeArgs(t *testing.T) {
 84  	tool := &ProcessTool{}
 85  	tests := []struct {
 86  		argsJSON string
 87  		safe     bool
 88  	}{
 89  		{`{"action": "list"}`, true},
 90  		{`{"action": "ports"}`, true},
 91  		{`{"action": "ports", "port": 8080}`, true},
 92  		{`{"action": "kill", "pid": 1234}`, false},
 93  		{`not valid json`, false},
 94  	}
 95  	for _, tt := range tests {
 96  		if tool.IsSafeArgs(tt.argsJSON) != tt.safe {
 97  			t.Errorf("IsSafeArgs(%q) = %v, want %v", tt.argsJSON, !tt.safe, tt.safe)
 98  		}
 99  	}
100  }