Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

contract-testing.md 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. # Contract Testing Essentials (Pact)
  2. ## Principle
  3. Contract testing validates API contracts between consumer and provider services without requiring integrated end-to-end tests. Store consumer contracts alongside integration specs, version contracts semantically, and publish on every CI run. Provider verification before merge surfaces breaking changes immediately, while explicit fallback behavior (timeouts, retries, error payloads) captures resilience guarantees in contracts.
  4. > **Pact.js Utils Note**: When `tea_use_pactjs_utils` is enabled, prefer the patterns in the `pactjs-utils-*.md` fragments over the raw Pact.js patterns shown below. The pactjs-utils library eliminates boilerplate for provider states, verifier configuration, and request filters. See `pactjs-utils-overview.md` for the decision tree.
  5. ## Rationale
  6. Traditional integration testing requires running both consumer and provider simultaneously, creating slow, flaky tests with complex setup. Contract testing decouples services: consumers define expectations (pact files), providers verify against those expectations independently. This enables parallel development, catches breaking changes early, and documents API behavior as executable specifications. Pair contract tests with API smoke tests to validate data mapping and UI rendering in tandem.
  7. > **Recommended**: When `tea_use_pactjs_utils` is enabled, use `@seontechnologies/pactjs-utils` utilities instead of the manual patterns below. The library handles JsonMap conversion, verifier configuration, and request filter assembly automatically. See the `pactjs-utils-overview.md`, `pactjs-utils-consumer-helpers.md`, `pactjs-utils-provider-verifier.md`, and `pactjs-utils-request-filter.md` fragments for the simplified approach.
  8. ## Pattern Examples
  9. ### Example 1: Pact Consumer Test (Frontend → Backend API)
  10. **Context**: React application consuming a user management API, defining expected interactions.
  11. **Implementation**:
  12. ```typescript
  13. // tests/contract/user-api.pact.spec.ts
  14. import { PactV3, MatchersV3 } from '@pact-foundation/pact';
  15. import { getUserById, createUser, User } from '@/api/user-service';
  16. const { like, eachLike, string, integer } = MatchersV3;
  17. /**
  18. * Consumer-Driven Contract Test
  19. * - Consumer (React app) defines expected API behavior
  20. * - Generates pact file for provider to verify
  21. * - Runs in isolation (no real backend required)
  22. */
  23. const provider = new PactV3({
  24. consumer: 'user-management-web',
  25. provider: 'user-api-service',
  26. dir: './pacts', // Output directory for pact files
  27. logLevel: 'warn',
  28. });
  29. describe('User API Contract', () => {
  30. describe('GET /users/:id', () => {
  31. it('should return user when user exists', async () => {
  32. // Arrange: Define expected interaction
  33. await provider
  34. .given('user with id 1 exists') // Provider state
  35. .uponReceiving('a request for user 1')
  36. .withRequest({
  37. method: 'GET',
  38. path: '/users/1',
  39. headers: {
  40. Accept: 'application/json',
  41. Authorization: like('Bearer token123'), // Matcher: any string
  42. },
  43. })
  44. .willRespondWith({
  45. status: 200,
  46. headers: {
  47. 'Content-Type': 'application/json',
  48. },
  49. body: like({
  50. id: integer(1),
  51. name: string('John Doe'),
  52. email: string('john@example.com'),
  53. role: string('user'),
  54. createdAt: string('2025-01-15T10:00:00Z'),
  55. }),
  56. })
  57. .executeTest(async (mockServer) => {
  58. // Act: Call consumer code against mock server
  59. const user = await getUserById(1, {
  60. baseURL: mockServer.url,
  61. headers: { Authorization: 'Bearer token123' },
  62. });
  63. // Assert: Validate consumer behavior
  64. expect(user).toEqual(
  65. expect.objectContaining({
  66. id: 1,
  67. name: 'John Doe',
  68. email: 'john@example.com',
  69. role: 'user',
  70. }),
  71. );
  72. });
  73. });
  74. it('should handle 404 when user does not exist', async () => {
  75. await provider
  76. .given('user with id 999 does not exist')
  77. .uponReceiving('a request for non-existent user')
  78. .withRequest({
  79. method: 'GET',
  80. path: '/users/999',
  81. headers: { Accept: 'application/json' },
  82. })
  83. .willRespondWith({
  84. status: 404,
  85. headers: { 'Content-Type': 'application/json' },
  86. body: {
  87. error: 'User not found',
  88. code: 'USER_NOT_FOUND',
  89. },
  90. })
  91. .executeTest(async (mockServer) => {
  92. // Act & Assert: Consumer handles 404 gracefully
  93. await expect(getUserById(999, { baseURL: mockServer.url })).rejects.toThrow('User not found');
  94. });
  95. });
  96. });
  97. describe('POST /users', () => {
  98. it('should create user and return 201', async () => {
  99. const newUser: Omit<User, 'id' | 'createdAt'> = {
  100. name: 'Jane Smith',
  101. email: 'jane@example.com',
  102. role: 'admin',
  103. };
  104. await provider
  105. .given('no users exist')
  106. .uponReceiving('a request to create a user')
  107. .withRequest({
  108. method: 'POST',
  109. path: '/users',
  110. headers: {
  111. 'Content-Type': 'application/json',
  112. Accept: 'application/json',
  113. },
  114. body: newUser,
  115. })
  116. .willRespondWith({
  117. status: 201,
  118. headers: { 'Content-Type': 'application/json' },
  119. body: like({
  120. id: integer(2),
  121. name: string('Jane Smith'),
  122. email: string('jane@example.com'),
  123. role: string('admin'),
  124. createdAt: string('2025-01-15T11:00:00Z'),
  125. }),
  126. })
  127. .executeTest(async (mockServer) => {
  128. const createdUser = await createUser(newUser, {
  129. baseURL: mockServer.url,
  130. });
  131. expect(createdUser).toEqual(
  132. expect.objectContaining({
  133. id: expect.any(Number),
  134. name: 'Jane Smith',
  135. email: 'jane@example.com',
  136. role: 'admin',
  137. }),
  138. );
  139. });
  140. });
  141. });
  142. });
  143. ```
  144. **package.json scripts** (when using pactjs-utils conventions, prefer `test:pact:consumer` naming — see `pact-consumer-framework-setup.md`):
  145. ```json
  146. {
  147. "scripts": {
  148. "test:pact:consumer": "./scripts/check-pact-determinism.sh 'npm run test:pact:consumer:run' 3 ./pacts",
  149. "test:pact:consumer:run": "vitest run --config vitest.config.pact.ts",
  150. "publish:pact": ". ./scripts/env-setup.sh && ./scripts/publish-pact.sh"
  151. }
  152. }
  153. ```
  154. **Key Points**:
  155. - **Consumer-driven**: Frontend defines expectations, not backend
  156. - **Matchers (Postel's Law)**: Use `like`, `string`, `integer` matchers in `willRespondWith` (responses) for flexible matching. Do NOT use `like()` on request bodies in `withRequest` — the consumer controls what it sends, so request bodies should use exact values. This follows Postel's Law: be strict in what you send (requests), be lenient in what you accept (responses).
  157. - **Provider states**: given() sets up test preconditions
  158. - **Isolation**: No real backend needed, runs fast
  159. - **Pact generation**: Automatically creates JSON pact files
  160. ---
  161. ### Example 2: Pact Provider Verification (Backend validates contracts)
  162. **Context**: Node.js/Express API verifying pacts published by consumers.
  163. **Implementation**:
  164. ```typescript
  165. // tests/contract/user-api.provider.spec.ts
  166. import { Verifier, VerifierOptions } from '@pact-foundation/pact';
  167. import { server } from '../../src/server'; // Your Express/Fastify app
  168. import { seedDatabase, resetDatabase } from '../support/db-helpers';
  169. /**
  170. * Provider Verification Test
  171. * - Provider (backend API) verifies against published pacts
  172. * - State handlers setup test data for each interaction
  173. * - Runs before merge to catch breaking changes
  174. */
  175. describe('Pact Provider Verification', () => {
  176. let serverInstance;
  177. const PORT = 3001;
  178. beforeAll(async () => {
  179. // Start provider server
  180. serverInstance = server.listen(PORT);
  181. console.log(`Provider server running on port ${PORT}`);
  182. });
  183. afterAll(async () => {
  184. // Cleanup
  185. await serverInstance.close();
  186. });
  187. it('should verify pacts from all consumers', async () => {
  188. const opts: VerifierOptions = {
  189. // Provider details
  190. provider: 'user-api-service',
  191. providerBaseUrl: `http://localhost:${PORT}`,
  192. // Pact Broker configuration
  193. pactBrokerUrl: process.env.PACT_BROKER_BASE_URL,
  194. pactBrokerToken: process.env.PACT_BROKER_TOKEN,
  195. publishVerificationResult: process.env.CI === 'true',
  196. providerVersion: process.env.GITHUB_SHA || 'dev',
  197. // State handlers: Setup provider state for each interaction
  198. stateHandlers: {
  199. 'user with id 1 exists': async () => {
  200. await seedDatabase({
  201. users: [
  202. {
  203. id: 1,
  204. name: 'John Doe',
  205. email: 'john@example.com',
  206. role: 'user',
  207. createdAt: '2025-01-15T10:00:00Z',
  208. },
  209. ],
  210. });
  211. return 'User seeded successfully';
  212. },
  213. 'user with id 999 does not exist': async () => {
  214. // Ensure user doesn't exist
  215. await resetDatabase();
  216. return 'Database reset';
  217. },
  218. 'no users exist': async () => {
  219. await resetDatabase();
  220. return 'Database empty';
  221. },
  222. },
  223. // Request filters: Add auth headers to all requests
  224. requestFilter: (req, res, next) => {
  225. // Mock authentication for verification
  226. req.headers['x-user-id'] = 'test-user';
  227. req.headers['authorization'] = 'Bearer valid-test-token';
  228. next();
  229. },
  230. // Timeout for verification
  231. timeout: 30000,
  232. };
  233. // Run verification
  234. await new Verifier(opts).verifyProvider();
  235. });
  236. });
  237. ```
  238. **CI integration**:
  239. ```yaml
  240. # .github/workflows/contract-test-provider.yml
  241. # NOTE: Canonical naming is contract-test-provider.yml per pactjs-utils conventions
  242. name: Pact Provider Verification
  243. on:
  244. pull_request:
  245. push:
  246. branches: [main]
  247. jobs:
  248. verify-contracts:
  249. runs-on: ubuntu-latest
  250. steps:
  251. - uses: actions/checkout@v4
  252. - name: Setup Node.js
  253. uses: actions/setup-node@v4
  254. with:
  255. node-version-file: '.nvmrc'
  256. - name: Install dependencies
  257. run: npm ci
  258. - name: Start database
  259. run: docker-compose up -d postgres
  260. - name: Run migrations
  261. run: npm run db:migrate
  262. - name: Verify pacts
  263. run: npm run test:pact:provider:remote:contract
  264. env:
  265. PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
  266. PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
  267. GITHUB_SHA: ${{ github.sha }}
  268. GITHUB_BRANCH: ${{ github.head_ref || github.ref_name }}
  269. - name: Can I Deploy?
  270. if: github.ref == 'refs/heads/main'
  271. run: npm run can:i:deploy:provider
  272. ```
  273. **Key Points**:
  274. - **State handlers**: Setup provider data for each given() state
  275. - **Request filters**: Add auth/headers for verification requests
  276. - **CI publishing**: Verification results sent to broker
  277. - **can-i-deploy**: Safety check before production deployment
  278. - **Database isolation**: Reset between state handlers
  279. ---
  280. ### Example 3: Contract CI Integration (Consumer & Provider Workflow)
  281. **Context**: Simplified overview of consumer and provider CI coordination. For the complete consumer CI workflow with env blocks, concurrency, and breaking-change detection, see `pact-consumer-framework-setup.md` Example 5.
  282. **Implementation**:
  283. ```yaml
  284. # .github/workflows/contract-test-consumer.yml (Consumer side)
  285. # NOTE: Canonical naming is contract-test-consumer.yml per pactjs-utils conventions
  286. name: Pact Consumer Tests
  287. on:
  288. pull_request:
  289. push:
  290. branches: [main]
  291. jobs:
  292. consumer-tests:
  293. runs-on: ubuntu-latest
  294. steps:
  295. - uses: actions/checkout@v4
  296. - name: Setup Node.js
  297. uses: actions/setup-node@v4
  298. with:
  299. node-version-file: '.nvmrc'
  300. - name: Install dependencies
  301. run: npm ci
  302. - name: Run consumer contract tests
  303. run: npm run test:pact:consumer
  304. - name: Publish pacts to broker
  305. run: npm run publish:pact
  306. - name: Can I deploy consumer? (main only)
  307. if: github.ref == 'refs/heads/main' && env.PACT_BREAKING_CHANGE != 'true'
  308. run: npm run can:i:deploy:consumer
  309. - name: Record consumer deployment (main only)
  310. if: github.ref == 'refs/heads/main'
  311. run: npm run record:consumer:deployment --env=dev
  312. ```
  313. ```yaml
  314. # .github/workflows/contract-test-provider.yml (Provider side)
  315. # NOTE: Canonical naming is contract-test-provider.yml per pactjs-utils conventions
  316. name: Pact Provider Verification
  317. on:
  318. pull_request:
  319. push:
  320. branches: [main]
  321. repository_dispatch:
  322. types: [pact_changed] # Webhook from Pact Broker
  323. jobs:
  324. verify-contracts:
  325. runs-on: ubuntu-latest
  326. steps:
  327. - uses: actions/checkout@v4
  328. - name: Setup Node.js
  329. uses: actions/setup-node@v4
  330. with:
  331. node-version-file: '.nvmrc'
  332. - name: Install dependencies
  333. run: npm ci
  334. - name: Start dependencies
  335. run: docker-compose up -d
  336. - name: Run provider verification
  337. run: npm run test:pact:provider:remote:contract
  338. env:
  339. PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
  340. PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
  341. GITHUB_SHA: ${{ github.sha }}
  342. GITHUB_BRANCH: ${{ github.head_ref || github.ref_name }}
  343. - name: Can I deploy provider? (main only)
  344. if: github.ref == 'refs/heads/main' && env.PACT_BREAKING_CHANGE != 'true'
  345. run: npm run can:i:deploy:provider
  346. - name: Record provider deployment (main only)
  347. if: github.ref == 'refs/heads/main'
  348. run: npm run record:provider:deployment --env=dev
  349. ```
  350. **Pact Broker Webhook Configuration**:
  351. ```json
  352. {
  353. "events": [
  354. {
  355. "name": "contract_content_changed"
  356. }
  357. ],
  358. "request": {
  359. "method": "POST",
  360. "url": "https://api.github.com/repos/your-org/user-api/dispatches",
  361. "headers": {
  362. "Authorization": "Bearer ${user.githubToken}",
  363. "Content-Type": "application/json",
  364. "Accept": "application/vnd.github.v3+json"
  365. },
  366. "body": {
  367. "event_type": "pact_changed",
  368. "client_payload": {
  369. "pact_url": "${pactbroker.pactUrl}",
  370. "consumer": "${pactbroker.consumerName}",
  371. "provider": "${pactbroker.providerName}"
  372. }
  373. }
  374. }
  375. }
  376. ```
  377. **Key Points**:
  378. - **Automatic trigger**: Consumer pact changes trigger provider verification via webhook
  379. - **Branch tracking**: Pacts published per branch for feature testing
  380. - **can-i-deploy**: Safety gate before production deployment
  381. - **Record deployment**: Track which version is in each environment
  382. - **Parallel dev**: Consumer and provider teams work independently
  383. ---
  384. ### Example 4: Resilience Coverage (Testing Fallback Behavior)
  385. **Context**: Capture timeout, retry, and error handling behavior explicitly in contracts.
  386. **Implementation**:
  387. ```typescript
  388. // tests/contract/user-api-resilience.pact.spec.ts
  389. import { PactV3, MatchersV3 } from '@pact-foundation/pact';
  390. import { getUserById, ApiError } from '@/api/user-service';
  391. const { like, string } = MatchersV3;
  392. const provider = new PactV3({
  393. consumer: 'user-management-web',
  394. provider: 'user-api-service',
  395. dir: './pacts',
  396. });
  397. describe('User API Resilience Contract', () => {
  398. /**
  399. * Test 500 error handling
  400. * Verifies consumer handles server errors gracefully
  401. */
  402. it('should handle 500 errors with retry logic', async () => {
  403. await provider
  404. .given('server is experiencing errors')
  405. .uponReceiving('a request that returns 500')
  406. .withRequest({
  407. method: 'GET',
  408. path: '/users/1',
  409. headers: { Accept: 'application/json' },
  410. })
  411. .willRespondWith({
  412. status: 500,
  413. headers: { 'Content-Type': 'application/json' },
  414. body: {
  415. error: 'Internal server error',
  416. code: 'INTERNAL_ERROR',
  417. retryable: true,
  418. },
  419. })
  420. .executeTest(async (mockServer) => {
  421. // Consumer should retry on 500
  422. try {
  423. await getUserById(1, {
  424. baseURL: mockServer.url,
  425. retries: 3,
  426. retryDelay: 100,
  427. });
  428. fail('Should have thrown error after retries');
  429. } catch (error) {
  430. expect(error).toBeInstanceOf(ApiError);
  431. expect((error as ApiError).code).toBe('INTERNAL_ERROR');
  432. expect((error as ApiError).retryable).toBe(true);
  433. }
  434. });
  435. });
  436. /**
  437. * Test 429 rate limiting
  438. * Verifies consumer respects rate limits
  439. */
  440. it('should handle 429 rate limit with backoff', async () => {
  441. await provider
  442. .given('rate limit exceeded for user')
  443. .uponReceiving('a request that is rate limited')
  444. .withRequest({
  445. method: 'GET',
  446. path: '/users/1',
  447. })
  448. .willRespondWith({
  449. status: 429,
  450. headers: {
  451. 'Content-Type': 'application/json',
  452. 'Retry-After': '60', // Retry after 60 seconds
  453. },
  454. body: {
  455. error: 'Too many requests',
  456. code: 'RATE_LIMIT_EXCEEDED',
  457. },
  458. })
  459. .executeTest(async (mockServer) => {
  460. try {
  461. await getUserById(1, {
  462. baseURL: mockServer.url,
  463. respectRateLimit: true,
  464. });
  465. fail('Should have thrown rate limit error');
  466. } catch (error) {
  467. expect(error).toBeInstanceOf(ApiError);
  468. expect((error as ApiError).code).toBe('RATE_LIMIT_EXCEEDED');
  469. expect((error as ApiError).retryAfter).toBe(60);
  470. }
  471. });
  472. });
  473. /**
  474. * Test timeout handling
  475. * Verifies consumer has appropriate timeout configuration
  476. */
  477. it('should timeout after 10 seconds', async () => {
  478. await provider
  479. .given('server is slow to respond')
  480. .uponReceiving('a request that times out')
  481. .withRequest({
  482. method: 'GET',
  483. path: '/users/1',
  484. })
  485. .willRespondWith({
  486. status: 200,
  487. headers: { 'Content-Type': 'application/json' },
  488. body: like({ id: 1, name: 'John' }),
  489. })
  490. .withDelay(15000) // Simulate 15 second delay
  491. .executeTest(async (mockServer) => {
  492. try {
  493. await getUserById(1, {
  494. baseURL: mockServer.url,
  495. timeout: 10000, // 10 second timeout
  496. });
  497. fail('Should have timed out');
  498. } catch (error) {
  499. expect(error).toBeInstanceOf(ApiError);
  500. expect((error as ApiError).code).toBe('TIMEOUT');
  501. }
  502. });
  503. });
  504. /**
  505. * Test partial response (optional fields)
  506. * Verifies consumer handles missing optional data
  507. */
  508. it('should handle response with missing optional fields', async () => {
  509. await provider
  510. .given('user exists with minimal data')
  511. .uponReceiving('a request for user with partial data')
  512. .withRequest({
  513. method: 'GET',
  514. path: '/users/1',
  515. })
  516. .willRespondWith({
  517. status: 200,
  518. headers: { 'Content-Type': 'application/json' },
  519. body: {
  520. id: integer(1),
  521. name: string('John Doe'),
  522. email: string('john@example.com'),
  523. // role, createdAt, etc. omitted (optional fields)
  524. },
  525. })
  526. .executeTest(async (mockServer) => {
  527. const user = await getUserById(1, { baseURL: mockServer.url });
  528. // Consumer handles missing optional fields gracefully
  529. expect(user.id).toBe(1);
  530. expect(user.name).toBe('John Doe');
  531. expect(user.role).toBeUndefined(); // Optional field
  532. expect(user.createdAt).toBeUndefined(); // Optional field
  533. });
  534. });
  535. });
  536. ```
  537. **API client with retry logic**:
  538. ```typescript
  539. // src/api/user-service.ts
  540. import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
  541. export class ApiError extends Error {
  542. constructor(
  543. message: string,
  544. public code: string,
  545. public retryable: boolean = false,
  546. public retryAfter?: number,
  547. ) {
  548. super(message);
  549. }
  550. }
  551. /**
  552. * User API client with retry and error handling
  553. */
  554. export async function getUserById(
  555. id: number,
  556. config?: AxiosRequestConfig & { retries?: number; retryDelay?: number; respectRateLimit?: boolean },
  557. ): Promise<User> {
  558. const { retries = 3, retryDelay = 1000, respectRateLimit = true, ...axiosConfig } = config || {};
  559. let lastError: Error;
  560. for (let attempt = 1; attempt <= retries; attempt++) {
  561. try {
  562. const response = await axios.get(`/users/${id}`, axiosConfig);
  563. return response.data;
  564. } catch (error: any) {
  565. lastError = error;
  566. // Handle rate limiting
  567. if (error.response?.status === 429) {
  568. const retryAfter = parseInt(error.response.headers['retry-after'] || '60');
  569. throw new ApiError('Too many requests', 'RATE_LIMIT_EXCEEDED', false, retryAfter);
  570. }
  571. // Retry on 500 errors
  572. if (error.response?.status === 500 && attempt < retries) {
  573. await new Promise((resolve) => setTimeout(resolve, retryDelay * attempt));
  574. continue;
  575. }
  576. // Handle 404
  577. if (error.response?.status === 404) {
  578. throw new ApiError('User not found', 'USER_NOT_FOUND', false);
  579. }
  580. // Handle timeout
  581. if (error.code === 'ECONNABORTED') {
  582. throw new ApiError('Request timeout', 'TIMEOUT', true);
  583. }
  584. break;
  585. }
  586. }
  587. throw new ApiError('Request failed after retries', 'INTERNAL_ERROR', true);
  588. }
  589. ```
  590. **Key Points**:
  591. - **Resilience contracts**: Timeouts, retries, errors explicitly tested
  592. - **State handlers**: Provider sets up each test scenario
  593. - **Error handling**: Consumer validates graceful degradation
  594. - **Retry logic**: Exponential backoff tested
  595. - **Optional fields**: Consumer handles partial responses
  596. ---
  597. ### Example 5: Pact Broker Housekeeping & Lifecycle Management
  598. **Context**: Automated broker maintenance to prevent contract sprawl and noise.
  599. **Implementation**:
  600. ```typescript
  601. // scripts/pact-broker-housekeeping.ts
  602. /**
  603. * Pact Broker Housekeeping Script
  604. * - Archive superseded contracts
  605. * - Expire unused pacts
  606. * - Tag releases for environment tracking
  607. */
  608. import { execFileSync } from 'node:child_process';
  609. const PACT_BROKER_BASE_URL = process.env.PACT_BROKER_BASE_URL!;
  610. const PACT_BROKER_TOKEN = process.env.PACT_BROKER_TOKEN!;
  611. const PACTICIPANT = 'user-api-service';
  612. /**
  613. * Tag release with environment
  614. */
  615. function tagRelease(version: string, environment: 'staging' | 'production') {
  616. console.log(`🏷️ Tagging ${PACTICIPANT} v${version} as ${environment}`);
  617. execFileSync(
  618. 'pact-broker',
  619. [
  620. 'create-version-tag',
  621. '--pacticipant',
  622. PACTICIPANT,
  623. '--version',
  624. version,
  625. '--tag',
  626. environment,
  627. '--broker-base-url',
  628. PACT_BROKER_BASE_URL,
  629. '--broker-token',
  630. PACT_BROKER_TOKEN,
  631. ],
  632. { stdio: 'inherit' },
  633. );
  634. }
  635. /**
  636. * Record deployment to environment
  637. */
  638. function recordDeployment(version: string, environment: 'staging' | 'production') {
  639. console.log(`📝 Recording deployment of ${PACTICIPANT} v${version} to ${environment}`);
  640. execFileSync(
  641. 'pact-broker',
  642. [
  643. 'record-deployment',
  644. '--pacticipant',
  645. PACTICIPANT,
  646. '--version',
  647. version,
  648. '--environment',
  649. environment,
  650. '--broker-base-url',
  651. PACT_BROKER_BASE_URL,
  652. '--broker-token',
  653. PACT_BROKER_TOKEN,
  654. ],
  655. { stdio: 'inherit' },
  656. );
  657. }
  658. /**
  659. * Clean up old pact versions (retention policy)
  660. * Keep: last 30 days, all production tags, latest from each branch
  661. */
  662. function cleanupOldPacts() {
  663. console.log(`🧹 Cleaning up old pacts for ${PACTICIPANT}`);
  664. execFileSync(
  665. 'pact-broker',
  666. [
  667. 'clean',
  668. '--pacticipant',
  669. PACTICIPANT,
  670. '--broker-base-url',
  671. PACT_BROKER_BASE_URL,
  672. '--broker-token',
  673. PACT_BROKER_TOKEN,
  674. '--keep-latest-for-branch',
  675. '1',
  676. '--keep-min-age',
  677. '30',
  678. ],
  679. { stdio: 'inherit' },
  680. );
  681. }
  682. /**
  683. * Check deployment compatibility
  684. */
  685. function canIDeploy(version: string, toEnvironment: string): boolean {
  686. console.log(`🔍 Checking if ${PACTICIPANT} v${version} can deploy to ${toEnvironment}`);
  687. try {
  688. execFileSync(
  689. 'pact-broker',
  690. [
  691. 'can-i-deploy',
  692. '--pacticipant',
  693. PACTICIPANT,
  694. '--version',
  695. version,
  696. '--to-environment',
  697. toEnvironment,
  698. '--broker-base-url',
  699. PACT_BROKER_BASE_URL,
  700. '--broker-token',
  701. PACT_BROKER_TOKEN,
  702. '--retry-while-unknown',
  703. '10',
  704. '--retry-interval',
  705. '30',
  706. ],
  707. { stdio: 'inherit' },
  708. );
  709. return true;
  710. } catch (error) {
  711. console.error(`❌ Cannot deploy to ${toEnvironment}`);
  712. return false;
  713. }
  714. }
  715. /**
  716. * Main housekeeping workflow
  717. */
  718. async function main() {
  719. const command = process.argv[2];
  720. const version = process.argv[3];
  721. const environment = process.argv[4] as 'staging' | 'production';
  722. switch (command) {
  723. case 'tag-release':
  724. tagRelease(version, environment);
  725. break;
  726. case 'record-deployment':
  727. recordDeployment(version, environment);
  728. break;
  729. case 'can-i-deploy':
  730. const canDeploy = canIDeploy(version, environment);
  731. process.exit(canDeploy ? 0 : 1);
  732. case 'cleanup':
  733. cleanupOldPacts();
  734. break;
  735. default:
  736. console.error('Unknown command. Use: tag-release | record-deployment | can-i-deploy | cleanup');
  737. process.exit(1);
  738. }
  739. }
  740. main();
  741. ```
  742. **package.json scripts**:
  743. ```json
  744. {
  745. "scripts": {
  746. "pact:tag": "ts-node scripts/pact-broker-housekeeping.ts tag-release",
  747. "pact:record": "ts-node scripts/pact-broker-housekeeping.ts record-deployment",
  748. "pact:can-deploy": "ts-node scripts/pact-broker-housekeeping.ts can-i-deploy",
  749. "pact:cleanup": "ts-node scripts/pact-broker-housekeeping.ts cleanup"
  750. }
  751. }
  752. ```
  753. **Deployment workflow integration**:
  754. ```yaml
  755. # .github/workflows/deploy-production.yml
  756. name: Deploy to Production
  757. on:
  758. push:
  759. tags:
  760. - 'v*'
  761. jobs:
  762. verify-contracts:
  763. runs-on: ubuntu-latest
  764. steps:
  765. - uses: actions/checkout@v4
  766. - name: Check pact compatibility
  767. run: npm run pact:can-deploy ${{ github.ref_name }} production
  768. env:
  769. PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
  770. PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
  771. deploy:
  772. needs: verify-contracts
  773. runs-on: ubuntu-latest
  774. steps:
  775. - name: Deploy to production
  776. run: ./scripts/deploy.sh production
  777. - name: Record deployment in Pact Broker
  778. run: npm run pact:record ${{ github.ref_name }} production
  779. env:
  780. PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
  781. PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
  782. ```
  783. **Scheduled cleanup**:
  784. ```yaml
  785. # .github/workflows/pact-housekeeping.yml
  786. name: Pact Broker Housekeeping
  787. on:
  788. schedule:
  789. - cron: '0 2 * * 0' # Weekly on Sunday at 2 AM
  790. jobs:
  791. cleanup:
  792. runs-on: ubuntu-latest
  793. steps:
  794. - uses: actions/checkout@v4
  795. - name: Cleanup old pacts
  796. run: npm run pact:cleanup
  797. env:
  798. PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
  799. PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
  800. ```
  801. **Key Points**:
  802. - **Automated tagging**: Releases tagged with environment
  803. - **Deployment tracking**: Broker knows which version is where
  804. - **Safety gate**: can-i-deploy blocks incompatible deployments
  805. - **Retention policy**: Keep recent, production, and branch-latest pacts
  806. - **Webhook triggers**: Provider verification runs on consumer changes
  807. ---
  808. ## Provider Scrutiny Protocol
  809. When generating consumer contract tests, the agent **MUST** analyze provider source code — or the provider's OpenAPI/Swagger spec — before writing any Pact interaction. Generating contracts from consumer-side assumptions alone leads to mismatches that only surface during provider verification — wrong response shapes, wrong status codes, wrong field names, wrong types, missing required fields, and wrong enum values.
  810. **Source priority**: Provider source code is the most authoritative reference. When an OpenAPI/Swagger spec exists (`openapi.yaml`, `openapi.json`, `swagger.json`), use it as a complementary or alternative source — it documents the provider's contract explicitly and can be faster to parse than tracing through handler code. When both exist, cross-reference them; if they disagree, the source code wins.
  811. ### Provider Endpoint Comment
  812. Every Pact interaction MUST include a provider endpoint comment immediately above the `.given()` call:
  813. ```typescript
  814. // Provider endpoint: server/src/routes/userRouteHandlers.ts -> GET /api/v2/users/:userId
  815. await provider.given('user with id 1 exists').uponReceiving('a request for user 1');
  816. ```
  817. **Format**: `// Provider endpoint: <relative-path-to-handler> -> <METHOD> <route-pattern>`
  818. If the provider source is not accessible, use: `// Provider endpoint: TODO — provider source not accessible, verify manually`
  819. ### Seven-Point Scrutiny Checklist
  820. Before generating each Pact interaction, read the provider route handler and/or OpenAPI spec and verify:
  821. | # | Check | What to Read (source code / OpenAPI spec) | Common Mismatch |
  822. | --- | --------------------- | ----------------------------------------------------------------- | ------------------------------------------------------------- |
  823. | 1 | **Response shape** | Handler's `res.json()` calls / OpenAPI `responses.content.schema` | Nested object vs flat; array wrapper vs direct |
  824. | 2 | **Status codes** | Handler's `res.status()` calls / OpenAPI `responses` keys | 200 vs 201 for creation; 204 vs 200 for delete |
  825. | 3 | **Field names** | Response type/DTO definitions / OpenAPI `schema.properties` | `transaction_id` vs `transactionId`; `fraud_score` vs `score` |
  826. | 4 | **Enum values** | Validation schemas, constants / OpenAPI `schema.enum` | `"active"` vs `"ACTIVE"`; `"pending"` vs `"in_progress"` |
  827. | 5 | **Required fields** | Request validation (Joi, Zod) / OpenAPI `schema.required` | Missing required header; optional field assumed required |
  828. | 6 | **Data types** | TypeScript types, DB models / OpenAPI `schema.type` + `format` | `string` ID vs `number` ID; ISO date vs Unix timestamp |
  829. | 7 | **Nested structures** | Response builder, serializer / OpenAPI `$ref` + `allOf`/`oneOf` | `{ data: { items: [] } }` vs `{ items: [] }` |
  830. ### Scrutiny Evidence Block
  831. Document what was found from provider source and/or OpenAPI spec as a block comment in the test file:
  832. ```typescript
  833. /*
  834. * Provider Scrutiny Evidence:
  835. * - Handler: server/src/routes/userRouteHandlers.ts:45
  836. * - OpenAPI: server/openapi.yaml paths./api/v2/users/{userId}.get (if available)
  837. * - Response type: UserResponseDto (server/src/types/user.ts:12)
  838. * - Status: 200 (line 52), 404 (line 48)
  839. * - Fields: { id: number, name: string, email: string, role: "user" | "admin", createdAt: string }
  840. * - Required request headers: Authorization (Bearer token)
  841. * - Validation: Zod schema at server/src/validation/user.ts:8
  842. */
  843. ```
  844. ### Graceful Degradation
  845. When provider source code is not accessible (different repo, no access, closed source):
  846. 1. **OpenAPI/Swagger spec available**: Use the spec as the source of truth for response shapes, status codes, and field names
  847. 2. **Pact Broker has existing contracts**: Use `pact_mcp` tools to fetch existing provider states and verified interactions as reference
  848. 3. **Neither available**: Generate contracts from consumer-side types but use the TODO form of the mandatory comment: `// Provider endpoint: TODO — provider source not accessible, verify manually` and add a `provider_scrutiny: "pending"` field to the output JSON
  849. 4. **Never silently guess**: If you cannot verify, document what you assumed and why
  850. ---
  851. ## Contract Testing Checklist
  852. Before implementing contract testing, verify:
  853. - [ ] **Pact Broker setup**: Hosted (Pactflow) or self-hosted broker configured
  854. - [ ] **Consumer tests**: Generate pacts in CI, publish to broker on merge
  855. - [ ] **Provider verification**: Runs on PR, verifies all consumer pacts
  856. - [ ] **State handlers**: Provider implements all given() states
  857. - [ ] **can-i-deploy**: Blocks deployment if contracts incompatible
  858. - [ ] **Webhooks configured**: Consumer changes trigger provider verification
  859. - [ ] **Retention policy**: Old pacts archived (keep 30 days, all production tags)
  860. - [ ] **Resilience tested**: Timeouts, retries, error codes in contracts
  861. - [ ] **Provider endpoint comments**: Every Pact interaction has `// Provider endpoint:` comment
  862. - [ ] **Provider scrutiny completed**: Seven-point checklist verified for each interaction
  863. - [ ] **Scrutiny evidence documented**: Block comment with handler, types, status codes, and fields
  864. ## Integration Points
  865. - Used in workflows: `*automate` (integration test generation), `*ci` (contract CI setup)
  866. - Related fragments: `test-levels-framework.md`, `ci-burn-in.md`, `pact-consumer-framework-setup.md` (consumer vitest `fileParallelism: false` + `pool: 'forks'` + `singleFork: true`), `pactjs-utils-consumer-helpers.md` (PactV4 one-interaction-per-`it()` rule), `pactjs-utils-provider-verifier.md` (provider vitest `pool: 'forks'` + `singleFork: true` — same rule as consumer), `pact-broker-webhooks.md` (PactFlow → GitHub webhook auth, PAT rotation, staleness monitoring)
  867. - Tools: Pact.js, Pact Broker (Pactflow or self-hosted), Pact CLI
  868. ---
  869. ## Pact.js Utils Accelerator
  870. When `tea_use_pactjs_utils` is enabled, the following utilities replace manual boilerplate:
  871. | Manual Pattern (raw Pact.js) | Pact.js Utils Equivalent | Benefit |
  872. | -------------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
  873. | Manual `JsonMap` casting for `.given()` params | `createProviderState({ name, params })` | Type-safe, auto-conversion of Date/null/nested objects |
  874. | Repeated builder callbacks for query/header/body | `setJsonContent({ query, headers, body })` | Reusable callback for `.withRequest(...)` and `.willRespondWith(...)` |
  875. | Inline body lambda `(builder) => builder.jsonBody(body)` | `setJsonBody(body)` | Body-only shorthand for cleaner response builders |
  876. | 30+ lines of `VerifierOptions` assembly | `buildVerifierOptions({ provider, port, includeMainAndDeployed, stateHandlers })` | One-call setup, env-aware, flow auto-detection |
  877. | Manual broker URL + selector logic from env vars | `handlePactBrokerUrlAndSelectors({ ..., options })` | Mutates options in-place with broker URL and selectors |
  878. | DIY Express middleware for auth injection | `createRequestFilter({ tokenGenerator })` | Bearer prefix contract prevents double-prefix bugs |
  879. | Manual CI branch/tag extraction | `getProviderVersionTags()` | CI-aware (GitHub Actions, GitLab CI, etc.) |
  880. | Message verifier config assembly | `buildMessageVerifierOptions({ provider, messageProviders })` | Same one-call pattern for Kafka/async contracts |
  881. | Inline no-op filter `(req, res, next) => next()` | `noOpRequestFilter` | Pre-built pass-through for no-auth providers |
  882. See the `pactjs-utils-*.md` knowledge fragments for complete examples and anti-patterns.
  883. ### PactV4 Determinism & FFI Safety (Mandatory)
  884. Four rules that together prevent both (a) non-deterministic pact generation failures that cause `Cannot change pact content for already published pact` errors at PactFlow publish, and (b) "request was expected but not received" flakes observed on Linux CI once a consumer+provider pair has more than one `.pacttest.ts` file:
  885. 1. **Consumer Vitest `fileParallelism: false`** in `vitest.config.pact.ts` — prevents parallel workers from racing on the shared pact JSON. See `pact-consumer-framework-setup.md` Example 2.
  886. 2. **Consumer Vitest `pool: 'forks'` + `poolOptions.forks.singleFork: true`** in `vitest.config.pact.ts` — same config as the provider side (`pactjs-utils-provider-verifier.md` Example 7). Best current understanding: the `@pact-foundation/pact` napi-rs binding is not robust across Vitest worker threads sharing a process; serialization alone (via `fileParallelism: false`) is insufficient on the default threads pool in Vitest v1. Forks + `singleFork: true` runs every pact file in one subprocess with a coherent FFI handle and eliminated a reproducible Linux-CI flake on two repos (`pactjs-utils`, `seon-mcp-server`). Single-file consumer suites have not been observed to flake; this rule is still recommended as a future-proof. See `pact-consumer-framework-setup.md` Example 2.
  887. 3. **One `addInteraction()` per `it()` block** — see `pactjs-utils-consumer-helpers.md` Example 6.
  888. 4. **Determinism gate** runs the consumer suite N times and fails on byte-different pact JSON before publish — see `pact-consumer-framework-setup.md` Example 10 (`scripts/check-pact-determinism.sh`).
  889. Provider suites require the same `pool: 'forks'` + `singleFork: true` combination — see `pactjs-utils-provider-verifier.md` Example 7.
  890. ### Webhook Auth & Staleness
  891. When `can-i-deploy` in a consumer repo times out with `There is no verified pact between <consumer> and the version of <provider> currently in <env>` — check the provider's PactFlow webhook. Silent failures from an expired/revoked GitHub PAT are the most common non-code cause of this symptom. See `pact-broker-webhooks.md` for the dedicated-machine-user pattern, classic-PAT-with-`repo`-scope rationale, rotation runbook, and staleness monitoring options.
  892. _Source: Pact consumer/provider sample repos, Murat contract testing blog, Pact official documentation, @seontechnologies/pactjs-utils library_