|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestDefaultGitHubToolsets(t *testing.T) { |
| 8 | + // Verify the default toolsets match the documented defaults |
| 9 | + expected := []string{"context", "repos", "issues", "pull_requests", "users"} |
| 10 | + |
| 11 | + if len(DefaultGitHubToolsets) != len(expected) { |
| 12 | + t.Errorf("Expected %d default toolsets, got %d", len(expected), len(DefaultGitHubToolsets)) |
| 13 | + } |
| 14 | + |
| 15 | + for i, toolset := range expected { |
| 16 | + if i >= len(DefaultGitHubToolsets) || DefaultGitHubToolsets[i] != toolset { |
| 17 | + t.Errorf("Expected default toolset[%d] to be %s, got %s", i, toolset, DefaultGitHubToolsets[i]) |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestParseGitHubToolsets(t *testing.T) { |
| 23 | + tests := []struct { |
| 24 | + name string |
| 25 | + input string |
| 26 | + expected []string |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "Empty string returns default", |
| 30 | + input: "", |
| 31 | + expected: []string{"context", "repos", "issues", "pull_requests", "users"}, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Default expands to default toolsets", |
| 35 | + input: "default", |
| 36 | + expected: []string{"context", "repos", "issues", "pull_requests", "users"}, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Specific toolsets", |
| 40 | + input: "repos,issues", |
| 41 | + expected: []string{"repos", "issues"}, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Default plus additional", |
| 45 | + input: "default,discussions", |
| 46 | + expected: []string{"context", "repos", "issues", "pull_requests", "users", "discussions"}, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "All expands to all toolsets", |
| 50 | + input: "all", |
| 51 | + // Should include all 19 toolsets - we'll check the count |
| 52 | + expected: nil, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Deduplication", |
| 56 | + input: "repos,issues,repos", |
| 57 | + expected: []string{"repos", "issues"}, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Whitespace handling", |
| 61 | + input: " repos , issues , pull_requests ", |
| 62 | + expected: []string{"repos", "issues", "pull_requests"}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "Single toolset", |
| 66 | + input: "actions", |
| 67 | + expected: []string{"actions"}, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Multiple with default in middle", |
| 71 | + input: "actions,default,discussions", |
| 72 | + expected: []string{"actions", "context", "repos", "issues", "pull_requests", "users", "discussions"}, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + result := ParseGitHubToolsets(tt.input) |
| 79 | + |
| 80 | + if tt.name == "All expands to all toolsets" { |
| 81 | + // Check that all toolsets are present |
| 82 | + if len(result) != len(toolsetPermissionsMap) { |
| 83 | + t.Errorf("Expected %d toolsets for 'all', got %d", len(toolsetPermissionsMap), len(result)) |
| 84 | + } |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + if len(result) != len(tt.expected) { |
| 89 | + t.Errorf("Expected %d toolsets, got %d: %v", len(tt.expected), len(result), result) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // Check that all expected toolsets are present (order doesn't matter for some tests) |
| 94 | + resultMap := make(map[string]bool) |
| 95 | + for _, ts := range result { |
| 96 | + resultMap[ts] = true |
| 97 | + } |
| 98 | + |
| 99 | + for _, expected := range tt.expected { |
| 100 | + if !resultMap[expected] { |
| 101 | + t.Errorf("Expected toolset %s not found in result: %v", expected, result) |
| 102 | + } |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestParseGitHubToolsetsPreservesOrder(t *testing.T) { |
| 109 | + // Test that specific toolsets maintain their order |
| 110 | + input := "repos,issues,pull_requests" |
| 111 | + result := ParseGitHubToolsets(input) |
| 112 | + expected := []string{"repos", "issues", "pull_requests"} |
| 113 | + |
| 114 | + if len(result) != len(expected) { |
| 115 | + t.Fatalf("Expected %d toolsets, got %d", len(expected), len(result)) |
| 116 | + } |
| 117 | + |
| 118 | + for i, toolset := range expected { |
| 119 | + if result[i] != toolset { |
| 120 | + t.Errorf("Expected toolset[%d] to be %s, got %s", i, toolset, result[i]) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestParseGitHubToolsetsDeduplication(t *testing.T) { |
| 126 | + tests := []struct { |
| 127 | + name string |
| 128 | + input string |
| 129 | + expected int |
| 130 | + }{ |
| 131 | + { |
| 132 | + name: "Duplicate in simple list", |
| 133 | + input: "repos,issues,repos,issues", |
| 134 | + expected: 2, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "Default includes duplicates", |
| 138 | + input: "context,default", |
| 139 | + expected: 5, // context already in default, so only 5 unique |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "All with duplicates", |
| 143 | + input: "all,repos,issues", |
| 144 | + expected: len(toolsetPermissionsMap), // All toolsets, duplicates ignored |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + for _, tt := range tests { |
| 149 | + t.Run(tt.name, func(t *testing.T) { |
| 150 | + result := ParseGitHubToolsets(tt.input) |
| 151 | + if len(result) != tt.expected { |
| 152 | + t.Errorf("Expected %d unique toolsets, got %d: %v", tt.expected, len(result), result) |
| 153 | + } |
| 154 | + |
| 155 | + // Verify no duplicates |
| 156 | + seen := make(map[string]bool) |
| 157 | + for _, toolset := range result { |
| 158 | + if seen[toolset] { |
| 159 | + t.Errorf("Found duplicate toolset: %s", toolset) |
| 160 | + } |
| 161 | + seen[toolset] = true |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments