|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package workflow |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "os" |
| 8 | + "sort" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestEngineCatalog_IDs verifies that IDs() returns all engine IDs in sorted order. |
| 16 | +func TestEngineCatalog_IDs(t *testing.T) { |
| 17 | + registry := NewEngineRegistry() |
| 18 | + catalog := NewEngineCatalog(registry) |
| 19 | + |
| 20 | + ids := catalog.IDs() |
| 21 | + require.NotEmpty(t, ids, "IDs() should return a non-empty list") |
| 22 | + |
| 23 | + // Verify all built-in engines are present |
| 24 | + expectedIDs := []string{"claude", "codex", "copilot", "gemini"} |
| 25 | + assert.Equal(t, expectedIDs, ids, "IDs() should return all built-in engines in sorted order") |
| 26 | + |
| 27 | + // Verify the list is sorted |
| 28 | + sorted := make([]string, len(ids)) |
| 29 | + copy(sorted, ids) |
| 30 | + sort.Strings(sorted) |
| 31 | + assert.Equal(t, sorted, ids, "IDs() should return IDs in sorted order") |
| 32 | +} |
| 33 | + |
| 34 | +// TestEngineCatalog_DisplayNames verifies that DisplayNames() returns names in sorted ID order. |
| 35 | +func TestEngineCatalog_DisplayNames(t *testing.T) { |
| 36 | + registry := NewEngineRegistry() |
| 37 | + catalog := NewEngineCatalog(registry) |
| 38 | + |
| 39 | + names := catalog.DisplayNames() |
| 40 | + require.NotEmpty(t, names, "DisplayNames() should return a non-empty list") |
| 41 | + assert.Len(t, names, len(catalog.IDs()), "DisplayNames() should have same length as IDs()") |
| 42 | + |
| 43 | + // Verify display names match expected values in sorted ID order (claude, codex, copilot, gemini) |
| 44 | + expectedNames := []string{"Claude Code", "Codex", "GitHub Copilot CLI", "Google Gemini CLI"} |
| 45 | + assert.Equal(t, expectedNames, names, "DisplayNames() should return display names in sorted ID order") |
| 46 | +} |
| 47 | + |
| 48 | +// TestEngineCatalog_All verifies that All() returns all definitions in sorted ID order. |
| 49 | +func TestEngineCatalog_All(t *testing.T) { |
| 50 | + registry := NewEngineRegistry() |
| 51 | + catalog := NewEngineCatalog(registry) |
| 52 | + |
| 53 | + defs := catalog.All() |
| 54 | + require.NotEmpty(t, defs, "All() should return a non-empty list") |
| 55 | + assert.Len(t, defs, len(catalog.IDs()), "All() should have same length as IDs()") |
| 56 | + |
| 57 | + ids := catalog.IDs() |
| 58 | + for i, def := range defs { |
| 59 | + assert.Equal(t, ids[i], def.ID, "All()[%d].ID should match IDs()[%d]", i, i) |
| 60 | + assert.NotEmpty(t, def.DisplayName, "All()[%d].DisplayName should not be empty", i) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// engineSchemaEnums parses the main workflow schema and extracts engine enum values |
| 65 | +// from both the string variant and the object id property of engine_config. |
| 66 | +func engineSchemaEnums(t *testing.T) []string { |
| 67 | + t.Helper() |
| 68 | + |
| 69 | + schemaBytes, err := os.ReadFile("../parser/schemas/main_workflow_schema.json") |
| 70 | + require.NoError(t, err, "should be able to read main_workflow_schema.json") |
| 71 | + |
| 72 | + var schema map[string]any |
| 73 | + require.NoError(t, json.Unmarshal(schemaBytes, &schema), "schema should be valid JSON") |
| 74 | + |
| 75 | + defs, ok := schema["$defs"].(map[string]any) |
| 76 | + require.True(t, ok, "schema should have $defs") |
| 77 | + |
| 78 | + engineConfig, ok := defs["engine_config"].(map[string]any) |
| 79 | + require.True(t, ok, "$defs should have engine_config") |
| 80 | + |
| 81 | + oneOf, ok := engineConfig["oneOf"].([]any) |
| 82 | + require.True(t, ok, "engine_config should have oneOf") |
| 83 | + |
| 84 | + // The first oneOf variant is the plain string enum |
| 85 | + for _, variant := range oneOf { |
| 86 | + v, ok := variant.(map[string]any) |
| 87 | + if !ok { |
| 88 | + continue |
| 89 | + } |
| 90 | + if v["type"] == "string" { |
| 91 | + rawEnum, ok := v["enum"].([]any) |
| 92 | + if !ok { |
| 93 | + continue |
| 94 | + } |
| 95 | + enums := make([]string, 0, len(rawEnum)) |
| 96 | + for _, e := range rawEnum { |
| 97 | + if s, ok := e.(string); ok { |
| 98 | + enums = append(enums, s) |
| 99 | + } |
| 100 | + } |
| 101 | + sort.Strings(enums) |
| 102 | + return enums |
| 103 | + } |
| 104 | + } |
| 105 | + t.Fatal("could not find string enum in engine_config oneOf") |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// TestEngineCatalogMatchesSchema asserts that the schema engine enum values exactly |
| 110 | +// match the catalog IDs. A failure here means the schema and catalog have drifted apart. |
| 111 | +func TestEngineCatalogMatchesSchema(t *testing.T) { |
| 112 | + registry := NewEngineRegistry() |
| 113 | + catalog := NewEngineCatalog(registry) |
| 114 | + |
| 115 | + catalogIDs := catalog.IDs() // already sorted |
| 116 | + schemaEnums := engineSchemaEnums(t) |
| 117 | + |
| 118 | + assert.Equal(t, catalogIDs, schemaEnums, |
| 119 | + "schema engine enum must match catalog IDs exactly — run 'make build' after updating the schema") |
| 120 | +} |
0 commit comments