|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/lugassawan/rimba/internal/config" |
| 10 | + mcplib "github.com/mark3labs/mcp-go/mcp" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + gitSymbolicRef = "symbolic-ref" |
| 15 | + refsOriginMain = "refs/remotes/origin/main" |
| 16 | + gitWorktree = "worktree" |
| 17 | + gitRevParse = "rev-parse" |
| 18 | + gitRevList = "rev-list" |
| 19 | + gitStatus = "status" |
| 20 | + gitList = "list" |
| 21 | + revListEven = "0\t0" |
| 22 | + dirtyOutput = " M dirty.go\n" |
| 23 | +) |
| 24 | + |
| 25 | +// mockRunner implements git.Runner for unit tests. |
| 26 | +type mockRunner struct { |
| 27 | + run func(args ...string) (string, error) |
| 28 | + runInDir func(dir string, args ...string) (string, error) |
| 29 | +} |
| 30 | + |
| 31 | +func (m *mockRunner) Run(args ...string) (string, error) { |
| 32 | + if m.run == nil { |
| 33 | + return "", nil |
| 34 | + } |
| 35 | + return m.run(args...) |
| 36 | +} |
| 37 | + |
| 38 | +func (m *mockRunner) RunInDir(dir string, args ...string) (string, error) { |
| 39 | + if m.runInDir == nil { |
| 40 | + return "", nil |
| 41 | + } |
| 42 | + return m.runInDir(dir, args...) |
| 43 | +} |
| 44 | + |
| 45 | +// testConfig returns a minimal config suitable for testing. |
| 46 | +func testConfig() *config.Config { |
| 47 | + return &config.Config{ |
| 48 | + WorktreeDir: ".worktrees", |
| 49 | + DefaultSource: "main", |
| 50 | + CopyFiles: []string{".editorconfig"}, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// testContext returns a HandlerContext with a mock runner and test config. |
| 55 | +func testContext(r *mockRunner) *HandlerContext { |
| 56 | + return &HandlerContext{ |
| 57 | + Runner: r, |
| 58 | + Config: testConfig(), |
| 59 | + RepoRoot: "/repo", |
| 60 | + Version: "test", |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// callTool is a test helper that invokes a tool handler with the given arguments. |
| 65 | +func callTool(t *testing.T, handler func(ctx context.Context, req mcplib.CallToolRequest) (*mcplib.CallToolResult, error), args map[string]any) *mcplib.CallToolResult { |
| 66 | + t.Helper() |
| 67 | + req := mcplib.CallToolRequest{} |
| 68 | + req.Params.Arguments = args |
| 69 | + result, err := handler(context.Background(), req) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("handler returned protocol error: %v", err) |
| 72 | + } |
| 73 | + return result |
| 74 | +} |
| 75 | + |
| 76 | +// resultJSON extracts the JSON text from a tool result. |
| 77 | +func resultJSON(t *testing.T, result *mcplib.CallToolResult) string { |
| 78 | + t.Helper() |
| 79 | + if result.IsError { |
| 80 | + t.Fatalf("expected success result, got error: %v", result.Content) |
| 81 | + } |
| 82 | + tc, ok := mcplib.AsTextContent(result.Content[0]) |
| 83 | + if !ok { |
| 84 | + t.Fatal("result content is not TextContent") |
| 85 | + } |
| 86 | + return tc.Text |
| 87 | +} |
| 88 | + |
| 89 | +// resultError extracts the error text from a tool result. |
| 90 | +func resultError(t *testing.T, result *mcplib.CallToolResult) string { |
| 91 | + t.Helper() |
| 92 | + if !result.IsError { |
| 93 | + t.Fatal("expected error result, got success") |
| 94 | + } |
| 95 | + tc, ok := mcplib.AsTextContent(result.Content[0]) |
| 96 | + if !ok { |
| 97 | + t.Fatal("error content is not TextContent") |
| 98 | + } |
| 99 | + return tc.Text |
| 100 | +} |
| 101 | + |
| 102 | +// unmarshalJSON is a helper to unmarshal JSON from a tool result into a target type. |
| 103 | +func unmarshalJSON[T any](t *testing.T, result *mcplib.CallToolResult) T { |
| 104 | + t.Helper() |
| 105 | + jsonStr := resultJSON(t, result) |
| 106 | + var v T |
| 107 | + if err := json.Unmarshal([]byte(jsonStr), &v); err != nil { |
| 108 | + t.Fatalf("failed to unmarshal: %v\njson: %s", err, jsonStr) |
| 109 | + } |
| 110 | + return v |
| 111 | +} |
| 112 | + |
| 113 | +func TestNewServer(t *testing.T) { |
| 114 | + hctx := testContext(&mockRunner{}) |
| 115 | + s := NewServer(hctx) |
| 116 | + |
| 117 | + tools := s.ListTools() |
| 118 | + expectedTools := []string{"list", "add", "remove", "status", "exec", "conflict-check", "merge", "sync", "clean"} |
| 119 | + for _, name := range expectedTools { |
| 120 | + if _, exists := tools[name]; !exists { |
| 121 | + t.Errorf("expected tool %q to be registered", name) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if len(tools) != len(expectedTools) { |
| 126 | + t.Errorf("expected %d tools, got %d", len(expectedTools), len(tools)) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// worktreePorcelain generates git worktree --porcelain output. |
| 131 | +func worktreePorcelain(entries ...struct{ path, branch string }) string { |
| 132 | + lines := make([]string, 0, 4*len(entries)) |
| 133 | + for _, e := range entries { |
| 134 | + lines = append(lines, |
| 135 | + "worktree "+e.path, |
| 136 | + "HEAD abc123", |
| 137 | + "branch refs/heads/"+e.branch, |
| 138 | + "", |
| 139 | + ) |
| 140 | + } |
| 141 | + return strings.Join(lines, "\n") |
| 142 | +} |
0 commit comments