|
| 1 | +package schema_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "sort" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/chenasraf/sofmani/appconfig" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// schemaPath returns the absolute path to the schema file regardless of where |
| 16 | +// the tests are run from. |
| 17 | +func schemaPath(t *testing.T) string { |
| 18 | + t.Helper() |
| 19 | + wd, err := os.Getwd() |
| 20 | + require.NoError(t, err) |
| 21 | + return filepath.Join(wd, "sofmani.schema.json") |
| 22 | +} |
| 23 | + |
| 24 | +func loadSchema(t *testing.T) map[string]any { |
| 25 | + t.Helper() |
| 26 | + data, err := os.ReadFile(schemaPath(t)) |
| 27 | + require.NoError(t, err) |
| 28 | + var m map[string]any |
| 29 | + require.NoError(t, json.Unmarshal(data, &m), "schema must be valid JSON") |
| 30 | + return m |
| 31 | +} |
| 32 | + |
| 33 | +func TestSchemaIsValidJSON(t *testing.T) { |
| 34 | + m := loadSchema(t) |
| 35 | + assert.Equal(t, "http://json-schema.org/draft-07/schema#", m["$schema"]) |
| 36 | + assert.NotEmpty(t, m["$id"]) |
| 37 | + assert.NotEmpty(t, m["title"]) |
| 38 | + assert.Equal(t, "object", m["type"]) |
| 39 | +} |
| 40 | + |
| 41 | +func TestSchemaTopLevelProperties(t *testing.T) { |
| 42 | + m := loadSchema(t) |
| 43 | + props, ok := m["properties"].(map[string]any) |
| 44 | + require.True(t, ok, "top-level properties must exist") |
| 45 | + |
| 46 | + // These must be declared at the top level of the schema. |
| 47 | + expected := []string{ |
| 48 | + "$schema", |
| 49 | + "debug", |
| 50 | + "check_updates", |
| 51 | + "summary", |
| 52 | + "category_display", |
| 53 | + "repo_update", |
| 54 | + "defaults", |
| 55 | + "env", |
| 56 | + "platform_env", |
| 57 | + "machine_aliases", |
| 58 | + "install", |
| 59 | + } |
| 60 | + for _, key := range expected { |
| 61 | + _, exists := props[key] |
| 62 | + assert.Truef(t, exists, "top-level property %q missing from schema", key) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// TestInstallerTypesMatchGoConstants ensures the schema's list of installer |
| 67 | +// types stays in lock-step with the Go InstallerType constants. If a new |
| 68 | +// installer type is added in code but not in the schema (or vice-versa), this |
| 69 | +// test fails and prompts the developer to update both sides. |
| 70 | +func TestInstallerTypesMatchGoConstants(t *testing.T) { |
| 71 | + m := loadSchema(t) |
| 72 | + defs, ok := m["definitions"].(map[string]any) |
| 73 | + require.True(t, ok) |
| 74 | + installerType, ok := defs["installerType"].(map[string]any) |
| 75 | + require.True(t, ok) |
| 76 | + enum, ok := installerType["enum"].([]any) |
| 77 | + require.True(t, ok) |
| 78 | + |
| 79 | + schemaTypes := make([]string, 0, len(enum)) |
| 80 | + for _, v := range enum { |
| 81 | + s, ok := v.(string) |
| 82 | + require.True(t, ok) |
| 83 | + schemaTypes = append(schemaTypes, s) |
| 84 | + } |
| 85 | + sort.Strings(schemaTypes) |
| 86 | + |
| 87 | + goTypes := []string{ |
| 88 | + string(appconfig.InstallerTypeGroup), |
| 89 | + string(appconfig.InstallerTypeShell), |
| 90 | + string(appconfig.InstallerTypeDocker), |
| 91 | + string(appconfig.InstallerTypeBrew), |
| 92 | + string(appconfig.InstallerTypeApt), |
| 93 | + string(appconfig.InstallerTypeApk), |
| 94 | + string(appconfig.InstallerTypeGit), |
| 95 | + string(appconfig.InstallerTypeGitHubRelease), |
| 96 | + string(appconfig.InstallerTypeRsync), |
| 97 | + string(appconfig.InstallerTypeNpm), |
| 98 | + string(appconfig.InstallerTypePnpm), |
| 99 | + string(appconfig.InstallerTypeYarn), |
| 100 | + string(appconfig.InstallerTypePipx), |
| 101 | + string(appconfig.InstallerTypeManifest), |
| 102 | + string(appconfig.InstallerTypePacman), |
| 103 | + string(appconfig.InstallerTypeYay), |
| 104 | + string(appconfig.InstallerTypeCargo), |
| 105 | + } |
| 106 | + sort.Strings(goTypes) |
| 107 | + |
| 108 | + assert.Equal(t, goTypes, schemaTypes, "installerType enum in schema is out of sync with Go constants") |
| 109 | +} |
| 110 | + |
| 111 | +func TestCategoryDisplayEnumMatchesGoConstants(t *testing.T) { |
| 112 | + m := loadSchema(t) |
| 113 | + props := m["properties"].(map[string]any) |
| 114 | + cat := props["category_display"].(map[string]any) |
| 115 | + enum, ok := cat["enum"].([]any) |
| 116 | + require.True(t, ok) |
| 117 | + |
| 118 | + schemaVals := make([]string, 0, len(enum)) |
| 119 | + for _, v := range enum { |
| 120 | + schemaVals = append(schemaVals, v.(string)) |
| 121 | + } |
| 122 | + sort.Strings(schemaVals) |
| 123 | + |
| 124 | + goVals := []string{ |
| 125 | + string(appconfig.CategoryDisplayBorder), |
| 126 | + string(appconfig.CategoryDisplayBorderCompact), |
| 127 | + string(appconfig.CategoryDisplayMinimal), |
| 128 | + } |
| 129 | + sort.Strings(goVals) |
| 130 | + |
| 131 | + assert.Equal(t, goVals, schemaVals) |
| 132 | +} |
| 133 | + |
| 134 | +func TestRepoUpdateEnumMatchesGoConstants(t *testing.T) { |
| 135 | + m := loadSchema(t) |
| 136 | + defs := m["definitions"].(map[string]any) |
| 137 | + mode := defs["repoUpdateMode"].(map[string]any) |
| 138 | + enum, ok := mode["enum"].([]any) |
| 139 | + require.True(t, ok) |
| 140 | + |
| 141 | + schemaVals := make([]string, 0, len(enum)) |
| 142 | + for _, v := range enum { |
| 143 | + schemaVals = append(schemaVals, v.(string)) |
| 144 | + } |
| 145 | + sort.Strings(schemaVals) |
| 146 | + |
| 147 | + goVals := []string{ |
| 148 | + string(appconfig.RepoUpdateOnce), |
| 149 | + string(appconfig.RepoUpdateAlways), |
| 150 | + string(appconfig.RepoUpdateNever), |
| 151 | + } |
| 152 | + sort.Strings(goVals) |
| 153 | + |
| 154 | + assert.Equal(t, goVals, schemaVals) |
| 155 | +} |
| 156 | + |
| 157 | +// TestRecipesParseAgainstSchemaShape is a structural smoke test: every recipe |
| 158 | +// shipped in docs/recipes must only use top-level keys that the schema |
| 159 | +// declares. This catches typos and schema drift without pulling in a full |
| 160 | +// JSON-schema validator as a dependency. |
| 161 | +func TestRecipesParseAgainstSchemaShape(t *testing.T) { |
| 162 | + recipesDir := filepath.Join("..", "docs", "recipes") |
| 163 | + entries, err := os.ReadDir(recipesDir) |
| 164 | + require.NoError(t, err) |
| 165 | + |
| 166 | + for _, entry := range entries { |
| 167 | + if entry.IsDir() { |
| 168 | + continue |
| 169 | + } |
| 170 | + name := entry.Name() |
| 171 | + if filepath.Ext(name) != ".yml" && filepath.Ext(name) != ".yaml" { |
| 172 | + continue |
| 173 | + } |
| 174 | + t.Run(name, func(t *testing.T) { |
| 175 | + path := filepath.Join(recipesDir, name) |
| 176 | + data, err := os.ReadFile(path) |
| 177 | + require.NoError(t, err) |
| 178 | + |
| 179 | + cfg, err := appconfig.ParseConfigFromContent(data) |
| 180 | + require.NoError(t, err, "recipe must parse as AppConfig") |
| 181 | + |
| 182 | + // Basic sanity: every installer in the recipe has a recognized |
| 183 | + // type (or is a category header). |
| 184 | + for _, inst := range cfg.Install { |
| 185 | + if inst.IsCategory() { |
| 186 | + continue |
| 187 | + } |
| 188 | + assert.NotEmptyf(t, string(inst.Type), "installer in %s missing type", name) |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments