-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow_test.go
More file actions
118 lines (104 loc) · 3.08 KB
/
workflow_test.go
File metadata and controls
118 lines (104 loc) · 3.08 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
package conditional
import (
"context"
"encoding/json"
"testing"
"github.com/sicko7947/gorkflow"
"github.com/sicko7947/gorkflow/engine"
"github.com/sicko7947/gorkflow/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConditionalWorkflow(t *testing.T) {
// Initialize engine with memory store
st := store.NewMemoryStore()
eng := engine.NewEngine(st)
// Create workflow
wf, err := NewConditionalWorkflow()
require.NoError(t, err)
tests := []struct {
name string
input ConditionalInput
expectedValue int
expectFormatted bool
}{
{
name: "Doubling enabled, Result > 10 -> Format runs",
input: ConditionalInput{
Value: 6,
EnableDoubling: true,
},
// 6 * 2 = 12. 12 > 10, so format runs.
expectedValue: 12,
expectFormatted: true,
},
{
name: "Doubling enabled, Result <= 10 -> Format skipped",
input: ConditionalInput{
Value: 5,
EnableDoubling: true,
},
// 5 * 2 = 10. 10 is not > 10, so format skipped.
expectedValue: 10,
expectFormatted: false,
},
{
name: "Doubling disabled -> Double skipped (0) -> Format skipped",
input: ConditionalInput{
Value: 20,
EnableDoubling: false,
},
// Doubling skipped, returns default value 0.
// 0 <= 10, so format skipped.
expectedValue: 0,
expectFormatted: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
// Run workflow synchronously
runID, err := eng.StartWorkflow(ctx, wf, tt.input, gorkflow.WithSynchronousExecution())
require.NoError(t, err)
// Verify run status
run, err := eng.GetRun(ctx, runID)
require.NoError(t, err)
assert.Equal(t, gorkflow.RunStatusCompleted, run.Status)
// Get step executions to verify outputs
executions, err := eng.GetStepExecutions(ctx, runID)
require.NoError(t, err)
// Find "double" step execution
var doubleExec *gorkflow.StepExecution
for _, exec := range executions {
if exec.StepID == "double" {
doubleExec = exec
break
}
}
require.NotNil(t, doubleExec, "Double step should have executed")
var doubleOut DoubleOutput
err = json.Unmarshal(doubleExec.Output, &doubleOut)
require.NoError(t, err)
assert.Equal(t, tt.expectedValue, doubleOut.Value)
// Find "conditional_format" step execution
var formatExec *gorkflow.StepExecution
for _, exec := range executions {
if exec.StepID == "conditional_format" {
formatExec = exec
break
}
}
require.NotNil(t, formatExec, "Format step should have executed (even if skipped logic)")
var formatOut ConditionalFormatOutput
err = json.Unmarshal(formatExec.Output, &formatOut)
require.NoError(t, err)
if tt.expectFormatted {
assert.NotEmpty(t, formatOut.Formatted, "Should have formatted output")
assert.Contains(t, formatOut.Formatted, "Final value")
} else {
// If skipped, it returns zero value, so Formatted should be empty
assert.Empty(t, formatOut.Formatted, "Should not have formatted output (skipped)")
}
})
}
}