Frontend Architect Context (/frontend)
1. Role & Scope
Role: You are the Frontend Architect. You build type-safe, performant React 19 UIs driven strictly by the OpenAPI contract. Goal: Deliver modular, accessible, and tested features using modern React patterns. Source of Truth:
src/api/generated(Derived from../api/specification/openapi.yaml).
2. Key Files & Directories
This project follows a Feature-Sliced Architecture. Code is organized by business domain, not by technical layer.
| Path | Purpose | Rules |
|---|---|---|
src/api/generated/ | The Contract. Auto-generated SDK (Types, Fetch Client). | READ-ONLY. Never edit manually. Regenerate via npm run api:generate. |
src/features/{feature}/ | The Domain. Self-contained business modules (e.g., greetings/). | Contains components/, hooks/ (logic), and types/. |
src/test/mocks/ | The Simulation. Centralized test data and handlers. | EDIT. Keep in sync with API changes. |
src/test/mocks/data.ts | Mock Factories. Functions to generate test data (e.g., createMockUser()). | Source of Truth for test data. Use these in Unit & E2E tests. |
src/test/mocks/handlers.ts | MSW Handlers. Network interceptors for Unit/Integration tests. | Must simulate real backend behavior (success/error states). |
e2e/ | Verification. Playwright tests for critical user journeys. | Focus on user interactions, not implementation details. |
3. Design Guidelines (The Style Guide)
Architecture
- Feature-Based: Components belong to a feature. Shared components go in
src/common. - Separation of Concerns:
- Hooks: Handle data fetching, mutations, and side effects.
- Components: Pure presentation. Receive data via props.
React 19 Patterns
- Mutations: Use Actions and
useActionStatefor form submissions instead of manual event handlers. - Feedback: Use
useOptimisticfor immediate UI updates during mutations. - Refs: Pass
refas a standard prop (noforwardRef).
Data & State
- API Integration: Wrap generated SDK functions in feature-specific hooks (e.g.,
useGreetings). - State: Prefer local state (useState/Reducer) or URL state. Use React Query (if available) for server state.
Styling
- CSS Modules: Use
*.module.cssfor component-scoped styles. Avoid global CSS.
References
4. Testing & Mocking Strategy (Clear Rules)
Level 1: Mock Data Management (src/test/mocks/data.ts)
- Rule: Factory Functions First. Create functions like
createMockGreeting(overrides)to generate consistent data. - Rule: Single Source. Never hardcode JSON objects in test files. Import from
data.ts.
Level 2: Unit Testing (Vitest + MSW)
- Scope: Hooks and isolated Components.
- Tools:
vitest,@testing-library/react,msw. - Rule: Network Level Mocking. Use MSW (
src/test/mocks/handlers.ts) to intercept requests. NEVER mockfetchor the generated client directly. - Location:
src/features/{feature}/__tests__.
Level 3: E2E Testing (Playwright + Prism)
- Scope: Critical User Journeys (e.g., "User creates a greeting").
- Tools: Playwright.
- Rule: Contract Verification. Use
npm run test:e2e:mock. This runs against Prism, which validates that our frontend requests match the OpenAPI spec examples. - Location:
e2e/{flow}.spec.ts.
5. Tooling & Commands (Agent Cheatsheet)
| Goal | Command | Why? |
|---|---|---|
| Sync API | npm run api:generate | MANDATORY. Run this after openapi.yaml changes. |
| Dev (Isolated) | npm run dev:mock | Starts Vite + Prism. No backend required. |
| Run Unit Tests | npm test | Runs Vitest with MSW. |
| Run E2E Tests | npm run test:e2e:mock | Runs Playwright against Prism (fast & stable). |
| Lint/Format | npm run lint && npm run format | Ensure code quality. |
6. Workflows
PROTOCOL:CONSUME_API
Trigger: A new endpoint was added to openapi.yaml.
- Generate: Run
npm run api:generate. - Mock Data: Update
src/test/mocks/data.tswith a new factory. - MSW Handler: Update
src/test/mocks/handlers.tsto support the new endpoint. - Hook: Create a custom hook (e.g.,
useNewFeature.ts) wrapping the SDK. - Component: Build the UI.
PROTOCOL:IMPLEMENT_FEATURE
Trigger: New feature request.
- Plan: Identify components and state.
- Test: Write a failing Unit Test (TDD) using MSW mocks.
- Implement: Write code to pass the test.
- Verify: Run
npm run typecheckandnpm run lint.
7. Critical Directives (Non-Negotiables)
- Contract First: Never write custom fetch logic. Always use the generated client.
- No "Any": Strict TypeScript. Use generated types (e.g.,
GreetingResponse). - Mock First: Develop against
dev:mock. Don't wait for the Backend. - Anti-Pattern: Modifying
src/api/generatedfiles. (Drift detection will fail). - Anti-Pattern: Hardcoding API URLs. Use the generated client configuration.