name: Test

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - main

permissions:
  contents: write

jobs:
  remove-cache:
    runs-on: ubuntu-latest
    permissions: write-all
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install github cli
        run: |
          (type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \
          && sudo mkdir -p -m 755 /etc/apt/keyrings \
          && wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
          && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
          && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
          && sudo apt update \
          && sudo apt install gh -y

      - run: |
          gh cache delete --all || true
        env:
          GH_TOKEN: ${{ github.token }}

  setup-bun:
    runs-on: ${{ matrix.os }}
    continue-on-error: true
    needs: [remove-cache]
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
        bun-version:
          - latest
          - canary
          - "1.1.0"
          - "1.x"
          - "1"
          - "> 1.0.0"
          - "< 2"
          - "08a9b6a98b14d3c23b58080bea31513546246c50"
          # Disable <sha> support for now. This is because Github Artifacts
          # expire after 90 days, and we don't have another source of truth yet.
          # - "822a00c4d508b54f650933a73ca5f4a3af9a7983" # 1.0.0 commit
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Bun
        uses: ./
        id: setup_bun
        with:
          bun-version: ${{ matrix.bun-version }}

      - name: Run Bun
        id: run_bun
        run: |
          bun --version

  setup-bun-from-file:
    name: setup-bun from (${{ matrix.os }}, ${{ matrix.file.name }})
    runs-on: ${{ matrix.os }}
    continue-on-error: true
    needs: [remove-cache]
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
        file:
          - name: package.json (bun@1.1.0)
            file: package.json
            run: |
              echo "$(jq '. += {"packageManager": "bun@1.1.0"}' package.json)" > package.json
          - name: package.json (yarn@bun@1.1.0)
            file: package.json
            run: |
              echo "$(jq '. += {"packageManager": "yarn@bun@1.1.0"}' package.json)" > package.json
          - name: .tool-versions (bun 1.1.0)
            file: .tool-versions
            run: |
              echo "bun 1.1.0" > .tool-versions
          - name: .tool-versions (bun1.1.0)
            file: .tool-versions
            run: |
              echo "bun1.1.0" > .tool-versions
          - name: .bumrc (1.1.0)
            file: .bumrc
            run: |
              echo "1.1.0" > .bumrc
          - name: .bun-version (1.1.0)
            file: .bun-version
            run: |
              echo "1.1.0" > .bun-version
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup file
        run: ${{ matrix.file.run }}

      - name: Setup Bun
        uses: ./
        with:
          bun-version-file: ${{ matrix.file.file }}

      - name: Compare versions
        uses: ./.github/actions/compare-bun-version
        with:
          bun-version: "1.1.0"
