Skip to content

feat(mock): add the mock generate command - #484

Merged
Shield-Jaguar merged 3 commits into
feat/mock-serverfrom
feat/mock-generate
Nov 27, 2026
Merged

Shield-Jaguar merged 3 commits into
feat/mock-serverfrom
feat/mock-generate

Conversation

@Shield-Jaguar

@Shield-Jaguar Shield-Jaguar commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Adds apimatic mock generate, stacked on #474. It sends the project's build to codegen, waits for the run, downloads the stub files and writes them to the folder apimatic mock serve reads. In a project directory the two commands work with no flags:

apimatic mock generate
apimatic mock serve

The endpoint it calls does not exist yet. The command is written against the planned /mock contract, which has the shape of /plugin, and is hidden until that endpoint is live, so a release cannot ship a command that always fails.

What it does

  • Uploads the build as plugin generate does: a zip of src/, to POST /mock. It asks for no language and needs no plugin block or metadata in apimatic.json.
  • Waits for the run on GET /mock/{id}/status with the shared pollUntilCompleted, then downloads GET /mock/{id}/download.
  • Checks the download before it writes. The zip is unpacked in a temporary folder and loaded with the loader mock serve uses. Stub files the mock would refuse are reported problem by problem, and the stubs directory is left as it was. So is a run that fails, and a download without a stub.
  • Replaces, never merges. mock serve loads every file in the folder, so one left from the last set would be served beside the new one. Only a .git folder is kept.
  • Asks before it replaces a destination that is not empty; --force skips the question.

Flags: --input (the project directory), --destination (default stubs in the project directory, so --input X writes to X/stubs), --force, --auth-key. Errors use the wording of plugin generate.

How it is built

  • It mirrors plugin generate layer by layer: commands/mock/generate.ts, actions/mock/generate.ts, prompts/mock/generate.ts, infrastructure/services/stub-generation-service.ts, with types/stubs-context.ts for the stubs directory.
  • Everything the contract fixes is in stub-generation-service.ts: the three routes, and the place of the stub files in the download (STUB_FILES_IN_DOWNLOAD). A change on the codegen side is an edit to that one file.
  • STUBS_DIRECTORY_NAME in types/project-layout.ts is now the one name both commands share; mock serve's default is built from it.
  • docs/mock/generator-handoff.md gains section 6, which states the contract as the CLI reads it. .ai/plans/mock-server.md records the work as phase 8, with what to do when the endpoint is live.

How it was checked

  • pnpm build, pnpm lint and pnpm test pass locally (3917 passing, 93 pending). 68 of the tests are new.
  • The service's tests run against a local HTTP stand-in for the endpoint (test/helpers/stub-generation-endpoint.ts), as plugin-service.test.ts does for /plugin.
  • One action test runs the whole path: the real service against the stand-in, the Spec S stub set as the download, then the mock host started on the folder it wrote (84 stubs, /health answers 200).
  • The built CLI was run by hand against a stand-in: mock generate with only an auth key, then mock serve with no stubs flag, served the generated set; a second run with --force removed a file left from the first.
  • Four mutations (a wrong status route, a wrong layout constant, a merge in place of the replace, the loader check skipped) each made tests fail.

Not run: the real endpoint, since there is none, and the overwrite question in a real terminal.

For the reviewer

  • Assumptions about the endpoint, each one line to change: the names of the three routes, and stub files at the root of the zip. Codegen's ADR-0036 leaves their folder to the packaging change.
  • Also not known until the endpoint exists: its status values, the keys of a validation error, and that the gateway answers a finished run with 302 on this route as it does for /plugin.
  • The origin sent is APIMATIC CLI mock:generate, since oclif spells the command's id with a colon, as plugin generate sends plugin:generate.
  • Stricter than plugin generate in two places. The overwrite question counts hidden files, because the replace removes them. A run that returns no stub is a failure and leaves the folder as it was.
  • The download is received whole, not as a stream. A connection that drops or stalls part-way is then one more failed request, reported with the wording of an unexpected error, and the stubs directory is left as it was. plugin generate and sdk generate stream theirs, so the same break throws while they save it; they are not changed here.
  • stubs is not added to the folders quickstart puts in a project's .gitignore. Whether it should be is left for when the command is shown.
  • To show the command once the endpoint is live, follow the list at the end of the plan's phase 8 findings: run it against the live endpoint, remove hidden, widen the mock topic's description, run pnpm readme.

🤖 Generated with Claude Code

Shield-Jaguar and others added 3 commits November 20, 2026 11:22
apimatic mock generate zips the project's src folder, sends it to
codegen's /mock endpoint, waits for the run and replaces the stubs
directory with the stub files it downloads. The default destination is
the folder mock serve reads by default, so the two commands work in a
project directory with no flags.

The download is unpacked and loaded as mock serve would load it before
the stubs directory is touched, so a run that fails or returns stub
files the mock would refuse leaves the last set in place. The three
routes and the layout of the download are stated in
stub-generation-service.ts alone.

The endpoint does not exist yet, so the command is hidden and its tests
run against a local stand-in for it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
docs/mock/generator-handoff.md gains a section on the three /mock routes
the command calls, the statuses it reads, the layout it expects in the
download and the two points still open on the codegen side. The plan
records phase 8: what was built, how it differs from plugin generate,
what could not be run without the endpoint and what to do when it is
live. CONTEXT.md says what mock generate does to the stubs directory.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
mock generate took the download as a stream, so a connection that
dropped or stalled after the headers failed while the action saved it,
and the error was thrown out of the command. The service now receives
the download whole: a break is one more failed request, reported like
the others, and the stubs directory keeps the last set.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

@Shield-Jaguar
Shield-Jaguar merged commit 5a0e2a4 into feat/mock-server Nov 27, 2026
2 checks passed
@Shield-Jaguar
Shield-Jaguar deleted the feat/mock-generate branch November 27, 2026 12:46
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.

1 participant