|
| 1 | +package evaluation_plans |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/gemaraproj/go-gemara" |
| 9 | + "github.com/goccy/go-yaml" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAllSteps(t *testing.T) { |
| 14 | + t.Run("Returns non-empty map", func(t *testing.T) { |
| 15 | + result := AllSteps() |
| 16 | + assert.NotEmpty(t, result) |
| 17 | + }) |
| 18 | + |
| 19 | + t.Run("Contains all OSPS entries", func(t *testing.T) { |
| 20 | + result := AllSteps() |
| 21 | + for id := range OSPS { |
| 22 | + assert.Contains(t, result, id, "AllSteps() missing OSPS key %s", id) |
| 23 | + } |
| 24 | + assert.Equal(t, len(OSPS), len(result)) |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("Every entry has non-empty steps", func(t *testing.T) { |
| 28 | + result := AllSteps() |
| 29 | + for id, steps := range result { |
| 30 | + assert.NotEmpty(t, steps, "AllSteps() entry %s has no steps", id) |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("Returns a copy not the original", func(t *testing.T) { |
| 35 | + result := AllSteps() |
| 36 | + result["fake-id"] = nil |
| 37 | + _, exists := OSPS["fake-id"] |
| 38 | + assert.False(t, exists, "mutating AllSteps() result should not affect OSPS") |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +// TestAllCatalogAssessmentIDsHaveSteps ensures every assessment requirement ID |
| 43 | +// defined in every catalog YAML has a corresponding entry in the combined step map. |
| 44 | +// This prevents silently producing "Unknown" results when a new catalog |
| 45 | +// introduces assessment IDs without adding step implementations. |
| 46 | +func TestAllCatalogAssessmentIDsHaveSteps(t *testing.T) { |
| 47 | + allSteps := AllSteps() |
| 48 | + catalogDir := filepath.Join("..", "data", "catalogs") |
| 49 | + entries, err := os.ReadDir(catalogDir) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("failed to read catalog directory: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + for _, entry := range entries { |
| 55 | + if entry.IsDir() { |
| 56 | + continue |
| 57 | + } |
| 58 | + catalogPath := filepath.Join(catalogDir, entry.Name()) |
| 59 | + t.Run(entry.Name(), func(t *testing.T) { |
| 60 | + data, err := os.ReadFile(catalogPath) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("failed to read catalog %s: %v", entry.Name(), err) |
| 63 | + } |
| 64 | + var catalog gemara.ControlCatalog |
| 65 | + if err := yaml.Unmarshal(data, &catalog); err != nil { |
| 66 | + t.Fatalf("failed to parse catalog %s: %v", entry.Name(), err) |
| 67 | + } |
| 68 | + |
| 69 | + for _, control := range catalog.Controls { |
| 70 | + for _, req := range control.AssessmentRequirements { |
| 71 | + if _, ok := allSteps[req.Id]; !ok { |
| 72 | + t.Errorf("catalog %s has assessment requirement %s but no step implementation exists", entry.Name(), req.Id) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments