name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [20, 22]

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          # no cache — lockfile not committed (platform-specific native deps)

      - run: npm install
      - run: npx tsc --noEmit
      # Build BEFORE test. Examples in test/examples-smoke.test.ts import
      # from the package name 'agentfootprint' (not relative paths), and
      # Node's resolver looks up the local package's main/exports which
      # point to dist/. Without an upstream build, those imports fail
      # with "Failed to resolve entry for package 'agentfootprint'".
      - run: npm run build
      - run: npm test

  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          # no cache — lockfile not committed (platform-specific native deps)

      - run: npm install
      - run: npm run lint
      - run: npm run format

  # Docs gate (option-(a) enforcement): builds the docs-site, which
  # reads every <CodeFile path="..." region="..." /> at build time via
  # Node fs + regionExtractor. A missing file -> ENOENT. A missing region
  # -> RegionNotFoundError. Either kills the build. Drift becomes
  # structurally impossible: docs and examples can't diverge because
  # there is no docs code, only imports of code that already exists in
  # examples/ (which the test job already proves compiles + runs).
  docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Install root deps (so docs-site can resolve imports)
        run: npm install

      - name: Install docs-site deps
        working-directory: docs-site
        run: npm install

      - name: Build docs-site (gates every CodeFile import)
        working-directory: docs-site
        run: npm run build

  # Coverage gate — runs vitest with v8 coverage; uploads lcov to Codecov.
  # No threshold enforcement (per design memo); badge surfaces the number
  # so it stays visible without becoming a hostage to chasing the metric.
  coverage:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - run: npm install
      - run: npm run build
      - run: npm run test:coverage

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          files: ./coverage/lcov.info
          flags: unittests
          fail_ci_if_error: false
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
