name: CI

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read
  checks: write
  pull-requests: write

jobs:
  test:
    name: lint-and-unit-test
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: oven-sh/setup-bun@v1
        with:
          bun-version: 1.3.3
      - uses: actions/cache@v4
        id: cache
        with:
          path: ~/.bun/install/cache
          key: ${{ runner.os }}-${{ matrix.os }}-bun-${{ hashFiles('**/bun.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.os }}-bun-
      - run: bun install --frozen-lockfile --linker=isolated
      - run: bun run lint
      - name: Run tests
        run: |
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            # Run tests with increased timeout on Windows to handle slower file operations
            bun test --timeout=15000 --reporter=junit --reporter-outfile=test-results.xml
          else
            # Run tests with increased timeout to handle Bun shell operations
            bun test --timeout=10000 --reporter=junit --reporter-outfile=test-results.xml
          fi
        shell: bash

  build-test:
    name: compile-and-smoke-test
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: bun-linux-x64-baseline
          - os: macos-latest
            target: bun-darwin-x64
          - os: windows-latest
            target: bun-windows-x64-baseline
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: oven-sh/setup-bun@v1
        with:
          bun-version: 1.3.3
      - uses: actions/cache@v4
        id: cache
        with:
          path: ~/.bun/install/cache
          key: ${{ runner.os }}-${{ matrix.os }}-bun-${{ hashFiles('**/bun.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.os }}-bun-
      - run: bun install --frozen-lockfile --linker=isolated
      - name: Prime Bun cache for baseline target (Windows workaround)
        if: ${{ contains(matrix.target, 'baseline') && matrix.os == 'windows-latest' }}
        shell: bash
        run: |
          # Workaround for https://github.com/oven-sh/bun/issues/13513
          # Build a dummy project from C:\ to prime the baseline binary cache
          cd /c
          mkdir -p bun-cache-primer
          cd bun-cache-primer
          echo 'console.log("cache primer")' > index.js
          bun build --compile --target=${{ matrix.target }} ./index.js --outfile primer.exe || true
          cd $GITHUB_WORKSPACE
      - name: Build standalone binary
        shell: bash
        run: |
          VER="$(jq -r .version package.json)"
          OUT="backlog-test${{ contains(matrix.target,'windows') && '.exe' || '' }}"
          bun build src/cli.ts \
            --compile --minify --sourcemap \
            --target=${{ matrix.target }} \
            --define __EMBEDDED_VERSION__="\"${VER}\"" \
            --outfile="$OUT"
      - name: Smoke-test binary
        shell: bash
        run: |
          FILE="backlog-test${{ contains(matrix.target,'windows') && '.exe' || '' }}"
          chmod +x "$FILE"
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            powershell -command ".\\$FILE --version"
            powershell -command ".\\$FILE --help"
          else
            "./$FILE" --version
            "./$FILE" --help
          fi
