|
| 1 | +package cursor |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestNew_SetsProjectRoot(t *testing.T) { |
| 10 | + a := New("/my/project", false) |
| 11 | + if a.Adapter == nil { |
| 12 | + t.Fatal("Adapter should not be nil") |
| 13 | + } |
| 14 | + if a.ProjectRoot != "/my/project" { |
| 15 | + t.Errorf("ProjectRoot = %q, want /my/project", a.ProjectRoot) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +func TestNew_SupportsRuntimeEnvSubstitutionFalse(t *testing.T) { |
| 20 | + a := New("/project", false) |
| 21 | + if a.Adapter.SupportsRuntimeEnvSubstitution { |
| 22 | + t.Error("SupportsRuntimeEnvSubstitution should be false for cursor") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestTargetName_ReturnsConstant(t *testing.T) { |
| 27 | + cases := []string{"/proj", "/other/root", ""} |
| 28 | + for _, root := range cases { |
| 29 | + a := New(root, false) |
| 30 | + if a.TargetName() != "cursor" { |
| 31 | + t.Errorf("TargetName() for root=%q = %q, want cursor", root, a.TargetName()) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestMCPServersKey_ReturnsConstant(t *testing.T) { |
| 37 | + a := New("/project", true) |
| 38 | + if a.MCPServersKey() != "mcpServers" { |
| 39 | + t.Errorf("MCPServersKey() = %q, want mcpServers", a.MCPServersKey()) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestSupportsUserScope_AlwaysFalse(t *testing.T) { |
| 44 | + for _, userScope := range []bool{true, false} { |
| 45 | + a := New("/proj", userScope) |
| 46 | + if a.SupportsUserScope() { |
| 47 | + t.Errorf("SupportsUserScope() should be false (userScope=%v)", userScope) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestGetConfigPath_Structure(t *testing.T) { |
| 53 | + a := New("/workspace/proj", false) |
| 54 | + got := a.GetConfigPath() |
| 55 | + if !filepath.IsAbs(got) { |
| 56 | + t.Errorf("GetConfigPath should be absolute: %q", got) |
| 57 | + } |
| 58 | + base := filepath.Base(got) |
| 59 | + if base != "mcp.json" { |
| 60 | + t.Errorf("config file name = %q, want mcp.json", base) |
| 61 | + } |
| 62 | + dir := filepath.Dir(got) |
| 63 | + if filepath.Base(dir) != ".cursor" { |
| 64 | + t.Errorf("parent dir = %q, want .cursor", filepath.Base(dir)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestGetConfigPath_ContainsProjectRoot(t *testing.T) { |
| 69 | + a := New("/home/user/my-project", false) |
| 70 | + got := a.GetConfigPath() |
| 71 | + if !filepath.HasPrefix(got, "/home/user/my-project") { |
| 72 | + t.Errorf("config path should be under project root: %q", got) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestGetCurrentConfig_ReturnsEmptyMapNotNil(t *testing.T) { |
| 77 | + dir := t.TempDir() |
| 78 | + a := New(dir, false) |
| 79 | + cfg := a.GetCurrentConfig() |
| 80 | + if cfg == nil { |
| 81 | + t.Error("GetCurrentConfig should never return nil") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestGetCurrentConfig_ValidJSON(t *testing.T) { |
| 86 | + dir := t.TempDir() |
| 87 | + cursorDir := filepath.Join(dir, ".cursor") |
| 88 | + os.MkdirAll(cursorDir, 0o755) |
| 89 | + cfgPath := filepath.Join(cursorDir, "mcp.json") |
| 90 | + os.WriteFile(cfgPath, []byte(`{"mcpServers":{"my-server":{"command":"npx"}}}`), 0o644) |
| 91 | + a := New(dir, false) |
| 92 | + cfg := a.GetCurrentConfig() |
| 93 | + if _, ok := cfg["mcpServers"]; !ok { |
| 94 | + t.Error("expected mcpServers key") |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestUpdateConfig_CursorDirMustExist(t *testing.T) { |
| 99 | + dir := t.TempDir() |
| 100 | + a := New(dir, false) |
| 101 | + // No .cursor dir: UpdateConfig should be a no-op (not an error) |
| 102 | + if err := a.UpdateConfig(map[string]interface{}{"mcpServers": map[string]interface{}{}}); err != nil { |
| 103 | + t.Errorf("expected no error when .cursor dir is absent: %v", err) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestUpdateConfig_CreatesMCPJSON(t *testing.T) { |
| 108 | + dir := t.TempDir() |
| 109 | + cursorDir := filepath.Join(dir, ".cursor") |
| 110 | + os.MkdirAll(cursorDir, 0o755) |
| 111 | + a := New(dir, false) |
| 112 | + err := a.UpdateConfig(map[string]interface{}{"mcpServers": map[string]interface{}{}}) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("UpdateConfig: %v", err) |
| 115 | + } |
| 116 | + cfgPath := filepath.Join(cursorDir, "mcp.json") |
| 117 | + if _, err := os.Stat(cfgPath); os.IsNotExist(err) { |
| 118 | + t.Error("mcp.json should have been created") |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestNew_MultipleInstances_Independent(t *testing.T) { |
| 123 | + a1 := New("/proj1", false) |
| 124 | + a2 := New("/proj2", false) |
| 125 | + if a1.ProjectRoot == a2.ProjectRoot { |
| 126 | + t.Error("distinct adapters should have distinct ProjectRoot values") |
| 127 | + } |
| 128 | + if a1.GetConfigPath() == a2.GetConfigPath() { |
| 129 | + t.Error("distinct adapters should have distinct config paths") |
| 130 | + } |
| 131 | +} |
0 commit comments