Class StepMockAbstract

The base class of the step mocks stepMock makes: a step that plays its script, ending each attempt through an Ending, so that it ends exactly as a real step would, and keeps the inputs it was built with.

import assert from "node:assert/strict";
import { LocalExecutor } from "itinera/executor";
import { input } from "itinera/step";
import {
Ending,
RecordingReporter,
type StepMock,
stepMock,
} from "itinera/testing";
import { Workflow } from "itinera/workflow";
import { string } from "itinera/value";

class Orders extends Workflow {}

export default (async () => {
const charge = stepMock({
attempts: [Ending.abnormalTermination("gateway down"), Ending.success],
asynchronous: true,
});
const orders = Orders.builder()
.name("orders")
.step(charge.builder("charge").retryBudget(1))
.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 terminated abnormally: gateway down",
"charge retrying",
"charge attempt 2 succeeded",
"journey succeeded",
]);

const built: StepMock[] = [];
class Ship extends stepMock({ attempts: [Ending.success] }) {
constructor(...inputs: unknown[]) {
super(...inputs);
built.push(this);
}
}
const shipping = Orders.builder()
.name("shipping")
.step(Ship.builder("ship").needs(input("parcel", string)))
.build();

await new LocalExecutor().run(shipping.instance({ parcel: "box" }));

assert.deepEqual(
built.map((ship) => ship.inputs),
[["box"]],
);
})();

Hierarchy (View Summary)

Index

  • Builds the step mock that ends its attempt with ending, given inputs.

    Parameters

    • ending: Ending
    • asynchronous: boolean
    • inputs: readonly unknown[]

    Returns StepMock

inputs: readonly unknown[]

The inputs it was built with, in the order its constructor requests them: each input's value, or an Option of it for an optional input.

  • The outcome of an attempt that could not do its work, for a reason, or only its code. The reason's details are captured now, so changing them afterwards changes nothing; details that are not a value abort the journey with not a value once the attempt ends.

    import assert from "node:assert/strict";
    import { input, type Outcome, Step, step } from "itinera/step";

    @step()
    class Charge extends Step {
    constructor(@input("amount") private readonly amount: number) {
    super();
    }

    override run(): Outcome {
    if (this.amount === 0) {
    return this.skip();
    }
    if (this.amount < 0) {
    return this.skipBecause("refunds are not charged");
    }
    if (this.amount > 100) {
    return this.fail({
    code: "declined",
    message: "over the limit",
    details: { limit: 100 },
    });
    }
    return this.succeed();
    }
    }

    assert.deepEqual(new Charge(40).run(), { kind: "success" });
    assert.deepEqual(new Charge(0).run(), { kind: "skipped" });
    assert.deepEqual(new Charge(-5).run(), {
    kind: "skipped",
    reason: { code: "refunds are not charged" },
    });
    assert.deepEqual(new Charge(140).run(), {
    kind: "failure",
    reason: {
    code: "declined",
    message: "over the limit",
    details: { limit: 100 },
    },
    });

    Type Parameters

    • T

    Parameters

    Returns Fail

  • The outcome of a step that decided not to do its work: it does not run again in the journey, and what it contributed is discarded.

    import assert from "node:assert/strict";
    import { input, type Outcome, Step, step } from "itinera/step";

    @step()
    class Charge extends Step {
    constructor(@input("amount") private readonly amount: number) {
    super();
    }

    override run(): Outcome {
    if (this.amount === 0) {
    return this.skip();
    }
    if (this.amount < 0) {
    return this.skipBecause("refunds are not charged");
    }
    if (this.amount > 100) {
    return this.fail({
    code: "declined",
    message: "over the limit",
    details: { limit: 100 },
    });
    }
    return this.succeed();
    }
    }

    assert.deepEqual(new Charge(40).run(), { kind: "success" });
    assert.deepEqual(new Charge(0).run(), { kind: "skipped" });
    assert.deepEqual(new Charge(-5).run(), {
    kind: "skipped",
    reason: { code: "refunds are not charged" },
    });
    assert.deepEqual(new Charge(140).run(), {
    kind: "failure",
    reason: {
    code: "declined",
    message: "over the limit",
    details: { limit: 100 },
    },
    });

    Returns Skip

  • The outcome of a step that decided not to do its work, saying why, with a reason or only its code, captured as Step.fail captures it.

    import assert from "node:assert/strict";
    import { input, type Outcome, Step, step } from "itinera/step";

    @step()
    class Charge extends Step {
    constructor(@input("amount") private readonly amount: number) {
    super();
    }

    override run(): Outcome {
    if (this.amount === 0) {
    return this.skip();
    }
    if (this.amount < 0) {
    return this.skipBecause("refunds are not charged");
    }
    if (this.amount > 100) {
    return this.fail({
    code: "declined",
    message: "over the limit",
    details: { limit: 100 },
    });
    }
    return this.succeed();
    }
    }

    assert.deepEqual(new Charge(40).run(), { kind: "success" });
    assert.deepEqual(new Charge(0).run(), { kind: "skipped" });
    assert.deepEqual(new Charge(-5).run(), {
    kind: "skipped",
    reason: { code: "refunds are not charged" },
    });
    assert.deepEqual(new Charge(140).run(), {
    kind: "failure",
    reason: {
    code: "declined",
    message: "over the limit",
    details: { limit: 100 },
    },
    });

    Type Parameters

    • T

    Parameters

    Returns Skip

  • Starts putting a step of this class together for a workflow's builder, named name, as Charge.builder("charge").

    import assert from "node:assert/strict";
    import type { Event } from "itinera/event";
    import { type DataBag, dataBag } from "itinera/journey";
    import type { Reporter } from "itinera/report";
    import { type Outcome, Step } from "itinera/step";
    import { Workflow } from "itinera/workflow";
    import { integer } from "itinera/value";

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

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

    class Audit implements Reporter {
    readonly kinds: string[] = [];

    report(event: Event): void {
    this.kinds.push(event.kind);
    }
    }

    class Orders extends Workflow {
    constructor(readonly shop: string) {
    super();
    }

    orderId(data: DataBag): string {
    const number = data.read("number", integer).option();
    return `${this.shop}-${number.map(String).unwrapOr("unnumbered")}`;
    }
    }

    const orders = Orders.builder()
    .name("orders")
    .step(Charge.builder("charge").retryBudget(1).noRetryOnAbnormalTermination())
    .step(Ship.builder("ship"))
    .reporter(Audit)
    .idGenerator("orderId", dataBag())
    .constructWith("corner")
    .build();

    const instance = orders.instance({ number: 7 });
    assert.equal(instance.descriptor.name, "orders");
    assert.equal(instance.journeyId, "corner-7");
    assert.deepEqual(
    instance.descriptor.steps().map((described) => described.name),
    ["charge", "ship"],
    );
    assert.equal(instance.descriptor.steps()[0]?.retryBudget, 1);
    assert.equal(instance.descriptor.steps()[0]?.retryOnAbnormalTermination, false);

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

    Type Parameters

    Parameters

    Returns StepBuilder<C>

    Error if the class keeps static data, or if the name is empty.