-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_example_test.go
More file actions
226 lines (201 loc) · 6.81 KB
/
action_example_test.go
File metadata and controls
226 lines (201 loc) · 6.81 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
package core_test
import . "dappco.re/go"
// ExampleAction declares action metadata for an agent dispatch workflow. Consumers copy
// the Result-shaped handler contract for dAppCore actions and tasks.
func ExampleAction() {
a := &Action{Name: "deploy", Description: "Deploy service"}
Println(a.Name)
Println(a.Description)
// Output:
// deploy
// Deploy service
}
// ExampleActionHandler declares an action handler through `ActionHandler` for an agent
// dispatch workflow. Consumers copy the Result-shaped handler contract for dAppCore
// actions and tasks.
func ExampleActionHandler() {
var handler ActionHandler = func(_ Context, opts Options) Result {
return Result{Value: opts.String("name"), OK: true}
}
Println(handler(Background(), NewOptions(Option{Key: "name", Value: "deploy"})).Value)
// Output: deploy
}
// ExampleAction_Run runs `Action.Run` with representative caller inputs for an agent
// dispatch workflow. Consumers copy the Result-shaped handler contract for dAppCore
// actions and tasks.
func ExampleAction_Run() {
c := New()
c.Action("double", func(_ Context, opts Options) Result {
return Result{Value: opts.Int("n") * 2, OK: true}
})
r := c.Action("double").Run(Background(), NewOptions(
Option{Key: "n", Value: 21},
))
Println(r.Value)
// Output: 42
}
// ExampleCore_Action registers or retrieves an action through `Core.Action` for an agent
// dispatch workflow. Consumers copy the Result-shaped handler contract for dAppCore
// actions and tasks.
func ExampleCore_Action() {
c := New()
c.Action("deploy", func(_ Context, _ Options) Result { return Result{OK: true} })
Println(c.Action("deploy").Exists())
// Output: true
}
// ExampleCore_Actions_action registers the action-oriented path through `Core.Actions` for
// an agent dispatch workflow. Consumers copy the Result-shaped handler contract for
// dAppCore actions and tasks.
func ExampleCore_Actions_action() {
c := New()
c.Action("deploy", func(_ Context, _ Options) Result { return Result{OK: true} })
c.Action("test", func(_ Context, _ Options) Result { return Result{OK: true} })
Println(c.Actions())
// Output: [deploy test]
}
// ExampleStep declares one task step through `Step` for an agent dispatch workflow.
// Consumers copy the Result-shaped handler contract for dAppCore actions and tasks.
func ExampleStep() {
step := Step{
Action: "deploy",
With: NewOptions(Option{Key: "target", Value: "homelab"}),
Input: "previous",
}
Println(step.Action)
Println(step.With.String("target"))
Println(step.Input)
// Output:
// deploy
// homelab
// previous
}
// ExampleTask declares a named task that points at a deployment planning step. Consumers
// copy the Result-shaped handler contract for dAppCore actions and tasks.
func ExampleTask() {
task := Task{Name: "deploy", Steps: []Step{{Action: "deploy.plan"}}}
Println(task.Name)
Println(task.Steps[0].Action)
// Output:
// deploy
// deploy.plan
}
// ExampleCore_Task_action registers the action-oriented path through `Core.Task` for an
// agent dispatch workflow. Consumers copy the Result-shaped handler contract for dAppCore
// actions and tasks.
func ExampleCore_Task_action() {
c := New()
c.Task("deploy", Task{Steps: []Step{{Action: "deploy.plan"}}})
Println(c.Tasks())
// Output: [deploy]
}
// ExampleCore_Tasks lists task names through `Core.Tasks` for an agent dispatch workflow.
// Consumers copy the Result-shaped handler contract for dAppCore actions and tasks.
func ExampleCore_Tasks() {
c := New()
c.Task("deploy", Task{})
c.Task("test", Task{})
Println(c.Tasks())
// Output: [deploy test]
}
// ExampleAction_Exists checks action availability before and after registering a handler.
// Consumers copy the Result-shaped handler contract for dAppCore actions and tasks.
func ExampleAction_Exists() {
c := New()
Println(c.Action("missing").Exists())
c.Action("present", func(_ Context, _ Options) Result { return Result{OK: true} })
Println(c.Action("present").Exists())
// Output:
// false
// true
}
// ExampleAction_Run_panicRecovery recovers a panicking handler through `Action.Run` for an
// agent dispatch workflow. Consumers copy the Result-shaped handler contract for dAppCore
// actions and tasks.
func ExampleAction_Run_panicRecovery() {
c := New()
c.Action("boom", func(_ Context, _ Options) Result {
panic("explosion")
})
r := c.Action("boom").Run(Background(), NewOptions())
Println(r.OK)
// Output: false
}
// ExampleAction_Run_entitlementDenied rejects a gated action through `Action.Run` when
// entitlement denies access for an agent dispatch workflow. Consumers copy the
// Result-shaped handler contract for dAppCore actions and tasks.
func ExampleAction_Run_entitlementDenied() {
c := New()
c.Action("premium", func(_ Context, _ Options) Result {
return Result{Value: "secret", OK: true}
})
c.SetEntitlementChecker(func(action string, _ int, _ Context) Entitlement {
if action == "premium" {
return Entitlement{Allowed: false, Reason: "upgrade"}
}
return Entitlement{Allowed: true, Unlimited: true}
})
r := c.Action("premium").Run(Background(), NewOptions())
Println(r.OK)
// Output: false
}
// ExampleAction_Task_Run runs `Task.Run` with representative caller inputs for background task
// progress. Asynchronous work reports progress through Core task helpers.
func ExampleTask_Run() {
c := New()
var order string
c.Action("step.a", func(_ Context, _ Options) Result {
order += "a"
return Result{Value: "from-a", OK: true}
})
c.Action("step.b", func(_ Context, opts Options) Result {
order += "b"
input := opts.Get("_input")
if input.OK {
return Result{Value: "got:" + input.Value.(string), OK: true}
}
return Result{OK: true}
})
c.Task("pipe", Task{
Steps: []Step{
{Action: "step.a"},
{Action: "step.b", Input: "previous"},
},
})
r := c.Task("pipe").Run(Background(), c, NewOptions())
Println(order)
Println(r.Value)
// Output:
// ab
// got:from-a
}
// ExampleCore_PerformAsync starts asynchronous work through `Core.PerformAsync` for
// background task progress. Asynchronous work reports progress through Core task helpers.
func ExampleCore_PerformAsync() {
c := New()
c.Action("bg.work", func(_ Context, _ Options) Result {
return Result{Value: "done", OK: true}
})
r := c.PerformAsync("bg.work", NewOptions())
Println(HasPrefix(r.Value.(string), "id-"))
// Output: true
}
// ExampleCore_Progress reports task progress through `Core.Progress` for background task
// progress. Asynchronous work reports progress through Core task helpers.
func ExampleCore_Progress() {
c := New()
var progress float64
var message string
c.RegisterAction(func(_ *Core, msg Message) Result {
if ev, ok := msg.(ActionTaskProgress); ok {
progress = ev.Progress
message = ev.Message
}
return Result{OK: true}
})
c.Progress("task-1", 0.5, "halfway", "deploy")
Println(progress)
Println(message)
// Output:
// 0.5
// halfway
}