-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_test.go
More file actions
384 lines (309 loc) · 9.23 KB
/
graph_test.go
File metadata and controls
384 lines (309 loc) · 9.23 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package debug_test
import (
"strings"
"testing"
"go.klarlabs.de/statekit"
"go.klarlabs.de/statekit/debug"
)
func buildGraphTestMachine() *statekit.MachineConfig[struct{}] {
machine, err := statekit.NewMachine[struct{}]("graph_test").
WithInitial("idle").
State("idle").
On("START").Target("running").
On("SKIP").Target("done").
Done().
State("running").
On("PAUSE").Target("paused").
On("STOP").Target("done").
Done().
State("paused").
On("RESUME").Target("running").
On("STOP").Target("done").
Done().
State("done").Final().Done().
Build()
if err != nil {
panic(err)
}
return machine
}
func TestStateGraph_NewStateGraph(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
if graph.ID != "graph_test" {
t.Errorf("expected ID 'graph_test', got %q", graph.ID)
}
if graph.Initial != "idle" {
t.Errorf("expected initial 'idle', got %q", graph.Initial)
}
if len(graph.Nodes) != 4 {
t.Errorf("expected 4 nodes, got %d", len(graph.Nodes))
}
if len(graph.Edges) != 6 {
t.Errorf("expected 6 edges, got %d", len(graph.Edges))
}
}
func TestStateGraph_GetNode(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
node := graph.GetNode("idle")
if node == nil {
t.Fatal("expected node for 'idle'")
}
if node.Type != "atomic" {
t.Errorf("expected type 'atomic', got %q", node.Type)
}
if graph.GetNode("nonexistent") != nil {
t.Error("expected nil for nonexistent node")
}
}
func TestStateGraph_GetEdgesFrom(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
edges := graph.GetEdgesFrom("idle")
if len(edges) != 2 {
t.Errorf("expected 2 edges from 'idle', got %d", len(edges))
}
}
func TestStateGraph_GetEdgesTo(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
edges := graph.GetEdgesTo("done")
if len(edges) != 3 {
t.Errorf("expected 3 edges to 'done', got %d", len(edges))
}
}
func TestStateGraph_RootNodes(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
roots := graph.RootNodes()
if len(roots) != 4 {
t.Errorf("expected 4 root nodes (flat machine), got %d", len(roots))
}
}
func TestStateGraph_LeafNodes(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
leaves := graph.LeafNodes()
if len(leaves) != 4 {
t.Errorf("expected 4 leaf nodes (flat machine), got %d", len(leaves))
}
}
func TestStateGraph_FinalNodes(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
finals := graph.FinalNodes()
if len(finals) != 1 {
t.Errorf("expected 1 final node, got %d", len(finals))
}
if finals[0].ID != "done" {
t.Errorf("expected final node 'done', got %q", finals[0].ID)
}
}
func TestStateGraph_GetPath(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
path := graph.GetPath("idle")
if len(path) != 1 || path[0] != "idle" {
t.Errorf("expected path [idle], got %v", path)
}
}
func TestStateGraph_Reachable(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
reachable := graph.Reachable("idle")
if len(reachable) != 4 {
t.Errorf("expected 4 reachable states from 'idle', got %d", len(reachable))
}
}
func TestStateGraph_UnreachableStates(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
unreachable := graph.UnreachableStates()
if len(unreachable) != 0 {
t.Errorf("expected 0 unreachable states, got %d: %v", len(unreachable), unreachable)
}
}
func TestStateGraph_DeadEndStates(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
deadEnds := graph.DeadEndStates()
// Final state is not a dead end
if len(deadEnds) != 0 {
t.Errorf("expected 0 dead end states, got %d: %v", len(deadEnds), deadEnds)
}
}
func TestStateGraph_String(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
str := graph.String()
if !strings.Contains(str, "StateGraph: graph_test") {
t.Error("string should contain graph ID")
}
if !strings.Contains(str, "Initial: idle") {
t.Error("string should contain initial state")
}
if !strings.Contains(str, "States: 4") {
t.Error("string should contain state count")
}
if !strings.Contains(str, "Transitions: 6") {
t.Error("string should contain transition count")
}
}
func TestStateGraph_ToMermaid(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
mermaid := graph.ToMermaid()
if !strings.Contains(mermaid, "stateDiagram-v2") {
t.Error("mermaid should start with stateDiagram-v2")
}
if !strings.Contains(mermaid, "[*] --> idle") {
t.Error("mermaid should contain initial state transition")
}
if !strings.Contains(mermaid, "idle --> running : START") {
t.Error("mermaid should contain transitions")
}
if !strings.Contains(mermaid, "done --> [*]") {
t.Error("mermaid should mark final state")
}
}
func TestStateGraph_ToDOT(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
dot := graph.ToDOT()
if !strings.Contains(dot, "digraph graph_test") {
t.Error("DOT should start with digraph")
}
if !strings.Contains(dot, "__start__") {
t.Error("DOT should contain start node")
}
if !strings.Contains(dot, "idle -> running") {
t.Error("DOT should contain transitions")
}
if !strings.Contains(dot, "done [shape=doublecircle]") {
t.Error("DOT should mark final state with doublecircle")
}
}
func TestStateGraph_Analyze(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
analysis := graph.Analyze()
if len(analysis.Unreachable) != 0 {
t.Errorf("expected 0 unreachable, got %v", analysis.Unreachable)
}
if len(analysis.DeadEnds) != 0 {
t.Errorf("expected 0 dead ends, got %v", analysis.DeadEnds)
}
if analysis.LeafCount != 4 {
t.Errorf("expected 4 leaves, got %d", analysis.LeafCount)
}
if analysis.FinalCount != 1 {
t.Errorf("expected 1 final, got %d", analysis.FinalCount)
}
if analysis.MaxDepth != 0 {
t.Errorf("expected max depth 0 (flat), got %d", analysis.MaxDepth)
}
// This machine has cycles (running <-> paused)
if !analysis.HasCycles {
t.Error("expected to detect cycles")
}
}
func TestStateGraph_HierarchicalMachine(t *testing.T) {
machine, err := statekit.NewMachine[struct{}]("nested").
WithInitial("active").
State("active").
WithInitial("idle").
On("EXIT").Target("done").End(). // TransitionBuilder.End() → StateBuilder[active]
State("idle").
On("START").Target("working").
End(). // TransitionBuilder.End() → StateBuilder[idle]
End(). // StateBuilder[idle].End() → StateBuilder[active]
State("working").
On("STOP").Target("idle").
End(). // TransitionBuilder.End() → StateBuilder[working]
End(). // StateBuilder[working].End() → StateBuilder[active]
Done().
State("done").Final().Done().
Build()
if err != nil {
t.Fatal(err)
}
graph := debug.NewStateGraph(machine)
if len(graph.Nodes) != 4 {
t.Errorf("expected 4 nodes, got %d", len(graph.Nodes))
}
// Check depth
activeNode := graph.GetNode("active")
if activeNode.Depth != 0 {
t.Errorf("expected 'active' depth 0, got %d", activeNode.Depth)
}
idleNode := graph.GetNode("idle")
if idleNode.Depth != 1 {
t.Errorf("expected 'idle' depth 1, got %d", idleNode.Depth)
}
// Check path
path := graph.GetPath("idle")
if len(path) != 2 || path[0] != "active" || path[1] != "idle" {
t.Errorf("expected path [active, idle], got %v", path)
}
// Analysis
analysis := graph.Analyze()
if analysis.MaxDepth != 1 {
t.Errorf("expected max depth 1, got %d", analysis.MaxDepth)
}
}
func TestStateGraph_WithUnreachableState(t *testing.T) {
// Build machine with an unreachable state
machine, err := statekit.NewMachine[struct{}]("unreachable_test").
WithInitial("a").
State("a").
On("NEXT").Target("b").
Done().
State("b").Final().Done().
State("orphan"). // No transitions lead here
On("X").Target("a").
Done().
Build()
if err != nil {
t.Fatal(err)
}
graph := debug.NewStateGraph(machine)
unreachable := graph.UnreachableStates()
if len(unreachable) != 1 || unreachable[0] != "orphan" {
t.Errorf("expected [orphan], got %v", unreachable)
}
}
func TestStateGraph_WithDeadEnd(t *testing.T) {
// Build machine with a dead end (non-final state with no outgoing transitions)
machine, err := statekit.NewMachine[struct{}]("deadend_test").
WithInitial("a").
State("a").
On("NEXT").Target("deadend").
Done().
State("deadend"). // No transitions, not final = dead end
Done().
Build()
if err != nil {
t.Fatal(err)
}
graph := debug.NewStateGraph(machine)
deadEnds := graph.DeadEndStates()
if len(deadEnds) != 1 || deadEnds[0] != "deadend" {
t.Errorf("expected [deadend], got %v", deadEnds)
}
}
func TestAnalysis_String(t *testing.T) {
machine := buildGraphTestMachine()
graph := debug.NewStateGraph(machine)
analysis := graph.Analyze()
str := analysis.String()
if !strings.Contains(str, "Analysis Results:") {
t.Error("should contain header")
}
if !strings.Contains(str, "Leaf States: 4") {
t.Error("should contain leaf count")
}
if !strings.Contains(str, "Final States: 1") {
t.Error("should contain final count")
}
}