@@ -24,22 +24,15 @@ func (s *eventsCapabilityStub) Publish(ctx context.Context, req EventPublishRequ
2424
2525func TestEventsCapabilityPublish (t * testing.T ) {
2626 stub := & eventsCapabilityStub {publishResult : NewBool (true )}
27- engine := MustNewEngine (Config {})
28- script , err := engine .Compile (`def run()
27+ script := compileScriptDefault (t , `def run()
2928 events.publish("players_totals", { id: "p-1", total: "55.00 USD" }, trace: "abc")
3029end` )
31- if err != nil {
32- t .Fatalf ("compile failed: %v" , err )
33- }
3430
3531 type ctxKey string
3632 ctx := context .WithValue (context .Background (), ctxKey ("request_id" ), "req-1" )
37- result , err := script .Call (ctx , "run" , nil , CallOptions {
38- Capabilities : []CapabilityAdapter {MustNewEventsCapability ("events" , stub )},
39- })
40- if err != nil {
41- t .Fatalf ("call failed: %v" , err )
42- }
33+ result := callScript (t , ctx , script , "run" , nil , callOptionsWithCapabilities (
34+ MustNewEventsCapability ("events" , stub ),
35+ ))
4336 if result .Kind () != KindBool || ! result .Bool () {
4437 t .Fatalf ("unexpected result: %#v" , result )
4538 }
@@ -63,48 +56,30 @@ end`)
6356
6457func TestEventsCapabilityRejectsCallablePayload (t * testing.T ) {
6558 stub := & eventsCapabilityStub {}
66- engine := MustNewEngine (Config {})
67- script , err := engine .Compile (`def helper(value)
59+ script := compileScriptDefault (t , `def helper(value)
6860 value
6961end
7062
7163def run()
7264 events.publish("topic", { callback: helper })
7365end` )
74- if err != nil {
75- t .Fatalf ("compile failed: %v" , err )
76- }
7766
78- _ , err = script .Call (context .Background (), "run" , nil , CallOptions {
79- Capabilities : []CapabilityAdapter {MustNewEventsCapability ("events" , stub )},
80- })
81- if err == nil {
82- t .Fatalf ("expected callable payload error" )
83- }
84- if got := err .Error (); ! strings .Contains (got , "events.publish payload must be data-only" ) {
85- t .Fatalf ("unexpected error: %s" , got )
86- }
67+ err := callScriptErr (t , context .Background (), script , "run" , nil , callOptionsWithCapabilities (
68+ MustNewEventsCapability ("events" , stub ),
69+ ))
70+ requireErrorContains (t , err , "events.publish payload must be data-only" )
8771}
8872
8973func TestEventsCapabilityRejectsNonHashPayload (t * testing.T ) {
9074 stub := & eventsCapabilityStub {}
91- engine := MustNewEngine (Config {})
92- script , err := engine .Compile (`def run()
75+ script := compileScriptDefault (t , `def run()
9376 events.publish("topic", 42)
9477end` )
95- if err != nil {
96- t .Fatalf ("compile failed: %v" , err )
97- }
9878
99- _ , err = script .Call (context .Background (), "run" , nil , CallOptions {
100- Capabilities : []CapabilityAdapter {MustNewEventsCapability ("events" , stub )},
101- })
102- if err == nil {
103- t .Fatalf ("expected non-hash payload error" )
104- }
105- if got := err .Error (); ! strings .Contains (got , "events.publish payload expected hash, got int" ) {
106- t .Fatalf ("unexpected error: %s" , got )
107- }
79+ err := callScriptErr (t , context .Background (), script , "run" , nil , callOptionsWithCapabilities (
80+ MustNewEventsCapability ("events" , stub ),
81+ ))
82+ requireErrorContains (t , err , "events.publish payload expected hash, got int" )
10883}
10984
11085func TestEventsCapabilityRejectsCallableReturn (t * testing.T ) {
@@ -115,23 +90,14 @@ func TestEventsCapabilityRejectsCallableReturn(t *testing.T) {
11590 }),
11691 }),
11792 }
118- engine := MustNewEngine (Config {})
119- script , err := engine .Compile (`def run()
93+ script := compileScriptDefault (t , `def run()
12094 events.publish("topic", { id: "p-1" })
12195end` )
122- if err != nil {
123- t .Fatalf ("compile failed: %v" , err )
124- }
12596
126- _ , err = script .Call (context .Background (), "run" , nil , CallOptions {
127- Capabilities : []CapabilityAdapter {MustNewEventsCapability ("events" , stub )},
128- })
129- if err == nil {
130- t .Fatalf ("expected return contract error" )
131- }
132- if got := err .Error (); ! strings .Contains (got , "events.publish return value must be data-only" ) {
133- t .Fatalf ("unexpected error: %s" , got )
134- }
97+ err := callScriptErr (t , context .Background (), script , "run" , nil , callOptionsWithCapabilities (
98+ MustNewEventsCapability ("events" , stub ),
99+ ))
100+ requireErrorContains (t , err , "events.publish return value must be data-only" )
135101}
136102
137103func TestEventsCapabilityReturnsAreClonedFromHostState (t * testing.T ) {
@@ -142,20 +108,14 @@ func TestEventsCapabilityReturnsAreClonedFromHostState(t *testing.T) {
142108 }),
143109 }),
144110 }
145- engine := MustNewEngine (Config {})
146- script , err := engine .Compile (`def run()
111+ script := compileScriptDefault (t , `def run()
147112 event = events.publish("topic", { id: "p-1" })
148113 event[:meta][:trace] = "script"
149114end` )
150- if err != nil {
151- t .Fatalf ("compile failed: %v" , err )
152- }
153115
154- if _ , err := script .Call (context .Background (), "run" , nil , CallOptions {
155- Capabilities : []CapabilityAdapter {MustNewEventsCapability ("events" , stub )},
156- }); err != nil {
157- t .Fatalf ("call failed: %v" , err )
158- }
116+ callScript (t , context .Background (), script , "run" , nil , callOptionsWithCapabilities (
117+ MustNewEventsCapability ("events" , stub ),
118+ ))
159119
160120 trace := stub .publishResult .Hash ()["meta" ].Hash ()["trace" ]
161121 if trace .Kind () != KindString || trace .String () != "host" {
0 commit comments