Skip to content

Commit c88475b

Browse files
committed
refactor capability foundations tests with helpers
1 parent 2e7f79c commit c88475b

1 file changed

Lines changed: 22 additions & 65 deletions

File tree

vibes/capability_foundations_test.go

Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package vibes
22

33
import (
44
"context"
5-
"strings"
65
"testing"
76
)
87

@@ -16,15 +15,11 @@ func TestCapabilityFoundationsMixedAdapters(t *testing.T) {
1615
events := &eventsCapabilityStub{publishResult: NewNil()}
1716
jobs := &jobQueueStub{}
1817

19-
engine := MustNewEngine(Config{})
20-
script, err := engine.Compile(`def run(player_id)
18+
script := compileScriptDefault(t, `def run(player_id)
2119
player = db.find("Player", player_id)
2220
events.publish("player_seen", { id: player[:id], actor: ctx.user.id })
2321
jobs.enqueue("audit_player", { id: player[:id], raised: player[:raised] })
2422
end`)
25-
if err != nil {
26-
t.Fatalf("compile failed: %v", err)
27-
}
2823

2924
ctxCap := MustNewContextCapability("ctx", func(context.Context) (Value, error) {
3025
return NewObject(map[string]Value{
@@ -34,17 +29,12 @@ end`)
3429
}), nil
3530
})
3631

37-
result, err := script.Call(context.Background(), "run", []Value{NewString("player-1")}, CallOptions{
38-
Capabilities: []CapabilityAdapter{
39-
MustNewDBCapability("db", db),
40-
MustNewEventsCapability("events", events),
41-
ctxCap,
42-
MustNewJobQueueCapability("jobs", jobs),
43-
},
44-
})
45-
if err != nil {
46-
t.Fatalf("call failed: %v", err)
47-
}
32+
result := callScript(t, context.Background(), script, "run", []Value{NewString("player-1")}, callOptionsWithCapabilities(
33+
MustNewDBCapability("db", db),
34+
MustNewEventsCapability("events", events),
35+
ctxCap,
36+
MustNewJobQueueCapability("jobs", jobs),
37+
))
4838
if result.Kind() != KindString || result.String() != "queued" {
4939
t.Fatalf("unexpected result: %#v", result)
5040
}
@@ -74,29 +64,18 @@ func TestCapabilityFoundationsEachRespectsStepQuota(t *testing.T) {
7464
}
7565
db := &dbCapabilityStub{eachRows: rows}
7666

77-
engine := MustNewEngine(Config{StepQuota: 50})
78-
script, err := engine.Compile(`def run()
67+
script := compileScriptWithConfig(t, Config{StepQuota: 50}, `def run()
7968
total = 0
8069
db.each("ScoreEntry") do |row|
8170
total = total + row[:amount]
8271
end
8372
total
8473
end`)
85-
if err != nil {
86-
t.Fatalf("compile failed: %v", err)
87-
}
8874

89-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
90-
Capabilities: []CapabilityAdapter{
91-
MustNewDBCapability("db", db),
92-
},
93-
})
94-
if err == nil {
95-
t.Fatalf("expected step quota error")
96-
}
97-
if got := err.Error(); !strings.Contains(got, "step quota exceeded") {
98-
t.Fatalf("unexpected error: %s", got)
99-
}
75+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
76+
MustNewDBCapability("db", db),
77+
))
78+
requireErrorContains(t, err, "step quota exceeded")
10079
}
10180

10281
func TestCapabilityFoundationsEachNoopBlockRespectsStepQuota(t *testing.T) {
@@ -106,26 +85,15 @@ func TestCapabilityFoundationsEachNoopBlockRespectsStepQuota(t *testing.T) {
10685
}
10786
db := &dbCapabilityStub{eachRows: rows}
10887

109-
engine := MustNewEngine(Config{StepQuota: 20})
110-
script, err := engine.Compile(`def run()
88+
script := compileScriptWithConfig(t, Config{StepQuota: 20}, `def run()
11189
db.each("ScoreEntry") do |row|
11290
end
11391
end`)
114-
if err != nil {
115-
t.Fatalf("compile failed: %v", err)
116-
}
11792

118-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
119-
Capabilities: []CapabilityAdapter{
120-
MustNewDBCapability("db", db),
121-
},
122-
})
123-
if err == nil {
124-
t.Fatalf("expected step quota error")
125-
}
126-
if got := err.Error(); !strings.Contains(got, "step quota exceeded") {
127-
t.Fatalf("unexpected error: %s", got)
128-
}
93+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
94+
MustNewDBCapability("db", db),
95+
))
96+
requireErrorContains(t, err, "step quota exceeded")
12997
}
13098

13199
func TestCapabilityFoundationsEachRespectsRecursionLimit(t *testing.T) {
@@ -135,8 +103,7 @@ func TestCapabilityFoundationsEachRespectsRecursionLimit(t *testing.T) {
135103
},
136104
}
137105

138-
engine := MustNewEngine(Config{RecursionLimit: 5, StepQuota: 10_000})
139-
script, err := engine.Compile(`def recurse(n)
106+
script := compileScriptWithConfig(t, Config{RecursionLimit: 5, StepQuota: 10_000}, `def recurse(n)
140107
if n <= 0
141108
0
142109
else
@@ -149,19 +116,9 @@ def run()
149116
recurse(20)
150117
end
151118
end`)
152-
if err != nil {
153-
t.Fatalf("compile failed: %v", err)
154-
}
155119

156-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
157-
Capabilities: []CapabilityAdapter{
158-
MustNewDBCapability("db", db),
159-
},
160-
})
161-
if err == nil {
162-
t.Fatalf("expected recursion limit error")
163-
}
164-
if got := err.Error(); !strings.Contains(got, "recursion depth exceeded") {
165-
t.Fatalf("unexpected error: %s", got)
166-
}
120+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
121+
MustNewDBCapability("db", db),
122+
))
123+
requireErrorContains(t, err, "recursion depth exceeded")
167124
}

0 commit comments

Comments
 (0)