-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
314 lines (264 loc) · 6.18 KB
/
example_test.go
File metadata and controls
314 lines (264 loc) · 6.18 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
package core_test
import (
"context"
. "dappco.re/go/core"
)
// --- Core Creation ---
func ExampleNew() {
c := New(
WithOption("name", "my-app"),
WithServiceLock(),
)
Println(c.App().Name)
// Output: my-app
}
func ExampleNew_withService() {
c := New(
WithOption("name", "example"),
WithService(func(c *Core) Result {
return c.Service("greeter", Service{
OnStart: func() Result {
Info("greeter started", "app", c.App().Name)
return Result{OK: true}
},
})
}),
)
c.ServiceStartup(context.Background(), nil)
Println(c.Services())
c.ServiceShutdown(context.Background())
// Output is non-deterministic (map order), so no Output comment
}
// --- Options ---
func ExampleNewOptions() {
opts := NewOptions(
Option{Key: "name", Value: "brain"},
Option{Key: "port", Value: 8080},
Option{Key: "debug", Value: true},
)
Println(opts.String("name"))
Println(opts.Int("port"))
Println(opts.Bool("debug"))
// Output:
// brain
// 8080
// true
}
// --- Result ---
func ExampleResult() {
r := Result{Value: "hello", OK: true}
if r.OK {
Println(r.Value)
}
// Output: hello
}
// --- Action ---
func ExampleCore_Action_register() {
c := New()
c.Action("greet", func(_ context.Context, opts Options) Result {
name := opts.String("name")
return Result{Value: Concat("hello ", name), OK: true}
})
Println(c.Action("greet").Exists())
// Output: true
}
func ExampleCore_Action_invoke() {
c := New()
c.Action("add", func(_ context.Context, opts Options) Result {
a := opts.Int("a")
b := opts.Int("b")
return Result{Value: a + b, OK: true}
})
r := c.Action("add").Run(context.Background(), NewOptions(
Option{Key: "a", Value: 3},
Option{Key: "b", Value: 4},
))
Println(r.Value)
// Output: 7
}
func ExampleCore_Actions() {
c := New()
c.Action("process.run", func(_ context.Context, _ Options) Result { return Result{OK: true} })
c.Action("brain.recall", func(_ context.Context, _ Options) Result { return Result{OK: true} })
Println(c.Actions())
// Output: [process.run brain.recall]
}
// --- Task ---
func ExampleCore_Task() {
c := New()
order := ""
c.Action("step.a", func(_ context.Context, _ Options) Result {
order += "a"
return Result{Value: "from-a", OK: true}
})
c.Action("step.b", func(_ context.Context, opts Options) Result {
order += "b"
return Result{OK: true}
})
c.Task("pipeline", Task{
Steps: []Step{
{Action: "step.a"},
{Action: "step.b", Input: "previous"},
},
})
c.Task("pipeline").Run(context.Background(), c, NewOptions())
Println(order)
// Output: ab
}
// --- Registry ---
func ExampleNewRegistry() {
r := NewRegistry[string]()
r.Set("alpha", "first")
r.Set("bravo", "second")
Println(r.Has("alpha"))
Println(r.Names())
Println(r.Len())
// Output:
// true
// [alpha bravo]
// 2
}
func ExampleRegistry_Lock() {
r := NewRegistry[string]()
r.Set("alpha", "first")
r.Lock()
result := r.Set("beta", "second")
Println(result.OK)
// Output: false
}
func ExampleRegistry_Seal() {
r := NewRegistry[string]()
r.Set("alpha", "first")
r.Seal()
// Can update existing
Println(r.Set("alpha", "updated").OK)
// Can't add new
Println(r.Set("beta", "new").OK)
// Output:
// true
// false
}
// --- Entitlement ---
func ExampleCore_Entitled_default() {
c := New()
e := c.Entitled("anything")
Println(e.Allowed)
Println(e.Unlimited)
// Output:
// true
// true
}
func ExampleCore_Entitled_custom() {
c := New()
c.SetEntitlementChecker(func(action string, qty int, _ context.Context) Entitlement {
if action == "premium" {
return Entitlement{Allowed: false, Reason: "upgrade required"}
}
return Entitlement{Allowed: true, Unlimited: true}
})
Println(c.Entitled("basic").Allowed)
Println(c.Entitled("premium").Allowed)
Println(c.Entitled("premium").Reason)
// Output:
// true
// false
// upgrade required
}
func ExampleEntitlement_NearLimit() {
e := Entitlement{Allowed: true, Limit: 100, Used: 85, Remaining: 15}
Println(e.NearLimit(0.8))
Println(e.UsagePercent())
// Output:
// true
// 85
}
// --- Process ---
func ExampleCore_Process() {
c := New()
// No go-process registered — permission by registration
Println(c.Process().Exists())
// Register a mock process handler
c.Action("process.run", func(_ context.Context, opts Options) Result {
return Result{Value: Concat("output of ", opts.String("command")), OK: true}
})
Println(c.Process().Exists())
r := c.Process().Run(context.Background(), "echo", "hello")
Println(r.Value)
// Output:
// false
// true
// output of echo
}
// --- JSON ---
func ExampleJSONMarshal() {
type config struct {
Host string `json:"host"`
Port int `json:"port"`
}
r := JSONMarshal(config{Host: "localhost", Port: 8080})
Println(string(r.Value.([]byte)))
// Output: {"host":"localhost","port":8080}
}
func ExampleJSONUnmarshalString() {
type config struct {
Host string `json:"host"`
Port int `json:"port"`
}
var cfg config
JSONUnmarshalString(`{"host":"localhost","port":8080}`, &cfg)
Println(cfg.Host, cfg.Port)
// Output: localhost 8080
}
// --- Utilities ---
func ExampleID() {
id := ID()
Println(HasPrefix(id, "id-"))
// Output: true
}
func ExampleValidateName() {
Println(ValidateName("brain").OK)
Println(ValidateName("").OK)
Println(ValidateName("..").OK)
Println(ValidateName("path/traversal").OK)
// Output:
// true
// false
// false
// false
}
func ExampleSanitisePath() {
Println(SanitisePath("../../etc/passwd"))
Println(SanitisePath(""))
Println(SanitisePath("/some/path/file.txt"))
// Output:
// passwd
// invalid
// file.txt
}
// --- Command ---
func ExampleCore_Command() {
c := New()
c.Command("deploy/to/homelab", Command{
Action: func(opts Options) Result {
return Result{Value: Concat("deployed to ", opts.String("_arg")), OK: true}
},
})
r := c.Cli().Run("deploy", "to", "homelab")
Println(r.OK)
// Output: true
}
// --- Config ---
func ExampleConfig() {
c := New()
c.Config().Set("database.host", "localhost")
c.Config().Set("database.port", 5432)
c.Config().Enable("dark-mode")
Println(c.Config().String("database.host"))
Println(c.Config().Int("database.port"))
Println(c.Config().Enabled("dark-mode"))
// Output:
// localhost
// 5432
// true
}
// Error examples in error_example_test.go