name: Node.js CI

on:
    push:
        branches: [trunk]
    pull_request:
        branches: [trunk]

jobs:
    build:
        runs-on: ubuntu-latest

        steps:
            - uses: actions/checkout@v2
            - name: Use Node.js 20.x
              uses: actions/setup-node@v2
              with:
                  node-version: 20.x

            - name: Get yarn cache directory path
              id: yarn-cache-dir-path
              run: echo "::set-output name=dir::$(yarn cache dir)"

            - name: Cache yarn modules
              uses: actions/cache@v4
              id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
              with:
                  path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
                  key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-yarn-

            - run: yarn install --frozen-lockfile

            # Uncomment this when all files are clean
            # - run: yarn lint:js

            - run: yarn test

            - run: yarn build

            - name: Install Playwright
              run: npx playwright install --with-deps
            - name: Serve Storybook and run tests
              run: yarn e2e
            - name: Upload results
              uses: actions/upload-artifact@v4
              if: always()
              with:
                name: playwright-report
                path: playwright-report/
                retention-days: 30
