Interface RescueStepMockScript

What a rescue step mock does: how it ends, whether it ends asynchronously, and whether it cannot be built.

import assert from "node:assert/strict";
import { LocalExecutor } from "itinera/executor";
import { failureReason } from "itinera/step";
import {
Ending,
RecordingReporter,
type RescueStepMock,
rescueStepMock,
stepMock,
} from "itinera/testing";
import { Workflow } from "itinera/workflow";

class Orders extends Workflow {}

export default (async () => {
const charge = stepMock({
attempts: [Ending.failure("expired"), Ending.success],
});
const built: RescueStepMock[] = [];
class Renew extends rescueStepMock({ attempts: [Ending.success] }) {
constructor(...needs: unknown[]) {
super(...needs);
built.push(this);
}
}
const orders = Orders.builder()
.name("orders")
.step(
charge.builder("charge").rescue(Renew.builder().needs(failureReason())),
)
.build();
const recorder = new RecordingReporter();

const result = await new LocalExecutor().run(
orders.instance({}, { reporters: [recorder] }),
);

assert.ok(result.isSucceeded());
assert.deepEqual(recorder.path, [
"charge attempt 1 failed: expired",
"charge rescuing with charge_rescue",
"charge_rescue attempt 1 succeeded",
"charge rescued by charge_rescue",
"charge attempt 2 succeeded",
"journey succeeded",
]);
assert.deepEqual(
built.map((renew) => renew.inputs),
[[{ code: "expired" }]],
);
})();
interface RescueStepMockScript {
    asynchronous?: boolean;
    attempts: readonly [RescueEnding, RescueEnding];
    buildFailure?: string;
}

Index

asynchronous?: boolean

true for a run that gives its outcome through a promise, settled after the current turn, rather than at once.

attempts: readonly [RescueEnding, RescueEnding]

How each rescue step built from the class ends, the first one's first; the last ending is played again by every one after it. A rescue step has one attempt per rescue, so a workflow whose step is rescued once plays only the first.

buildFailure?: string

The message of the error its constructor throws, when the rescue step cannot be built.