test.yml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. name: 🧪 Test Suite
  2. on:
  3. push:
  4. branches: [main, dev]
  5. pull_request:
  6. branches: [main, dev]
  7. # 取消同一分支的进行中的工作流
  8. concurrency:
  9. group: ${{ github.workflow }}-${{ github.ref }}
  10. cancel-in-progress: true
  11. jobs:
  12. # ==================== 代码质量检查 ====================
  13. quality:
  14. name: 📋 Code Quality
  15. runs-on: ubuntu-latest
  16. steps:
  17. - name: Checkout code
  18. uses: actions/checkout@v4
  19. - name: Setup Bun
  20. uses: oven-sh/setup-bun@v2
  21. with:
  22. bun-version: latest
  23. - name: Install dependencies
  24. run: bun install --frozen-lockfile
  25. - name: Run linting
  26. run: bun run lint
  27. - name: Run type checking
  28. run: bun run typecheck
  29. - name: Check formatting
  30. run: bun run format:check
  31. # ==================== 单元测试 ====================
  32. unit-tests:
  33. name: ⚡ Unit Tests
  34. runs-on: ubuntu-latest
  35. steps:
  36. - name: Checkout code
  37. uses: actions/checkout@v4
  38. - name: Setup Bun
  39. uses: oven-sh/setup-bun@v2
  40. - name: Install dependencies
  41. run: bun install --frozen-lockfile
  42. - name: Run unit tests
  43. run: bun run test -- tests/unit/ --passWithNoTests
  44. # ==================== 集成测试(需要数据库)====================
  45. integration-tests:
  46. name: 🔗 Integration Tests
  47. runs-on: ubuntu-latest
  48. services:
  49. postgres:
  50. image: postgres:16-alpine
  51. env:
  52. POSTGRES_USER: test_user
  53. POSTGRES_PASSWORD: test_password
  54. POSTGRES_DB: claude_code_hub_test
  55. options: >-
  56. --health-cmd "pg_isready -U test_user -d claude_code_hub_test"
  57. --health-interval 10s
  58. --health-timeout 5s
  59. --health-retries 5
  60. ports:
  61. - 5432:5432
  62. redis:
  63. image: redis:7-alpine
  64. options: >-
  65. --health-cmd "redis-cli ping"
  66. --health-interval 10s
  67. --health-timeout 5s
  68. --health-retries 5
  69. ports:
  70. - 6379:6379
  71. env:
  72. DSN: postgres://test_user:test_password@localhost:5432/claude_code_hub_test
  73. REDIS_URL: redis://localhost:6379/1
  74. ADMIN_TOKEN: test-admin-token-for-ci
  75. AUTO_MIGRATE: true
  76. ENABLE_RATE_LIMIT: true
  77. SESSION_TTL: 300
  78. steps:
  79. - name: Checkout code
  80. uses: actions/checkout@v4
  81. - name: Setup Bun
  82. uses: oven-sh/setup-bun@v2
  83. - name: Install dependencies
  84. run: bun install --frozen-lockfile
  85. - name: Run database migrations
  86. run: bun run db:migrate
  87. - name: Run integration tests
  88. run: bun run test -- tests/integration/ --passWithNoTests
  89. # ==================== API 测试(需要运行服务)====================
  90. api-tests:
  91. name: 🌐 API Tests
  92. runs-on: ubuntu-latest
  93. services:
  94. postgres:
  95. image: postgres:16-alpine
  96. env:
  97. POSTGRES_USER: test_user
  98. POSTGRES_PASSWORD: test_password
  99. POSTGRES_DB: claude_code_hub_test
  100. options: >-
  101. --health-cmd "pg_isready -U test_user -d claude_code_hub_test"
  102. --health-interval 10s
  103. --health-timeout 5s
  104. --health-retries 5
  105. ports:
  106. - 5432:5432
  107. redis:
  108. image: redis:7-alpine
  109. options: >-
  110. --health-cmd "redis-cli ping"
  111. --health-interval 10s
  112. --health-timeout 5s
  113. --health-retries 5
  114. ports:
  115. - 6379:6379
  116. env:
  117. DSN: postgres://test_user:test_password@localhost:5432/claude_code_hub_test
  118. REDIS_URL: redis://localhost:6379/1
  119. ADMIN_TOKEN: test-admin-token-for-ci
  120. AUTO_MIGRATE: true
  121. PORT: 13500
  122. ENABLE_RATE_LIMIT: true
  123. SESSION_TTL: 300
  124. steps:
  125. - name: Checkout code
  126. uses: actions/checkout@v4
  127. - name: Setup Bun
  128. uses: oven-sh/setup-bun@v2
  129. - name: Install dependencies
  130. run: bun install --frozen-lockfile
  131. - name: Run database migrations
  132. run: bun run db:migrate
  133. - name: Build application
  134. run: bun run build
  135. - name: Start server (background)
  136. run: |
  137. bun run start &
  138. echo $! > server.pid
  139. sleep 15 # 等待服务启动
  140. - name: Wait for server ready
  141. run: |
  142. timeout 60 bash -c 'until curl -f http://localhost:13500/api/actions/health; do sleep 2; done'
  143. - name: Run E2E API tests
  144. run: bun run test:e2e
  145. env:
  146. API_BASE_URL: http://localhost:13500/api/actions
  147. TEST_ADMIN_TOKEN: test-admin-token-for-ci
  148. AUTO_CLEANUP_TEST_DATA: true
  149. - name: Stop server
  150. if: always()
  151. run: |
  152. if [ -f server.pid ]; then
  153. kill $(cat server.pid) || true
  154. fi
  155. # ==================== 测试结果汇总 ====================
  156. test-summary:
  157. name: 📊 Test Summary
  158. runs-on: ubuntu-latest
  159. needs: [quality, unit-tests, integration-tests, api-tests]
  160. if: always()
  161. steps:
  162. - name: Check test results
  163. run: |
  164. if [ "${{ needs.quality.result }}" != "success" ] || \
  165. [ "${{ needs.unit-tests.result }}" != "success" ] || \
  166. [ "${{ needs.integration-tests.result }}" != "success" ] || \
  167. [ "${{ needs.api-tests.result }}" != "success" ]; then
  168. echo "❌ 部分测试失败"
  169. exit 1
  170. else
  171. echo "✅ 所有测试通过"
  172. fi
  173. - name: Create summary
  174. if: github.event_name == 'pull_request'
  175. uses: actions/github-script@v7
  176. with:
  177. script: |
  178. const summary = `## 🧪 测试结果
  179. | 测试类型 | 状态 |
  180. |---------|------|
  181. | 代码质量 | ${{ needs.quality.result == 'success' && '✅' || '❌' }} |
  182. | 单元测试 | ${{ needs.unit-tests.result == 'success' && '✅' || '❌' }} |
  183. | 集成测试 | ${{ needs.integration-tests.result == 'success' && '✅' || '❌' }} |
  184. | API 测试 | ${{ needs.api-tests.result == 'success' && '✅' || '❌' }} |
  185. **总体结果**: ${{ (needs.quality.result == 'success' && needs.unit-tests.result == 'success' && needs.integration-tests.result == 'success' && needs.api-tests.result == 'success') && '✅ 所有测试通过' || '❌ 部分测试失败' }}
  186. `;
  187. github.rest.issues.createComment({
  188. issue_number: context.issue.number,
  189. owner: context.repo.owner,
  190. repo: context.repo.repo,
  191. body: summary
  192. });