返回 DeepSeek-Reasonix
rebuild_config_classification_test.go
根目录 / internal / boot / rebuild_config_classification_test.go
1 package boot
2
3 import (
4 "fmt"
5 "maps"
6 "reflect"
7 "slices"
8 "strings"
9 "testing"
10
11 "reasonix/internal/config"
12 )
13
14 // Classes describe ownership, not a promise that each raw field is hashed:
15 // runtime includes derived dependencies, live is updated without replacement,
16 // and excluded covers presentation, other host lifetimes and retired settings.
17 // Container classes never exempt their nested exported fields from review.
18 type configFieldClasses struct {
19 runtime, live, excluded string
20 }
21
22 type configTypeClassification struct {
23 typeOf reflect.Type
24 fields configFieldClasses
25 }
26
27 func classifyConfigType[T any](fields configFieldClasses) configTypeClassification {
28 return configTypeClassification{reflect.TypeFor[T](), fields}
29 }
30
31 func runtimeConfigClassification() []configTypeClassification {
32 return append(runtimeConfigCoreClassification(), runtimeConfigHostClassification()...)
33 }
34
35 func TestRuntimeConfigClassificationCoversExportedFields(t *testing.T) {
36 for _, issue := range configClassificationIssues(reflect.TypeFor[config.Config](), runtimeConfigClassification()) {
37 t.Error(issue)
38 }
39 }
40
41 func configClassificationIssues(root reflect.Type, schema []configTypeClassification) []string {
42 index := make(map[reflect.Type]configFieldClasses, len(schema))
43 var issues []string
44 for _, entry := range schema {
45 if _, duplicate := index[entry.typeOf]; duplicate {
46 issues = append(issues, fmt.Sprintf("duplicate type classification: %v", entry.typeOf))
47 }
48 index[entry.typeOf] = entry.fields
49 }
50 visited := map[reflect.Type]bool{}
51 var visit func(reflect.Type, string)
52 visit = func(typ reflect.Type, path string) {
53 switch typ.Kind() {
54 case reflect.Pointer, reflect.Slice, reflect.Array:
55 visit(typ.Elem(), path+"[]")
56 case reflect.Map:
57 visit(typ.Key(), path+"{key}")
58 visit(typ.Elem(), path+"{value}")
59 case reflect.Struct:
60 if visited[typ] {
61 return
62 }
63 visited[typ] = true
64 classes, known := index[typ]
65 if !known {
66 issues = append(issues, "unclassified type at "+path)
67 }
68 issues = append(issues, classifiedFieldIssues(typ, path, classes)...)
69 for field := range typ.NumField() {
70 f := typ.Field(field)
71 if f.IsExported() {
72 visit(f.Type, path+"."+f.Name)
73 }
74 }
75 }
76 }
77 visit(root, "Config")
78 for typ := range index {
79 if !visited[typ] {
80 issues = append(issues, fmt.Sprintf("stale type classification: %v", typ))
81 }
82 }
83 slices.Sort(issues)
84 return issues
85 }
86
87 func classifiedFieldIssues(typ reflect.Type, path string, classes configFieldClasses) []string {
88 classified := map[string]bool{}
89 var issues []string
90 for _, group := range []string{classes.runtime, classes.live, classes.excluded} {
91 for name := range strings.FieldsSeq(group) {
92 if classified[name] {
93 issues = append(issues, "duplicate field classification: "+path+"."+name)
94 }
95 classified[name] = true
96 }
97 }
98 remaining := maps.Clone(classified)
99 for field := range typ.NumField() {
100 f := typ.Field(field)
101 if !f.IsExported() {
102 continue
103 }
104 if !classified[f.Name] {
105 issues = append(issues, "unclassified field: "+path+"."+f.Name)
106 }
107 delete(remaining, f.Name)
108 }
109 for name := range remaining {
110 issues = append(issues, "stale field classification: "+path+"."+name)
111 }
112 return issues
113 }
114
115 func TestRuntimeConfigClassificationRejectsAddedFields(t *testing.T) {
116 cases := []struct {
117 name, parent string
118 target reflect.Type
119 }{
120 {"top level", "", reflect.TypeFor[config.Config]()},
121 {"nested runtime", "Agent", reflect.TypeFor[config.AgentConfig]()},
122 {"nested excluded", "UI", reflect.TypeFor[config.UIConfig]()},
123 {"upload admission", "Attachments", reflect.TypeFor[config.AttachmentsConfig]()},
124 {"slice element", "Providers", reflect.TypeFor[config.ProviderEntry]()},
125 }
126 for _, tc := range cases {
127 t.Run(tc.name, func(t *testing.T) {
128 fields := configClassificationStructFields(tc.target)
129 mutant := reflect.StructOf(append(fields, reflect.StructField{Name: "FutureSetting", Type: reflect.TypeFor[string]()}))
130 schema := replaceClassifiedType(runtimeConfigClassification(), tc.target, mutant)
131 root := mutant
132 if tc.parent != "" {
133 rootFields := configClassificationStructFields(reflect.TypeFor[config.Config]())
134 for i := range rootFields {
135 if rootFields[i].Name == tc.parent {
136 rootFields[i].Type = mutant
137 if tc.parent == "Providers" {
138 rootFields[i].Type = reflect.SliceOf(mutant)
139 }
140 }
141 }
142 root = reflect.StructOf(rootFields)
143 schema = replaceClassifiedType(schema, reflect.TypeFor[config.Config](), root)
144 }
145 path := "Config.FutureSetting"
146 if tc.parent != "" {
147 parent := tc.parent
148 if parent == "Providers" {
149 parent += "[]"
150 }
151 path = "Config." + parent + ".FutureSetting"
152 }
153 assertConfigClassificationIssue(t, root, schema, "unclassified field: ", path)
154 })
155 }
156 }
157
158 func configClassificationStructFields(typ reflect.Type) []reflect.StructField {
159 fields := make([]reflect.StructField, typ.NumField())
160 for i := range fields {
161 fields[i] = typ.Field(i)
162 }
163 return fields
164 }
165
166 func replaceClassifiedType(schema []configTypeClassification, old, next reflect.Type) []configTypeClassification {
167 for i := range schema {
168 if schema[i].typeOf == old {
169 schema[i].typeOf = next
170 }
171 }
172 return schema
173 }
174
175 func assertConfigClassificationIssue(t *testing.T, root reflect.Type, schema []configTypeClassification, prefix, field string) {
176 t.Helper()
177 for _, issue := range configClassificationIssues(root, schema) {
178 if strings.Contains(issue, prefix) && strings.HasSuffix(issue, field) {
179 return
180 }
181 }
182 t.Fatalf("classification guard missed %q ending in %q", prefix, field)
183 }
184
184 lines GO