Class StepPolicyMockAbstract

The base class of the step policy mocks stepPolicyMock makes: a step policy with a method for each step hook, taking whatever its requests are answered with, which keeps every call and ends it as its script says, through the helpers of StepPolicy, as a real hook does. A builder names the methods of the hooks the policy defines, with their requests. A class extending it may override a hook to do more first, returning a promise of what the hook it overrides returns.

import assert from "node:assert/strict";
import { LocalExecutor } from "itinera/executor";
import { failureCause } from "itinera/policy";
import {
Ending,
HookEnding,
RecordingReporter,
type StepPolicyMock,
stepMock,
stepPolicyMock,
workflowPolicyMock,
} from "itinera/testing";
import { Workflow } from "itinera/workflow";

class Orders extends Workflow {}

export default (async () => {
const built: StepPolicyMock[] = [];
class Alarm extends stepPolicyMock({
onStepFailure: HookEnding.failWorkflow("declined"),
}) {
constructor() {
super();
built.push(this);
}
}
const audit = workflowPolicyMock();
const charge = stepMock({ attempts: [Ending.failure("card expired")] });
const orders = Orders.builder()
.name("orders")
.step(
charge
.builder("charge")
.policy(
Alarm.builder("alarm").onStepFailure("onStepFailure", failureCause()),
),
)
.policy(audit.builder("audit").onWorkflowFailure("onWorkflowFailure"))
.build();
const recorder = new RecordingReporter();

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

assert.ok(result.isFailed());
assert.deepEqual(recorder.path, [
"charge attempt 1 failed: card expired",
"charge given up: failure",
"journey failed at charge: declined",
]);
assert.deepEqual(
built.map((alarm) => alarm.calls),
[[{ hook: "on step failure", received: ["failure"] }]],
);
})();

Hierarchy (View Summary)

Index

"[roles]": unknown

The roles the policy requests. It exists only in the types, where a workflow it is attached to must provide them.

  • Starts putting a step policy of this class together, for a class declared without decorators, named name, or else after the class, as Audit.builder("audit").

    import assert from "node:assert/strict";
    import { type Contributor, contributor } from "itinera/journey";
    import {
    failureCause,
    fromStep,
    type OnFailure,
    type OnSuccess,
    type PolicyDescriptor,
    role,
    type StepFailureCause,
    StepPolicy,
    type StepPolicyBuilder,
    WorkflowPolicy,
    type WorkflowPolicyBuilder,
    } from "itinera/policy";
    import { Step, type Outcome } from "itinera/step";
    import { Workflow } from "itinera/workflow";
    import { integer } from "itinera/value";

    /** A role the shop provides: telling a customer something. */
    interface Messages {
    tell(message: string): void;
    }

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

    class Audit extends StepPolicy {
    audited(charged: number, contributing: Contributor): OnSuccess {
    contributing.contribute("audit", `charged ${String(charged)}`);
    return this.keepDefault();
    }

    failed(cause: StepFailureCause): OnFailure {
    return this.failWorkflow(cause);
    }
    }

    class Notify extends WorkflowPolicy<Messages> {
    paid(messages: Messages): void {
    messages.tell("paid");
    }
    }

    class Orders extends Workflow implements Messages {
    readonly told: string[] = [];

    tell(message: string): void {
    this.told.push(message);
    }
    }

    const audit: StepPolicyBuilder<typeof Audit> = Audit.builder("audit")
    .onStepSuccess("audited", fromStep("charged", integer), contributor())
    .onStepFailure("failed", failureCause());
    const notify: WorkflowPolicyBuilder<typeof Notify> = Notify.builder(
    "notify",
    ).onWorkflowSuccess("paid", role<Messages>());

    const orders = Orders.builder()
    .name("orders")
    .step(Charge.builder("charge").policy(audit))
    .policy(notify)
    .build();

    const descriptor = orders.instance().descriptor;
    const attached: readonly PolicyDescriptor[] =
    descriptor.steps()[0]?.policies() ?? [];

    assert.deepEqual(
    attached.map((policy) => policy.name),
    ["audit"],
    );
    assert.deepEqual(attached[0]?.hooks(), ["on step success", "on step failure"]);
    const built = attached[0].build();
    assert.ok(built instanceof Audit);
    assert.deepEqual(attached[0].call(built, "on step failure", ["failure"]), {
    kind: "FailWorkflow",
    reason: { code: "failure" },
    });
    assert.deepEqual(
    descriptor.policies().map((policy) => policy.name),
    ["notify"],
    );

    class Counter extends Workflow {}

    // @ts-expect-error Counter does not implement Messages, which notify requests
    Counter.builder().policy(notify);

    A step policy keeps no static data, which every attempt of every journey would share, refused as WithoutStaticData says: at compile time, or else when the builder starts.

    Type Parameters

    Parameters

    Returns StepPolicyBuilder<C>

    Error if the class keeps static data, if the name is empty, or if the class is declared with a policy's decorator.