name: Self-Hosting Docker Compose Tests

on:
  workflow_dispatch:
  pull_request:
  push:
    branches: [ master ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: 1.23
    - name: Docker Compose setup 
      run: |
          set -euo pipefail
          sudo apt-get update  
          sudo apt-get install -y zsh fish 
          curl -fsSL https://get.docker.com | sudo sh
          sudo chmod 0755 -R /usr/share/zsh/  # Work around a weird bug where zsh on ubuntu actions gives that diretory 0777 which makes zsh refuse to start
          sudo hostname ghaction-runner-hostname  # Set a consistent hostname so we can run tests that depend on it
          docker compose -f backend/server/docker-compose.yml build
          HISHTORY_COMPOSE_TEST=1 docker compose -f backend/server/docker-compose.yml up -d 
          export HISHTORY_SERVER=http://localhost
          go build
          ./hishtory install
    - name: Docker Compose test 
      shell: bash -il {0} 
      run: |
          set -eo pipefail
          export HISHTORY_SERVER=http://localhost
          source ~/.bashrc
          # Check that hishtory query runs without errors
          ./hishtory query 
          # Run a command such that hishtory will record it
          echo -e 'ls -Slah /\n' | zsh -is
          # Sleep to ensure there is time for it to be recorded, since recordings are async
          sleep 1
          # Check that it was recorded
          ./hishtory export | grep "ls -Slah /"
          # Check that we can redact it
          HISHTORY_REDACT_FORCE=1 ./hishtory redact ls Slah
          # And that it was redacted
          ! (./hishtory export | grep "ls -Slah /")
          # Assert that the entry is syncing properly
          ./hishtory status -v | grep 'Sync Status: Synced'
          # And that we are properly using the self-hosted server
          ./hishtory status -v | grep 'Sync Server: http://localhost'
          # Show the full status output for debugging
          ./hishtory status -v
    # - name: Setup tmate session
    #   # if: ${{ failure() }}
    #   uses: mxschmitt/action-tmate@v3
    #   with:
    #     limit-access-to-actor: true