name: e2e-integration-test

env:
  LIGHTPANDA_DISABLE_TELEMETRY: true

on:
  schedule:
    - cron: "4 4 * * *"
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  zig-build-release:
    name: zig build release

    runs-on: ubuntu-latest
    timeout-minutes: 15

    # Don't run the CI with draft PR.
    if: github.event.pull_request.draft == false

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - uses: ./.github/actions/install

      - name: zig build release
        run: zig build -Dprebuilt_v8_path=v8/libc_v8.a -Doptimize=ReleaseFast -Dcpu=x86_64

      - name: upload artifact
        uses: actions/upload-artifact@v7
        with:
          name: lightpanda-build-release
          path: |
            zig-out/bin/lightpanda
          retention-days: 1

  demo-scripts:
    name: demo-integration-scripts
    needs: zig-build-release

    runs-on: ubuntu-latest
    timeout-minutes: 15

    steps:
      - uses: actions/checkout@v6
        with:
          repository: 'lightpanda-io/demo'
          fetch-depth: 0

      - run: npm install

      - name: download artifact
        uses: actions/download-artifact@v8
        with:
          name: lightpanda-build-release

      - run: chmod a+x ./lightpanda

      - name: LP Cache
        id: lp-cache
        uses: actions/cache@v5
        with:
          path: /tmp/lp-cache
          key: v1

      - name: run end to end integration tests
        continue-on-error: true
        run: |
          ./lightpanda serve --http-cache-dir /tmp/lp-cache --http-proxy ${{ secrets.MASSIVE_PROXY_RESIDENTIAL_US }} --log-level error & echo $! > LPD.pid
          go run integration/main.go |tee result.log
          kill `cat LPD.pid`

      - name: Send result to slack
        uses: slackapi/slack-github-action@v3.0.1
        with:
          errors: true
          method: files.uploadV2
          token: ${{ secrets.CI_SLACK_BOT_TOKEN }}
          payload: |
            channel_id: ${{ vars.E2E_SLACK_CHANNEL_ID }}
            initial_comment: "Last e2e integration tests"
            file: "./result.log"
            filename: "e2e-integration-${{ github.sha }}.txt"
