3.7 KiB
3.7 KiB
API-Centric CLI Pattern (Skill Blueprint)
Goal
Define one final implementation path for product logic:
- API = single source of truth for business logic
- CLI = passthrough client to API only
- Tests = mandatory, with mocks, for both API behavior and CLI contracts
Use this pattern when building or refactoring any web app that exposes both a UI and CLI.
Non-Negotiable Rules
- All business logic lives in API/server code.
- CLI scripts must not implement decision logic, validation rules, or data model behavior.
- CLI scripts only:
- parse args
- resolve user-friendly identifiers (if needed)
- call API endpoints
- format output/errors
- If CLI needs behavior that API does not expose, add it to API first.
- No direct DB calls from CLI.
- Every change requires tests.
- Every CLI test suite must run against a mocked API transport.
Architecture Contract
Source of truth
- Domain rules: API
- State transitions: API
- Date/sprint logic: API/shared server module
- Persistence: API + data layer
- Auth/session policy: API
Client responsibilities
- Web UI: call API, render state
- CLI: call API, render terminal output
- Both are clients of the same backend contract
Refactor Pattern (DB-calling CLI -> API passthrough CLI)
- Inventory CLI commands and all direct DB access.
- Create/confirm matching API endpoints.
- Move duplicated logic into API modules.
- Rewrite CLI commands to call API endpoints only.
- Keep one shared API client helper in CLI (
api_call). - Remove DB env vars/credentials from CLI.
- Add regression tests before removing old paths.
- Delete dead logic after tests pass.
Testing Standard
Required layers
- API unit tests
- domain logic (date windows, status transitions, validation, selectors)
- API route/integration tests
- request/response shape, status codes, error handling
- CLI contract tests with mocked API transport
- assert commands hit correct endpoints/methods/payload paths
- assert cookie/session handling
- assert no direct DB references remain
Mock requirements for CLI tests
- Replace network transport (for shell CLIs, mock
curlinPATH). - Log request triples:
METHOD URL PAYLOAD. - Return deterministic fixtures per endpoint.
- Fail test on unhandled endpoint calls.
- Assert expected calls occurred.
Definition of done for a refactor
- All CLI commands route through API.
- No direct DB references in CLI codebase.
- Shared business logic exists in API/server modules only.
- Tests pass locally and in CI.
- API contract changes are documented.
Anti-Patterns to Block
- CLI reads DB tables directly "for convenience"
- CLI re-implements sprint/task/business status logic
- API and CLI each have separate versions of the same selector/validator
- "Temporary" bypasses that never get removed
Recommended Repository Layout
src/
app/api/...
lib/server/domain/...
scripts/
lib/api_client.sh
task.sh
project.sh
sprint.sh
tests/
refactor-cli-api.sh
*.test.ts
PR Checklist (copy/paste)
- No business logic added to CLI.
- Needed behavior implemented in API first.
- CLI commands call API endpoints only.
- Direct DB access removed from CLI.
- Unit tests added/updated for moved logic.
- CLI mock-contract tests added/updated.
- Unhandled endpoint calls fail tests.
- CI includes the refactor test suite.
Skill Conversion Notes
If converting this into a reusable skill:
- Skill name suggestion:
api-cli-passthrough. - Put these rules in
SKILL.md. - Include reusable test harness scripts under
scripts/. - Include endpoint/contract references under
references/. - Trigger when user asks to build/refactor web API + CLI with no logic duplication.