-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathvalidator.go
More file actions
172 lines (156 loc) · 4.66 KB
/
validator.go
File metadata and controls
172 lines (156 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package gotaskflow
import (
"fmt"
"strings"
)
// validate checks a traceRecord against the expected TaskFlow DAG.
// A nil rec (e.g. no tracer configured) is treated as "no trace data" and always returns valid.
// Internal-only: used in tests to verify execution correctness.
func validate(rec traceRecord, tf *TaskFlow) *validationResult {
if rec == nil {
return &validationResult{valid: true}
}
return newValidator(rec).run(tf)
}
// validationResult contains the result of validating trace events against a TaskFlow.
type validationResult struct {
valid bool
missingTasks []string // Tasks defined but not executed
unexpectedTasks []string // Tasks executed but not defined
dependencyErrors []dependencyError // Dependency mismatches
skippedBranches []string // Condition branches that were skipped (not errors)
}
// dependencyError represents a mismatch in task dependencies.
type dependencyError struct {
task string
expected []string
actual []string
}
func (e dependencyError) String() string {
return fmt.Sprintf("task %q: expected deps %v, actual %v", e.task, e.expected, e.actual)
}
func (r *validationResult) String() string {
if r.valid {
return "validation passed"
}
var sb strings.Builder
sb.WriteString("validation failed:\n")
if len(r.missingTasks) > 0 {
sb.WriteString(fmt.Sprintf(" missing tasks: %v\n", r.missingTasks))
}
if len(r.unexpectedTasks) > 0 {
sb.WriteString(fmt.Sprintf(" unexpected tasks: %v\n", r.unexpectedTasks))
}
for _, e := range r.dependencyErrors {
sb.WriteString(fmt.Sprintf(" %s\n", e.String()))
}
if len(r.skippedBranches) > 0 {
sb.WriteString(fmt.Sprintf(" skipped branches (OK): %v\n", r.skippedBranches))
}
return sb.String()
}
// validator validates a traceRecord against TaskFlow definitions.
type validator struct {
rec traceRecord
}
func newValidator(rec traceRecord) *validator {
return &validator{rec: rec}
}
// run compares executed trace events against the expected TaskFlow DAG.
func (v *validator) run(tf *TaskFlow) *validationResult {
result := &validationResult{valid: true}
// --- Step 1: collect expected nodes via eGraph.walk ---
expected := make(map[string]*innerNode)
tf.graph.walk(func(n *innerNode) { expected[n.name] = n })
// --- Step 2: build executed map from the immutable record ---
executed := make(map[string]chromeTraceEvent, len(v.rec))
for _, ev := range v.rec {
executed[ev.Name] = ev
}
// --- Step 3: missing / skipped check ---
// A task is a skipped branch if:
// (a) it is a direct successor of a condition node that chose a different branch, OR
// (b) any of its non-condition predecessors is also skipped (transitive skip).
skipped := make(map[string]bool)
for name, node := range expected {
if _, ran := executed[name]; !ran && node.hasCondPredecessor() {
skipped[name] = true
}
}
for changed := true; changed; {
changed = false
for name, node := range expected {
if _, ran := executed[name]; ran || skipped[name] {
continue
}
for _, dep := range node.dependents {
if dep.Typ != nodeCondition && skipped[dep.name] {
skipped[name] = true
changed = true
break
}
}
}
}
for name := range expected {
if _, ran := executed[name]; !ran {
if skipped[name] {
result.skippedBranches = append(result.skippedBranches, name)
} else {
result.missingTasks = append(result.missingTasks, name)
result.valid = false
}
}
}
// --- Step 4: unexpected check ---
for name := range executed {
if _, defined := expected[name]; !defined {
result.unexpectedTasks = append(result.unexpectedTasks, name)
result.valid = false
}
}
// --- Step 5: dependency check ---
for name, ev := range executed {
node, ok := expected[name]
if !ok {
continue
}
var expDeps []string
for _, dep := range node.dependents {
if _, ran := executed[dep.name]; ran {
expDeps = append(expDeps, dep.name)
}
}
var actDeps []string
if raw := ev.Args["dependents"]; raw != "" {
for _, d := range strings.Split(raw, ",") {
if _, ran := executed[d]; ran {
actDeps = append(actDeps, d)
}
}
}
if !stringSliceEqual(expDeps, actDeps) {
result.dependencyErrors = append(result.dependencyErrors, dependencyError{
task: name, expected: expDeps, actual: actDeps,
})
result.valid = false
}
}
return result
}
// stringSliceEqual checks if two string slices contain the same elements (order-independent).
func stringSliceEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
count := make(map[string]int, len(a))
for _, s := range a {
count[s]++
}
for _, s := range b {
if count[s]--; count[s] < 0 {
return false
}
}
return true
}