Skip to content

Commit 2e7f79c

Browse files
committed
refactor job queue capability tests with helpers
1 parent eb3a550 commit 2e7f79c

1 file changed

Lines changed: 61 additions & 163 deletions

File tree

vibes/capability_jobqueue_test.go

Lines changed: 61 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,16 @@ func (s *sharedReturnQueue) Retry(ctx context.Context, req JobQueueRetryRequest)
7373

7474
func TestJobQueueCapabilityEnqueue(t *testing.T) {
7575
stub := &jobQueueStub{}
76-
engine := MustNewEngine(Config{})
77-
script, err := engine.Compile(`def run()
76+
script := compileScriptDefault(t, `def run()
7877
jobs.enqueue("demo", { foo: "bar" }, delay: 2.seconds, key: "abc", queue: "standard")
7978
end`)
80-
if err != nil {
81-
t.Fatalf("compile failed: %v", err)
82-
}
8379

8480
type ctxKey string
8581
ctx := context.WithValue(context.Background(), ctxKey("trace"), "on")
8682

87-
result, err := script.Call(ctx, "run", nil, CallOptions{
88-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
89-
})
90-
if err != nil {
91-
t.Fatalf("call failed: %v", err)
92-
}
83+
result := callScript(t, ctx, script, "run", nil, callOptionsWithCapabilities(
84+
MustNewJobQueueCapability("jobs", stub),
85+
))
9386
if result.Kind() != KindString || result.String() != "queued" {
9487
t.Fatalf("unexpected enqueue result: %#v", result)
9588
}
@@ -133,20 +126,13 @@ end`)
133126

134127
func TestJobQueueCapabilityRetry(t *testing.T) {
135128
stub := &jobQueueStub{}
136-
engine := MustNewEngine(Config{})
137-
script, err := engine.Compile(`def run()
129+
script := compileScriptDefault(t, `def run()
138130
jobs.retry("job-7", attempts: 3, priority: "high")
139131
end`)
140-
if err != nil {
141-
t.Fatalf("compile failed: %v", err)
142-
}
143132

144-
result, err := script.Call(context.Background(), "run", nil, CallOptions{
145-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
146-
})
147-
if err != nil {
148-
t.Fatalf("call failed: %v", err)
149-
}
133+
result := callScript(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
134+
MustNewJobQueueCapability("jobs", stub),
135+
))
150136
if result.Kind() != KindBool || !result.Bool() {
151137
t.Fatalf("unexpected retry result: %#v", result)
152138
}
@@ -170,23 +156,16 @@ end`)
170156
}
171157

172158
func TestJobQueueCapabilityEnqueueOptionsAreClonedFromScriptState(t *testing.T) {
173-
engine := MustNewEngine(Config{})
174-
script, err := engine.Compile(`def run()
159+
script := compileScriptDefault(t, `def run()
175160
payload = { foo: "script-payload" }
176161
meta = { trace: "script-meta" }
177162
jobs.enqueue("demo", payload, meta: meta)
178163
{ payload: payload[:foo], trace: meta[:trace] }
179164
end`)
180-
if err != nil {
181-
t.Fatalf("compile failed: %v", err)
182-
}
183165

184-
result, err := script.Call(context.Background(), "run", nil, CallOptions{
185-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", mutatingInputQueue{})},
186-
})
187-
if err != nil {
188-
t.Fatalf("call failed: %v", err)
189-
}
166+
result := callScript(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
167+
MustNewJobQueueCapability("jobs", mutatingInputQueue{}),
168+
))
190169
hash := result.Hash()
191170
if hash["payload"].Kind() != KindString || hash["payload"].String() != "script-payload" {
192171
t.Fatalf("script payload was mutated by host: %#v", result)
@@ -197,23 +176,16 @@ end`)
197176
}
198177

199178
func TestJobQueueCapabilityRetryOptionsAreClonedFromScriptState(t *testing.T) {
200-
engine := MustNewEngine(Config{})
201-
script, err := engine.Compile(`def run()
179+
script := compileScriptDefault(t, `def run()
202180
arg = { attempt: { value: "script-attempt" } }
203181
kw = { value: "script-kw" }
204182
jobs.retry("job-1", arg, kw: kw)
205183
{ attempt: arg[:attempt][:value], kw: kw[:value] }
206184
end`)
207-
if err != nil {
208-
t.Fatalf("compile failed: %v", err)
209-
}
210185

211-
result, err := script.Call(context.Background(), "run", nil, CallOptions{
212-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", mutatingInputQueue{})},
213-
})
214-
if err != nil {
215-
t.Fatalf("call failed: %v", err)
216-
}
186+
result := callScript(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
187+
MustNewJobQueueCapability("jobs", mutatingInputQueue{}),
188+
))
217189
hash := result.Hash()
218190
if hash["attempt"].Kind() != KindString || hash["attempt"].String() != "script-attempt" {
219191
t.Fatalf("script retry arg was mutated by host: %#v", result)
@@ -236,23 +208,17 @@ func TestJobQueueCapabilityReturnsAreClonedFromHostState(t *testing.T) {
236208
}),
237209
}),
238210
}
239-
engine := MustNewEngine(Config{})
240-
script, err := engine.Compile(`def run()
211+
script := compileScriptDefault(t, `def run()
241212
queued = jobs.enqueue("demo", { foo: "bar" })
242213
queued[:meta][:status] = "script-enqueue"
243214
244215
retried = jobs.retry("job-1")
245216
retried[:meta][:status] = "script-retry"
246217
end`)
247-
if err != nil {
248-
t.Fatalf("compile failed: %v", err)
249-
}
250218

251-
if _, err := script.Call(context.Background(), "run", nil, CallOptions{
252-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
253-
}); err != nil {
254-
t.Fatalf("call failed: %v", err)
255-
}
219+
callScript(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
220+
MustNewJobQueueCapability("jobs", stub),
221+
))
256222

257223
enqueueStatus := stub.enqueueResult.Hash()["meta"].Hash()["status"]
258224
if enqueueStatus.Kind() != KindString || enqueueStatus.String() != "host-enqueue" {
@@ -267,66 +233,43 @@ end`)
267233

268234
func TestJobQueueCapabilityRejectsInvalidPayload(t *testing.T) {
269235
stub := &jobQueueStub{}
270-
engine := MustNewEngine(Config{})
271-
script, err := engine.Compile(`def run()
236+
script := compileScriptDefault(t, `def run()
272237
jobs.enqueue("demo", 42)
273238
end`)
274-
if err != nil {
275-
t.Fatalf("compile failed: %v", err)
276-
}
277239

278-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
279-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
280-
})
281-
if err == nil {
282-
t.Fatalf("expected error for invalid payload")
283-
}
284-
if got := err.Error(); !strings.Contains(got, "jobs.enqueue payload expected hash, got int") {
285-
t.Fatalf("unexpected error: %s", got)
286-
}
240+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
241+
MustNewJobQueueCapability("jobs", stub),
242+
))
243+
requireErrorContains(t, err, "jobs.enqueue payload expected hash, got int")
287244
}
288245

289246
func TestJobQueueCapabilityRejectsCallablePayload(t *testing.T) {
290247
stub := &jobQueueStub{}
291-
engine := MustNewEngine(Config{})
292-
script, err := engine.Compile(`def helper(value)
248+
script := compileScriptDefault(t, `def helper(value)
293249
value
294250
end
295251
296252
def run()
297253
jobs.enqueue("demo", { callback: helper })
298254
end`)
299-
if err != nil {
300-
t.Fatalf("compile failed: %v", err)
301-
}
302255

303-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
304-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
305-
})
306-
if err == nil {
307-
t.Fatalf("expected callable payload contract error")
308-
}
309-
if got := err.Error(); !strings.Contains(got, "jobs.enqueue payload must be data-only") {
310-
t.Fatalf("unexpected error: %s", got)
311-
}
256+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
257+
MustNewJobQueueCapability("jobs", stub),
258+
))
259+
requireErrorContains(t, err, "jobs.enqueue payload must be data-only")
312260
}
313261

314262
func TestNilCapabilityAdapterFiltering(t *testing.T) {
315263
stub := &jobQueueStub{}
316-
engine := MustNewEngine(Config{})
317-
script, err := engine.Compile(`def run()
264+
script := compileScriptDefault(t, `def run()
318265
jobs.enqueue("test", { foo: "bar" })
319266
end`)
320-
if err != nil {
321-
t.Fatalf("compile failed: %v", err)
322-
}
323267

324-
result, err := script.Call(context.Background(), "run", nil, CallOptions{
325-
Capabilities: []CapabilityAdapter{nil, MustNewJobQueueCapability("jobs", stub), nil},
326-
})
327-
if err != nil {
328-
t.Fatalf("call failed: %v", err)
329-
}
268+
result := callScript(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
269+
nil,
270+
MustNewJobQueueCapability("jobs", stub),
271+
nil,
272+
))
330273
if result.Kind() != KindString || result.String() != "queued" {
331274
t.Fatalf("unexpected result: %#v", result)
332275
}
@@ -337,106 +280,61 @@ end`)
337280

338281
func TestJobQueueRejectsNegativeDelay(t *testing.T) {
339282
stub := &jobQueueStub{}
340-
engine := MustNewEngine(Config{})
341-
script, err := engine.Compile(`def run()
283+
script := compileScriptDefault(t, `def run()
342284
jobs.enqueue("demo", { foo: "bar" }, delay: -5)
343285
end`)
344-
if err != nil {
345-
t.Fatalf("compile failed: %v", err)
346-
}
347286

348-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
349-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
350-
})
351-
if err == nil {
352-
t.Fatalf("expected error for negative delay")
353-
}
354-
if got := err.Error(); !strings.Contains(got, "jobs.enqueue delay must be non-negative") {
355-
t.Fatalf("unexpected error: %s", got)
356-
}
287+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
288+
MustNewJobQueueCapability("jobs", stub),
289+
))
290+
requireErrorContains(t, err, "jobs.enqueue delay must be non-negative")
357291
}
358292

359293
func TestJobQueueRejectsUnexpectedEnqueuePositionalArgs(t *testing.T) {
360294
stub := &jobQueueStub{}
361-
engine := MustNewEngine(Config{})
362-
script, err := engine.Compile(`def run()
295+
script := compileScriptDefault(t, `def run()
363296
jobs.enqueue("demo", { foo: "bar" }, { extra: true })
364297
end`)
365-
if err != nil {
366-
t.Fatalf("compile failed: %v", err)
367-
}
368298

369-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
370-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
371-
})
372-
if err == nil {
373-
t.Fatalf("expected positional arg error")
374-
}
375-
if got := err.Error(); !strings.Contains(got, "jobs.enqueue expects job name and payload") {
376-
t.Fatalf("unexpected error: %s", got)
377-
}
299+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
300+
MustNewJobQueueCapability("jobs", stub),
301+
))
302+
requireErrorContains(t, err, "jobs.enqueue expects job name and payload")
378303
}
379304

380305
func TestJobQueueRejectsEmptyKey(t *testing.T) {
381306
stub := &jobQueueStub{}
382-
engine := MustNewEngine(Config{})
383-
script, err := engine.Compile(`def run()
307+
script := compileScriptDefault(t, `def run()
384308
jobs.enqueue("demo", { foo: "bar" }, key: "")
385309
end`)
386-
if err != nil {
387-
t.Fatalf("compile failed: %v", err)
388-
}
389310

390-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
391-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
392-
})
393-
if err == nil {
394-
t.Fatalf("expected error for empty key")
395-
}
396-
if got := err.Error(); !strings.Contains(got, "jobs.enqueue key must be non-empty") {
397-
t.Fatalf("unexpected error: %s", got)
398-
}
311+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
312+
MustNewJobQueueCapability("jobs", stub),
313+
))
314+
requireErrorContains(t, err, "jobs.enqueue key must be non-empty")
399315
}
400316

401317
func TestJobQueueRejectsUnexpectedRetryPositionalArgs(t *testing.T) {
402318
stub := &jobQueueStub{}
403-
engine := MustNewEngine(Config{})
404-
script, err := engine.Compile(`def run()
319+
script := compileScriptDefault(t, `def run()
405320
jobs.retry("job-7", { attempts: 1 }, { force: true })
406321
end`)
407-
if err != nil {
408-
t.Fatalf("compile failed: %v", err)
409-
}
410322

411-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
412-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", stub)},
413-
})
414-
if err == nil {
415-
t.Fatalf("expected retry positional arg error")
416-
}
417-
if got := err.Error(); !strings.Contains(got, "jobs.retry expects job id and optional options hash") {
418-
t.Fatalf("unexpected error: %s", got)
419-
}
323+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
324+
MustNewJobQueueCapability("jobs", stub),
325+
))
326+
requireErrorContains(t, err, "jobs.retry expects job id and optional options hash")
420327
}
421328

422329
func TestJobQueueRejectsCallableReturnValue(t *testing.T) {
423-
engine := MustNewEngine(Config{})
424-
script, err := engine.Compile(`def run()
330+
script := compileScriptDefault(t, `def run()
425331
jobs.enqueue("demo", { foo: "bar" })
426332
end`)
427-
if err != nil {
428-
t.Fatalf("compile failed: %v", err)
429-
}
430333

431-
_, err = script.Call(context.Background(), "run", nil, CallOptions{
432-
Capabilities: []CapabilityAdapter{MustNewJobQueueCapability("jobs", invalidReturnQueue{})},
433-
})
434-
if err == nil {
435-
t.Fatalf("expected return contract error")
436-
}
437-
if got := err.Error(); !strings.Contains(got, "jobs.enqueue return value must be data-only") {
438-
t.Fatalf("unexpected error: %s", got)
439-
}
334+
err := callScriptErr(t, context.Background(), script, "run", nil, callOptionsWithCapabilities(
335+
MustNewJobQueueCapability("jobs", invalidReturnQueue{}),
336+
))
337+
requireErrorContains(t, err, "jobs.enqueue return value must be data-only")
440338
}
441339

442340
func TestNewJobQueueCapabilityRejectsEmptyName(t *testing.T) {

0 commit comments

Comments
 (0)