name: Test

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  # L1 单元测试
  unit:
    name: Unit Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.26'
          cache: true

      - name: Download dependencies
        run: go mod download

      - name: Run unit tests
        run: make test

      - name: Run linter
        uses: golangci/golangci-lint-action@v6
        with:
          version: latest

  # L2 集成测试（无需 Docker）
  integration:
    name: Integration Tests (L2)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.26'
          cache: true

      - name: Download dependencies
        run: go mod download

      - name: Run integration tests
        run: make test-integration

  # L3 E2E 测试（需要 Docker）
  e2e:
    name: E2E Tests (L3)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.26'
          cache: true

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Download dependencies
        run: go mod download

      - name: Run E2E tests
        run: make test-e2e
        continue-on-error: true  # E2E 测试可能因 Docker 限制失败

  # 构建验证
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.26'
          cache: true

      - name: Build
        run: make build

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: lolly
          path: bin/lolly

  # 代码覆盖率
  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.26'
          cache: true

      - name: Generate coverage report
        run: make test-cover

      - name: Upload coverage
        uses: codecov/codecov-action@v4
        with:
          files: ./coverage.out
          fail_ci_if_error: false
