You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AGENTS.md
+48-14Lines changed: 48 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,7 @@ This document provides essential context for AI coding assistants (Claude, GitHu
9
9
1. This file (AGENTS.md) - Project overview and key patterns
10
10
2.`CLAUDE.md` - Detailed guidelines for AI assistants
11
11
3.`README.md` - Setup and deployment instructions
12
+
4.`docs/mocks.md` - MSW auto-mocker, fixtures, and dev server
12
13
13
14
## Project Summary
14
15
@@ -36,10 +37,11 @@ This document provides essential context for AI coding assistants (Claude, GitHu
36
37
## Core Principles
37
38
38
39
1.**Server Components First** - Use `'use client'` only when necessary
39
-
2.**Generated API Client** - Never write manual fetch logic, use hey-api hooks
40
-
3.**Async/Await Only** - No `.then()` promise chains
41
-
4.**🚫 NEVER USE `any`** - STRICTLY FORBIDDEN. Use `unknown` with type guards or proper types
42
-
5.**Stateless Auth** - JWT tokens, no server-side sessions
40
+
2.**Generated API Client** - Never write manual fetch logic, use hey-api functions in server actions
41
+
3.**Server Actions for API Calls** - Client components fetch data via server actions, not direct API calls
42
+
4.**Async/Await Only** - No `.then()` promise chains
43
+
5.**🚫 NEVER USE `any`** - STRICTLY FORBIDDEN. Use `unknown` with type guards or proper types
44
+
6.**Stateless Auth** - JWT tokens, no server-side sessions
43
45
44
46
## ⚠️ Before Suggesting Code
45
47
@@ -68,6 +70,8 @@ src/
68
70
├── generated/ # hey-api output (DO NOT EDIT)
69
71
└── hooks/ # Custom React hooks
70
72
73
+
src/mocks/ # MSW auto-mocker, handlers, fixtures, and dev server
74
+
71
75
dev-auth/ # Development OIDC mock
72
76
helm/ # Kubernetes deployment
73
77
scripts/ # Build scripts
@@ -78,6 +82,7 @@ scripts/ # Build scripts
78
82
```bash
79
83
# Development
80
84
pnpm dev # Start dev server + OIDC mock
85
+
pnpm mock:server # Start standalone MSW mock server (default: http://localhost:9090)
81
86
pnpm dev:next # Start only Next.js
82
87
pnpm oidc # Start only OIDC mock
83
88
@@ -91,6 +96,14 @@ pnpm test # Run tests
91
96
pnpm generate-client # Regenerate from backend API
92
97
```
93
98
99
+
## Mocking & Fixtures
100
+
101
+
- Schema-based mocks are generated automatically. To create a new mock for an endpoint, run a Vitest test (or the app in dev) that calls that endpoint. The first call writes a fixture under `src/mocks/fixtures/<sanitized-path>/<method>.ts`.
102
+
- To adjust the payload, edit the generated fixture file. Prefer this over adding a non-schema mock when you only need more realistic sample data.
103
+
- Non-schema mocks live in `src/mocks/customHandlers` and take precedence over schema-based mocks. Use these for behavior overrides or endpoints without schema.
104
+
105
+
- Global test setup: Add common mocks to `vitest.setup.ts` (e.g., `next/headers`, `next/navigation`, `next/image`, `sonner`, auth client). Before copying a mock into a test file, check if it can be centralized globally. Reset all mocks globally between tests.
-**Never edit generated files** - They are regenerated from OpenAPI spec and changes will be lost
72
+
-**Use server actions for all API calls** - Client components should not call the API directly
71
73
- API client regenerated from OpenAPI spec via custom script
72
-
- Type-safe API calls with automatic loading/error states
74
+
- Type-safe API calls with proper error handling
73
75
74
-
**Why**: Generated client ensures type safety, eliminates manual state management, and stays in sync with backend API.
76
+
**Why**: Generated client ensures type safetyand stays in sync with backend API. Server-only API access keeps the backend URL secure and reduces client bundle size.
75
77
76
78
### 4. Async/Await Over Promises
77
79
@@ -137,7 +139,9 @@ pnpm generate-client:nofetch # Regenerate without fetching
137
139
### DON'T ❌
138
140
139
141
1.**🚫 Don't EVER use `any` type** - STRICTLY FORBIDDEN. Use `unknown` + type guards or proper types
140
-
2.**Don't edit generated files** - `src/generated/*` is auto-generated
142
+
2.**🚫 Don't EVER edit generated files** - `src/generated/*` is auto-generated and will be overwritten
143
+
- Generated files are overwritten on every `pnpm generate-client` run
144
+
- If you need to configure or extend the client, do it in your own files
141
145
3.**Don't use `'use client'` everywhere** - Only when necessary
142
146
4.**Don't create custom fetch logic** - Use hey-api hooks
143
147
5.**Don't use `.then()`** - Use async/await
@@ -198,11 +202,19 @@ pnpm generate-client:nofetch # Regenerate without fetching
- Auto-generates handlers from `swagger.json` and creates fixtures under `src/mocks/fixtures` on first run.
209
+
- Strict validation with Ajv + ajv-formats; fixtures are type-checked against `@api/types.gen` by default.
210
+
- Hand-written, non-schema mocks live in `src/mocks/customHandlers` and take precedence over schema-based mocks.
211
+
- Dev: `pnpm mock:server` starts a standalone HTTP mock on `http://localhost:9090` (configurable via `API_BASE_URL`). In dev, Next rewrites proxy `/registry/*` there; always use relative URLs like `/registry/v0.1/servers`.
212
+
- Regenerate by deleting specific fixture files.
213
+
- Create new fixtures by calling the desired endpoint in a Vitest test (or via the app in dev). The first call generates a TypeScript fixture file; customize the payload by editing that file instead of writing a new custom handler when you only need different sample data.
214
+
- Prefer global test setup for common mocks: add shared mocks to `vitest.setup.ts` (e.g., `next/headers`, `next/navigation`, `next/image`, `sonner`, auth client). Before adding a mock in a specific test file, check if it belongs in the global setup.
- Handlers: `src/mocks/handlers.ts` combines non-schema mocks and auto-generated mocks.
4
+
- Non-schema mocks: add hand-written handlers in `src/mocks/customHandlers/index.ts`. These take precedence over schema-based mocks.
5
+
- Auto-generated: `src/mocks/mocker.ts` reads `swagger.json` and creates fixtures under `src/mocks/fixtures` on first run.
6
+
- Validation: Loaded fixtures are validated with Ajv; errors log to console.
7
+
8
+
Usage
9
+
- Vitest: tests initialize MSW in `src/mocks/test.setup.ts`. Run `pnpm test`.
10
+
- Browser (optional): call `startWorker()` from `src/mocks/browser.ts` in your development entry point to mock requests in the browser.
11
+
- Standalone server (dev): `pnpm mock:server` starts an HTTP mock server at `http://localhost:9090` (configurable via `API_BASE_URL`). In dev, Next.js rewrites proxy `/registry/*` to this origin; use relative URLs like `/registry/v0.1/servers` from both client and server code.
12
+
13
+
Generating fixtures
14
+
- To create a new fixture for an endpoint, simply run a Vitest test (or the app in dev) that calls that endpoint. The auto‑mocker will generate `src/mocks/fixtures/<sanitized-path>/<method>.ts` on first use using schema‑based fake data.
15
+
- To customize the response, edit the generated TypeScript file. This is preferred over writing a non‑schema mock for simple data tweaks (e.g., replacing lorem ipsum with realistic text). Non‑schema mocks are intended for behavior overrides or endpoints without schema.
16
+
17
+
Regeneration
18
+
- Delete a fixture file to re-generate it on next request.
19
+
20
+
Failure behavior (always strict)
21
+
- If a schema is missing or faker fails, the handler responds 500 and does not write a placeholder.
22
+
- Invalid fixtures (including empty `{}` when the schema defines properties) respond 500.
23
+
24
+
Types
25
+
- Fixtures default to strict types. Generated modules import response types from `@api/types.gen` and use a `satisfies` clause to ensure compatibility.
26
+
- Make sure `tsconfig.json` includes: `"paths": { "@api/*": ["./src/generated/*"] }`.
0 commit comments