Skip to main content

🧪 Testing Strategy - Phase 2移行の軽量テスト戦略

🎯 目標: 最小工数で最大品質保証、Phase 2移行時の重厚テスト基盤準備


📋 テスト戦略概要

🏃‍♂️ Phase 1: 軽量テスト (現在)

原則: "Critical Path Only"
工数: 開発時間の15% (業界標準30%の半分)
対象: API・Queue・認証のハッピーパス
品質ゲート: 90%成功率でリリース可

🏋️‍♂️ Phase 2: 企業級テスト (将来)

原則: "Zero Defect Production"
工数: 開発時間の35% (エンタープライズ水準)
対象: 全シナリオ・エッジケース・負荷テスト
品質ゲート: 99.9%成功率でリリース可

Phase 1: 軽量テスト実装

🔥 Critical Path Testing (工数: 2-3日)

1. API Core Tests

// tests/api/core.test.ts - 15分で実装
describe('Critical API Endpoints', () => {
test('Health Check', async () => {
const response = await request(app).get('/api/health')
expect(response.status).toBe(200)
expect(response.body.status).toBe('ok')
})

test('Order Creation (Happy Path)', async () => {
const orderData = { brand: 'neko', photo_ids: ['test_photo'] }
const response = await request(app)
.post('/api/orders/create')
.set('Authorization', `Bearer ${mockFirebaseToken}`)
.send(orderData)

expect(response.status).toBe(200)
expect(response.body.order_id).toBeDefined()
expect(response.body.status).toBe('processing')
})

test('Photo Upload (Happy Path)', async () => {
const response = await request(app)
.post('/api/photos/upload')
.set('Authorization', `Bearer ${mockFirebaseToken}`)
.attach('photo', mockImageBuffer, 'test.jpg')

expect(response.status).toBe(200)
expect(response.body.photo_id).toBeDefined()
})
})

2. Queue Processing Tests

// tests/queue/core.test.ts - 10分で実装
describe('Queue Core Functions', () => {
test('Order Queue Processing', async () => {
const mockEvent = {
type: 'ORDER_CONFIRMED',
order_id: 'test_order',
brand: 'neko',
idempotency_key: 'test_key'
}

const result = await processOrderEvent(mockEvent, mockEnv)
expect(result.success).toBe(true)

// Idempotency確認
const duplicate = await processOrderEvent(mockEvent, mockEnv)
expect(duplicate.duplicate).toBe(true)
})

test('Queue Error Handling', async () => {
const invalidEvent = { type: 'INVALID_EVENT' }
const result = await processOrderEvent(invalidEvent, mockEnv)
expect(result.success).toBe(false)
expect(result.retry).toBe(true)
})
})

3. Authentication Tests

// tests/auth/core.test.ts - 5分で実装  
describe('Authentication Core', () => {
test('Valid Firebase Token', async () => {
const validToken = await generateMockFirebaseToken()
const user = await verifyFirebaseToken(validToken)
expect(user.uid).toBeDefined()
})

test('Invalid Token Rejection', async () => {
const invalidToken = 'invalid_token'
await expect(verifyFirebaseToken(invalidToken)).rejects.toThrow()
})
})

🛠️ Mock & Test Data (工数: 1日)

軽量Mock実装

// tests/mocks/services.ts
export const mockShopifyAPI = {
orders: {
create: jest.fn().mockResolvedValue({ id: 'shopify_order_123' })
},
customers: {
get: jest.fn().mockResolvedValue({ id: 'customer_456' })
}
}

export const mockFirebaseToken = 'mock_firebase_token_123'
export const mockImageBuffer = Buffer.from('fake_image_data')

// D1 Mock
export const mockD1 = {
prepare: jest.fn().mockReturnValue({
bind: jest.fn().mockReturnValue({
first: jest.fn().mockResolvedValue(null),
run: jest.fn().mockResolvedValue({ success: true })
})
})
}

📊 Test Configuration (工数: 30分)

package.json scripts

{
"scripts": {
"test": "jest --coverage --watchAll=false",
"test:watch": "jest --watch",
"test:ci": "jest --coverage --watchAll=false --ci",
"test:integration": "jest --testPathPattern=integration",
"test:unit": "jest --testPathPattern=unit"
}
}

jest.config.js

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/tests'],
coverageThreshold: {
global: {
branches: 70,
functions: 80,
lines: 80,
statements: 80
}
},
coveragePathIgnorePatterns: [
'/node_modules/',
'/tests/mocks/',
'/tests/fixtures/'
]
}

🚀 実行方法

⚡ 日常開発時

# 🔄 開発中の継続テスト
npm run test:watch

# ✅ コミット前チェック
npm test

# 📊 カバレッジ確認
npm test -- --coverage

🏗️ CI/CD Pipeline

# .github/workflows/test.yml (軽量版)
name: Fast Tests
on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '18' }
- run: npm ci
- run: npm run test:ci
- name: Upload Coverage
uses: codecov/codecov-action@v3

🎯 品質ゲート定義

Phase 1 品質基準

Required (リリースブロック):
- ✅ Health Check API: 100%成功
- ✅ Order Creation: 100%成功
- ✅ Photo Upload: 100%成功
- ✅ Queue Processing: 95%成功
- ✅ Authentication: 100%成功

Optional (改善推奨):
- 📊 Unit Test Coverage: >70%
- 🐛 Integration Tests: >80%成功
- ⚡ Test Execution: <30秒

🔮 Phase 2 拡張プラン (将来)

🏋️‍♂️ エンタープライズテスト機能

追加予定機能:
Load Testing:
- 1000 concurrent users simulation
- Shopify API rate limit stress test
- Queue system throughput testing

Security Testing:
- JWT token vulnerability scan
- SQL injection prevention test
- PII leak detection automation

E2E Testing:
- Complete user journey (iOS/Android/Web)
- Cross-browser compatibility
- Payment flow integration test

Performance Testing:
- P95 response time validation (<200ms)
- Memory leak detection
- Database query optimization verification

Chaos Engineering:
- Shopify API failure simulation
- Network partition testing
- Database connection failure recovery

🎖️ Phase 2 品質基準

Enterprise Quality Gates:
- 🎯 Functional Coverage: >95%
- 🔒 Security Scan: 0 Critical issues
- ⚡ Performance: P95 < 200ms
- 🛡️ Reliability: 99.9% uptime simulation
- 📱 Cross-Platform: iOS + Android + Web

💡 実装優先順位

Week 1: 基盤構築

  1. ✅ Core API Tests (3時間)
  2. ✅ Queue Processing Tests (2時間)
  3. ✅ Authentication Tests (1時間)

Week 2: 自動化

  1. 🔧 CI/CD Pipeline (2時間)
  2. 📊 Coverage Reporting (1時間)
  3. 🛠️ Mock Data Enhancement (1時間)

Phase 2 移行時

  1. 🚀 Load Testing Framework
  2. 🔒 Security Testing Suite
  3. 📱 E2E Testing Platform
  4. 🎯 Performance Monitoring

🎊 Result: 最小工数(7-8時間)で Critical Path の品質保証を実現、Phase 2 での企業級テスト基盤への移行準備完了。

関連ドキュメント: 開発環境構築 | 品質基準 | Phase 1実装