zitejs 0.9.147 → 0.9.149

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 (37) hide show
  1. package/dist/cjs/backend/index.d.ts +7 -1
  2. package/dist/cjs/bundle/index.d.ts +8 -0
  3. package/dist/cjs/bundle/index.js +9 -8
  4. package/dist/cjs/check/index.js +41 -25
  5. package/dist/cjs/client/index.d.ts +24 -0
  6. package/dist/cjs/integrations/rules.js +5 -3
  7. package/dist/cjs/runtime/index.d.ts +49 -19
  8. package/dist/cjs/runtime/sendEmailParamsTypes.test-d.d.ts +1 -0
  9. package/dist/cjs/runtime/sendEmailParamsTypes.test-d.js +32 -0
  10. package/dist/cjs/steps/index.d.ts +78 -0
  11. package/dist/cjs/steps/index.js +507 -0
  12. package/dist/cjs/steps/index.test.d.ts +1 -0
  13. package/dist/cjs/steps/index.test.js +635 -0
  14. package/dist/cjs/sync/lib.js +10 -6
  15. package/dist/cjs/sync/lib.test.js +100 -36
  16. package/dist/cjs/types/fields.d.ts +8 -1
  17. package/dist/cjs/types/fields.js +7 -1
  18. package/dist/cjs/types/tables.d.ts +16 -0
  19. package/dist/esm/backend/index.d.ts +7 -1
  20. package/dist/esm/bundle/index.d.ts +8 -0
  21. package/dist/esm/bundle/index.js +8 -8
  22. package/dist/esm/check/index.js +10 -27
  23. package/dist/esm/client/index.d.ts +24 -0
  24. package/dist/esm/integrations/rules.js +5 -3
  25. package/dist/esm/runtime/index.d.ts +49 -19
  26. package/dist/esm/runtime/sendEmailParamsTypes.test-d.d.ts +1 -0
  27. package/dist/esm/runtime/sendEmailParamsTypes.test-d.js +30 -0
  28. package/dist/esm/steps/index.d.ts +78 -0
  29. package/dist/esm/steps/index.js +502 -0
  30. package/dist/esm/steps/index.test.d.ts +1 -0
  31. package/dist/esm/steps/index.test.js +633 -0
  32. package/dist/esm/sync/lib.js +10 -6
  33. package/dist/esm/sync/lib.test.js +100 -36
  34. package/dist/esm/types/fields.d.ts +8 -1
  35. package/dist/esm/types/fields.js +6 -0
  36. package/dist/esm/types/tables.d.ts +16 -0
  37. package/package.json +9 -1
@@ -0,0 +1,635 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const index_js_1 = require("./index.js");
5
+ const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
6
+ /**
7
+ * One call as a platform makes it: the code races the call's first stop, and
8
+ * the call answers once every step still doing work has saved.
9
+ */
10
+ const call = async (code, options = {}) => {
11
+ const memos = Object.assign(Object.create(null), options.memos ?? {});
12
+ const saves = [];
13
+ const events = [];
14
+ const runtime = (0, index_js_1.createStepRuntime)({
15
+ durable: true,
16
+ save: async (name, value) => {
17
+ saves.push({ name, value });
18
+ },
19
+ onEvent: (name, status) => events.push([name, status]),
20
+ ...options,
21
+ memos,
22
+ });
23
+ const execution = Promise.resolve()
24
+ .then(() => code(runtime.step))
25
+ .then((value) => ({ value }), (error) => ({ error }));
26
+ const ended = await Promise.race([
27
+ execution,
28
+ runtime.stopped.then(() => undefined),
29
+ ]);
30
+ await runtime.settle();
31
+ return {
32
+ ended,
33
+ stop: runtime.stop(),
34
+ suspension: runtime.suspension(),
35
+ retry: runtime.retry(),
36
+ savedCount: runtime.saved(),
37
+ saved: saves.map((s) => s.name),
38
+ saves,
39
+ events,
40
+ memos,
41
+ };
42
+ };
43
+ /** A whole run: each call gets what the one before saved and its retry's tries. */
44
+ const drive = async (code, options = {}) => {
45
+ let memos = {};
46
+ let attempts = {};
47
+ for (let calls = 1; calls <= 20; calls++) {
48
+ const last = await call(code, { ...options, memos, attempts });
49
+ if (!last.retry && !last.suspension)
50
+ return { calls, last };
51
+ memos = last.memos;
52
+ attempts = last.retry?.attempts ?? attempts;
53
+ }
54
+ throw new Error("the run never ended");
55
+ };
56
+ const errorOf = (ended) => ended.error;
57
+ // Every way a durable call stops, inside every shape code puts steps in. The
58
+ // stop always ends the call promptly, the code past it never runs, and a
59
+ // sibling still doing work gets to save.
60
+ (0, vitest_1.describe)("a stop, wherever the step is", () => {
61
+ const STOPS = {
62
+ retry: {
63
+ act: (step) => step.run("s", async () => {
64
+ await wait(5);
65
+ throw new Error("provider blip");
66
+ }),
67
+ options: {},
68
+ kind: "retry",
69
+ },
70
+ sleep: {
71
+ act: (step) => step.sleep("s", "1h"),
72
+ options: {},
73
+ kind: "sleep",
74
+ },
75
+ budget: {
76
+ act: async (step) => {
77
+ await step.run("first", () => 1);
78
+ await wait(30);
79
+ await step.run("s", () => 2);
80
+ },
81
+ options: { budgetMs: 10 },
82
+ kind: "step",
83
+ },
84
+ };
85
+ const SHAPES = {
86
+ flat: (act, marks) => async (step) => {
87
+ await act(step);
88
+ marks.push("after");
89
+ },
90
+ "try/catch": (act, marks) => async (step) => {
91
+ try {
92
+ await act(step);
93
+ }
94
+ catch {
95
+ marks.push("caught");
96
+ }
97
+ marks.push("after");
98
+ },
99
+ finally: (act, marks) => async (step) => {
100
+ try {
101
+ await act(step);
102
+ }
103
+ finally {
104
+ marks.push("finally");
105
+ }
106
+ },
107
+ "Promise.all": (act, marks) => async (step) => {
108
+ await Promise.all([
109
+ act(step),
110
+ step.run("sibling", async () => {
111
+ await wait(40);
112
+ return 1;
113
+ }),
114
+ ]);
115
+ marks.push("after");
116
+ },
117
+ nested: (act, marks) => async (step) => {
118
+ await step.run("outer", async () => {
119
+ await act(step);
120
+ marks.push("inner after");
121
+ });
122
+ marks.push("after");
123
+ },
124
+ "nested two deep": (act, marks) => async (step) => {
125
+ await step.run("outer", () => step.run("middle", async () => {
126
+ await act(step);
127
+ marks.push("inner after");
128
+ }));
129
+ marks.push("after");
130
+ },
131
+ "nested, beside a sibling": (act, marks) => async (step) => {
132
+ await Promise.all([
133
+ step.run("outer", async () => {
134
+ await act(step);
135
+ marks.push("inner after");
136
+ }),
137
+ step.run("sibling", async () => {
138
+ await wait(40);
139
+ return 1;
140
+ }),
141
+ ]);
142
+ marks.push("after");
143
+ },
144
+ };
145
+ // A sleep inside a step is refused rather than stopping the call.
146
+ const cases = Object.entries(SHAPES)
147
+ .flatMap(([shape, wrap]) => Object.entries(STOPS).map(([stop, spec]) => ({
148
+ shape,
149
+ stop,
150
+ spec,
151
+ wrap,
152
+ })))
153
+ .filter(({ shape, stop }) => !(stop === "sleep" && shape.startsWith("nested")));
154
+ vitest_1.it.each(cases)("$stop inside $shape", async ({ spec, wrap, shape }) => {
155
+ const marks = [];
156
+ const result = await call(wrap(spec.act, marks), spec.options);
157
+ (0, vitest_1.expect)(result.ended).toBeUndefined();
158
+ (0, vitest_1.expect)(result.stop).toMatchObject({ kind: spec.kind, name: "s" });
159
+ (0, vitest_1.expect)(marks).toEqual([]);
160
+ if (shape.includes("sibling") || shape === "Promise.all") {
161
+ (0, vitest_1.expect)(result.saved).toContain("sibling");
162
+ }
163
+ (0, vitest_1.expect)(result.saved).not.toContain("outer");
164
+ (0, vitest_1.expect)(result.saved).not.toContain("middle");
165
+ }, 1_000);
166
+ });
167
+ (0, vitest_1.describe)("step.run", () => {
168
+ (0, vitest_1.it)("saves each result before the code after it runs, and replays it on a later call", async () => {
169
+ const first = await call(async (step) => {
170
+ const a = await step.run("a", () => ({ id: 7 }));
171
+ return step.run("b", () => a);
172
+ });
173
+ (0, vitest_1.expect)(first.ended).toEqual({ value: { id: 7 } });
174
+ (0, vitest_1.expect)(first.saved).toEqual(["a", "b"]);
175
+ (0, vitest_1.expect)(first.savedCount).toBe(2);
176
+ let ran = false;
177
+ const later = await call((step) => step.run("a", () => {
178
+ ran = true;
179
+ return { id: 8 };
180
+ }), { memos: first.memos });
181
+ (0, vitest_1.expect)(ran).toBe(false);
182
+ (0, vitest_1.expect)(later.ended).toEqual({ value: { id: 7 } });
183
+ (0, vitest_1.expect)(later.events).toContainEqual(["a", "memoized"]);
184
+ });
185
+ (0, vitest_1.it)("saves a step's final failure, and a later call throws it again without running the step", async () => {
186
+ const failing = await call((step) => step.run("charge", () => {
187
+ throw new TypeError("card declined");
188
+ }, { retries: 0 }));
189
+ const error = failing.ended.error;
190
+ (0, vitest_1.expect)(error.message).toBe("card declined");
191
+ (0, vitest_1.expect)(error[index_js_1.STEP_FAILED_STEP_KEY]).toBe("charge");
192
+ (0, vitest_1.expect)(failing.memos.charge).toEqual({
193
+ [index_js_1.STEP_FAILURE_MEMO_KEY]: { message: "card declined", name: "TypeError" },
194
+ });
195
+ let ran = false;
196
+ const later = await call((step) => step.run("charge", () => {
197
+ ran = true;
198
+ }), { memos: failing.memos });
199
+ (0, vitest_1.expect)(ran).toBe(false);
200
+ (0, vitest_1.expect)(later.ended.error.name).toBe("TypeError");
201
+ });
202
+ (0, vitest_1.it)("counts each failing step's own tries, and says each one's next", async () => {
203
+ const result = await call((step) => Promise.all([
204
+ step.run("a", async () => {
205
+ throw new Error("a down");
206
+ }),
207
+ step.run("b", async () => {
208
+ throw new Error("b down");
209
+ }),
210
+ ]), { attempts: { b: 2, c: 3 } });
211
+ (0, vitest_1.expect)(result.retry).toEqual({
212
+ step: "a",
213
+ attempt: 2,
214
+ attempts: { a: 2, b: 3, c: 3 },
215
+ });
216
+ });
217
+ (0, vitest_1.it)("drops the count of a step that has since saved", async () => {
218
+ const result = await call(async (step) => {
219
+ await step.run("a", () => 1);
220
+ await step.run("b", () => {
221
+ throw new Error("down");
222
+ });
223
+ }, { attempts: { a: 2 } });
224
+ (0, vitest_1.expect)(result.retry?.attempts).toEqual({ b: 2 });
225
+ });
226
+ (0, vitest_1.it)("gives a step's code its error once its tries are spent", async () => {
227
+ const result = await call((step) => step.run("a", () => {
228
+ throw new Error("still down");
229
+ }, { retries: 1 }), { attempts: { a: 2 } });
230
+ (0, vitest_1.expect)(result.ended.error.message).toBe("still down");
231
+ (0, vitest_1.expect)(result.stop).toBeUndefined();
232
+ });
233
+ (0, vitest_1.it)("lets a retry outrank a sleep or the budget, so the call ends as that step's failure", async () => {
234
+ const result = await call((step) => Promise.all([
235
+ step.sleep("nap", "1h"),
236
+ step.run("a", async () => {
237
+ await wait(5);
238
+ throw new Error("down");
239
+ }),
240
+ ]));
241
+ (0, vitest_1.expect)(result.stop).toMatchObject({ kind: "retry", name: "a" });
242
+ (0, vitest_1.expect)(result.suspension).toBeUndefined();
243
+ });
244
+ (0, vitest_1.it)("starts no new step once a stop is recorded", async () => {
245
+ let started = false;
246
+ const result = await call(async (step) => {
247
+ await Promise.all([
248
+ step.run("a", () => {
249
+ throw new Error("down");
250
+ }),
251
+ step.run("b", () => {
252
+ started = true;
253
+ }),
254
+ ]);
255
+ });
256
+ (0, vitest_1.expect)(result.stop).toMatchObject({ kind: "retry", name: "a" });
257
+ (0, vitest_1.expect)(started).toBe(false);
258
+ });
259
+ (0, vitest_1.it)("outside a durable call, hands the error straight to the code and saves nothing", async () => {
260
+ const result = await call((step) => step.run("a", () => {
261
+ throw new Error("down");
262
+ }), { durable: false });
263
+ (0, vitest_1.expect)(result.ended.error.message).toBe("down");
264
+ (0, vitest_1.expect)(result.saved).toEqual([]);
265
+ });
266
+ (0, vitest_1.it)("tells the platform a failed save is its own fault", async () => {
267
+ const faults = [];
268
+ const result = await call((step) => step.run("a", () => 1, { retries: 0 }), {
269
+ save: async () => {
270
+ throw new Error("backend blip");
271
+ },
272
+ onPlatformFault: (error) => faults.push(error),
273
+ });
274
+ (0, vitest_1.expect)(faults).toHaveLength(1);
275
+ (0, vitest_1.expect)(result.events).toEqual([["a", "failed"]]);
276
+ (0, vitest_1.expect)(result.ended.error.message).toMatch(/Saving the result of step "a" failed \(backend blip\)/);
277
+ });
278
+ (0, vitest_1.it)("refuses names that can't match a saved result", async () => {
279
+ const twice = await call(async (step) => {
280
+ await step.run("a", () => 1);
281
+ await step.run("a", () => 2);
282
+ });
283
+ (0, vitest_1.expect)(twice.ended.error.message).toMatch(/called twice with the name "a"/);
284
+ const unnamed = await call((step) => step.run("", () => 1));
285
+ (0, vitest_1.expect)(unnamed.ended.error.message).toMatch(/needs a stable name/);
286
+ });
287
+ (0, vitest_1.it)("runs a step whose result can't be saved once, and fails it for good", async () => {
288
+ let runs = 0;
289
+ const { calls, last } = await drive((step) => step.run("create", () => {
290
+ runs++;
291
+ return "x".repeat(300_000);
292
+ }));
293
+ (0, vitest_1.expect)(calls).toBe(1);
294
+ (0, vitest_1.expect)(runs).toBe(1);
295
+ (0, vitest_1.expect)(errorOf(last.ended).message).toMatch(/more than 256KB/);
296
+ });
297
+ vitest_1.it.each([
298
+ [
299
+ "an error",
300
+ Object.assign(new TypeError("declined"), {
301
+ code: "card_declined",
302
+ status: 402,
303
+ }),
304
+ {
305
+ name: "TypeError",
306
+ message: "declined",
307
+ code: "card_declined",
308
+ status: 402,
309
+ },
310
+ ],
311
+ [
312
+ "a thrown object",
313
+ { status: 404, detail: "gone" },
314
+ {
315
+ message: '{"status":404,"detail":"gone"}',
316
+ status: 404,
317
+ detail: "gone",
318
+ },
319
+ ],
320
+ ])("hands %s to the call that saves its failure, and rebuilds it with its plain fields later", async (_, thrown, rebuilt) => {
321
+ const first = await call((step) => step.run("charge", () => {
322
+ throw thrown;
323
+ }, { retries: 0 }));
324
+ (0, vitest_1.expect)(errorOf(first.ended)).toBe(thrown);
325
+ (0, vitest_1.expect)(errorOf(first.ended)[index_js_1.STEP_FAILED_STEP_KEY]).toBe("charge");
326
+ (0, vitest_1.expect)(Object.keys(thrown)).not.toContain(index_js_1.STEP_FAILED_STEP_KEY);
327
+ const later = await call((step) => step.run("charge", () => 1), {
328
+ memos: first.memos,
329
+ });
330
+ (0, vitest_1.expect)(errorOf(later.ended)).toBeInstanceOf(Error);
331
+ (0, vitest_1.expect)(errorOf(later.ended)).toMatchObject({
332
+ ...rebuilt,
333
+ [index_js_1.STEP_FAILED_STEP_KEY]: "charge",
334
+ });
335
+ });
336
+ (0, vitest_1.it)("saves an error's plain fields past any number of others", async () => {
337
+ const thrown = Object.assign(new Error("declined"), Object.fromEntries(Array.from({ length: 25 }, (_, i) => [`detail${i}`, { i }])), { code: "card_declined" });
338
+ const first = await call((step) => step.run("charge", () => {
339
+ throw thrown;
340
+ }, { retries: 0 }));
341
+ const later = await call((step) => step.run("charge", () => 1), {
342
+ memos: first.memos,
343
+ });
344
+ (0, vitest_1.expect)(errorOf(later.ended).code).toBe("card_declined");
345
+ });
346
+ (0, vitest_1.it)("retries a step that rethrows an inner step's final failure, as its own code may recover", async () => {
347
+ let fallbacks = 0;
348
+ const { calls, last } = await drive((step) => step.run("outer", async () => {
349
+ try {
350
+ return await step.run("primary", () => {
351
+ throw new Error("primary down");
352
+ }, { retries: 0 });
353
+ }
354
+ catch (primaryError) {
355
+ if (++fallbacks === 1)
356
+ throw primaryError;
357
+ return "from fallback";
358
+ }
359
+ }));
360
+ (0, vitest_1.expect)(calls).toBe(2);
361
+ (0, vitest_1.expect)(last.ended).toEqual({ value: "from fallback" });
362
+ });
363
+ (0, vitest_1.it)("fails with the step's own error when its failure can't be saved", async () => {
364
+ const faults = [];
365
+ const result = await call((step) => step.run("charge", () => {
366
+ throw new Error("card declined");
367
+ }, { retries: 0 }), {
368
+ save: async () => {
369
+ throw new Error("db down");
370
+ },
371
+ onPlatformFault: (error) => faults.push(error),
372
+ });
373
+ (0, vitest_1.expect)(errorOf(result.ended).message).toBe("card declined");
374
+ (0, vitest_1.expect)(result.memos.charge).toBeUndefined();
375
+ (0, vitest_1.expect)(faults).toEqual([]);
376
+ (0, vitest_1.expect)(result.events).toEqual([["charge", "failed"]]);
377
+ });
378
+ (0, vitest_1.it)("fails a step for good when a step inside it is misused", async () => {
379
+ const { calls, last } = await drive((step) => step.run("outer", () => step.run("inner", () => 1, { retries: 99 })));
380
+ (0, vitest_1.expect)(calls).toBe(1);
381
+ (0, vitest_1.expect)(errorOf(last.ended).message).toMatch(/retries must be a whole number/);
382
+ });
383
+ (0, vitest_1.it)("keeps the platform's fault mark on a failed save that a later call replays", async () => {
384
+ const first = await call((step) => step.run("a", () => 1, { retries: 0 }), {
385
+ save: async (_name, value) => {
386
+ if (!(0, index_js_1.isStepFailureMemo)(value))
387
+ throw new Error("backend blip");
388
+ },
389
+ onPlatformFault: (error) => Object.assign(error, { platformFault: true }),
390
+ });
391
+ const later = await call((step) => step.run("a", () => 1), {
392
+ memos: first.memos,
393
+ });
394
+ for (const { ended } of [first, later]) {
395
+ (0, vitest_1.expect)(errorOf(ended)).toMatchObject({ platformFault: true });
396
+ }
397
+ });
398
+ (0, vitest_1.it)("stops at the budget when the steps past it fail for good", async () => {
399
+ let ran = 0;
400
+ const result = await call(async (step) => {
401
+ for (const name of ["s1", "s2", "s3"]) {
402
+ try {
403
+ await step.run(name, async () => {
404
+ ran++;
405
+ await wait(15);
406
+ throw new Error("down");
407
+ }, { retries: 0 });
408
+ }
409
+ catch {
410
+ // The code handles each failure and moves on.
411
+ }
412
+ }
413
+ }, { budgetMs: 10 });
414
+ (0, vitest_1.expect)(ran).toBe(1);
415
+ (0, vitest_1.expect)(result.suspension).toEqual({ kind: "step", name: "s2" });
416
+ });
417
+ (0, vitest_1.it)("saves a step named __proto__ under its name in a plain object", async () => {
418
+ const memos = {};
419
+ const runtime = (0, index_js_1.createStepRuntime)({
420
+ durable: true,
421
+ memos,
422
+ save: async () => { },
423
+ });
424
+ await runtime.step.run("__proto__", () => ({ a: 1 }));
425
+ (0, vitest_1.expect)(Object.getPrototypeOf(memos)).toBe(Object.prototype);
426
+ (0, vitest_1.expect)(Object.keys(memos)).toEqual(["__proto__"]);
427
+ });
428
+ (0, vitest_1.it)("refuses a result that isn't plain JSON", async () => {
429
+ const result = await call((step) => step.run("a", () => BigInt(1)));
430
+ (0, vitest_1.expect)(errorOf(result.ended).message).toMatch(/cannot be saved for retries/);
431
+ (0, vitest_1.expect)(result.retry).toBeUndefined();
432
+ });
433
+ (0, vitest_1.it)("refuses retries outside 0 to 10", async () => {
434
+ const result = await call((step) => step.run("a", () => 1, { retries: 11 }));
435
+ (0, vitest_1.expect)(result.ended.error.message).toMatch(/retries must be a whole number from 0 to 10/);
436
+ });
437
+ });
438
+ (0, vitest_1.describe)("step.sleep", () => {
439
+ (0, vitest_1.it)("saves its wake time and stops; a call before it stops again at the same time, one after it goes on", async () => {
440
+ const first = await call((step) => step.sleep("nap", "2h"));
441
+ const wakeAt = first.memos.nap.wakeAt;
442
+ (0, vitest_1.expect)(first.suspension).toEqual({ kind: "sleep", name: "nap", wakeAt });
443
+ const early = await call((step) => step.sleep("nap", "2h"), {
444
+ memos: first.memos,
445
+ });
446
+ (0, vitest_1.expect)(early.suspension).toEqual({ kind: "sleep", name: "nap", wakeAt });
447
+ (0, vitest_1.expect)(early.saved).toEqual([]);
448
+ const late = await call(async (step) => {
449
+ await step.sleep("nap", "2h");
450
+ return "woke";
451
+ }, { memos: first.memos, now: () => Date.parse(wakeAt) + 1 });
452
+ (0, vitest_1.expect)(late.ended).toEqual({ value: "woke" });
453
+ });
454
+ (0, vitest_1.it)("is skipped by an editor test and refused by an app's request", async () => {
455
+ const test = await call(async (step) => {
456
+ await step.sleep("nap", "1h");
457
+ return "past it";
458
+ }, { durable: false, sleeps: "skip" });
459
+ (0, vitest_1.expect)(test.ended).toEqual({ value: "past it" });
460
+ const request = await call((step) => step.sleep("nap", "1h"), {
461
+ durable: false,
462
+ });
463
+ (0, vitest_1.expect)(request.ended.error.message).toMatch(/can only wait in an automation run/);
464
+ });
465
+ (0, vitest_1.it)("refuses to wait inside a step, which would run that step again after the wait", async () => {
466
+ let sent = 0;
467
+ const code = (step) => step.run("notify", async () => {
468
+ sent++;
469
+ await step.run("inner", async () => {
470
+ await step.sleep("wait", "1h");
471
+ });
472
+ });
473
+ const { calls, last } = await drive(code);
474
+ (0, vitest_1.expect)(calls).toBe(1);
475
+ (0, vitest_1.expect)(sent).toBe(1);
476
+ (0, vitest_1.expect)(errorOf(last.ended).message).toMatch(/step\.sleep\("wait"\) can't wait inside step\.run\("inner"\)/);
477
+ (0, vitest_1.expect)((0, index_js_1.isStepFailureMemo)(last.memos.notify)).toBe(true);
478
+ (0, vitest_1.expect)(last.saved).not.toContain("wait");
479
+ const editorTest = await call(code, { durable: false, sleeps: "skip" });
480
+ (0, vitest_1.expect)(errorOf(editorTest.ended).message).toMatch(/can't wait inside/);
481
+ });
482
+ (0, vitest_1.it)("asks sleepUntil for a date, not a duration", async () => {
483
+ const result = await call((step) => step.sleepUntil("nap", "tomorrow"));
484
+ (0, vitest_1.expect)(errorOf(result.ended).message).toMatch(/step\.sleepUntil\("nap"\) needs a Date or a date string/);
485
+ });
486
+ (0, vitest_1.it)("waits at most 30 days", async () => {
487
+ const result = await call((step) => step.sleep("nap", "31d"));
488
+ (0, vitest_1.expect)(result.ended.error.message).toMatch(/can wait at most 30 days/);
489
+ });
490
+ (0, vitest_1.it)("reads units from ms to weeks, and sleepUntil a date", async () => {
491
+ const now = Date.parse("2026-10-25T00:00:00.000Z");
492
+ const weeks = await call((step) => step.sleep("w", "1w"), {
493
+ now: () => now,
494
+ });
495
+ (0, vitest_1.expect)(weeks.memos.w.wakeAt).toBe("2026-11-01T00:00:00.000Z");
496
+ const until = await call((step) => step.sleepUntil("u", "2026-10-26T12:00:00.000Z"), { now: () => now });
497
+ (0, vitest_1.expect)(until.suspension).toMatchObject({
498
+ wakeAt: "2026-10-26T12:00:00.000Z",
499
+ });
500
+ });
501
+ (0, vitest_1.it)("keeps to its saved wake time when its argument, worked out from the clock or data, changes", async () => {
502
+ const start = Date.parse("2026-10-25T00:00:00.000Z");
503
+ const first = await call((step) => step.sleepUntil("remind", new Date(start + 20 * 864e5)), {
504
+ now: () => start,
505
+ });
506
+ const savedWake = first.memos.remind.wakeAt;
507
+ const moved = await call((step) => step.sleepUntil("remind", new Date(start + 45 * 864e5)), { memos: first.memos, now: () => start + 864e5 });
508
+ (0, vitest_1.expect)(moved.suspension).toEqual({
509
+ kind: "sleep",
510
+ name: "remind",
511
+ wakeAt: savedWake,
512
+ });
513
+ const late = await call(async (step) => {
514
+ await step.sleep("remind", "-2m");
515
+ return "woke";
516
+ }, { memos: first.memos, now: () => Date.parse(savedWake) + 120_000 });
517
+ (0, vitest_1.expect)(late.ended).toEqual({ value: "woke" });
518
+ });
519
+ (0, vitest_1.it)("reads the clock when it needs it, so code that sets the clock is followed", async () => {
520
+ try {
521
+ const result = await call((step) => {
522
+ vitest_1.vi.spyOn(Date, "now").mockReturnValue(Date.parse("2026-10-25T00:00:00.000Z"));
523
+ return step.sleep("nap", "1h");
524
+ });
525
+ (0, vitest_1.expect)(result.suspension).toMatchObject({
526
+ wakeAt: "2026-10-25T01:00:00.000Z",
527
+ });
528
+ }
529
+ finally {
530
+ vitest_1.vi.restoreAllMocks();
531
+ }
532
+ });
533
+ });
534
+ (0, vitest_1.describe)("settle", () => {
535
+ (0, vitest_1.it)("answers at the deadline when a step waits on a stopped step it didn't start", async () => {
536
+ const result = await call(async (step) => {
537
+ const config = step.run("config", async () => {
538
+ await wait(5);
539
+ throw new Error("blip");
540
+ });
541
+ await step.run("use", async () => await config);
542
+ }, { deadlineMs: 50 });
543
+ (0, vitest_1.expect)(result.stop).toMatchObject({ kind: "retry", name: "config" });
544
+ }, 1_000);
545
+ (0, vitest_1.it)("waits for a step that starts while it waits", async () => {
546
+ const result = await call((step) => Promise.all([
547
+ step.run("a", () => {
548
+ throw new Error("bad input");
549
+ }, { retries: 0 }),
550
+ (async () => {
551
+ await step.run("b1", async () => {
552
+ await wait(20);
553
+ return 1;
554
+ });
555
+ await step.run("b2", async () => {
556
+ await wait(20);
557
+ return 2;
558
+ });
559
+ })(),
560
+ ]));
561
+ (0, vitest_1.expect)(result.saved).toEqual(["a", "b1", "b2"]);
562
+ });
563
+ (0, vitest_1.it)("waits for this call's steps when another call's code runs inside one", async () => {
564
+ const other = (0, index_js_1.createStepRuntime)({
565
+ durable: true,
566
+ memos: Object.create(null),
567
+ save: async () => { },
568
+ });
569
+ const result = await call((step) => Promise.all([
570
+ step.run("a", async () => {
571
+ void other.step.run("b", () => {
572
+ throw new Error("down");
573
+ });
574
+ await wait(30);
575
+ return 1;
576
+ }),
577
+ step.sleep("nap", "1h"),
578
+ ]));
579
+ (0, vitest_1.expect)(other.stop()).toMatchObject({ kind: "retry", name: "b" });
580
+ (0, vitest_1.expect)(result.saved).toContain("a");
581
+ });
582
+ (0, vitest_1.it)("waits for a sleep's save beside a step that stops the call", async () => {
583
+ const result = await call((step) => Promise.all([
584
+ step.sleep("nap", "1h"),
585
+ step.run("a", async () => {
586
+ await wait(1);
587
+ throw new Error("down");
588
+ }),
589
+ ]), {
590
+ save: async (name) => {
591
+ if (name === "nap")
592
+ await wait(30);
593
+ },
594
+ });
595
+ (0, vitest_1.expect)(result.stop).toMatchObject({ kind: "retry", name: "a" });
596
+ (0, vitest_1.expect)(result.savedCount).toBe(1);
597
+ });
598
+ (0, vitest_1.it)("waits for a sleep's final failure to save", async () => {
599
+ const result = await call((step) => Promise.all([
600
+ step.sleep("nap", "1h"),
601
+ step.run("a", async () => {
602
+ await wait(1);
603
+ throw new Error("down");
604
+ }),
605
+ ]), {
606
+ attempts: { nap: 4 },
607
+ save: async (name, value) => {
608
+ if (name !== "nap")
609
+ return;
610
+ if (!(0, index_js_1.isStepFailureMemo)(value))
611
+ throw new Error("blip");
612
+ await wait(30);
613
+ },
614
+ });
615
+ (0, vitest_1.expect)((0, index_js_1.isStepFailureMemo)(result.memos.nap)).toBe(true);
616
+ });
617
+ (0, vitest_1.it)("still waits for a working step when the deadline is past what a timer holds", async () => {
618
+ const result = await call((step) => Promise.all([
619
+ step.sleep("nap", "1h"),
620
+ step.run("a", async () => {
621
+ await wait(20);
622
+ return 1;
623
+ }),
624
+ ]), { deadlineMs: Infinity });
625
+ (0, vitest_1.expect)(result.saved).toContain("a");
626
+ });
627
+ (0, vitest_1.it)("isn't failed by a log callback that rejects", async () => {
628
+ const result = await call((step) => step.run("a", () => 1), {
629
+ onEvent: async () => {
630
+ throw new Error("log sink down");
631
+ },
632
+ });
633
+ (0, vitest_1.expect)(result.ended).toEqual({ value: 1 });
634
+ });
635
+ });