openworkflow 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +41 -346
  2. package/dist/backend.js +0 -1
  3. package/dist/bin/openworkflow.js +0 -1
  4. package/dist/client.js +0 -1
  5. package/dist/core/duration.js +0 -1
  6. package/dist/core/error.js +0 -1
  7. package/dist/core/json.js +0 -1
  8. package/dist/core/result.js +0 -1
  9. package/dist/core/retry.js +0 -1
  10. package/dist/core/schema.js +0 -1
  11. package/dist/core/step.js +0 -1
  12. package/dist/core/workflow.js +0 -1
  13. package/dist/execution.js +0 -1
  14. package/dist/index.js +0 -1
  15. package/dist/internal.d.ts +3 -5
  16. package/dist/internal.d.ts.map +1 -1
  17. package/dist/internal.js +0 -4
  18. package/dist/{pg → postgres}/backend.d.ts +3 -1
  19. package/dist/postgres/backend.d.ts.map +1 -0
  20. package/dist/{pg → postgres}/backend.js +5 -5
  21. package/dist/postgres/postgres.d.ts.map +1 -0
  22. package/dist/{backend-postgres → postgres}/postgres.js +0 -1
  23. package/dist/postgres/scripts/db-migrate.d.ts.map +1 -0
  24. package/dist/{pg → postgres}/scripts/db-migrate.js +0 -1
  25. package/dist/postgres/scripts/db-reset.d.ts.map +1 -0
  26. package/dist/{pg → postgres}/scripts/db-reset.js +0 -1
  27. package/dist/{pg → postgres}/scripts/squawk.d.ts.map +1 -1
  28. package/dist/{pg → postgres}/scripts/squawk.js +0 -1
  29. package/dist/postgres.d.ts +2 -0
  30. package/dist/postgres.d.ts.map +1 -0
  31. package/dist/postgres.js +1 -0
  32. package/dist/registry.js +0 -1
  33. package/dist/sqlite/backend.d.ts +3 -1
  34. package/dist/sqlite/backend.d.ts.map +1 -1
  35. package/dist/sqlite/backend.js +5 -5
  36. package/dist/sqlite/sqlite.js +0 -1
  37. package/dist/sqlite.d.ts +2 -0
  38. package/dist/sqlite.d.ts.map +1 -0
  39. package/dist/sqlite.js +1 -0
  40. package/dist/worker.js +0 -1
  41. package/dist/workflow.js +0 -1
  42. package/package.json +24 -6
  43. package/dist/backend-postgres/index.d.ts +0 -44
  44. package/dist/backend-postgres/index.d.ts.map +0 -1
  45. package/dist/backend-postgres/index.js +0 -535
  46. package/dist/backend-postgres/index.js.map +0 -1
  47. package/dist/backend-postgres/postgres.d.ts.map +0 -1
  48. package/dist/backend-postgres/postgres.js.map +0 -1
  49. package/dist/backend-sqlite/index.d.ts +0 -42
  50. package/dist/backend-sqlite/index.d.ts.map +0 -1
  51. package/dist/backend-sqlite/index.js +0 -655
  52. package/dist/backend-sqlite/index.js.map +0 -1
  53. package/dist/backend-sqlite/sqlite.d.ts +0 -61
  54. package/dist/backend-sqlite/sqlite.d.ts.map +0 -1
  55. package/dist/backend-sqlite/sqlite.js +0 -247
  56. package/dist/backend-sqlite/sqlite.js.map +0 -1
  57. package/dist/backend.js.map +0 -1
  58. package/dist/bin/openworkflow.js.map +0 -1
  59. package/dist/client.js.map +0 -1
  60. package/dist/config.d.ts +0 -34
  61. package/dist/config.d.ts.map +0 -1
  62. package/dist/config.js +0 -49
  63. package/dist/config.js.map +0 -1
  64. package/dist/core/duration.js.map +0 -1
  65. package/dist/core/error.js.map +0 -1
  66. package/dist/core/json.js.map +0 -1
  67. package/dist/core/result.js.map +0 -1
  68. package/dist/core/retry.js.map +0 -1
  69. package/dist/core/schema.js.map +0 -1
  70. package/dist/core/step.js.map +0 -1
  71. package/dist/core/workflow.js.map +0 -1
  72. package/dist/execution.js.map +0 -1
  73. package/dist/index.js.map +0 -1
  74. package/dist/internal.js.map +0 -1
  75. package/dist/pg/backend.d.ts.map +0 -1
  76. package/dist/pg/backend.js.map +0 -1
  77. package/dist/pg/index.d.ts +0 -3
  78. package/dist/pg/index.d.ts.map +0 -1
  79. package/dist/pg/index.js +0 -3
  80. package/dist/pg/index.js.map +0 -1
  81. package/dist/pg/postgres.d.ts +0 -42
  82. package/dist/pg/postgres.d.ts.map +0 -1
  83. package/dist/pg/postgres.js +0 -234
  84. package/dist/pg/postgres.js.map +0 -1
  85. package/dist/pg/scripts/db-migrate.d.ts.map +0 -1
  86. package/dist/pg/scripts/db-migrate.js.map +0 -1
  87. package/dist/pg/scripts/db-reset.d.ts.map +0 -1
  88. package/dist/pg/scripts/db-reset.js.map +0 -1
  89. package/dist/pg/scripts/squawk.js.map +0 -1
  90. package/dist/pg/vitest.global-setup.d.ts +0 -3
  91. package/dist/pg/vitest.global-setup.d.ts.map +0 -1
  92. package/dist/pg/vitest.global-setup.js +0 -8
  93. package/dist/pg/vitest.global-setup.js.map +0 -1
  94. package/dist/registry.js.map +0 -1
  95. package/dist/sqlite/backend.js.map +0 -1
  96. package/dist/sqlite/index.d.ts +0 -3
  97. package/dist/sqlite/index.d.ts.map +0 -1
  98. package/dist/sqlite/index.js +0 -3
  99. package/dist/sqlite/index.js.map +0 -1
  100. package/dist/sqlite/sqlite.js.map +0 -1
  101. package/dist/testing/backend.testsuite.d.ts +0 -20
  102. package/dist/testing/backend.testsuite.d.ts.map +0 -1
  103. package/dist/testing/backend.testsuite.js +0 -1091
  104. package/dist/testing/backend.testsuite.js.map +0 -1
  105. package/dist/testing/index.d.ts +0 -2
  106. package/dist/testing/index.d.ts.map +0 -1
  107. package/dist/testing/index.js +0 -2
  108. package/dist/testing/index.js.map +0 -1
  109. package/dist/tsconfig.tsbuildinfo +0 -1
  110. package/dist/worker.js.map +0 -1
  111. package/dist/workflow.js.map +0 -1
  112. /package/dist/{backend-postgres → postgres}/postgres.d.ts +0 -0
  113. /package/dist/{pg → postgres}/scripts/db-migrate.d.ts +0 -0
  114. /package/dist/{pg → postgres}/scripts/db-reset.d.ts +0 -0
  115. /package/dist/{pg → postgres}/scripts/squawk.d.ts +0 -0
@@ -1,1091 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { afterAll, beforeAll, describe, expect, test } from "vitest";
3
- /**
4
- * Runs the Backend test suite.
5
- * @param options - Test suite options
6
- */
7
- export function testBackend(options) {
8
- const { setup, teardown } = options;
9
- describe("Backend", () => {
10
- let backend;
11
- beforeAll(async () => {
12
- backend = await setup();
13
- });
14
- afterAll(async () => {
15
- await teardown(backend);
16
- });
17
- describe("createWorkflowRun()", () => {
18
- test("creates a workflow run", async () => {
19
- const expected = {
20
- namespaceId: "", // -
21
- id: "", // -
22
- workflowName: randomUUID(),
23
- version: randomUUID(),
24
- status: "pending",
25
- idempotencyKey: randomUUID(),
26
- config: { key: "val" },
27
- context: { key: "val" },
28
- input: { key: "val" },
29
- output: null,
30
- error: null,
31
- attempts: 0,
32
- parentStepAttemptNamespaceId: null,
33
- parentStepAttemptId: null,
34
- workerId: null,
35
- availableAt: newDateInOneYear(), // -
36
- deadlineAt: newDateInOneYear(),
37
- startedAt: null,
38
- finishedAt: null,
39
- createdAt: new Date(), // -
40
- updatedAt: new Date(), // -
41
- };
42
- // Create with all fields
43
- const created = await backend.createWorkflowRun({
44
- workflowName: expected.workflowName,
45
- version: expected.version,
46
- idempotencyKey: expected.idempotencyKey,
47
- input: expected.input,
48
- config: expected.config,
49
- context: expected.context,
50
- availableAt: expected.availableAt,
51
- deadlineAt: expected.deadlineAt,
52
- });
53
- expect(created.namespaceId).toHaveLength(36);
54
- expect(created.id).toHaveLength(36);
55
- expect(deltaSeconds(created.availableAt)).toBeGreaterThan(1);
56
- expect(deltaSeconds(created.createdAt)).toBeLessThan(1);
57
- expect(deltaSeconds(created.updatedAt)).toBeLessThan(1);
58
- expected.namespaceId = created.namespaceId;
59
- expected.id = created.id;
60
- expected.availableAt = created.availableAt;
61
- expected.createdAt = created.createdAt;
62
- expected.updatedAt = created.updatedAt;
63
- expect(created).toEqual(expected);
64
- // Create with minimal fields
65
- const createdMin = await backend.createWorkflowRun({
66
- workflowName: expected.workflowName,
67
- version: null,
68
- idempotencyKey: null,
69
- input: null,
70
- config: {},
71
- context: null,
72
- availableAt: null,
73
- deadlineAt: null,
74
- });
75
- expect(createdMin.version).toBeNull();
76
- expect(createdMin.idempotencyKey).toBeNull();
77
- expect(createdMin.input).toBeNull();
78
- expect(createdMin.context).toBeNull();
79
- expect(deltaSeconds(createdMin.availableAt)).toBeLessThan(1); // defaults to NOW()
80
- expect(createdMin.deadlineAt).toBeNull();
81
- });
82
- });
83
- describe("listWorkflowRuns()", () => {
84
- test("lists workflow runs ordered by creation time", async () => {
85
- const backend = await setup();
86
- const first = await createPendingWorkflowRun(backend);
87
- await sleep(10); // ensure timestamp difference
88
- const second = await createPendingWorkflowRun(backend);
89
- const listed = await backend.listWorkflowRuns({});
90
- const listedIds = listed.data.map((run) => run.id);
91
- expect(listedIds).toEqual([first.id, second.id]);
92
- await teardown(backend);
93
- });
94
- test("paginates workflow runs", async () => {
95
- const backend = await setup();
96
- const runs = [];
97
- for (let i = 0; i < 5; i++) {
98
- runs.push(await createPendingWorkflowRun(backend));
99
- await sleep(10);
100
- }
101
- // p1
102
- const page1 = await backend.listWorkflowRuns({ limit: 2 });
103
- expect(page1.data).toHaveLength(2);
104
- expect(page1.data[0]?.id).toBe(runs[0]?.id);
105
- expect(page1.data[1]?.id).toBe(runs[1]?.id);
106
- expect(page1.pagination.next).not.toBeNull();
107
- expect(page1.pagination.prev).toBeNull();
108
- // p2
109
- const page2 = await backend.listWorkflowRuns({
110
- limit: 2,
111
- after: page1.pagination.next, // eslint-disable-line @typescript-eslint/no-non-null-assertion
112
- });
113
- expect(page2.data).toHaveLength(2);
114
- expect(page2.data[0]?.id).toBe(runs[2]?.id);
115
- expect(page2.data[1]?.id).toBe(runs[3]?.id);
116
- expect(page2.pagination.next).not.toBeNull();
117
- expect(page2.pagination.prev).not.toBeNull();
118
- // p3
119
- const page3 = await backend.listWorkflowRuns({
120
- limit: 2,
121
- after: page2.pagination.next, // eslint-disable-line @typescript-eslint/no-non-null-assertion
122
- });
123
- expect(page3.data).toHaveLength(1);
124
- expect(page3.data[0]?.id).toBe(runs[4]?.id);
125
- expect(page3.pagination.next).toBeNull();
126
- expect(page3.pagination.prev).not.toBeNull();
127
- // p2 again
128
- const page2Back = await backend.listWorkflowRuns({
129
- limit: 2,
130
- before: page3.pagination.prev, // eslint-disable-line @typescript-eslint/no-non-null-assertion
131
- });
132
- expect(page2Back.data).toHaveLength(2);
133
- expect(page2Back.data[0]?.id).toBe(runs[2]?.id);
134
- expect(page2Back.data[1]?.id).toBe(runs[3]?.id);
135
- expect(page2Back.pagination.next).toEqual(page2.pagination.next);
136
- expect(page2Back.pagination.prev).toEqual(page2.pagination.prev);
137
- await teardown(backend);
138
- });
139
- test("handles empty results", async () => {
140
- const backend = await setup();
141
- const listed = await backend.listWorkflowRuns({});
142
- expect(listed.data).toHaveLength(0);
143
- expect(listed.pagination.next).toBeNull();
144
- expect(listed.pagination.prev).toBeNull();
145
- await teardown(backend);
146
- });
147
- test("paginates correctly with id as tiebreaker when multiple items have the same created_at timestamp", async () => {
148
- const backend = await setup();
149
- const runs = [];
150
- for (let i = 0; i < 5; i++) {
151
- runs.push(await createPendingWorkflowRun(backend));
152
- }
153
- runs.sort((a, b) => {
154
- const timeDiff = a.createdAt.getTime() - b.createdAt.getTime();
155
- if (timeDiff !== 0)
156
- return timeDiff;
157
- return a.id.localeCompare(b.id);
158
- });
159
- const page1 = await backend.listWorkflowRuns({ limit: 2 });
160
- expect(page1.data).toHaveLength(2);
161
- expect(page1.data[0]?.id).toBe(runs[0]?.id);
162
- expect(page1.data[1]?.id).toBe(runs[1]?.id);
163
- expect(page1.pagination.next).not.toBeNull();
164
- const page2 = await backend.listWorkflowRuns({
165
- limit: 2,
166
- after: page1.pagination.next, // eslint-disable-line @typescript-eslint/no-non-null-assertion
167
- });
168
- expect(page2.data).toHaveLength(2);
169
- expect(page2.data[0]?.id).toBe(runs[2]?.id);
170
- expect(page2.data[1]?.id).toBe(runs[3]?.id);
171
- expect(page2.pagination.next).not.toBeNull();
172
- const page3 = await backend.listWorkflowRuns({
173
- limit: 2,
174
- after: page2.pagination.next, // eslint-disable-line @typescript-eslint/no-non-null-assertion
175
- });
176
- expect(page3.data).toHaveLength(1);
177
- expect(page3.data[0]?.id).toBe(runs[4]?.id);
178
- expect(page3.pagination.next).toBeNull();
179
- const page2Back = await backend.listWorkflowRuns({
180
- limit: 2,
181
- before: page3.pagination.prev, // eslint-disable-line @typescript-eslint/no-non-null-assertion
182
- });
183
- expect(page2Back.data).toHaveLength(2);
184
- expect(page2Back.data[0]?.id).toBe(runs[2]?.id);
185
- expect(page2Back.data[1]?.id).toBe(runs[3]?.id);
186
- await teardown(backend);
187
- });
188
- });
189
- describe("claimWorkflowRun()", () => {
190
- // because claims involve timing and leases, we create and teardown a new
191
- // namespaced backend instance for each test
192
- test("claims workflow runs and respects leases, reclaiming if lease expires", async () => {
193
- const backend = await setup();
194
- await createPendingWorkflowRun(backend);
195
- const firstLeaseMs = 30;
196
- const firstWorker = randomUUID();
197
- const claimed = await backend.claimWorkflowRun({
198
- workerId: firstWorker,
199
- leaseDurationMs: firstLeaseMs,
200
- });
201
- expect(claimed?.status).toBe("running");
202
- expect(claimed?.workerId).toBe(firstWorker);
203
- expect(claimed?.attempts).toBe(1);
204
- expect(claimed?.startedAt).not.toBeNull();
205
- const secondWorker = randomUUID();
206
- const blocked = await backend.claimWorkflowRun({
207
- workerId: secondWorker,
208
- leaseDurationMs: 10,
209
- });
210
- expect(blocked).toBeNull();
211
- await sleep(firstLeaseMs + 5); // small buffer for timing variability
212
- const reclaimed = await backend.claimWorkflowRun({
213
- workerId: secondWorker,
214
- leaseDurationMs: 10,
215
- });
216
- expect(reclaimed?.id).toBe(claimed?.id);
217
- expect(reclaimed?.attempts).toBe(2);
218
- expect(reclaimed?.workerId).toBe(secondWorker);
219
- expect(reclaimed?.startedAt?.getTime()).toBe(claimed?.startedAt?.getTime());
220
- await teardown(backend);
221
- });
222
- test("prioritizes pending workflow runs over expired running ones", async () => {
223
- const backend = await setup();
224
- const running = await createPendingWorkflowRun(backend);
225
- const runningClaim = await backend.claimWorkflowRun({
226
- workerId: "worker-running",
227
- leaseDurationMs: 5,
228
- });
229
- if (!runningClaim)
230
- throw new Error("expected claim");
231
- expect(runningClaim.id).toBe(running.id);
232
- await sleep(10); // wait for running's lease to expire
233
- // pending claimed first, even though running expired
234
- const pending = await createPendingWorkflowRun(backend);
235
- const claimedFirst = await backend.claimWorkflowRun({
236
- workerId: "worker-second",
237
- leaseDurationMs: 100,
238
- });
239
- expect(claimedFirst?.id).toBe(pending.id);
240
- // running claimed second
241
- const claimedSecond = await backend.claimWorkflowRun({
242
- workerId: "worker-third",
243
- leaseDurationMs: 100,
244
- });
245
- expect(claimedSecond?.id).toBe(running.id);
246
- await teardown(backend);
247
- });
248
- test("returns null when no workflow runs are available", async () => {
249
- const backend = await setup();
250
- const claimed = await backend.claimWorkflowRun({
251
- workerId: randomUUID(),
252
- leaseDurationMs: 10,
253
- });
254
- expect(claimed).toBeNull();
255
- await teardown(backend);
256
- });
257
- });
258
- describe("extendWorkflowRunLease()", () => {
259
- test("extends the lease for running workflow runs", async () => {
260
- const workerId = randomUUID();
261
- await createPendingWorkflowRun(backend);
262
- const claimed = await backend.claimWorkflowRun({
263
- workerId,
264
- leaseDurationMs: 20,
265
- });
266
- if (!claimed)
267
- throw new Error("Expected workflow run to be claimed"); // for type narrowing
268
- const previousExpiry = claimed.availableAt;
269
- const extended = await backend.extendWorkflowRunLease({
270
- workflowRunId: claimed.id,
271
- workerId,
272
- leaseDurationMs: 200,
273
- });
274
- expect(extended.availableAt?.getTime()).toBeGreaterThan(previousExpiry?.getTime() ?? Infinity);
275
- });
276
- });
277
- describe("sleepWorkflowRun()", () => {
278
- test("sets a running workflow to sleeping status until a future time", async () => {
279
- const workerId = randomUUID();
280
- await createPendingWorkflowRun(backend);
281
- const claimed = await backend.claimWorkflowRun({
282
- workerId,
283
- leaseDurationMs: 100,
284
- });
285
- if (!claimed)
286
- throw new Error("Expected workflow run to be claimed");
287
- const sleepUntil = new Date(Date.now() + 5000); // 5 seconds from now
288
- await backend.sleepWorkflowRun({
289
- workflowRunId: claimed.id,
290
- workerId,
291
- availableAt: sleepUntil,
292
- });
293
- const fetched = await backend.getWorkflowRun({
294
- workflowRunId: claimed.id,
295
- });
296
- expect(fetched).not.toBeNull();
297
- expect(fetched?.availableAt?.getTime()).toBe(sleepUntil.getTime());
298
- expect(fetched?.workerId).toBeNull();
299
- expect(fetched?.status).toBe("sleeping");
300
- });
301
- test("fails when trying to sleep a canceled workflow", async () => {
302
- const backend = await setup();
303
- // completed run
304
- let claimed = await createClaimedWorkflowRun(backend);
305
- await backend.completeWorkflowRun({
306
- workflowRunId: claimed.id,
307
- workerId: claimed.workerId ?? "",
308
- output: null,
309
- });
310
- await expect(backend.sleepWorkflowRun({
311
- workflowRunId: claimed.id,
312
- workerId: claimed.workerId ?? "",
313
- availableAt: new Date(Date.now() + 60_000),
314
- })).rejects.toThrow("Failed to sleep workflow run");
315
- // failed run
316
- claimed = await createClaimedWorkflowRun(backend);
317
- await backend.failWorkflowRun({
318
- workflowRunId: claimed.id,
319
- workerId: claimed.workerId ?? "",
320
- error: { message: "failed" },
321
- });
322
- await expect(backend.sleepWorkflowRun({
323
- workflowRunId: claimed.id,
324
- workerId: claimed.workerId ?? "",
325
- availableAt: new Date(Date.now() + 60_000),
326
- })).rejects.toThrow("Failed to sleep workflow run");
327
- // canceled run
328
- claimed = await createClaimedWorkflowRun(backend);
329
- await backend.cancelWorkflowRun({
330
- workflowRunId: claimed.id,
331
- });
332
- await expect(backend.sleepWorkflowRun({
333
- workflowRunId: claimed.id,
334
- workerId: claimed.workerId ?? "",
335
- availableAt: new Date(Date.now() + 60_000),
336
- })).rejects.toThrow("Failed to sleep workflow run");
337
- await teardown(backend);
338
- });
339
- });
340
- describe("completeWorkflowRun()", () => {
341
- test("marks running workflow runs as completed", async () => {
342
- const workerId = randomUUID();
343
- await createPendingWorkflowRun(backend);
344
- const claimed = await backend.claimWorkflowRun({
345
- workerId,
346
- leaseDurationMs: 20,
347
- });
348
- if (!claimed)
349
- throw new Error("Expected workflow run to be claimed"); // for type narrowing
350
- const output = { ok: true };
351
- const completed = await backend.completeWorkflowRun({
352
- workflowRunId: claimed.id,
353
- workerId,
354
- output,
355
- });
356
- expect(completed.status).toBe("completed");
357
- expect(completed.output).toEqual(output);
358
- expect(completed.error).toBeNull();
359
- expect(completed.finishedAt).not.toBeNull();
360
- expect(completed.availableAt).toBeNull();
361
- });
362
- });
363
- describe("failWorkflowRun()", () => {
364
- test("reschedules workflow runs with exponential backoff on first failure", async () => {
365
- const workerId = randomUUID();
366
- await createPendingWorkflowRun(backend);
367
- const claimed = await backend.claimWorkflowRun({
368
- workerId,
369
- leaseDurationMs: 20,
370
- });
371
- if (!claimed)
372
- throw new Error("Expected workflow run to be claimed");
373
- const beforeFailTime = Date.now();
374
- const error = { message: "boom" };
375
- const failed = await backend.failWorkflowRun({
376
- workflowRunId: claimed.id,
377
- workerId,
378
- error,
379
- });
380
- // rescheduled, not permanently failed
381
- expect(failed.status).toBe("pending");
382
- expect(failed.error).toEqual(error);
383
- expect(failed.output).toBeNull();
384
- expect(failed.finishedAt).toBeNull();
385
- expect(failed.workerId).toBeNull();
386
- expect(failed.startedAt).toBeNull(); // cleared on failure for retry
387
- expect(failed.availableAt).not.toBeNull();
388
- if (!failed.availableAt)
389
- throw new Error("Expected availableAt");
390
- const delayMs = failed.availableAt.getTime() - beforeFailTime;
391
- expect(delayMs).toBeGreaterThanOrEqual(900); // ~1s with some tolerance
392
- expect(delayMs).toBeLessThan(1500);
393
- });
394
- test("reschedules with increasing backoff on multiple failures", async () => {
395
- // this test needs isolated namespace
396
- const backend = await setup();
397
- await createPendingWorkflowRun(backend);
398
- // fail first attempt
399
- let workerId = randomUUID();
400
- let claimed = await backend.claimWorkflowRun({
401
- workerId,
402
- leaseDurationMs: 20,
403
- });
404
- if (!claimed)
405
- throw new Error("Expected workflow run to be claimed");
406
- expect(claimed.attempts).toBe(1);
407
- const firstFailed = await backend.failWorkflowRun({
408
- workflowRunId: claimed.id,
409
- workerId,
410
- error: { message: "first failure" },
411
- });
412
- expect(firstFailed.status).toBe("pending");
413
- await sleep(1100); // wait for first backoff (~1s)
414
- // fail second attempt
415
- workerId = randomUUID();
416
- claimed = await backend.claimWorkflowRun({
417
- workerId,
418
- leaseDurationMs: 20,
419
- });
420
- if (!claimed)
421
- throw new Error("Expected workflow run to be claimed");
422
- expect(claimed.attempts).toBe(2);
423
- const beforeSecondFail = Date.now();
424
- const secondFailed = await backend.failWorkflowRun({
425
- workflowRunId: claimed.id,
426
- workerId,
427
- error: { message: "second failure" },
428
- });
429
- expect(secondFailed.status).toBe("pending");
430
- // second attempt should have ~2s backoff (1s * 2^1)
431
- if (!secondFailed.availableAt)
432
- throw new Error("Expected availableAt");
433
- const delayMs = secondFailed.availableAt.getTime() - beforeSecondFail;
434
- expect(delayMs).toBeGreaterThanOrEqual(1900); // ~2s with some tolerance
435
- expect(delayMs).toBeLessThan(2500);
436
- await teardown(backend);
437
- });
438
- });
439
- describe("createStepAttempt()", () => {
440
- test("creates a step attempt", async () => {
441
- const workflowRun = await createClaimedWorkflowRun(backend);
442
- const expected = {
443
- namespaceId: workflowRun.namespaceId,
444
- id: "", // -
445
- workflowRunId: workflowRun.id,
446
- stepName: randomUUID(),
447
- kind: "function",
448
- status: "running",
449
- config: { key: "val" },
450
- context: null,
451
- output: null,
452
- error: null,
453
- childWorkflowRunNamespaceId: null,
454
- childWorkflowRunId: null,
455
- startedAt: null,
456
- finishedAt: null,
457
- createdAt: new Date(), // -
458
- updatedAt: new Date(), // -
459
- };
460
- const created = await backend.createStepAttempt({
461
- workflowRunId: expected.workflowRunId,
462
- workerId: workflowRun.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
463
- stepName: expected.stepName,
464
- kind: expected.kind,
465
- config: expected.config,
466
- context: expected.context,
467
- });
468
- expect(created.id).toHaveLength(36);
469
- expect(deltaSeconds(created.startedAt)).toBeLessThan(1);
470
- expect(deltaSeconds(created.createdAt)).toBeLessThan(1);
471
- expect(deltaSeconds(created.updatedAt)).toBeLessThan(1);
472
- expected.id = created.id;
473
- expected.startedAt = created.startedAt;
474
- expected.createdAt = created.createdAt;
475
- expected.updatedAt = created.updatedAt;
476
- expect(created).toEqual(expected);
477
- });
478
- });
479
- describe("getStepAttempt()", () => {
480
- test("returns a persisted step attempt", async () => {
481
- const claimed = await createClaimedWorkflowRun(backend);
482
- const created = await backend.createStepAttempt({
483
- workflowRunId: claimed.id,
484
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
485
- stepName: randomUUID(),
486
- kind: "function",
487
- config: {},
488
- context: null,
489
- });
490
- const got = await backend.getStepAttempt({
491
- stepAttemptId: created.id,
492
- });
493
- expect(got).toEqual(created);
494
- });
495
- });
496
- describe("listStepAttempts()", () => {
497
- test("lists step attempts ordered by creation time", async () => {
498
- const claimed = await createClaimedWorkflowRun(backend);
499
- const first = await backend.createStepAttempt({
500
- workflowRunId: claimed.id,
501
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
502
- stepName: randomUUID(),
503
- kind: "function",
504
- config: {},
505
- context: null,
506
- });
507
- await backend.completeStepAttempt({
508
- workflowRunId: claimed.id,
509
- stepAttemptId: first.id,
510
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion,
511
- output: { ok: true },
512
- });
513
- await sleep(10); // ensure timestamp difference
514
- const second = await backend.createStepAttempt({
515
- workflowRunId: claimed.id,
516
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
517
- stepName: randomUUID(),
518
- kind: "function",
519
- config: {},
520
- context: null,
521
- });
522
- const listed = await backend.listStepAttempts({
523
- workflowRunId: claimed.id,
524
- });
525
- const listedStepNames = listed.data.map((step) => step.stepName);
526
- expect(listedStepNames).toEqual([first.stepName, second.stepName]);
527
- });
528
- test("paginates step attempts", async () => {
529
- const claimed = await createClaimedWorkflowRun(backend);
530
- for (let i = 0; i < 5; i++) {
531
- await backend.createStepAttempt({
532
- workflowRunId: claimed.id,
533
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
534
- stepName: `step-${String(i)}`,
535
- kind: "function",
536
- config: {},
537
- context: null,
538
- });
539
- await sleep(10); // ensure createdAt differs
540
- }
541
- // p1
542
- const page1 = await backend.listStepAttempts({
543
- workflowRunId: claimed.id,
544
- limit: 2,
545
- });
546
- expect(page1.data).toHaveLength(2);
547
- expect(page1.data[0]?.stepName).toBe("step-0");
548
- expect(page1.data[1]?.stepName).toBe("step-1");
549
- expect(page1.pagination.next).not.toBeNull();
550
- expect(page1.pagination.prev).toBeNull();
551
- // p2
552
- const page2 = await backend.listStepAttempts({
553
- workflowRunId: claimed.id,
554
- limit: 2,
555
- after: page1.pagination.next, // eslint-disable-line @typescript-eslint/no-non-null-assertion
556
- });
557
- expect(page2.data).toHaveLength(2);
558
- expect(page2.data[0]?.stepName).toBe("step-2");
559
- expect(page2.data[1]?.stepName).toBe("step-3");
560
- expect(page2.pagination.next).not.toBeNull();
561
- expect(page2.pagination.prev).not.toBeNull();
562
- // p3
563
- const page3 = await backend.listStepAttempts({
564
- workflowRunId: claimed.id,
565
- limit: 2,
566
- after: page2.pagination.next, // eslint-disable-line @typescript-eslint/no-non-null-assertion
567
- });
568
- expect(page3.data).toHaveLength(1);
569
- expect(page3.data[0]?.stepName).toBe("step-4");
570
- expect(page3.pagination.next).toBeNull();
571
- expect(page3.pagination.prev).not.toBeNull();
572
- // p2 again
573
- const page2Back = await backend.listStepAttempts({
574
- workflowRunId: claimed.id,
575
- limit: 2,
576
- before: page3.pagination.prev, // eslint-disable-line @typescript-eslint/no-non-null-assertion
577
- });
578
- expect(page2Back.data).toHaveLength(2);
579
- expect(page2Back.data[0]?.stepName).toBe("step-2");
580
- expect(page2Back.data[1]?.stepName).toBe("step-3");
581
- expect(page2Back.pagination.next).toEqual(page2.pagination.next);
582
- expect(page2Back.pagination.prev).toEqual(page2.pagination.prev);
583
- });
584
- test("handles empty results", async () => {
585
- const claimed = await createClaimedWorkflowRun(backend);
586
- const listed = await backend.listStepAttempts({
587
- workflowRunId: claimed.id,
588
- });
589
- expect(listed.data).toHaveLength(0);
590
- expect(listed.pagination.next).toBeNull();
591
- expect(listed.pagination.prev).toBeNull();
592
- });
593
- test("handles exact limit match", async () => {
594
- const claimed = await createClaimedWorkflowRun(backend);
595
- await backend.createStepAttempt({
596
- workflowRunId: claimed.id,
597
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
598
- stepName: "step-1",
599
- kind: "function",
600
- config: {},
601
- context: null,
602
- });
603
- const listed = await backend.listStepAttempts({
604
- workflowRunId: claimed.id,
605
- limit: 1,
606
- });
607
- expect(listed.data).toHaveLength(1);
608
- expect(listed.pagination.next).toBeNull();
609
- expect(listed.pagination.prev).toBeNull();
610
- });
611
- });
612
- describe("completeStepAttempt()", () => {
613
- test("marks running step attempts as completed", async () => {
614
- const claimed = await createClaimedWorkflowRun(backend);
615
- const created = await backend.createStepAttempt({
616
- workflowRunId: claimed.id,
617
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
618
- stepName: randomUUID(),
619
- kind: "function",
620
- config: {},
621
- context: null,
622
- });
623
- const output = { foo: "bar" };
624
- const completed = await backend.completeStepAttempt({
625
- workflowRunId: claimed.id,
626
- stepAttemptId: created.id,
627
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
628
- output,
629
- });
630
- expect(completed.status).toBe("completed");
631
- expect(completed.output).toEqual(output);
632
- expect(completed.error).toBeNull();
633
- expect(completed.finishedAt).not.toBeNull();
634
- const fetched = await backend.getStepAttempt({
635
- stepAttemptId: created.id,
636
- });
637
- expect(fetched?.status).toBe("completed");
638
- expect(fetched?.output).toEqual(output);
639
- expect(fetched?.error).toBeNull();
640
- expect(fetched?.finishedAt).not.toBeNull();
641
- });
642
- test("throws when workflow is not running", async () => {
643
- const backend = await setup();
644
- await createPendingWorkflowRun(backend);
645
- // create a step attempt by first claiming the workflow
646
- const claimed = await backend.claimWorkflowRun({
647
- workerId: randomUUID(),
648
- leaseDurationMs: 100,
649
- });
650
- if (!claimed)
651
- throw new Error("Failed to claim workflow run");
652
- const stepAttempt = await backend.createStepAttempt({
653
- workflowRunId: claimed.id,
654
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
655
- stepName: randomUUID(),
656
- kind: "function",
657
- config: {},
658
- context: null,
659
- });
660
- // complete the workflow so it's no longer running
661
- await backend.completeWorkflowRun({
662
- workflowRunId: claimed.id,
663
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
664
- output: null,
665
- });
666
- // try to complete the step attempt
667
- await expect(backend.completeStepAttempt({
668
- workflowRunId: claimed.id,
669
- stepAttemptId: stepAttempt.id,
670
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
671
- output: { foo: "bar" },
672
- })).rejects.toThrow("Failed to mark step attempt completed");
673
- await teardown(backend);
674
- });
675
- test("throws when step attempt does not exist", async () => {
676
- const backend = await setup();
677
- const claimed = await createClaimedWorkflowRun(backend);
678
- await expect(backend.completeStepAttempt({
679
- workflowRunId: claimed.id,
680
- stepAttemptId: randomUUID(),
681
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
682
- output: { foo: "bar" },
683
- })).rejects.toThrow("Failed to mark step attempt completed");
684
- await teardown(backend);
685
- });
686
- });
687
- describe("failStepAttempt()", () => {
688
- test("marks running step attempts as failed", async () => {
689
- const claimed = await createClaimedWorkflowRun(backend);
690
- const created = await backend.createStepAttempt({
691
- workflowRunId: claimed.id,
692
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
693
- stepName: randomUUID(),
694
- kind: "function",
695
- config: {},
696
- context: null,
697
- });
698
- const error = { message: "nope" };
699
- const failed = await backend.failStepAttempt({
700
- workflowRunId: claimed.id,
701
- stepAttemptId: created.id,
702
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
703
- error,
704
- });
705
- expect(failed.status).toBe("failed");
706
- expect(failed.error).toEqual(error);
707
- expect(failed.output).toBeNull();
708
- expect(failed.finishedAt).not.toBeNull();
709
- const fetched = await backend.getStepAttempt({
710
- stepAttemptId: created.id,
711
- });
712
- expect(fetched?.status).toBe("failed");
713
- expect(fetched?.error).toEqual(error);
714
- expect(fetched?.output).toBeNull();
715
- expect(fetched?.finishedAt).not.toBeNull();
716
- });
717
- test("throws when workflow is not running", async () => {
718
- const backend = await setup();
719
- await createPendingWorkflowRun(backend);
720
- // create a step attempt by first claiming the workflow
721
- const claimed = await backend.claimWorkflowRun({
722
- workerId: randomUUID(),
723
- leaseDurationMs: 100,
724
- });
725
- if (!claimed)
726
- throw new Error("Failed to claim workflow run");
727
- const stepAttempt = await backend.createStepAttempt({
728
- workflowRunId: claimed.id,
729
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
730
- stepName: randomUUID(),
731
- kind: "function",
732
- config: {},
733
- context: null,
734
- });
735
- // complete the workflow so it's no longer running
736
- await backend.completeWorkflowRun({
737
- workflowRunId: claimed.id,
738
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
739
- output: null,
740
- });
741
- // try to fail the step attempt
742
- await expect(backend.failStepAttempt({
743
- workflowRunId: claimed.id,
744
- stepAttemptId: stepAttempt.id,
745
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
746
- error: { message: "nope" },
747
- })).rejects.toThrow("Failed to mark step attempt failed");
748
- await teardown(backend);
749
- });
750
- test("throws when step attempt does not exist", async () => {
751
- const backend = await setup();
752
- const claimed = await createClaimedWorkflowRun(backend);
753
- await expect(backend.failStepAttempt({
754
- workflowRunId: claimed.id,
755
- stepAttemptId: randomUUID(),
756
- workerId: claimed.workerId, // eslint-disable-line @typescript-eslint/no-non-null-assertion
757
- error: { message: "nope" },
758
- })).rejects.toThrow("Failed to mark step attempt failed");
759
- await teardown(backend);
760
- });
761
- });
762
- describe("deadline_at", () => {
763
- test("creates a workflow run with a deadline", async () => {
764
- const deadline = new Date(Date.now() + 60_000); // in 1 minute
765
- const created = await backend.createWorkflowRun({
766
- workflowName: randomUUID(),
767
- version: null,
768
- idempotencyKey: null,
769
- input: null,
770
- config: {},
771
- context: null,
772
- availableAt: null,
773
- deadlineAt: deadline,
774
- });
775
- expect(created.deadlineAt).not.toBeNull();
776
- expect(created.deadlineAt?.getTime()).toBe(deadline.getTime());
777
- });
778
- test("does not claim workflow runs past their deadline", async () => {
779
- const backend = await setup();
780
- const pastDeadline = new Date(Date.now() - 1000);
781
- await backend.createWorkflowRun({
782
- workflowName: randomUUID(),
783
- version: null,
784
- idempotencyKey: null,
785
- input: null,
786
- config: {},
787
- context: null,
788
- availableAt: null,
789
- deadlineAt: pastDeadline,
790
- });
791
- const claimed = await backend.claimWorkflowRun({
792
- workerId: randomUUID(),
793
- leaseDurationMs: 1000,
794
- });
795
- expect(claimed).toBeNull();
796
- await teardown(backend);
797
- });
798
- test("marks deadline-expired workflow runs as failed when claiming", async () => {
799
- const backend = await setup();
800
- const pastDeadline = new Date(Date.now() - 1000);
801
- const created = await backend.createWorkflowRun({
802
- workflowName: randomUUID(),
803
- version: null,
804
- idempotencyKey: null,
805
- input: null,
806
- config: {},
807
- context: null,
808
- availableAt: null,
809
- deadlineAt: pastDeadline,
810
- });
811
- // attempt to claim triggers deadline check
812
- const claimed = await backend.claimWorkflowRun({
813
- workerId: randomUUID(),
814
- leaseDurationMs: 1000,
815
- });
816
- expect(claimed).toBeNull();
817
- // verify it was marked as failed
818
- const failed = await backend.getWorkflowRun({
819
- workflowRunId: created.id,
820
- });
821
- expect(failed?.status).toBe("failed");
822
- expect(failed?.error).toEqual({
823
- message: "Workflow run deadline exceeded",
824
- });
825
- expect(failed?.finishedAt).not.toBeNull();
826
- expect(failed?.availableAt).toBeNull();
827
- await teardown(backend);
828
- });
829
- test("does not reschedule failed workflow runs if next retry would exceed deadline", async () => {
830
- const backend = await setup();
831
- const deadline = new Date(Date.now() + 500); // 500ms from now
832
- const created = await backend.createWorkflowRun({
833
- workflowName: randomUUID(),
834
- version: null,
835
- idempotencyKey: null,
836
- input: null,
837
- config: {},
838
- context: null,
839
- availableAt: null,
840
- deadlineAt: deadline,
841
- });
842
- const workerId = randomUUID();
843
- const claimed = await backend.claimWorkflowRun({
844
- workerId,
845
- leaseDurationMs: 100,
846
- });
847
- expect(claimed).not.toBeNull();
848
- // should mark as permanently failed since retry backoff (1s) would exceed deadline (500ms)
849
- const failed = await backend.failWorkflowRun({
850
- workflowRunId: created.id,
851
- workerId,
852
- error: { message: "test error" },
853
- });
854
- expect(failed.status).toBe("failed");
855
- expect(failed.availableAt).toBeNull();
856
- expect(failed.finishedAt).not.toBeNull();
857
- expect(failed.startedAt).toBeNull(); // cleared on permanent failure
858
- await teardown(backend);
859
- });
860
- test("reschedules failed workflow runs if retry would complete before deadline", async () => {
861
- const backend = await setup();
862
- const deadline = new Date(Date.now() + 5000); // in 5 seconds
863
- const created = await backend.createWorkflowRun({
864
- workflowName: randomUUID(),
865
- version: null,
866
- idempotencyKey: null,
867
- input: null,
868
- config: {},
869
- context: null,
870
- availableAt: null,
871
- deadlineAt: deadline,
872
- });
873
- const workerId = randomUUID();
874
- const claimed = await backend.claimWorkflowRun({
875
- workerId,
876
- leaseDurationMs: 100,
877
- });
878
- expect(claimed).not.toBeNull();
879
- // should reschedule since retry backoff (1s) is before deadline (5s
880
- const failed = await backend.failWorkflowRun({
881
- workflowRunId: created.id,
882
- workerId,
883
- error: { message: "test error" },
884
- });
885
- expect(failed.status).toBe("pending");
886
- expect(failed.availableAt).not.toBeNull();
887
- expect(failed.finishedAt).toBeNull();
888
- await teardown(backend);
889
- });
890
- });
891
- describe("cancelWorkflowRun()", () => {
892
- test("cancels a pending workflow run", async () => {
893
- const backend = await setup();
894
- const created = await createPendingWorkflowRun(backend);
895
- expect(created.status).toBe("pending");
896
- const canceled = await backend.cancelWorkflowRun({
897
- workflowRunId: created.id,
898
- });
899
- expect(canceled.status).toBe("canceled");
900
- expect(canceled.workerId).toBeNull();
901
- expect(canceled.availableAt).toBeNull();
902
- expect(canceled.finishedAt).not.toBeNull();
903
- expect(deltaSeconds(canceled.finishedAt)).toBeLessThan(1);
904
- await teardown(backend);
905
- });
906
- test("cancels a running workflow run", async () => {
907
- const backend = await setup();
908
- const created = await createClaimedWorkflowRun(backend);
909
- expect(created.status).toBe("running");
910
- expect(created.workerId).not.toBeNull();
911
- const canceled = await backend.cancelWorkflowRun({
912
- workflowRunId: created.id,
913
- });
914
- expect(canceled.status).toBe("canceled");
915
- expect(canceled.workerId).toBeNull();
916
- expect(canceled.availableAt).toBeNull();
917
- expect(canceled.finishedAt).not.toBeNull();
918
- await teardown(backend);
919
- });
920
- test("cancels a sleeping workflow run", async () => {
921
- const backend = await setup();
922
- const claimed = await createClaimedWorkflowRun(backend);
923
- // put workflow to sleep
924
- const sleepUntil = new Date(Date.now() + 60_000); // 1 minute from now
925
- const sleeping = await backend.sleepWorkflowRun({
926
- workflowRunId: claimed.id,
927
- workerId: claimed.workerId ?? "",
928
- availableAt: sleepUntil,
929
- });
930
- expect(sleeping.status).toBe("sleeping");
931
- const canceled = await backend.cancelWorkflowRun({
932
- workflowRunId: sleeping.id,
933
- });
934
- expect(canceled.status).toBe("canceled");
935
- expect(canceled.workerId).toBeNull();
936
- expect(canceled.availableAt).toBeNull();
937
- expect(canceled.finishedAt).not.toBeNull();
938
- await teardown(backend);
939
- });
940
- test("throws error when canceling a completed workflow run", async () => {
941
- const backend = await setup();
942
- const claimed = await createClaimedWorkflowRun(backend);
943
- // mark as completed
944
- await backend.completeWorkflowRun({
945
- workflowRunId: claimed.id,
946
- workerId: claimed.workerId ?? "",
947
- output: { result: "success" },
948
- });
949
- await expect(backend.cancelWorkflowRun({
950
- workflowRunId: claimed.id,
951
- })).rejects.toThrow(/Cannot cancel workflow run .* with status completed/);
952
- await teardown(backend);
953
- });
954
- test("throws error when canceling a failed workflow run", async () => {
955
- const backend = await setup();
956
- // create with deadline that's already passed to make it fail
957
- const workflowWithDeadline = await backend.createWorkflowRun({
958
- workflowName: randomUUID(),
959
- version: null,
960
- idempotencyKey: null,
961
- input: null,
962
- config: {},
963
- context: null,
964
- availableAt: null,
965
- deadlineAt: new Date(Date.now() - 1000), // deadline in the past
966
- });
967
- // try to claim it, which should mark it as failed due to deadline
968
- const claimed = await backend.claimWorkflowRun({
969
- workerId: randomUUID(),
970
- leaseDurationMs: 100,
971
- });
972
- // if claim succeeds, manually fail it
973
- if (claimed?.workerId) {
974
- await backend.failWorkflowRun({
975
- workflowRunId: claimed.id,
976
- workerId: claimed.workerId,
977
- error: { message: "test error" },
978
- });
979
- }
980
- // get a workflow that's definitely failed
981
- const failedRun = await backend.getWorkflowRun({
982
- workflowRunId: workflowWithDeadline.id,
983
- });
984
- if (failedRun?.status === "failed") {
985
- await expect(backend.cancelWorkflowRun({
986
- workflowRunId: failedRun.id,
987
- })).rejects.toThrow(/Cannot cancel workflow run .* with status failed/);
988
- }
989
- await teardown(backend);
990
- });
991
- test("is idempotent when canceling an already canceled workflow run", async () => {
992
- const backend = await setup();
993
- const created = await createPendingWorkflowRun(backend);
994
- const firstCancel = await backend.cancelWorkflowRun({
995
- workflowRunId: created.id,
996
- });
997
- expect(firstCancel.status).toBe("canceled");
998
- const secondCancel = await backend.cancelWorkflowRun({
999
- workflowRunId: created.id,
1000
- });
1001
- expect(secondCancel.status).toBe("canceled");
1002
- expect(secondCancel.id).toBe(firstCancel.id);
1003
- await teardown(backend);
1004
- });
1005
- test("throws error when canceling a non-existent workflow run", async () => {
1006
- const backend = await setup();
1007
- const nonExistentId = randomUUID();
1008
- await expect(backend.cancelWorkflowRun({
1009
- workflowRunId: nonExistentId,
1010
- })).rejects.toThrow(`Workflow run ${nonExistentId} does not exist`);
1011
- await teardown(backend);
1012
- });
1013
- test("canceled workflow is not claimed by workers", async () => {
1014
- const backend = await setup();
1015
- const created = await createPendingWorkflowRun(backend);
1016
- // cancel the workflow
1017
- await backend.cancelWorkflowRun({
1018
- workflowRunId: created.id,
1019
- });
1020
- // try to claim work
1021
- const claimed = await backend.claimWorkflowRun({
1022
- workerId: randomUUID(),
1023
- leaseDurationMs: 100,
1024
- });
1025
- // should not claim the canceled workflow
1026
- expect(claimed).toBeNull();
1027
- await teardown(backend);
1028
- });
1029
- });
1030
- });
1031
- }
1032
- /**
1033
- * Create a pending workflow run for tests.
1034
- * @param b - Backend
1035
- * @returns Created workflow run
1036
- */
1037
- async function createPendingWorkflowRun(b) {
1038
- return await b.createWorkflowRun({
1039
- workflowName: randomUUID(),
1040
- version: null,
1041
- idempotencyKey: null,
1042
- input: null,
1043
- config: {},
1044
- context: null,
1045
- availableAt: null,
1046
- deadlineAt: null,
1047
- });
1048
- }
1049
- /**
1050
- * Create and claim a workflow run for tests.
1051
- * @param b - Backend
1052
- * @returns Claimed workflow run
1053
- */
1054
- async function createClaimedWorkflowRun(b) {
1055
- await createPendingWorkflowRun(b);
1056
- const claimed = await b.claimWorkflowRun({
1057
- workerId: randomUUID(),
1058
- leaseDurationMs: 100,
1059
- });
1060
- if (!claimed)
1061
- throw new Error("Failed to claim workflow run");
1062
- return claimed;
1063
- }
1064
- /**
1065
- * Get delta in seconds from now.
1066
- * @param date - Date to compare
1067
- * @returns Delta in seconds
1068
- */
1069
- function deltaSeconds(date) {
1070
- if (!date)
1071
- return Infinity;
1072
- return Math.abs((Date.now() - date.getTime()) / 1000);
1073
- }
1074
- /**
1075
- * Create a Date one year in the future.
1076
- * @returns Future Date
1077
- */
1078
- function newDateInOneYear() {
1079
- const d = new Date();
1080
- d.setFullYear(d.getFullYear() + 1);
1081
- return d;
1082
- }
1083
- /**
1084
- * Sleep for a given duration.
1085
- * @param ms - Milliseconds to sleep
1086
- * @returns Promise resolved after sleeping
1087
- */
1088
- function sleep(ms) {
1089
- return new Promise((resolve) => setTimeout(resolve, ms));
1090
- }
1091
- //# sourceMappingURL=backend.testsuite.js.map