Skip to content

fix(domain): Verify answers a stubbed call with an instance of its class - #3898

Open
creatifcoding wants to merge 1 commit into
alchemy-run:mainfrom
creatifcoding:fix/domain-verify-replay-class-errors
Open

creatifcoding wants to merge 1 commit into
alchemy-run:mainfrom
creatifcoding:fix/domain-verify-replay-class-errors

Conversation

@creatifcoding

Copy link
Copy Markdown
Contributor

Verify.model now runs a sequence whose external system answers with a class as a passing story. A declared error made with Domain.Rejection or Schema.TaggedError, or a success that is a Schema.Class, reaches the stub as an instance of that class. The story printed for the report, to paste into a test, makes the answer with new.

export class Receipt extends Schema.Class<Receipt>("Receipt")({ reference: Schema.String }) {}
export class Declined extends Domain.Rejection("Declined", {}) {}
export class NetworkDown extends Schema.TaggedError<NetworkDown>()("NetworkDown", {}) {}

export class Card extends Domain.ExternalSystem("Card", {
  charge: {
    args: { paymentId: Schema.String, amount: Schema.Int },
    success: Receipt,
    error: [Declined, NetworkDown],
  },
}) {}

yield* Verify.model(Checkout, {
  apis: [CheckoutApi],
  depth: 1,
  story: Story.make(Checkout, { layer: ChargeLive, stub: [Card] }),
});
  Run on the real model: 4 sequences the explorer found and 18 walks of commands picked at random, as stories
- ✗ the model does something else in 22
+ ✓ the model does what the explorer read in each

  story(
    Story.when(new Pay({ id: "A", amount: 0 })),
    Story.call(Card.charge, {}),
-   Story.fail(Card.charge, { _tag: "Declined" }),
+   Story.fail(Card.charge, new Declined({})),
    Story.then(Payment.ref("A"), new PaymentRequested({ amount: 0 }), new PaymentRefused({})),
  )

The explorer solves a class answer through its fields ("A class's own type is opaque to the solver: its fields are what an answer is made of"), and the story checks its answers the way it checks an adapter's. Nothing turned the fields back into an instance between the two, so the stub rejected every class answer Verify gave it: Card.charge failed with an error its error schema rejects / Expected Declined.

Each recorded call now keeps the schema its answer was picked from, and the answer is made again through it when the sequence is built:

const answerOf = (schema: unknown, value: unknown): unknown =>
  Option.getOrElse((schema as Schema.Top).makeOption(value as never), () => value);

The story script writes an instance of a schema class with new, and every other answer as before:

const answer = (value: unknown): string => {
  const cls: unknown =
    typeof value === "object" && value !== null
      ? Object.getPrototypeOf(value)?.constructor
      : undefined;
  if (!Schema.isSchema(cls)) return literal(value);
  const { _tag: _, ...fields } = value as { readonly _tag?: unknown };
  return `new ${(cls as unknown as { readonly name: string }).name}(${literal(fields)})`;
};

Struct answers stay as they are: makeOption on a struct gives back a plain object, so Shop's RiskUnavailable (Schema.TaggedStruct), FraudCheck's { risk } and Payments' { refundId } print and run as before. The report's "asks" lines show an instance by its class name, so for a class answer they now read → ✗ Declined and → Receipt { reference: "C" } instead of → ✗ { _tag: "Declined" } and → { reference: "C" }.

The new Checkout fixture has a card whose three answers are a Schema.Class, a Domain.Rejection and a Schema.TaggedError. Its test fails on main with the 22 runs above and passes with this change. A sentence in the verifying-models docs says how class answers are given.

#3868 (open) edits Script.ts around its clock lines (38 and 65), #3743 (open) edits ModelSearch.ts around 1906, 3577 and 3769, and #3607 (open) edits ModelSearch.ts around 261 and 3910. This change touches Script.ts at its imports (3), after made (145) and in the Answer case (166), and ModelSearch.ts at the Call interface (539-544), where a call is recorded (1712) and where a sequence is built (2843). No textual conflict is expected.

Closes #3896

@alchemy-version-bot

Copy link
Copy Markdown
Contributor

Install the packages built from this commit:

alchemy

pnpm install https://pkg.alchemy.run/alchemy/pr:3898:536e743
@alchemy.run (21)
pnpm install https://pkg.alchemy.run/@alchemy.run/better-auth/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/git/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/drizzle/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/acme/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/planetscale/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/neon/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/hetzner/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/railway/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/fly/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/cloudflare/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/gcp/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/aws/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/domain/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/kubernetes/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/docker/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/redis/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/sql/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/cloudflare-runtime/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/frontend-frameworks/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/node-utils/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/pkg/pr:3898:536e743
Transitive Dependencies (17)
pnpm install https://pkg.alchemy.run/@distilled.cloud/aws/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/acme/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/core/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/planetscale/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/neon/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/s3/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/hetzner/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/railway/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/fly-io/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/github/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/aws4signer/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/cloudflare/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/iceberg/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/gcp/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/effect-cluster/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@alchemy.run/rivet/pr:3898:536e743
pnpm install https://pkg.alchemy.run/@distilled.cloud/rivet/pr:3898:536e743

Published Dec 1, 2026, 1:22 AM UTC. Expires Dec 8, 2026, 1:22 AM UTC, extended while this pull request is open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(domain): Verify replays an external system's class answers as plain objects

1 participant