name: Puppeteer Canary CI

# Declare default permissions as read only.
permissions: read-all

on:
  schedule:
    # Run everyday at: https://crontab.guru/#0_7_*_*_1-5 UTC
    - cron: '0 7 * * 1-5'
  workflow_dispatch:

jobs:
  chrome-canary-tests:
    name: ${{ matrix.suite }} tests on ${{ matrix.os }} (${{ matrix.shard }}) ${{ matrix.configs }}
    runs-on: ${{ matrix.os }}
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        suite:
          - chrome-headless
          - chrome-headful
          - chrome-headless-shell
          - chrome-bidi
        os:
          - ubuntu-latest
          - windows-latest
          - macos-15
        shard:
          - 1-2
          - 2-2
        configs:
          - stable
        exclude:
          - os: windows-latest
            suite: chrome-bidi
          - os: macos-15
            suite: chrome-headful
    steps:
      - name: Check out repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Set up Node.js
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          cache: npm
          node-version-file: '.nvmrc'
      - name: Disable AppArmor
        if: ${{ matrix.os == 'ubuntu-latest' }}
        run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns
      - name: Install dependencies
        run: npm ci
        env:
          PUPPETEER_SKIP_DOWNLOAD: true
      # Set up GitHub Actions caching for Wireit.
      - uses: google/wireit@9d7c2e5346f8ef1b1ab6014c4e96c74e234f7ed5 # setup-github-actions-caching/v2.0.4
      - name: Build packages
        run: npm run build -w @puppeteer-test/test
      - name: Install browsers
        run: npm run postinstall
        env:
          PUPPETEER_CHROME_VERSION: canary
          PUPPETEER_CHROME_HEADLESS_SHELL_VERSION: canary
          PUPPETEER_FIREFOX_SKIP_DOWNLOAD: true
      - name: Apply Canary expectations
        run: node tools/merge-canary-test-expectations.mjs
      - name: Run all tests (MacOS)
        if: ${{ matrix.os == 'macos-15' }}
        run: npm run test -- --ignore-unexpectedly-passing --shard '${{ matrix.shard }}' --test-suite ${{ matrix.suite }} --save-stats-to /tmp/artifacts/${{ github.event_name }}_INSERTID.json
        env:
          PUPPETEER_CHROME_VERSION: canary
          PUPPETEER_CHROME_HEADLESS_SHELL_VERSION: canary
          PUPPETEER_CHROME_HEADLESS_SHELL_SKIP_DOWNLOAD: true
          PUPPETEER_TEST_EXPERIMENTAL_CHROME_FEATURES: ${{ matrix.configs == 'experimental' }}
      - name: Run all tests (Windows)
        if: ${{ matrix.os == 'windows-latest' }}
        run: npm run test -- --ignore-unexpectedly-passing --shard '${{ matrix.shard }}' --test-suite ${{ matrix.suite }} --save-stats-to /tmp/artifacts/${{ github.event_name }}_INSERTID.json
        env:
          PUPPETEER_CHROME_VERSION: canary
          PUPPETEER_CHROME_HEADLESS_SHELL_VERSION: canary
          PUPPETEER_CHROME_HEADLESS_SHELL_SKIP_DOWNLOAD: true
          PUPPETEER_TEST_EXPERIMENTAL_CHROME_FEATURES: ${{ matrix.configs == 'experimental' }}
      - name: Run all tests (Linux)
        if: ${{ matrix.os == 'ubuntu-latest' }}
        run: xvfb-run --auto-servernum npm run test -- --ignore-unexpectedly-passing --shard '${{ matrix.shard }}' --test-suite ${{ matrix.suite }} --save-stats-to /tmp/artifacts/${{ github.event_name }}_INSERTID.json
        env:
          PUPPETEER_CHROME_VERSION: canary
          PUPPETEER_CHROME_HEADLESS_SHELL_VERSION: canary
          PUPPETEER_CHROME_HEADLESS_SHELL_SKIP_DOWNLOAD: true
          PUPPETEER_TEST_EXPERIMENTAL_CHROME_FEATURES: ${{ matrix.configs == 'experimental' }}
      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        if: ${{ !cancelled() }}
        with:
          name: test-results-${{ matrix.os }}-${{ matrix.suite }}-${{ matrix.shard }}-${{ matrix.configs }}
          path: /tmp/artifacts/*.json

  firefox-nightly-tests:
    name: ${{ matrix.suite }} (${{ matrix.shard }}) on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        suite:
          - firefox-headful
          - firefox-headless
        os:
          - ubuntu-latest
          - macos-15
          - windows-latest
        shard:
          - 1-4
          - 2-4
          - 3-4
          - 4-4
    steps:
      - name: Check out repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Set up Node.js
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          cache: npm
          node-version-file: '.nvmrc'
      - name: Install dependencies
        run: npm ci
        env:
          PUPPETEER_SKIP_DOWNLOAD: true
      - name: Disable AppArmor
        if: ${{ matrix.os == 'ubuntu-latest' }}
        run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns
      # Set up GitHub Actions caching for Wireit.
      - uses: google/wireit@9d7c2e5346f8ef1b1ab6014c4e96c74e234f7ed5 # setup-github-actions-caching/v2.0.4
      - name: Build packages
        run: npm run build -w @puppeteer-test/test
      - name: Install browsers
        run: npm run postinstall
        env:
          PUPPETEER_CHROME_SKIP_DOWNLOAD: true
          PUPPETEER_CHROME_HEADLESS_SHELL_SKIP_DOWNLOAD: true
          PUPPETEER_FIREFOX_VERSION: nightly
      - name: Apply Canary expectations
        run: node tools/merge-canary-test-expectations.mjs
      - name: Run all tests (MacOS)
        if: ${{ matrix.os == 'macos-15' }}
        run: npm run test -- --ignore-unexpectedly-passing --shard '${{ matrix.shard }}' --test-suite ${{ matrix.suite }} --save-stats-to /tmp/artifacts/${{ github.event_name }}_INSERTID.json
        env:
          PUPPETEER_FIREFOX_VERSION: nightly
      - name: Run all tests (Windows)
        if: ${{ matrix.os == 'windows-latest' }}
        run: npm run test -- --ignore-unexpectedly-passing --shard '${{ matrix.shard }}' --test-suite ${{ matrix.suite }} --save-stats-to /tmp/artifacts/${{ github.event_name }}_INSERTID.json
        env:
          PUPPETEER_FIREFOX_VERSION: nightly
      - name: Run all tests (Linux)
        if: ${{ matrix.os == 'ubuntu-latest' }}
        run: xvfb-run --auto-servernum npm run test -- --ignore-unexpectedly-passing --shard '${{ matrix.shard }}' --test-suite ${{ matrix.suite }} --save-stats-to /tmp/artifacts/${{ github.event_name }}_INSERTID.json
        env:
          PUPPETEER_FIREFOX_VERSION: nightly
      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        if: ${{ !cancelled() }}
        with:
          name: test-results-${{ matrix.os }}-${{ matrix.suite }}-${{ matrix.shard }}
          path: /tmp/artifacts/*.json

  report-results:
    name: 'Report results'
    needs: [chrome-canary-tests, firefox-nightly-tests]
    runs-on: ubuntu-latest
    permissions:
      issues: write
    if: ${{ !cancelled() }}
    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - name: Set up Node.js
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          cache: npm
          node-version-file: '.nvmrc'
      - name: Install dependencies
        run: npm ci
        env:
          PUPPETEER_SKIP_DOWNLOAD: true
      - name: Build packages
        run: npm run build -w @puppeteer/browsers
      - name: Generate comment
        id: genearate-comment-body
        run: node tools/build_daily_comment.mjs
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Find Comment
        uses: peter-evans/find-comment@b30e6a3c0ed37e7c023ccd3f1db5c6c0b0c23aad # v4.0.0
        id: find-comment
        with:
          # https://github.com/puppeteer/puppeteer/issues/12379
          issue-number: 12379
          body-includes: ${{ steps.genearate-comment-body.outputs.today }}
          direction: last
      - name: Post comment
        uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0
        if: ${{ needs.chrome-canary-tests.result}}
        with:
          # https://github.com/puppeteer/puppeteer/issues/12379
          issue-number: 12379
          # Try to find the same comment this day
          comment-id: ${{ steps.find-comment.outputs.comment-id }}
          body: ${{ steps.genearate-comment-body.outputs.body }}
          edit-mode: replace
