Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ---
  2. name: 'step-04a-subagent-api-failing'
  3. description: 'Subagent: Generate red-phase API test scaffolds (TDD red phase)'
  4. subagent: true
  5. outputFile: '/tmp/tea-atdd-api-tests-{{timestamp}}.json'
  6. ---
  7. # Subagent 4A: Generate Red-Phase API Test Scaffolds (TDD Red Phase)
  8. ## SUBAGENT CONTEXT
  9. This is an **isolated subagent** running in parallel with E2E red-phase test generation.
  10. **What you have from parent workflow:**
  11. - Story acceptance criteria from Step 1
  12. - Test strategy and scenarios from Step 3
  13. - Knowledge fragments loaded: api-request, data-factories, api-testing-patterns
  14. - Config: test framework, Playwright Utils enabled/disabled, Pact.js Utils enabled/disabled (`use_pactjs_utils`), Pact MCP mode (`pact_mcp`)
  15. - Provider Endpoint Map (if `use_pactjs_utils` enabled and provider source accessible)
  16. **If `use_pactjs_utils` is enabled:** Also generate consumer contract tests alongside API tests. Use the loaded pactjs-utils fragments (`pactjs-utils-overview`, `pactjs-utils-consumer-helpers`, `pactjs-utils-provider-verifier`, `pactjs-utils-request-filter`, `pact-consumer-di`, `pact-consumer-framework-setup`) for patterns. When generating consumer tests, enforce **one `pact.addInteraction()` per `it()` block** (see `pactjs-utils-consumer-helpers.md` Example 6) — PactV4's Rust FFI non-deterministically drops interactions if multiple are chained in one test. If `pact_mcp` is `"mcp"`, use SmartBear MCP tools (Fetch Provider States, Generate Pact Tests) to inform test generation.
  17. **Your task:** Generate API test scaffolds for the feature's expected behavior. They stay in `test.skip()` until the developer activates them for the current task (TDD RED PHASE).
  18. ---
  19. ## MANDATORY EXECUTION RULES
  20. - 📖 Read this entire subagent file before acting
  21. - ✅ Generate red-phase API test scaffolds ONLY
  22. - ✅ Tests MUST be emitted with `test.skip()` until the developer activates them
  23. - ✅ Output structured JSON to temp file
  24. - ✅ Follow knowledge fragment patterns
  25. - ❌ Do NOT generate E2E tests (that's subagent 4B)
  26. - ❌ Do NOT generate active passing tests (this is TDD red phase)
  27. - ❌ Do NOT run tests (that's step 5)
  28. ---
  29. ## SUBAGENT TASK
  30. ### 1. Identify API Endpoints from Acceptance Criteria
  31. From the story acceptance criteria (Step 1 output), identify:
  32. - Which API endpoints will be created for this story
  33. - Expected request/response contracts
  34. - Authentication requirements
  35. - Expected status codes and error scenarios
  36. **Example Acceptance Criteria:**
  37. ```
  38. Story: User Registration
  39. - As a user, I can POST to /api/users/register with email and password
  40. - System returns 201 Created with user object
  41. - System returns 400 Bad Request if email already exists
  42. - System returns 422 Unprocessable Entity if validation fails
  43. ```
  44. ### 2. Generate Red-Phase API Test Files
  45. For each API endpoint, create test file in `tests/api/[feature].spec.ts`:
  46. **Test Structure (ATDD - Red Phase):**
  47. ```typescript
  48. import { test, expect } from '@playwright/test';
  49. // If Playwright Utils enabled:
  50. // import { apiRequest } from '@playwright-utils/api';
  51. test.describe('[Story Name] API Tests (ATDD)', () => {
  52. test.skip('[P0] should register new user successfully', async ({ request }) => {
  53. // THIS TEST WILL FAIL - Endpoint not implemented yet
  54. const response = await request.post('/api/users/register', {
  55. data: {
  56. email: 'newuser@example.com',
  57. password: 'SecurePass123!',
  58. },
  59. });
  60. // Expect 201 but will get 404 (endpoint doesn't exist)
  61. expect(response.status()).toBe(201);
  62. const user = await response.json();
  63. expect(user).toMatchObject({
  64. id: expect.any(Number),
  65. email: 'newuser@example.com',
  66. });
  67. });
  68. test.skip('[P1] should return 400 if email exists', async ({ request }) => {
  69. // THIS TEST WILL FAIL - Endpoint not implemented yet
  70. const response = await request.post('/api/users/register', {
  71. data: {
  72. email: 'existing@example.com',
  73. password: 'SecurePass123!',
  74. },
  75. });
  76. expect(response.status()).toBe(400);
  77. const error = await response.json();
  78. expect(error.message).toContain('Email already exists');
  79. });
  80. });
  81. ```
  82. **CRITICAL ATDD Requirements:**
  83. - ✅ Use `test.skip()` to mark tests as red-phase scaffolds
  84. - ✅ Write assertions for EXPECTED behavior (even though not implemented)
  85. - ✅ Use realistic test data (not placeholder data)
  86. - ✅ Test both happy path and error scenarios from acceptance criteria
  87. - ✅ Use `apiRequest()` helper if Playwright Utils enabled
  88. - ✅ Use data factories for test data (from data-factories fragment)
  89. - ✅ Include priority tags [P0], [P1], [P2], [P3]
  90. ### 1.5 Provider Source Scrutiny for CDC in TDD Red Phase (If `use_pactjs_utils` Enabled)
  91. When generating Pact consumer contract tests in the ATDD red phase, provider scrutiny applies with TDD-specific rules. Apply the **Seven-Point Scrutiny Checklist** from `contract-testing.md` (Response shape, Status codes, Field names, Enum values, Required fields, Data types, Nested structures) for both existing and new endpoints.
  92. **If provider endpoint already exists** (extending an existing API):
  93. - READ the provider route handler, types, and validation schemas
  94. - Verify all seven scrutiny points against the provider source: Response shape, Status codes, Field names, Enum values, Required fields, Data types, Nested structures
  95. - Add `// Provider endpoint:` comment and scrutiny evidence block documenting findings for each point
  96. - Wrap the entire test function in `test.skip()` (so the whole test including `executeTest` is skipped), not just the callback
  97. **If provider endpoint is new** (TDD — endpoint not implemented yet):
  98. - Use acceptance criteria as the source of truth for expected behavior
  99. - Acceptance criteria should specify all seven scrutiny points where possible (status codes, field names, types, etc.) — note any gaps as assumptions in the evidence block
  100. - Add `// Provider endpoint: TODO — new endpoint, not yet implemented`
  101. - Document expected behavior from acceptance criteria in scrutiny evidence block
  102. - Wrap the entire test function in `test.skip()` and use realistic expectations from the story
  103. **Graceful degradation when provider source is inaccessible:**
  104. 1. **OpenAPI/Swagger spec available**: Use the spec as the source of truth for response shapes, status codes, and field names
  105. 2. **Pact Broker available** (when `pact_mcp` is `"mcp"`): Use SmartBear MCP tools to fetch existing provider states and verified interactions as reference
  106. 3. **Neither available**: For new endpoints, use acceptance criteria; for existing endpoints, use consumer-side types. Mark with `// Provider endpoint: TODO — provider source not accessible, verify manually` and set `provider_scrutiny: "pending"` in output JSON
  107. 4. **Never silently guess**: Document all assumptions in the scrutiny evidence block
  108. **Provider endpoint comments are MANDATORY** even in red-phase tests — they document the intent.
  109. **Example: Red-phase Pact test with provider scrutiny:**
  110. ```typescript
  111. // Provider endpoint: TODO — new endpoint, not yet implemented
  112. /*
  113. * Provider Scrutiny Evidence:
  114. * - Handler: NEW — not yet implemented (TDD red phase)
  115. * - Expected from acceptance criteria:
  116. * - Endpoint: POST /api/v2/users/register
  117. * - Status: 201 for success, 400 for duplicate email, 422 for validation error
  118. * - Response: { id: number, email: string, createdAt: string }
  119. */
  120. test.skip('[P0] should generate consumer contract for user registration', async () => {
  121. await provider
  122. .given('no users exist')
  123. .uponReceiving('a request to register a new user')
  124. .withRequest({
  125. method: 'POST',
  126. path: '/api/v2/users/register',
  127. headers: { 'Content-Type': 'application/json' },
  128. body: { email: 'newuser@example.com', password: 'SecurePass123!' },
  129. })
  130. .willRespondWith({
  131. status: 201,
  132. headers: { 'Content-Type': 'application/json' },
  133. body: like({
  134. id: integer(1),
  135. email: string('newuser@example.com'),
  136. createdAt: string('2025-01-15T10:00:00Z'),
  137. }),
  138. })
  139. .executeTest(async (mockServer) => {
  140. const result = await registerUser({ email: 'newuser@example.com', password: 'SecurePass123!' }, { baseUrl: mockServer.url });
  141. expect(result.id).toEqual(expect.any(Number));
  142. });
  143. });
  144. ```
  145. **Why test.skip():**
  146. - Tests are written correctly for EXPECTED behavior
  147. - But we know they'll fail because feature isn't implemented
  148. - `test.skip()` documents this is intentional (TDD red phase)
  149. - Once feature is implemented, remove `test.skip()` to verify green phase
  150. ### 3. Track Fixture Needs
  151. Identify fixtures needed for API tests:
  152. - Authentication fixtures (if endpoints require auth)
  153. - Data factories (user data, etc.)
  154. - API client configurations
  155. **Do NOT create fixtures yet** - just track what's needed for aggregation step.
  156. ---
  157. ## OUTPUT FORMAT
  158. Write JSON to temp file: `/tmp/tea-atdd-api-tests-{{timestamp}}.json`
  159. ```json
  160. {
  161. "success": true,
  162. "subagent": "atdd-api-tests",
  163. "tests": [
  164. {
  165. "file": "tests/api/user-registration.spec.ts",
  166. "content": "[full TypeScript test file content with test.skip()]",
  167. "description": "ATDD API test scaffolds for user registration (RED PHASE)",
  168. "expected_to_fail": true,
  169. "acceptance_criteria_covered": [
  170. "User can register with email/password",
  171. "System returns 201 on success",
  172. "System returns 400 if email exists"
  173. ],
  174. "priority_coverage": {
  175. "P0": 1,
  176. "P1": 2,
  177. "P2": 0,
  178. "P3": 0
  179. }
  180. }
  181. ],
  182. "fixture_needs": ["userDataFactory"],
  183. "knowledge_fragments_used": [
  184. "api-request",
  185. "data-factories",
  186. "api-testing-patterns",
  187. "pactjs-utils-consumer-helpers",
  188. "pact-consumer-di"
  189. ],
  190. "test_count": 3,
  191. "tdd_phase": "RED",
  192. "provider_scrutiny": "completed",
  193. "summary": "Generated 3 red-phase API test scaffolds for user registration story"
  194. }
  195. ```
  196. **On Error:**
  197. ```json
  198. {
  199. "success": false,
  200. "subagent": "atdd-api-tests",
  201. "error": "Error message describing what went wrong",
  202. "partial_output": {
  203. /* any tests generated before error */
  204. }
  205. }
  206. ```
  207. ---
  208. ## EXIT CONDITION
  209. Subagent completes when:
  210. - ✅ All API endpoints from acceptance criteria have test files
  211. - ✅ All tests use `test.skip()` (documented red-phase scaffolds)
  212. - ✅ All tests assert EXPECTED behavior (not placeholder assertions)
  213. - ✅ JSON output written to temp file
  214. - ✅ Fixture needs to be tracked
  215. **Subagent terminates here.** Parent workflow will read output and proceed to aggregation.
  216. ---
  217. ## 🚨 SUBAGENT SUCCESS METRICS
  218. ### ✅ SUCCESS:
  219. - All API tests generated with test.skip()
  220. - Tests assert expected behavior (not placeholders)
  221. - JSON output valid and complete
  222. - No E2E/component/unit tests included (out of scope)
  223. - Tests follow knowledge fragment patterns
  224. - Every Pact interaction has `// Provider endpoint:` comment (if CDC enabled)
  225. - Provider scrutiny completed or TODO markers added for new endpoints (if CDC enabled)
  226. ### ❌ FAILURE:
  227. - Generated active passing tests (wrong - this is RED phase)
  228. - Tests without test.skip() (will break CI)
  229. - Placeholder assertions (expect(true).toBe(true))
  230. - Did not follow knowledge fragment patterns
  231. - Invalid or missing JSON output
  232. - Pact interactions missing provider endpoint comments (if CDC enabled)