Class RescueStepMockAbstract

The base class of the rescue step mocks rescueStepMock makes: a rescue step that plays its script, ending through a RescueEnding, so that it ends exactly as a real rescue step would, and keeps what it was built with.

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" }]],
);
})();

Hierarchy (View Summary)

Index

inputs: readonly unknown[]

What it was built with, in the order its constructor requests it: each input's value, each value from the step it rescues, the failure's reason and the error, or an Option of one for an optional request.

  • The outcome of an attempt that could not do its work, for a reason, or only its code, captured as a step's fail captures it.

    import assert from "node:assert/strict";
    import { type Contributor, contributor } from "itinera/journey";
    import { None, type Option, Some } from "itinera/option";
    import {
    failureReason,
    fromRescuedStep,
    input,
    type Outcome,
    type Reason,
    RescueStep,
    rescueStep,
    type RescueOutcome,
    type RescueStepBuilder,
    type RescueStepClass,
    Step,
    step,
    } from "itinera/step";
    import { Contributions } from "itinera/testing";
    import { withStep, Workflow, workflow } from "itinera/workflow";
    import { integer, optional, string } from "itinera/value";

    @rescueStep()
    class Refund extends RescueStep {
    constructor(
    @input("order", string) readonly order: string,
    @fromRescuedStep("charged", optional(integer))
    readonly charged: Option<number>,
    @failureReason({ optional: true }) readonly reason: Option<Reason>,
    ) {
    super();
    }

    override run(@contributor() contributor: Contributor): RescueOutcome {
    const why = this.reason.isSome() ? this.reason.value.code : "a throw";
    contributor.contribute(
    "refunded",
    `${this.order}: ${String(this.charged.unwrapOr(0))} after ${why}`,
    );
    return this.succeed();
    }
    }

    @step()
    class Charge extends Step {
    override run(): Outcome {
    return this.succeed();
    }
    }

    class Weigh extends RescueStep {
    constructor(readonly grams: number) {
    super();
    }

    override run(): RescueOutcome {
    return this.grams > 0 ? this.succeed() : this.fail("nothing to weigh");
    }
    }

    const rescue: RescueStepClass = Refund;

    @workflow({ name: "orders" })
    @withStep(Charge, { name: "charge", rescue })
    class Orders extends Workflow {}

    const weighing: RescueStepBuilder<typeof Weigh> = Weigh.builder().needs(
    fromRescuedStep("grams", integer),
    );
    const shipping = Orders.builder()
    .step(Charge.builder("ship").rescue(weighing))
    .build();

    const refund = new Refund("A-1", Some(40), Some({ code: "declined" }));
    const unknown = new Refund("B-2", None, None);

    const refunding = new Contributions();
    const guessing = new Contributions();

    assert.deepEqual(refund.run(refunding.contributor), { kind: "success" });
    assert.equal(refunded(refunding), "A-1: 40 after declined");
    unknown.run(guessing.contributor);
    assert.equal(refunded(guessing), "B-2: 0 after a throw");
    assert.deepEqual(new Weigh(0).run(), {
    kind: "failure",
    reason: { code: "nothing to weigh" },
    });
    assert.ok(Orders.build() instanceof Orders);
    assert.deepEqual(
    shipping
    .instance()
    .descriptor.steps()
    .map((described) => described.name),
    ["charge", "ship"],
    );

    function refunded(contributions: Contributions): string {
    return contributions.read("refunded", string).option().unwrap();
    }

    Type Parameters

    • T

    Parameters

    Returns RescueOutcome

  • Starts putting a rescue step of this class together for a step's builder, as Refund.builder().

    A rescue step keeps no static data, refused as WithoutStaticData says: at compile time, or else when the builder starts.

    import assert from "node:assert/strict";
    import { type Contributor, contributor } from "itinera/journey";
    import { None, type Option, Some } from "itinera/option";
    import {
    failureReason,
    fromRescuedStep,
    input,
    type Outcome,
    type Reason,
    RescueStep,
    rescueStep,
    type RescueOutcome,
    type RescueStepBuilder,
    type RescueStepClass,
    Step,
    step,
    } from "itinera/step";
    import { Contributions } from "itinera/testing";
    import { withStep, Workflow, workflow } from "itinera/workflow";
    import { integer, optional, string } from "itinera/value";

    @rescueStep()
    class Refund extends RescueStep {
    constructor(
    @input("order", string) readonly order: string,
    @fromRescuedStep("charged", optional(integer))
    readonly charged: Option<number>,
    @failureReason({ optional: true }) readonly reason: Option<Reason>,
    ) {
    super();
    }

    override run(@contributor() contributor: Contributor): RescueOutcome {
    const why = this.reason.isSome() ? this.reason.value.code : "a throw";
    contributor.contribute(
    "refunded",
    `${this.order}: ${String(this.charged.unwrapOr(0))} after ${why}`,
    );
    return this.succeed();
    }
    }

    @step()
    class Charge extends Step {
    override run(): Outcome {
    return this.succeed();
    }
    }

    class Weigh extends RescueStep {
    constructor(readonly grams: number) {
    super();
    }

    override run(): RescueOutcome {
    return this.grams > 0 ? this.succeed() : this.fail("nothing to weigh");
    }
    }

    const rescue: RescueStepClass = Refund;

    @workflow({ name: "orders" })
    @withStep(Charge, { name: "charge", rescue })
    class Orders extends Workflow {}

    const weighing: RescueStepBuilder<typeof Weigh> = Weigh.builder().needs(
    fromRescuedStep("grams", integer),
    );
    const shipping = Orders.builder()
    .step(Charge.builder("ship").rescue(weighing))
    .build();

    const refund = new Refund("A-1", Some(40), Some({ code: "declined" }));
    const unknown = new Refund("B-2", None, None);

    const refunding = new Contributions();
    const guessing = new Contributions();

    assert.deepEqual(refund.run(refunding.contributor), { kind: "success" });
    assert.equal(refunded(refunding), "A-1: 40 after declined");
    unknown.run(guessing.contributor);
    assert.equal(refunded(guessing), "B-2: 0 after a throw");
    assert.deepEqual(new Weigh(0).run(), {
    kind: "failure",
    reason: { code: "nothing to weigh" },
    });
    assert.ok(Orders.build() instanceof Orders);
    assert.deepEqual(
    shipping
    .instance()
    .descriptor.steps()
    .map((described) => described.name),
    ["charge", "ship"],
    );

    function refunded(contributions: Contributions): string {
    return contributions.read("refunded", string).option().unwrap();
    }

    Type Parameters

    Parameters

    Returns RescueStepBuilder<C>

    Error if the class keeps static data.