fix(frontend): generate MSW mocks that satisfy their Zod schemas

This commit is contained in:
shira committed 2026-10-06 11:23:52 +03:00
1 parent ad4bb94426
commit 1aa505c743
18 files changed
+1439 -133

No files matched your search

+9 -1
View File
@@ -9,7 +9,7 @@ A React and TypeScript SPA built with Vite and organized using FSD. The interfac
- `src/widgets/` holds shells: `public-shell`, `candidate-shell`, `employer-shell`, plus the technical `system-status` widget.
- `src/features/` and `src/entities/` are still empty.
- `src/shared/ui/` holds design tokens, fonts, logos, and primitives; `src/shared/config/` holds route paths; `src/shared/api/` holds the API client with generated endpoints, Zod schemas, and MSW mocks.
- `codegen/orval.config.ts` configures generation from the shared OpenAPI contract.
- `codegen/orval.config.ts` configures generation from the shared OpenAPI contract; `codegen/mock-patterns.ts` is the step after Orval that fixes MSW mocks of strings with a `pattern` (see [API client generation](#api-client-generation)).
- `scripts/` checks FSD boundaries and Orval compatibility with the OpenAPI 3.2 fixture.
- `e2e/` holds Playwright specs and `playwright.config.ts` configures them; they run only against a Compose stack.
@@ -35,6 +35,14 @@ Generated Orval hooks call the shared same-origin `apiRequest` client, so the `Q
- `staleTime` of 30 s; window focus does not refetch, and screens that need fresh data opt in per query;
- mutations do not retry until the API supports `Idempotency-Key`.
## API client generation
`make -C frontend generate` runs Orval with `codegen/orval.config.ts` and then `codegen/mock-patterns.ts`. For a string with a `pattern`, Orval 8.40 writes `faker.helpers.fromRegExp(pattern)` into the MSW mock: faker prints the `^` and `$` anchors and `(?:...)` groups as plain text, and Orval ignores `minLength` and `maxLength` when a `pattern` is present. The second step replaces such calls with `fakeFromPattern(pattern, {minLength, maxLength})` from `src/shared/api/mock-pattern.ts`. It takes the length bounds from the OpenAPI contract by the name of the property that holds the call and by the pattern. The step fails when a call has an unexpected form, when the contract has no such property with that pattern, or when the matching schemas disagree on the length bounds. It leaves its own output unchanged, so generation is reproducible and `make check-generated` stays stable. Enums in the mocks are written as literals (`enumGenerationType: "union"`): `Object.values` of a `zod.enum` returns Zod internals, not the values.
`fakeFromPattern` parses a regular expression subset (characters, escapes, classes, groups, alternatives, quantifiers, and `^` and `$` at the ends), picks a length within the pattern and the bounds, and builds the string with the faker generator, so the same seed gives the same data. Every value is checked with `new RegExp(pattern, "u")` and the length bounds; a pattern outside the subset (for example, a lookahead or a back reference) or unreachable bounds throw an error.
`src/shared/api/mock-validity.test.ts` calls every `get*ResponseMock` factory in `endpoints/*/*.msw.ts` with 25 faker seeds and checks the result with the generated Zod response schema. Orval's Zod schemas do not check `uniqueItems`, so the test does not cover that constraint; the response arrays with `uniqueItems` currently hold enum values, and the mock picks them with `faker.helpers.arrayElements` without repeats.
## Tokens and theme
Tokens live in `shared/ui/theme/tokens.css`: palette, semantic colors, type scale, spacing in 4px steps, radii, lines, focus ring, content widths, and motion. Breakpoints are fixed in media queries: 360px minimum, 768px tablet (`48rem`), 1024px desktop navigation (`64rem`), and 1280px verified wide layout.