1package config
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7)
8
9func TestLoad(t *testing.T) {
10 tests := []struct {
11 name string
12 yaml string
13 wantErr bool
14 check func(t *testing.T, cfg *Config)
15 }{
16 {
17 name: "valid config",
18 yaml: `version: 1
19components:
20 core:
21 paths: [internal/domain/**]
22 role: core
23 adapters:
24 paths: [internal/infrastructure/**]
25 role: adapter
26rules:
27 no-adapter-imports-in-core: error
28allow:
29 - rule: no-local-fakes-for-ports
30 path: internal/foo_test.go
31 reason: stateful fake
32`,
33 check: func(t *testing.T, cfg *Config) {
34 t.Helper()
35 if cfg.Version != 1 {
36 t.Fatalf("Version = %d", cfg.Version)
37 }
38 if cfg.Components["core"].Role != RoleCore {
39 t.Fatalf("core role = %q", cfg.Components["core"].Role)
40 }
41 if cfg.Severity("no-adapter-imports-in-core") != SeverityError {
42 t.Fatalf("severity not parsed")
43 }
44 if !cfg.IsAllowed("no-local-fakes-for-ports", "internal/foo_test.go") {
45 t.Fatalf("allow entry not matched")
46 }
47 },
48 },
49 {
50 name: "invalid role",
51 yaml: `version: 1
52components:
53 bad:
54 paths: [internal/**]
55 role: wat
56`,
57 wantErr: true,
58 },
59 }
60
61 for _, tt := range tests {
62 t.Run(tt.name, func(t *testing.T) {
63 dir := t.TempDir()
64 path := filepath.Join(dir, ".hexcheck.yaml")
65 if err := os.WriteFile(path, []byte(tt.yaml), 0o600); err != nil {
66 t.Fatal(err)
67 }
68 cfg, err := Load(path)
69 if tt.wantErr {
70 if err == nil {
71 t.Fatal("expected error")
72 }
73 return
74 }
75 if err != nil {
76 t.Fatal(err)
77 }
78 if tt.check != nil {
79 tt.check(t, cfg)
80 }
81 })
82 }
83}
84
85func TestLoadRejectsExplicitZeroVersion(t *testing.T) {
86 tests := []struct {
87 name string
88 yaml string
89 }{
90 {name: "zero", yaml: "version: 0\n"},
91 {name: "positive zero", yaml: "version: +0\n"},
92 {name: "double zero", yaml: "version: 00\n"},
93 }
94 for _, tt := range tests {
95 t.Run(tt.name, func(t *testing.T) {
96 dir := t.TempDir()
97 path := filepath.Join(dir, ".hexcheck.yaml")
98 data := []byte(tt.yaml + `components:
99 adapters:
100 paths: [internal/infrastructure/**]
101 role: adapter
102`)
103 if err := os.WriteFile(path, data, 0o600); err != nil {
104 t.Fatal(err)
105 }
106 if _, err := Load(path); err == nil {
107 t.Fatal("expected explicit zero version to be rejected")
108 }
109 })
110 }
111}
112
113func TestLoadHonorsExplicitExcludeTestFilesFalse(t *testing.T) {
114 dir := t.TempDir()
115 path := filepath.Join(dir, ".hexcheck.yaml")
116 data := []byte(`version: 1
117components:
118 adapters:
119 paths: [internal/infrastructure/**]
120 role: adapter
121heuristics:
122 excludeTestFiles: false
123`)
124 if err := os.WriteFile(path, data, 0o600); err != nil {
125 t.Fatal(err)
126 }
127 cfg, err := Load(path)
128 if err != nil {
129 t.Fatal(err)
130 }
131 if cfg.Heuristics.ExcludeTestFiles == nil || *cfg.Heuristics.ExcludeTestFiles {
132 t.Fatal("excludeTestFiles=false was not preserved")
133 }
134 if !filepath.IsAbs(cfg.Root) {
135 t.Fatalf("Root = %q, want absolute path", cfg.Root)
136 }
137}
138
139func TestValidateRejectsInvalidHeuristicKnobs(t *testing.T) {
140 tests := []struct {
141 name string
142 mutate func(*Config)
143 }{
144 {name: "negative threshold", mutate: func(c *Config) { c.Heuristics.BusinessLogicThreshold = intPtr(-1) }},
145 {name: "negative strong", mutate: func(c *Config) { c.Heuristics.BusinessLogicMinStrongSignals = intPtr(-1) }},
146 {name: "negative weak", mutate: func(c *Config) { c.Heuristics.BusinessLogicMinWeakSignals = intPtr(-1) }},
147 {name: "zero max nodes", mutate: func(c *Config) { c.Heuristics.BusinessLogicMaxFunctionNodes = intPtr(0) }},
148 {name: "zero max diagnostics", mutate: func(c *Config) { c.Heuristics.BusinessLogicMaxDiagnosticsPerPackage = intPtr(0) }},
149 }
150 for _, tt := range tests {
151 t.Run(tt.name, func(t *testing.T) {
152 cfg := &Config{Version: 1, Components: map[string]Component{"adapters": {Role: RoleAdapter, Paths: []string{"internal/**"}}}}
153 cfg.applyDefaults()
154 tt.mutate(cfg)
155 if err := cfg.Validate(); err == nil {
156 t.Fatal("expected validation error")
157 }
158 })
159 }
160}
161
162func TestComponentForPathPrecedence(t *testing.T) {
163 cfg := &Config{Components: map[string]Component{
164 "all": {Role: RoleCore, Paths: []string{"internal/**"}},
165 "adapter": {Role: RoleAdapter, Paths: []string{"internal/infrastructure/**"}},
166 "ignore": {Role: RoleIgnore, Paths: []string{"internal/infrastructure/generated/**"}},
167 }}
168 cfg.applyDefaults()
169
170 tests := []struct {
171 name string
172 path string
173 want Role
174 }{
175 {name: "specific adapter beats broad core", path: "internal/infrastructure/postgres/repo.go", want: RoleAdapter},
176 {name: "ignore beats adapter", path: "internal/infrastructure/generated/foo.go", want: RoleIgnore},
177 }
178 for _, tt := range tests {
179 t.Run(tt.name, func(t *testing.T) {
180 match, ok := cfg.ComponentForPath(tt.path)
181 if !ok || match.Role != tt.want {
182 t.Fatalf("match = %#v, %v; want role %q", match, ok, tt.want)
183 }
184 })
185 }
186}