# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Tests

on: [push]

env:
  TAP_COLORS: 1

jobs:
  build:
    name: Build and run unit tests
    runs-on: ubuntu-latest
    env:
      TAP_NO_ESM: 1
    steps:
      - uses: actions/checkout@v4.1.5
      - name: Use Node.js v14
        uses: actions/setup-node@v4.0.2
        with:
          node-version: 14.x
      - run: npm install

      - name: Build Jdenticon
        run: npm run build

      - name: TypeScript typings tests
        run: npm run test:types
      - name: Unit tests
        run: npm run test:unit

      - name: Webpack 4 bundle test
        run: npm run test:webpack4
      - name: Webpack 5 bundle test
        run: npm run test:webpack5

      - name: Rollup bundle test
        run: npm run test:rollup

      - name: Node test (CommonJS)
        run: npm run test:node-cjs
      - name: Node test (ESM)
        run: npm run test:node-esm

      - name: Publish artifacts
        uses: actions/upload-artifact@v4.3.3
        if: ${{ always() }}
        with:
          name: package
          path: ./test/node_modules/jdenticon

  e2e:
    name: E2E tests (Node ${{ matrix.node }})
    runs-on: ubuntu-latest
    needs: build
    strategy:
      fail-fast: false
      matrix:
        node: [ '6.4', '8.x', '10.x', '12.x', '18.x', '20.x' ]
    steps:
      - uses: actions/checkout@v4.1.5
      
      - uses: actions/download-artifact@v4.1.7
        with:
          name: package
          path: test/node_modules/jdenticon

      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v4.0.2
        with:
          node-version: ${{ matrix.node }}

      # Use an older tap version to ensure compatibility with the old Node version
      # bind-obj-methods broke old Node 6 support in 2.0.1
      - name: npm install (Node 6.4)
        if: ${{ matrix.node == '6.4' }}
        run: |
          npm install -g npm@6.14.17
          npm install tap@12.7.0 bind-obj-methods@2.0.0

      - name: npm install (Node 8.x)
        if: ${{ matrix.node == '8.x' }}
        run: npm install tap@14.11.0

      - name: npm install (Node 10+)
        if: ${{ matrix.node != '6.4' && matrix.node != '8.x' }}
        run: npm install

      - name: Node test (CommonJS)
        run: npm run test:node-cjs
        
      - name: Node test (ESM, Node 12+)
        if: ${{ matrix.node != '6.4' && matrix.node != '8.x' && matrix.node != '10.x' }}
        run: npm run test:node-esm

      - name: Publish artifacts
        uses: actions/upload-artifact@v4.3.3
        if: ${{ failure() }}
        with:
          name: e2e-${{ matrix.node }}
          path: ./test/e2e/node/expected

  visual:
    name: Visual tests
    needs: build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ 'macos-latest', 'windows-latest' ]
    env:
      ARTIFACTS_DIR: ./artifacts
      BROWSER_SCREENSHOT_DIR: ./artifacts/screenshots
      BROWSER_DIFF_DIR: ./artifacts/diffs
    steps:
      - uses: actions/checkout@v4.1.5
      - name: Use Node.js
        uses: actions/setup-node@v4.0.2
        with:
          node-version: 16.x
      - run: npm install

      - uses: actions/download-artifact@v4.1.7
        with:
          name: package
          path: test/node_modules/jdenticon

      - name: Run visual tests (Windows)
        if: ${{ startsWith(matrix.os, 'windows') }}
        run: |
          $env:PATH = "C:\SeleniumWebDrivers\IEDriver;$env:PATH"
          npm run test:browser-win
      - name: Run visual tests (macOS)
        if: ${{ startsWith(matrix.os, 'macos') }}
        run: npm run test:browser-macos

      - name: Publish artifacts
        uses: actions/upload-artifact@v4.3.3
        if: ${{ always() }}
        with:
          name: visual-${{ matrix.os }}
          path: ${{ env.ARTIFACTS_DIR }}
      