|
| 1 | +/* |
| 2 | +Copyright © 2025 Joseph Goksu josephgoksu@gmail.com |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +// TestInstallMCPServers_OpenCode tests that installMCPServers correctly installs OpenCode MCP config. |
| 14 | +func TestInstallMCPServers_OpenCode(t *testing.T) { |
| 15 | + tmpDir := t.TempDir() |
| 16 | + |
| 17 | + // Mock binPath - in tests we can use any path |
| 18 | + binPath := "/usr/local/bin/taskwing" |
| 19 | + |
| 20 | + // Call installMCPServers with opencode |
| 21 | + installMCPServers(tmpDir, []string{"opencode"}) |
| 22 | + |
| 23 | + // Verify opencode.json was created |
| 24 | + configPath := filepath.Join(tmpDir, "opencode.json") |
| 25 | + content, err := os.ReadFile(configPath) |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("Failed to read opencode.json: %v", err) |
| 28 | + } |
| 29 | + |
| 30 | + // Parse and verify structure |
| 31 | + var config OpenCodeConfig |
| 32 | + if err := json.Unmarshal(content, &config); err != nil { |
| 33 | + t.Fatalf("Invalid JSON in opencode.json: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + // Verify schema |
| 37 | + if config.Schema != "https://opencode.ai/config.json" { |
| 38 | + t.Errorf("Schema = %q, want %q", config.Schema, "https://opencode.ai/config.json") |
| 39 | + } |
| 40 | + |
| 41 | + // Verify MCP section exists |
| 42 | + if config.MCP == nil { |
| 43 | + t.Fatal("MCP section is nil") |
| 44 | + } |
| 45 | + |
| 46 | + // Server name should include project directory name |
| 47 | + expectedServerName := "taskwing-mcp-" + filepath.Base(tmpDir) |
| 48 | + serverCfg, ok := config.MCP[expectedServerName] |
| 49 | + if !ok { |
| 50 | + // Check if any taskwing-mcp server exists |
| 51 | + found := false |
| 52 | + for name := range config.MCP { |
| 53 | + if len(name) >= 12 && name[:12] == "taskwing-mcp" { |
| 54 | + found = true |
| 55 | + serverCfg = config.MCP[name] |
| 56 | + break |
| 57 | + } |
| 58 | + } |
| 59 | + if !found { |
| 60 | + t.Fatalf("No taskwing-mcp server entry found in MCP section. Got: %v", config.MCP) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Verify type is "local" |
| 65 | + if serverCfg.Type != "local" { |
| 66 | + t.Errorf("Type = %q, want %q", serverCfg.Type, "local") |
| 67 | + } |
| 68 | + |
| 69 | + // Verify command is array format |
| 70 | + if len(serverCfg.Command) != 2 { |
| 71 | + t.Fatalf("Command length = %d, want 2", len(serverCfg.Command)) |
| 72 | + } |
| 73 | + // Command[0] will use the actual executable path, not our mock binPath |
| 74 | + // Just verify the second element is "mcp" |
| 75 | + if serverCfg.Command[1] != "mcp" { |
| 76 | + t.Errorf("Command[1] = %q, want %q", serverCfg.Command[1], "mcp") |
| 77 | + } |
| 78 | + |
| 79 | + _ = binPath // suppress unused variable warning |
| 80 | +} |
| 81 | + |
| 82 | +// TestInstallMCPServers_AllIncludesOpenCode tests that "all" AIs doesn't break when including opencode. |
| 83 | +func TestInstallMCPServers_AllIncludesOpenCode(t *testing.T) { |
| 84 | + tmpDir := t.TempDir() |
| 85 | + |
| 86 | + // Install multiple AIs including opencode |
| 87 | + installMCPServers(tmpDir, []string{"claude", "opencode"}) |
| 88 | + |
| 89 | + // Verify opencode.json was created |
| 90 | + configPath := filepath.Join(tmpDir, "opencode.json") |
| 91 | + if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 92 | + t.Error("opencode.json was not created when installing multiple AIs including opencode") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// TestAIConfigOrder_IncludesOpenCode verifies opencode is in the AI selection list. |
| 97 | +func TestAIConfigOrder_IncludesOpenCode(t *testing.T) { |
| 98 | + found := false |
| 99 | + for _, ai := range aiConfigOrder { |
| 100 | + if ai == "opencode" { |
| 101 | + found = true |
| 102 | + break |
| 103 | + } |
| 104 | + } |
| 105 | + if !found { |
| 106 | + t.Error("opencode is not in aiConfigOrder") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// TestAIConfigs_OpenCodeEntry verifies opencode config entry exists with correct values. |
| 111 | +func TestAIConfigs_OpenCodeEntry(t *testing.T) { |
| 112 | + cfg, ok := aiConfigs["opencode"] |
| 113 | + if !ok { |
| 114 | + t.Fatal("opencode entry not found in aiConfigs") |
| 115 | + } |
| 116 | + |
| 117 | + if cfg.name != "opencode" { |
| 118 | + t.Errorf("name = %q, want %q", cfg.name, "opencode") |
| 119 | + } |
| 120 | + if cfg.displayName != "OpenCode" { |
| 121 | + t.Errorf("displayName = %q, want %q", cfg.displayName, "OpenCode") |
| 122 | + } |
| 123 | + if cfg.commandsDir != ".opencode/skills" { |
| 124 | + t.Errorf("commandsDir = %q, want %q", cfg.commandsDir, ".opencode/skills") |
| 125 | + } |
| 126 | + if cfg.fileExt != ".md" { |
| 127 | + t.Errorf("fileExt = %q, want %q", cfg.fileExt, ".md") |
| 128 | + } |
| 129 | +} |
0 commit comments