export { F as Fold, L as LawBreak, S as Step, f as foldEvents, l as lawInvariant, a as lawTerminates, b as lawTrace } from '../../laws-I-3KrM0I.js'; import { e as DriveRoundsExceededError, d as DriveNoHandlerError, D as DriveOptions, a as DriveResult } from '../../drive-loop-C0XIs2TB.js'; export { b as DEFAULT_MAX_ROUNDS, c as DriveCtxArg, f as DriveTraceEntry, g as driveTraceOf } from '../../drive-loop-C0XIs2TB.js'; import * as effect_Cause from 'effect/Cause'; import * as effect_Types from 'effect/Types'; import { Arbitrary, Result, Effect } from 'effect'; import { b as Cmd, c as Sub, M as Machine } from '../../core-sEWVgnpI.js'; import { C as CellErrors, S as SubscribeOption, E as EffectInterpret, a as EffectSubscribe, I as InterpretServices, b as SubscribeServices } from '../../run-DhgptktR.js'; import '../../runtime-types-f231H8Gq.js'; import '@standard-schema/spec'; import '../../loop-CKqoteig.js'; declare const NoCmdsError_base: new = {}>(args: effect_Types.VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => effect_Cause.YieldableError & { readonly _tag: "NoCmdsError"; } & Readonly; /** * The machine has no `cmds` list, so there is no schema to draw an answer from. * * @experimental */ declare class NoCmdsError extends NoCmdsError_base<{ readonly message: string; }> { } declare const NotEffectSchemaError_base: new = {}>(args: effect_Types.VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => effect_Cause.YieldableError & { readonly _tag: "NotEffectSchemaError"; } & Readonly; /** * A Cmd's `ok` schema is not an Effect schema, so no answer can be drawn from * it. A Cmd made with `Cmd.define` from `@demlik/tea` over zod, or any other * Standard Schema, lands here. `cmdType` names that Cmd. * * @experimental */ declare class NotEffectSchemaError extends NotEffectSchemaError_base<{ readonly cmdType: string; readonly message: string; }> { } declare const NothingToDrawError_base: new = {}>(args: effect_Types.VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => effect_Cause.YieldableError & { readonly _tag: "NothingToDrawError"; } & Readonly; /** * `machine.cmds` is empty and no `outside` Msg was given, so every list drawn * would be empty. * * @experimental */ declare class NothingToDrawError extends NothingToDrawError_base<{ readonly message: string; }> { } /** * Why {@link arbSchemaGuidedSequence} drew no lists. * * @experimental */ type SchemaAnswersError = NoCmdsError | NotEffectSchemaError | NothingToDrawError; /** * What {@link arbSchemaGuidedSequence} takes beside the machine and its ctx. * * @experimental */ interface SchemaGuidedOptions { /** * The Msgs nothing sends a Cmd for, by Msg type: a user's click, a timer. A * Msg type here and in no Cmd's answers is drawn only from here. */ readonly outside?: { readonly [K in M["_tag"]]?: Arbitrary.Arbitrary>; }; /** * Caps the events drawn. Default 60. `checkEffect` and `sampleEffect` cap a * list at their `size` too, which defaults to 10, so pass a `size` of at least * this to draw lists this long. */ readonly maxLength?: number; /** * The saved state the fold starts from. Give `foldEvents` the same one, or it * reads the list from a different start. Default `null`. */ readonly loaded?: S | null; } /** * Draw Msg lists for `Arbitrary.checkEffect` in which every answer answers a Cmd * that was sent and not yet answered, and comes from that Cmd's own schema: an * `ok` answer is drawn from its `schema.ok`, an `err` answer carries one of its * `errTags`. Edit a schema and what is drawn changes with it, with no change to * the test. * * Each list is drawn as blind events folded through the machine. An answer event * names one waiting Cmd and is minted with that Cmd's def, `def.ok(cmd, value)` * or `def.err(cmd, { _tag })`. With nothing waiting the answer is dropped, and an * `outside` Msg the event drew goes in its place, or nothing when there is no * `outside`. An event whose Msg the state has no cell for is dropped too, and a * refused answer leaves its Cmd waiting. A Cmd no def in `machine.cmds` builds is * never answered. * * It runs no check. Fold each list with `foldEvents` and judge it with a law, * `lawInvariant`, `lawTerminates` or `lawTrace`, inside `Arbitrary.checkEffect`. * A list it finds there shrinks by deleting events and shrinking them, and what * is left still folds by the same rule. With `outside` given and every Msg * accepted in every state, each event means one Msg, so a shrunk list is never * longer than the one first found. A dropped event breaks that: deleting an * answer before it can let it back in. * * A `machine.cmds` that names one Cmd twice throws `CmdNameCollisionError`, as it * does in `run` and `drive`: it is a wrong program, not a machine to draw for. * * It returns a failed `Result`, never an empty arbitrary, on a machine with no * `machine.cmds` ({@link NoCmdsError}), on a Cmd whose `ok` schema is not an * Effect schema ({@link NotEffectSchemaError}, naming it), and when there is * nothing at all to draw ({@link NothingToDrawError}). * * @example * ```ts * import type { CmdOf, Machine, Settled } from "@demlik/tea"; * import { Cmd } from "@demlik/tea/effect"; * import { * arbSchemaGuidedSequence, * foldEvents, * lawInvariant, * } from "@demlik/tea/testing/effect"; * import { Arbitrary, Effect, Result, Schema } from "effect"; * * const fetchCount = Cmd.make("fetch_count", { * input: Schema.Struct({}), * ok: Schema.Struct({ n: Schema.Int }), * err: ["offline"], * }); * type Msg = { _tag: "start" } | Settled; * declare const machine: Machine< * { readonly total: number }, * Msg, * CmdOf, * never, * object * >; * * const lists = Result.getOrThrow( * arbSchemaGuidedSequence(machine, {}, { * outside: { start: Arbitrary.Constant({ _tag: "start" as const }) }, * }), * ); * const result = await Effect.runPromise( * Arbitrary.checkEffect( * lists, * (msgs) => * lawInvariant(foldEvents(machine, {}, null, msgs), ({ next }) => * Number.isFinite(next.total), * ) === null, * { runs: 500, size: 60 }, * ), * ); * ``` * * @experimental */ declare function arbSchemaGuidedSequence(machine: Machine, ctx: NoInfer, opts?: SchemaGuidedOptions, M>): Result.Result, SchemaAnswersError>; /** * The Effect `drive`'s options: the Promise `drive`'s, plus `subscribe` — the * runners the engine's `run` takes, required exactly when `run` requires them. */ type EffectDriveOptions = DriveOptions & SubscribeOption; /** The failures a drive ends with: the loop's own, and a hand-written cell's. */ type EffectDriveError = DriveRoundsExceededError | DriveNoHandlerError | CellErrors; /** * Drive `machine` from `initial` through `msg` against the REAL Effect * `interpret` map — the one a host hands the Effect engine's `run` — and the * Subs the machine wants, feeding every Msg back until the machine goes quiet. * Yields the settled state together with the whole history, `{ state, trace }`. * * The rounds, the trace, `maxRounds` and `clock` are the Promise `drive`'s * (`@demlik/tea/testing/promise`). What is the Effect engine's: * * - a cell returns an Effect. A `Cmd.define`d Cmd's success mints `_ok` and * its declared failure `_err`; a hand-written Cmd's success is its * follow-up — a Msg, a list of them folded in order, or nothing (#324) — * and its failure fails the drive, typed. A defect dies. A cell takes no * `dispatch`, so a defined Cmd's handler cannot send its own `_ok` / * `_err`: the engine's rule holds by construction; * - the services the cells and runners read are this Effect's requirements, * so a test provides them the way a host does: `Effect.provide(layer)`; * - the Subs the machine wants run, through `opts.subscribe` or the engine's * built-in runner, started and stopped after every fold. Their Msgs are * folded once no Cmd's follow-up is pending. The drive settles once no Msg * is pending and no Sub is still running, so hand it a runner whose Stream * ends — a Sub that never ends keeps it waiting. A Stream that fails with * an error it did not map to a Msg dies, as it stops the engine's run. * * Interrupting the drive interrupts every cell and Sub still running. * * ```ts * const { state, trace } = await Effect.runPromise( * drive(machine, initial, { _tag: "go" }, { * fetch: (cmd) => * Effect.gen(function* () { * const api = yield* Api; * return yield* api.get(cmd.id); * }), * }).pipe(Effect.provide(ApiTest)), * ); * ``` * * Fails with {@link DriveRoundsExceededError} when the machine is still * emitting work after `opts.maxRounds` rounds, {@link DriveNoHandlerError} * for a Cmd no cell answers, or a hand-written cell's own failure — each with * the partial trace, readable via `driveTraceOf`. */ declare function drive = EffectInterpret, B extends EffectSubscribe = EffectSubscribe>(machine: Machine, initial: S, msg: M, interpret: I, ...[opts]: Record extends EffectDriveOptions ? [opts?: EffectDriveOptions] : [opts: EffectDriveOptions]): Effect.Effect, EffectDriveError, InterpretServices | SubscribeServices>; export { DriveNoHandlerError, DriveOptions, DriveResult, DriveRoundsExceededError, type EffectDriveError, type EffectDriveOptions, NoCmdsError, NotEffectSchemaError, NothingToDrawError, type SchemaAnswersError, type SchemaGuidedOptions, arbSchemaGuidedSequence, drive };