# SPDX-License-Identifier: Apache-2.0

name: "PR Checks"
on:
  workflow_dispatch:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
  push:
    branches:
      - main

defaults:
  run:
    shell: bash

permissions:
  contents: read

concurrency:
  group: pr-checks-${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  smoke-tests:
    name: Smoke Tests
    runs-on: hiero-local-node-linux-medium

    strategy:
      fail-fast: false
      matrix:
        node-version: [18.x]

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
        with:
          egress-policy: audit

      - name: Checkout repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm

      - name: Install dependencies
        run: |
          npm ci
          cd test/smoke
          npm ci

      - name: Start the local node
        run: npm run start -- -d --verbose=trace

      - name: Run smoke test
        uses: step-security/retry@e1d59ce1f574b32f0915e3a8df055cfe9f99be5d # v3.0.4
        with:
          max_attempts: 3
          timeout_minutes: 3
          command: npm run test:smoke

      - name: Stop the local node
        run: npm run stop

  smoke-tests-default-block-streams:
    name: Smoke Tests with Consensus node with Block Streams enabled by default
    runs-on: hiero-local-node-linux-medium

    strategy:
      fail-fast: false
      matrix:
        node-version: [18.x]

    env:
      HAVEGED_IMAGE_PREFIX: us-docker.pkg.dev/swirlds-registry/local-node/
      NETWORK_NODE_IMAGE_PREFIX: us-docker.pkg.dev/swirlds-registry/local-node/
      NETWORK_NODE_IMAGE_NAME: main-network-node
      NETWORK_NODE_IMAGE_TAG: 0.73.0-cn-only-blockstream.x047c0c1
      HAVEGED_IMAGE_TAG: 0.73.0-cn-only-blockstream.x047c0c1

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
        with:
          egress-policy: audit

      - name: Checkout repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm

      - name: Install dependencies
        run: |
          npm ci
          cd test/smoke
          npm ci

      - name: Start the local node
        run: npm run start -- -d --verbose=trace

      - name: Run smoke test
        uses: step-security/retry@e1d59ce1f574b32f0915e3a8df055cfe9f99be5d # v3.0.4
        with:
          max_attempts: 3
          timeout_minutes: 3
          command: npm run test:smoke

      - name: Stop the local node
        run: npm run stop

  browser-tests:
    name: Browser Tests
    runs-on: hiero-local-node-linux-large

    strategy:
      fail-fast: false
      matrix:
        node-version: [18.x]

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
        with:
          egress-policy: audit

      - name: Checkout repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev  -y
          sudo apt-get install libnss3 libxss1 libasound2 libxtst6 xauth xvfb -y
          npm ci

      - name: Start local node
        run: npm run start -- -d --verbose=trace

      - name: Prepare and run the tests
        run: |
          cd test/cypress
          npm ci
          npm run build
          npm run start
          npm run test

      - name: Stop the local node
        run: npm run stop

  unit-tests:
    name: Unit Tests
    runs-on: hiero-local-node-linux-medium

    strategy:
      fail-fast: false
      matrix:
        node-version: [18.x]

    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@58077d3c7e43986b6b15fba718e8ea69e387dfcc # v2.15.1
        with:
          egress-policy: audit

      - name: Checkout repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm run test

      - name: Upload coverage report
        if: ${{ always() && !cancelled() }}
        uses: codecov/codecov-action@5c47607acb93fed5485fdbf7232e8a31425f672a # v5.0.2
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
