bros-harness 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +183 -0
- package/SECURITY.md +16 -0
- package/assets/agents.manifest.json +55 -0
- package/assets/commands.manifest.json +35 -0
- package/assets/docs.manifest.json +20 -0
- package/assets/import-report.md +25 -0
- package/assets/manifest.json +799 -0
- package/assets/opencode/agents/README.md +3 -0
- package/assets/opencode/agents/bro-build.md +256 -0
- package/assets/opencode/agents/bro-design.md +77 -0
- package/assets/opencode/agents/bro-docs.md +72 -0
- package/assets/opencode/agents/bro-explore.md +143 -0
- package/assets/opencode/agents/bro-ops.md +195 -0
- package/assets/opencode/agents/bro-shield.md +77 -0
- package/assets/opencode/agents/bro-test.md +204 -0
- package/assets/opencode/agents/bro-ui.md +135 -0
- package/assets/opencode/agents/mighty-bro.md +252 -0
- package/assets/opencode/commands/README.md +3 -0
- package/assets/opencode/commands/bros-assemble.md +32 -0
- package/assets/opencode/commands/bros-build.md +58 -0
- package/assets/opencode/commands/bros-plan.md +83 -0
- package/assets/opencode/commands/bros-review.md +38 -0
- package/assets/opencode/commands/bros-status.md +26 -0
- package/assets/opencode/docs/README.md +3 -0
- package/assets/opencode/docs/bros-builtin-skills.md +63 -0
- package/assets/opencode/docs/bros-harness.md +194 -0
- package/assets/opencode/skills/README.md +3 -0
- package/assets/opencode/skills/agent-architecture-audit/SKILL.md +256 -0
- package/assets/opencode/skills/agent-harness-construction/.openskills.json +7 -0
- package/assets/opencode/skills/agent-harness-construction/SKILL.md +73 -0
- package/assets/opencode/skills/agent-introspection-debugging/.openskills.json +7 -0
- package/assets/opencode/skills/agent-introspection-debugging/SKILL.md +153 -0
- package/assets/opencode/skills/api-design/.openskills.json +7 -0
- package/assets/opencode/skills/api-design/agents/openai.yaml +7 -0
- package/assets/opencode/skills/architecture-decision-records/.openskills.json +7 -0
- package/assets/opencode/skills/architecture-decision-records/SKILL.md +179 -0
- package/assets/opencode/skills/article-writing/.openskills.json +7 -0
- package/assets/opencode/skills/article-writing/SKILL.md +79 -0
- package/assets/opencode/skills/article-writing/agents/openai.yaml +7 -0
- package/assets/opencode/skills/automation-audit-ops/.openskills.json +7 -0
- package/assets/opencode/skills/automation-audit-ops/SKILL.md +142 -0
- package/assets/opencode/skills/backend-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/backend-patterns/SKILL.md +561 -0
- package/assets/opencode/skills/backend-patterns/agents/openai.yaml +7 -0
- package/assets/opencode/skills/benchmark/.openskills.json +7 -0
- package/assets/opencode/skills/benchmark/SKILL.md +93 -0
- package/assets/opencode/skills/bros-orchestrate/SKILL.md +455 -0
- package/assets/opencode/skills/browser-qa/.openskills.json +7 -0
- package/assets/opencode/skills/browser-qa/SKILL.md +87 -0
- package/assets/opencode/skills/canary-watch/.openskills.json +7 -0
- package/assets/opencode/skills/canary-watch/SKILL.md +107 -0
- package/assets/opencode/skills/code-review-expert/SKILL.md +155 -0
- package/assets/opencode/skills/code-review-expert/agents/agent.yaml +7 -0
- package/assets/opencode/skills/code-review-expert/references/code-quality-checklist.md +130 -0
- package/assets/opencode/skills/code-review-expert/references/removal-plan.md +52 -0
- package/assets/opencode/skills/code-review-expert/references/security-checklist.md +118 -0
- package/assets/opencode/skills/code-review-expert/references/solid-checklist.md +65 -0
- package/assets/opencode/skills/code-tour/.openskills.json +7 -0
- package/assets/opencode/skills/code-tour/SKILL.md +236 -0
- package/assets/opencode/skills/coding-standards/.openskills.json +7 -0
- package/assets/opencode/skills/coding-standards/SKILL.md +549 -0
- package/assets/opencode/skills/coding-standards/agents/openai.yaml +7 -0
- package/assets/opencode/skills/context-budget/.openskills.json +7 -0
- package/assets/opencode/skills/context-budget/SKILL.md +135 -0
- package/assets/opencode/skills/database-migrations/.openskills.json +7 -0
- package/assets/opencode/skills/database-migrations/SKILL.md +429 -0
- package/assets/opencode/skills/deployment-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/deployment-patterns/SKILL.md +427 -0
- package/assets/opencode/skills/design-system/.openskills.json +7 -0
- package/assets/opencode/skills/design-system/SKILL.md +82 -0
- package/assets/opencode/skills/docker-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/docker-patterns/SKILL.md +364 -0
- package/assets/opencode/skills/documentation-lookup/.openskills.json +7 -0
- package/assets/opencode/skills/documentation-lookup/SKILL.md +90 -0
- package/assets/opencode/skills/documentation-lookup/agents/openai.yaml +7 -0
- package/assets/opencode/skills/e2e-testing/.openskills.json +7 -0
- package/assets/opencode/skills/e2e-testing/SKILL.md +326 -0
- package/assets/opencode/skills/e2e-testing/agents/openai.yaml +7 -0
- package/assets/opencode/skills/error-handling/SKILL.md +376 -0
- package/assets/opencode/skills/frontend-design/.openskills.json +7 -0
- package/assets/opencode/skills/frontend-design/SKILL.md +145 -0
- package/assets/opencode/skills/frontend-design-direction/SKILL.md +92 -0
- package/assets/opencode/skills/frontend-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/frontend-patterns/SKILL.md +642 -0
- package/assets/opencode/skills/frontend-patterns/agents/openai.yaml +7 -0
- package/assets/opencode/skills/gateguard/.openskills.json +7 -0
- package/assets/opencode/skills/gateguard/SKILL.md +125 -0
- package/assets/opencode/skills/git-master/SKILL.md +60 -0
- package/assets/opencode/skills/golang-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/golang-patterns/SKILL.md +674 -0
- package/assets/opencode/skills/golang-testing/.openskills.json +7 -0
- package/assets/opencode/skills/golang-testing/SKILL.md +720 -0
- package/assets/opencode/skills/grafana-dashboard-design/SKILL.md +65 -0
- package/assets/opencode/skills/hexagonal-architecture/.openskills.json +7 -0
- package/assets/opencode/skills/hexagonal-architecture/SKILL.md +276 -0
- package/assets/opencode/skills/java-coding-standards/.openskills.json +7 -0
- package/assets/opencode/skills/java-coding-standards/SKILL.md +383 -0
- package/assets/opencode/skills/jpa-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/jpa-patterns/SKILL.md +151 -0
- package/assets/opencode/skills/knowledge-ops/.openskills.json +7 -0
- package/assets/opencode/skills/knowledge-ops/SKILL.md +154 -0
- package/assets/opencode/skills/make-interfaces-feel-better/SKILL.md +151 -0
- package/assets/opencode/skills/mysql-patterns/SKILL.md +412 -0
- package/assets/opencode/skills/nestjs-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/nestjs-patterns/SKILL.md +230 -0
- package/assets/opencode/skills/nextjs-turbopack/.openskills.json +7 -0
- package/assets/opencode/skills/nextjs-turbopack/SKILL.md +57 -0
- package/assets/opencode/skills/nextjs-turbopack/agents/openai.yaml +7 -0
- package/assets/opencode/skills/parallel-execution-optimizer/SKILL.md +72 -0
- package/assets/opencode/skills/postgres-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/postgres-patterns/SKILL.md +147 -0
- package/assets/opencode/skills/prisma-patterns/SKILL.md +371 -0
- package/assets/opencode/skills/product-capability/.openskills.json +7 -0
- package/assets/opencode/skills/product-capability/SKILL.md +141 -0
- package/assets/opencode/skills/product-lens/.openskills.json +7 -0
- package/assets/opencode/skills/product-lens/SKILL.md +92 -0
- package/assets/opencode/skills/production-audit/SKILL.md +206 -0
- package/assets/opencode/skills/python-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/python-patterns/SKILL.md +750 -0
- package/assets/opencode/skills/python-testing/.openskills.json +7 -0
- package/assets/opencode/skills/python-testing/SKILL.md +816 -0
- package/assets/opencode/skills/redis-patterns/SKILL.md +403 -0
- package/assets/opencode/skills/requirements-clarity/README.md +260 -0
- package/assets/opencode/skills/requirements-clarity/SKILL.md +324 -0
- package/assets/opencode/skills/rust-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/rust-patterns/SKILL.md +499 -0
- package/assets/opencode/skills/rust-testing/.openskills.json +7 -0
- package/assets/opencode/skills/rust-testing/SKILL.md +500 -0
- package/assets/opencode/skills/safety-guard/.openskills.json +7 -0
- package/assets/opencode/skills/safety-guard/SKILL.md +75 -0
- package/assets/opencode/skills/search-first/.openskills.json +7 -0
- package/assets/opencode/skills/search-first/SKILL.md +181 -0
- package/assets/opencode/skills/security-review/.openskills.json +7 -0
- package/assets/opencode/skills/security-review/agents/openai.yaml +7 -0
- package/assets/opencode/skills/security-review/cloud-infrastructure-security.md +361 -0
- package/assets/opencode/skills/security-scan/.openskills.json +7 -0
- package/assets/opencode/skills/security-scan/SKILL.md +165 -0
- package/assets/opencode/skills/springboot-patterns/.openskills.json +7 -0
- package/assets/opencode/skills/springboot-patterns/SKILL.md +314 -0
- package/assets/opencode/skills/springboot-tdd/.openskills.json +7 -0
- package/assets/opencode/skills/springboot-tdd/SKILL.md +158 -0
- package/assets/opencode/skills/springboot-verification/.openskills.json +7 -0
- package/assets/opencode/skills/springboot-verification/SKILL.md +231 -0
- package/assets/opencode/skills/strategic-compact/.openskills.json +7 -0
- package/assets/opencode/skills/strategic-compact/SKILL.md +131 -0
- package/assets/opencode/skills/strategic-compact/agents/openai.yaml +7 -0
- package/assets/opencode/skills/strategic-compact/suggest-compact.sh +54 -0
- package/assets/opencode/skills/tdd-workflow/.openskills.json +7 -0
- package/assets/opencode/skills/tdd-workflow/SKILL.md +463 -0
- package/assets/opencode/skills/tdd-workflow/agents/openai.yaml +7 -0
- package/assets/opencode/skills/verification-loop/.openskills.json +7 -0
- package/assets/opencode/skills/verification-loop/SKILL.md +126 -0
- package/assets/opencode/skills/verification-loop/agents/openai.yaml +7 -0
- package/assets/opencode/skills/vite-patterns/SKILL.md +449 -0
- package/assets/opencode/skills/web-doc-search/SKILL.md +51 -0
- package/assets/opencode/templates/README.md +3 -0
- package/assets/opencode/templates/bros/adr.md +39 -0
- package/assets/opencode/templates/bros/delivery-report.md +71 -0
- package/assets/opencode/templates/bros/explorer-evidence-packet.md +51 -0
- package/assets/opencode/templates/bros/prd.md +72 -0
- package/assets/opencode/templates/bros/security-review.md +48 -0
- package/assets/opencode/templates/bros/status-board.md +33 -0
- package/assets/opencode/templates/bros/task-packet.md +94 -0
- package/assets/opencode/templates/bros/test-strategy.md +57 -0
- package/assets/opencode/templates/bros/ui-implementation-packet.md +64 -0
- package/assets/skills.manifest.json +650 -0
- package/assets/templates.manifest.json +55 -0
- package/bin/bros.mjs +122 -0
- package/docs/compatibility.md +9 -0
- package/docs/installation.md +66 -0
- package/docs/integrations/claude.md +5 -0
- package/docs/integrations/codex.md +5 -0
- package/docs/integrations/opencode.md +39 -0
- package/docs/migration/from-local-opencode-config.md +10 -0
- package/docs/release-process.md +11 -0
- package/docs/repository-structure.md +15 -0
- package/docs/roadmap.md +20 -0
- package/docs/security.md +18 -0
- package/docs/testing.md +9 -0
- package/examples/opencode/README.md +11 -0
- package/examples/opencode/opencode.example.jsonc +4 -0
- package/package.json +43 -0
- package/scripts/validate-assets.mjs +22 -0
- package/scripts/verify-no-secrets.mjs +38 -0
- package/src/plugin.mjs +98 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: e2e-testing
|
|
3
|
+
description: Playwright E2E testing patterns, Page Object Model, configuration, CI/CD integration, artifact management, and flaky test strategies.
|
|
4
|
+
origin: ECC
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# E2E Testing Patterns
|
|
8
|
+
|
|
9
|
+
Comprehensive Playwright patterns for building stable, fast, and maintainable E2E test suites.
|
|
10
|
+
|
|
11
|
+
## Test File Organization
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
tests/
|
|
15
|
+
├── e2e/
|
|
16
|
+
│ ├── auth/
|
|
17
|
+
│ │ ├── login.spec.ts
|
|
18
|
+
│ │ ├── logout.spec.ts
|
|
19
|
+
│ │ └── register.spec.ts
|
|
20
|
+
│ ├── features/
|
|
21
|
+
│ │ ├── browse.spec.ts
|
|
22
|
+
│ │ ├── search.spec.ts
|
|
23
|
+
│ │ └── create.spec.ts
|
|
24
|
+
│ └── api/
|
|
25
|
+
│ └── endpoints.spec.ts
|
|
26
|
+
├── fixtures/
|
|
27
|
+
│ ├── auth.ts
|
|
28
|
+
│ └── data.ts
|
|
29
|
+
└── playwright.config.ts
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Page Object Model (POM)
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { Page, Locator } from '@playwright/test'
|
|
36
|
+
|
|
37
|
+
export class ItemsPage {
|
|
38
|
+
readonly page: Page
|
|
39
|
+
readonly searchInput: Locator
|
|
40
|
+
readonly itemCards: Locator
|
|
41
|
+
readonly createButton: Locator
|
|
42
|
+
|
|
43
|
+
constructor(page: Page) {
|
|
44
|
+
this.page = page
|
|
45
|
+
this.searchInput = page.locator('[data-testid="search-input"]')
|
|
46
|
+
this.itemCards = page.locator('[data-testid="item-card"]')
|
|
47
|
+
this.createButton = page.locator('[data-testid="create-btn"]')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async goto() {
|
|
51
|
+
await this.page.goto('/items')
|
|
52
|
+
await this.page.waitForLoadState('networkidle')
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async search(query: string) {
|
|
56
|
+
await this.searchInput.fill(query)
|
|
57
|
+
await this.page.waitForResponse(resp => resp.url().includes('/api/search'))
|
|
58
|
+
await this.page.waitForLoadState('networkidle')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async getItemCount() {
|
|
62
|
+
return await this.itemCards.count()
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Test Structure
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { test, expect } from '@playwright/test'
|
|
71
|
+
import { ItemsPage } from '../../pages/ItemsPage'
|
|
72
|
+
|
|
73
|
+
test.describe('Item Search', () => {
|
|
74
|
+
let itemsPage: ItemsPage
|
|
75
|
+
|
|
76
|
+
test.beforeEach(async ({ page }) => {
|
|
77
|
+
itemsPage = new ItemsPage(page)
|
|
78
|
+
await itemsPage.goto()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('should search by keyword', async ({ page }) => {
|
|
82
|
+
await itemsPage.search('test')
|
|
83
|
+
|
|
84
|
+
const count = await itemsPage.getItemCount()
|
|
85
|
+
expect(count).toBeGreaterThan(0)
|
|
86
|
+
|
|
87
|
+
await expect(itemsPage.itemCards.first()).toContainText(/test/i)
|
|
88
|
+
await page.screenshot({ path: 'artifacts/search-results.png' })
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('should handle no results', async ({ page }) => {
|
|
92
|
+
await itemsPage.search('xyznonexistent123')
|
|
93
|
+
|
|
94
|
+
await expect(page.locator('[data-testid="no-results"]')).toBeVisible()
|
|
95
|
+
expect(await itemsPage.getItemCount()).toBe(0)
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Playwright Configuration
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { defineConfig, devices } from '@playwright/test'
|
|
104
|
+
|
|
105
|
+
export default defineConfig({
|
|
106
|
+
testDir: './tests/e2e',
|
|
107
|
+
fullyParallel: true,
|
|
108
|
+
forbidOnly: !!process.env.CI,
|
|
109
|
+
retries: process.env.CI ? 2 : 0,
|
|
110
|
+
workers: process.env.CI ? 1 : undefined,
|
|
111
|
+
reporter: [
|
|
112
|
+
['html', { outputFolder: 'playwright-report' }],
|
|
113
|
+
['junit', { outputFile: 'playwright-results.xml' }],
|
|
114
|
+
['json', { outputFile: 'playwright-results.json' }]
|
|
115
|
+
],
|
|
116
|
+
use: {
|
|
117
|
+
baseURL: process.env.BASE_URL || 'http://localhost:3000',
|
|
118
|
+
trace: 'on-first-retry',
|
|
119
|
+
screenshot: 'only-on-failure',
|
|
120
|
+
video: 'retain-on-failure',
|
|
121
|
+
actionTimeout: 10000,
|
|
122
|
+
navigationTimeout: 30000,
|
|
123
|
+
},
|
|
124
|
+
projects: [
|
|
125
|
+
{ name: 'chromium', use: { ...devices['Desktop Chrome'] } },
|
|
126
|
+
{ name: 'firefox', use: { ...devices['Desktop Firefox'] } },
|
|
127
|
+
{ name: 'webkit', use: { ...devices['Desktop Safari'] } },
|
|
128
|
+
{ name: 'mobile-chrome', use: { ...devices['Pixel 5'] } },
|
|
129
|
+
],
|
|
130
|
+
webServer: {
|
|
131
|
+
command: 'npm run dev',
|
|
132
|
+
url: 'http://localhost:3000',
|
|
133
|
+
reuseExistingServer: !process.env.CI,
|
|
134
|
+
timeout: 120000,
|
|
135
|
+
},
|
|
136
|
+
})
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Flaky Test Patterns
|
|
140
|
+
|
|
141
|
+
### Quarantine
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
test('flaky: complex search', async ({ page }) => {
|
|
145
|
+
test.fixme(true, 'Flaky - Issue #123')
|
|
146
|
+
// test code...
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('conditional skip', async ({ page }) => {
|
|
150
|
+
test.skip(process.env.CI, 'Flaky in CI - Issue #123')
|
|
151
|
+
// test code...
|
|
152
|
+
})
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Identify Flakiness
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
npx playwright test tests/search.spec.ts --repeat-each=10
|
|
159
|
+
npx playwright test tests/search.spec.ts --retries=3
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Common Causes & Fixes
|
|
163
|
+
|
|
164
|
+
**Race conditions:**
|
|
165
|
+
```typescript
|
|
166
|
+
// Bad: assumes element is ready
|
|
167
|
+
await page.click('[data-testid="button"]')
|
|
168
|
+
|
|
169
|
+
// Good: auto-wait locator
|
|
170
|
+
await page.locator('[data-testid="button"]').click()
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
**Network timing:**
|
|
174
|
+
```typescript
|
|
175
|
+
// Bad: arbitrary timeout
|
|
176
|
+
await page.waitForTimeout(5000)
|
|
177
|
+
|
|
178
|
+
// Good: wait for specific condition
|
|
179
|
+
await page.waitForResponse(resp => resp.url().includes('/api/data'))
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**Animation timing:**
|
|
183
|
+
```typescript
|
|
184
|
+
// Bad: click during animation
|
|
185
|
+
await page.click('[data-testid="menu-item"]')
|
|
186
|
+
|
|
187
|
+
// Good: wait for stability
|
|
188
|
+
await page.locator('[data-testid="menu-item"]').waitFor({ state: 'visible' })
|
|
189
|
+
await page.waitForLoadState('networkidle')
|
|
190
|
+
await page.locator('[data-testid="menu-item"]').click()
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Artifact Management
|
|
194
|
+
|
|
195
|
+
### Screenshots
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
await page.screenshot({ path: 'artifacts/after-login.png' })
|
|
199
|
+
await page.screenshot({ path: 'artifacts/full-page.png', fullPage: true })
|
|
200
|
+
await page.locator('[data-testid="chart"]').screenshot({ path: 'artifacts/chart.png' })
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Traces
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
await browser.startTracing(page, {
|
|
207
|
+
path: 'artifacts/trace.json',
|
|
208
|
+
screenshots: true,
|
|
209
|
+
snapshots: true,
|
|
210
|
+
})
|
|
211
|
+
// ... test actions ...
|
|
212
|
+
await browser.stopTracing()
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Video
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
// In playwright.config.ts
|
|
219
|
+
use: {
|
|
220
|
+
video: 'retain-on-failure',
|
|
221
|
+
videosPath: 'artifacts/videos/'
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## CI/CD Integration
|
|
226
|
+
|
|
227
|
+
```yaml
|
|
228
|
+
# .github/workflows/e2e.yml
|
|
229
|
+
name: E2E Tests
|
|
230
|
+
on: [push, pull_request]
|
|
231
|
+
|
|
232
|
+
jobs:
|
|
233
|
+
test:
|
|
234
|
+
runs-on: ubuntu-latest
|
|
235
|
+
steps:
|
|
236
|
+
- uses: actions/checkout@v4
|
|
237
|
+
- uses: actions/setup-node@v4
|
|
238
|
+
with:
|
|
239
|
+
node-version: 20
|
|
240
|
+
- run: npm ci
|
|
241
|
+
- run: npx playwright install --with-deps
|
|
242
|
+
- run: npx playwright test
|
|
243
|
+
env:
|
|
244
|
+
BASE_URL: ${{ vars.STAGING_URL }}
|
|
245
|
+
- uses: actions/upload-artifact@v4
|
|
246
|
+
if: always()
|
|
247
|
+
with:
|
|
248
|
+
name: playwright-report
|
|
249
|
+
path: playwright-report/
|
|
250
|
+
retention-days: 30
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Test Report Template
|
|
254
|
+
|
|
255
|
+
```markdown
|
|
256
|
+
# E2E Test Report
|
|
257
|
+
|
|
258
|
+
**Date:** YYYY-MM-DD HH:MM
|
|
259
|
+
**Duration:** Xm Ys
|
|
260
|
+
**Status:** PASSING / FAILING
|
|
261
|
+
|
|
262
|
+
## Summary
|
|
263
|
+
- Total: X | Passed: Y (Z%) | Failed: A | Flaky: B | Skipped: C
|
|
264
|
+
|
|
265
|
+
## Failed Tests
|
|
266
|
+
|
|
267
|
+
### test-name
|
|
268
|
+
**File:** `tests/e2e/feature.spec.ts:45`
|
|
269
|
+
**Error:** Expected element to be visible
|
|
270
|
+
**Screenshot:** artifacts/failed.png
|
|
271
|
+
**Recommended Fix:** [description]
|
|
272
|
+
|
|
273
|
+
## Artifacts
|
|
274
|
+
- HTML Report: playwright-report/index.html
|
|
275
|
+
- Screenshots: artifacts/*.png
|
|
276
|
+
- Videos: artifacts/videos/*.webm
|
|
277
|
+
- Traces: artifacts/*.zip
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Wallet / Web3 Testing
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
test('wallet connection', async ({ page, context }) => {
|
|
284
|
+
// Mock wallet provider
|
|
285
|
+
await context.addInitScript(() => {
|
|
286
|
+
window.ethereum = {
|
|
287
|
+
isMetaMask: true,
|
|
288
|
+
request: async ({ method }) => {
|
|
289
|
+
if (method === 'eth_requestAccounts')
|
|
290
|
+
return ['0x1234567890123456789012345678901234567890']
|
|
291
|
+
if (method === 'eth_chainId') return '0x1'
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
await page.goto('/')
|
|
297
|
+
await page.locator('[data-testid="connect-wallet"]').click()
|
|
298
|
+
await expect(page.locator('[data-testid="wallet-address"]')).toContainText('0x1234')
|
|
299
|
+
})
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Financial / Critical Flow Testing
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
test('trade execution', async ({ page }) => {
|
|
306
|
+
// Skip on production — real money
|
|
307
|
+
test.skip(process.env.NODE_ENV === 'production', 'Skip on production')
|
|
308
|
+
|
|
309
|
+
await page.goto('/markets/test-market')
|
|
310
|
+
await page.locator('[data-testid="position-yes"]').click()
|
|
311
|
+
await page.locator('[data-testid="trade-amount"]').fill('1.0')
|
|
312
|
+
|
|
313
|
+
// Verify preview
|
|
314
|
+
const preview = page.locator('[data-testid="trade-preview"]')
|
|
315
|
+
await expect(preview).toContainText('1.0')
|
|
316
|
+
|
|
317
|
+
// Confirm and wait for blockchain
|
|
318
|
+
await page.locator('[data-testid="confirm-trade"]').click()
|
|
319
|
+
await page.waitForResponse(
|
|
320
|
+
resp => resp.url().includes('/api/trade') && resp.status() === 200,
|
|
321
|
+
{ timeout: 30000 }
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
await expect(page.locator('[data-testid="trade-success"]')).toBeVisible()
|
|
325
|
+
})
|
|
326
|
+
```
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: error-handling
|
|
3
|
+
description: Patterns for robust error handling across TypeScript, Python, and Go. Covers typed errors, error boundaries, retries, circuit breakers, and user-facing error messages.
|
|
4
|
+
origin: ECC
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Error Handling Patterns
|
|
8
|
+
|
|
9
|
+
Consistent, robust error handling patterns for production applications.
|
|
10
|
+
|
|
11
|
+
## When to Activate
|
|
12
|
+
|
|
13
|
+
- Designing error types or exception hierarchies for a new module or service
|
|
14
|
+
- Adding retry logic or circuit breakers for unreliable external dependencies
|
|
15
|
+
- Reviewing API endpoints for missing error handling
|
|
16
|
+
- Implementing user-facing error messages and feedback
|
|
17
|
+
- Debugging cascading failures or silent error swallowing
|
|
18
|
+
|
|
19
|
+
## Core Principles
|
|
20
|
+
|
|
21
|
+
1. **Fail fast and loudly** — surface errors at the boundary where they occur; don't bury them
|
|
22
|
+
2. **Typed errors over string messages** — errors are first-class values with structure
|
|
23
|
+
3. **User messages ≠ developer messages** — show friendly text to users, log full context server-side
|
|
24
|
+
4. **Never swallow errors silently** — every `catch` block must either handle, re-throw, or log
|
|
25
|
+
5. **Errors are part of your API contract** — document every error code a client may receive
|
|
26
|
+
|
|
27
|
+
## TypeScript / JavaScript
|
|
28
|
+
|
|
29
|
+
### Typed Error Classes
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// Define an error hierarchy for your domain
|
|
33
|
+
export class AppError extends Error {
|
|
34
|
+
constructor(
|
|
35
|
+
message: string,
|
|
36
|
+
public readonly code: string,
|
|
37
|
+
public readonly statusCode: number = 500,
|
|
38
|
+
public readonly details?: unknown,
|
|
39
|
+
) {
|
|
40
|
+
super(message)
|
|
41
|
+
this.name = this.constructor.name
|
|
42
|
+
// Maintain correct prototype chain in transpiled ES5 JavaScript.
|
|
43
|
+
// Required for `instanceof` checks (e.g., `error instanceof NotFoundError`)
|
|
44
|
+
// to work correctly when extending the built-in Error class.
|
|
45
|
+
Object.setPrototypeOf(this, new.target.prototype)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export class NotFoundError extends AppError {
|
|
50
|
+
constructor(resource: string, id: string) {
|
|
51
|
+
super(`${resource} not found: ${id}`, 'NOT_FOUND', 404)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export class ValidationError extends AppError {
|
|
56
|
+
constructor(message: string, details: { field: string; message: string }[]) {
|
|
57
|
+
super(message, 'VALIDATION_ERROR', 422, details)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class UnauthorizedError extends AppError {
|
|
62
|
+
constructor(reason = 'Authentication required') {
|
|
63
|
+
super(reason, 'UNAUTHORIZED', 401)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class RateLimitError extends AppError {
|
|
68
|
+
constructor(public readonly retryAfterMs: number) {
|
|
69
|
+
super('Rate limit exceeded', 'RATE_LIMITED', 429)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Result Pattern (no-throw style)
|
|
75
|
+
|
|
76
|
+
For operations where failure is expected and common (parsing, external calls):
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
type Result<T, E = AppError> =
|
|
80
|
+
| { ok: true; value: T }
|
|
81
|
+
| { ok: false; error: E }
|
|
82
|
+
|
|
83
|
+
function ok<T>(value: T): Result<T> {
|
|
84
|
+
return { ok: true, value }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function err<E>(error: E): Result<never, E> {
|
|
88
|
+
return { ok: false, error }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Usage
|
|
92
|
+
async function fetchUser(id: string): Promise<Result<User>> {
|
|
93
|
+
try {
|
|
94
|
+
const user = await db.users.findUnique({ where: { id } })
|
|
95
|
+
if (!user) return err(new NotFoundError('User', id))
|
|
96
|
+
return ok(user)
|
|
97
|
+
} catch (e) {
|
|
98
|
+
return err(new AppError('Database error', 'DB_ERROR'))
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const result = await fetchUser('abc-123')
|
|
103
|
+
if (!result.ok) {
|
|
104
|
+
// TypeScript knows result.error here
|
|
105
|
+
logger.error('Failed to fetch user', { error: result.error })
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
// TypeScript knows result.value here
|
|
109
|
+
console.log(result.value.email)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### API Error Handler (Next.js / Express)
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { NextRequest, NextResponse } from 'next/server'
|
|
116
|
+
|
|
117
|
+
function handleApiError(error: unknown): NextResponse {
|
|
118
|
+
// Known application error
|
|
119
|
+
if (error instanceof AppError) {
|
|
120
|
+
return NextResponse.json(
|
|
121
|
+
{
|
|
122
|
+
error: {
|
|
123
|
+
code: error.code,
|
|
124
|
+
message: error.message,
|
|
125
|
+
...(error.details ? { details: error.details } : {}),
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
{ status: error.statusCode },
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Zod validation error
|
|
133
|
+
if (error instanceof z.ZodError) {
|
|
134
|
+
return NextResponse.json(
|
|
135
|
+
{
|
|
136
|
+
error: {
|
|
137
|
+
code: 'VALIDATION_ERROR',
|
|
138
|
+
message: 'Request validation failed',
|
|
139
|
+
details: error.issues.map(i => ({
|
|
140
|
+
field: i.path.join('.'),
|
|
141
|
+
message: i.message,
|
|
142
|
+
})),
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{ status: 422 },
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Unexpected error — log details, return generic message
|
|
150
|
+
console.error('Unexpected error:', error)
|
|
151
|
+
return NextResponse.json(
|
|
152
|
+
{ error: { code: 'INTERNAL_ERROR', message: 'An unexpected error occurred' } },
|
|
153
|
+
{ status: 500 },
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function POST(req: NextRequest) {
|
|
158
|
+
try {
|
|
159
|
+
// ... handler logic
|
|
160
|
+
} catch (error) {
|
|
161
|
+
return handleApiError(error)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### React Error Boundary
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import { Component, ErrorInfo, ReactNode } from 'react'
|
|
170
|
+
|
|
171
|
+
interface Props {
|
|
172
|
+
fallback: ReactNode
|
|
173
|
+
onError?: (error: Error, info: ErrorInfo) => void
|
|
174
|
+
children: ReactNode
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface State {
|
|
178
|
+
hasError: boolean
|
|
179
|
+
error: Error | null
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export class ErrorBoundary extends Component<Props, State> {
|
|
183
|
+
state: State = { hasError: false, error: null }
|
|
184
|
+
|
|
185
|
+
static getDerivedStateFromError(error: Error): State {
|
|
186
|
+
return { hasError: true, error }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
componentDidCatch(error: Error, info: ErrorInfo) {
|
|
190
|
+
this.props.onError?.(error, info)
|
|
191
|
+
console.error('Unhandled React error:', error, info)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
render() {
|
|
195
|
+
if (this.state.hasError) return this.props.fallback
|
|
196
|
+
return this.props.children
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Usage
|
|
201
|
+
<ErrorBoundary fallback={<p>Something went wrong. Please refresh.</p>}>
|
|
202
|
+
<MyComponent />
|
|
203
|
+
</ErrorBoundary>
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Python
|
|
207
|
+
|
|
208
|
+
### Custom Exception Hierarchy
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
class AppError(Exception):
|
|
212
|
+
"""Base application error."""
|
|
213
|
+
def __init__(self, message: str, code: str, status_code: int = 500):
|
|
214
|
+
super().__init__(message)
|
|
215
|
+
self.code = code
|
|
216
|
+
self.status_code = status_code
|
|
217
|
+
|
|
218
|
+
class NotFoundError(AppError):
|
|
219
|
+
def __init__(self, resource: str, id: str):
|
|
220
|
+
super().__init__(f"{resource} not found: {id}", "NOT_FOUND", 404)
|
|
221
|
+
|
|
222
|
+
class ValidationError(AppError):
|
|
223
|
+
def __init__(self, message: str, details: list[dict] | None = None):
|
|
224
|
+
super().__init__(message, "VALIDATION_ERROR", 422)
|
|
225
|
+
self.details = details or []
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### FastAPI Global Exception Handler
|
|
229
|
+
|
|
230
|
+
```python
|
|
231
|
+
from fastapi import FastAPI, Request
|
|
232
|
+
from fastapi.responses import JSONResponse
|
|
233
|
+
|
|
234
|
+
app = FastAPI()
|
|
235
|
+
|
|
236
|
+
@app.exception_handler(AppError)
|
|
237
|
+
async def app_error_handler(request: Request, exc: AppError) -> JSONResponse:
|
|
238
|
+
return JSONResponse(
|
|
239
|
+
status_code=exc.status_code,
|
|
240
|
+
content={"error": {"code": exc.code, "message": str(exc)}},
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
@app.exception_handler(Exception)
|
|
244
|
+
async def generic_error_handler(request: Request, exc: Exception) -> JSONResponse:
|
|
245
|
+
# Log full details, return generic message
|
|
246
|
+
logger.exception("Unexpected error", exc_info=exc)
|
|
247
|
+
return JSONResponse(
|
|
248
|
+
status_code=500,
|
|
249
|
+
content={"error": {"code": "INTERNAL_ERROR", "message": "An unexpected error occurred"}},
|
|
250
|
+
)
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Go
|
|
254
|
+
|
|
255
|
+
### Sentinel Errors and Error Wrapping
|
|
256
|
+
|
|
257
|
+
```go
|
|
258
|
+
package domain
|
|
259
|
+
|
|
260
|
+
import "errors"
|
|
261
|
+
|
|
262
|
+
// Sentinel errors for type-checking
|
|
263
|
+
var (
|
|
264
|
+
ErrNotFound = errors.New("not found")
|
|
265
|
+
ErrUnauthorized = errors.New("unauthorized")
|
|
266
|
+
ErrConflict = errors.New("conflict")
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
// Wrap errors with context — never lose the original
|
|
270
|
+
func (r *UserRepository) FindByID(ctx context.Context, id string) (*User, error) {
|
|
271
|
+
user, err := r.db.QueryRow(ctx, "SELECT * FROM users WHERE id = $1", id)
|
|
272
|
+
if errors.Is(err, sql.ErrNoRows) {
|
|
273
|
+
return nil, fmt.Errorf("user %s: %w", id, ErrNotFound)
|
|
274
|
+
}
|
|
275
|
+
if err != nil {
|
|
276
|
+
return nil, fmt.Errorf("querying user %s: %w", id, err)
|
|
277
|
+
}
|
|
278
|
+
return user, nil
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// At the handler level, unwrap to determine response
|
|
282
|
+
func (h *Handler) GetUser(w http.ResponseWriter, r *http.Request) {
|
|
283
|
+
user, err := h.service.GetUser(r.Context(), chi.URLParam(r, "id"))
|
|
284
|
+
if err != nil {
|
|
285
|
+
switch {
|
|
286
|
+
case errors.Is(err, domain.ErrNotFound):
|
|
287
|
+
writeError(w, http.StatusNotFound, "not_found", err.Error())
|
|
288
|
+
case errors.Is(err, domain.ErrUnauthorized):
|
|
289
|
+
writeError(w, http.StatusForbidden, "forbidden", "Access denied")
|
|
290
|
+
default:
|
|
291
|
+
slog.Error("unexpected error", "err", err)
|
|
292
|
+
writeError(w, http.StatusInternalServerError, "internal_error", "An unexpected error occurred")
|
|
293
|
+
}
|
|
294
|
+
return
|
|
295
|
+
}
|
|
296
|
+
writeJSON(w, http.StatusOK, user)
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
## Retry with Exponential Backoff
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
interface RetryOptions {
|
|
304
|
+
maxAttempts?: number
|
|
305
|
+
baseDelayMs?: number
|
|
306
|
+
maxDelayMs?: number
|
|
307
|
+
retryIf?: (error: unknown) => boolean
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async function withRetry<T>(
|
|
311
|
+
fn: () => Promise<T>,
|
|
312
|
+
options: RetryOptions = {},
|
|
313
|
+
): Promise<T> {
|
|
314
|
+
const {
|
|
315
|
+
maxAttempts = 3,
|
|
316
|
+
baseDelayMs = 500,
|
|
317
|
+
maxDelayMs = 10_000,
|
|
318
|
+
retryIf = () => true,
|
|
319
|
+
} = options
|
|
320
|
+
|
|
321
|
+
let lastError: unknown
|
|
322
|
+
|
|
323
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
324
|
+
try {
|
|
325
|
+
return await fn()
|
|
326
|
+
} catch (error) {
|
|
327
|
+
lastError = error
|
|
328
|
+
if (attempt === maxAttempts || !retryIf(error)) throw error
|
|
329
|
+
|
|
330
|
+
const jitter = Math.random() * baseDelayMs
|
|
331
|
+
const delay = Math.min(baseDelayMs * 2 ** (attempt - 1) + jitter, maxDelayMs)
|
|
332
|
+
await new Promise(resolve => setTimeout(resolve, delay))
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
throw lastError
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Usage: retry transient network errors, not 4xx
|
|
340
|
+
const data = await withRetry(() => fetch('/api/data').then(r => r.json()), {
|
|
341
|
+
maxAttempts: 3,
|
|
342
|
+
retryIf: (error) => !(error instanceof AppError && error.statusCode < 500),
|
|
343
|
+
})
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
## User-Facing Error Messages
|
|
347
|
+
|
|
348
|
+
Map error codes to human-readable messages. Keep technical details out of user-visible text.
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
const USER_ERROR_MESSAGES: Record<string, string> = {
|
|
352
|
+
NOT_FOUND: 'The requested item could not be found.',
|
|
353
|
+
UNAUTHORIZED: 'Please sign in to continue.',
|
|
354
|
+
FORBIDDEN: "You don't have permission to do that.",
|
|
355
|
+
VALIDATION_ERROR: 'Please check your input and try again.',
|
|
356
|
+
RATE_LIMITED: 'Too many requests. Please wait a moment and try again.',
|
|
357
|
+
INTERNAL_ERROR: 'Something went wrong on our end. Please try again later.',
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function getUserMessage(code: string): string {
|
|
361
|
+
return USER_ERROR_MESSAGES[code] ?? USER_ERROR_MESSAGES.INTERNAL_ERROR
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
## Error Handling Checklist
|
|
366
|
+
|
|
367
|
+
Before merging any code that touches error handling:
|
|
368
|
+
|
|
369
|
+
- [ ] Every `catch` block handles, re-throws, or logs — no silent swallowing
|
|
370
|
+
- [ ] API errors follow the standard envelope `{ error: { code, message } }`
|
|
371
|
+
- [ ] User-facing messages contain no stack traces or internal details
|
|
372
|
+
- [ ] Full error context is logged server-side
|
|
373
|
+
- [ ] Custom error classes extend a base `AppError` with a `code` field
|
|
374
|
+
- [ ] Async functions surface errors to callers — no fire-and-forget without fallback
|
|
375
|
+
- [ ] Retry logic only retries retriable errors (not 4xx client errors)
|
|
376
|
+
- [ ] React components are wrapped in `ErrorBoundary` for rendering errors
|