name: Test

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

jobs:
  build-test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        smp-feature: [ "", "smp" ]

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Build dockerfile
        run: docker build -t moss .
      # If all features are disabled
      - name: Run tests inside docker container
        if: ${{ matrix.smp-feature == '' }}
        run: docker run moss "/bin/bash" -c "cargo run -r --no-default-features -- /bin/usertest" >> out.log
      # If any feature is enabled
      - name: Run tests inside docker container
        if: ${{ matrix.smp-feature == 'smp' }}
        run: docker run moss "/bin/bash" -c "cargo run -r --no-default-features --features "${{ matrix.smp-feature }}" -- /bin/usertest" >> out.log
      - name: Display test output
        run: cat out.log
      - name: Check for success line
        run: grep -q "All tests passed in " out.log || (echo "Tests failed" && exit 1)
      - name: Upload test output as artifact
        uses: actions/upload-artifact@v6
        with:
          name: test-output-${{ matrix.smp-feature || 'up' }}
          path: out.log
  upload-image:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Build dockerfile
        run: docker build -t moss .
      - name: Run container
        run: docker run -d --name moss-container moss
      - name: Copy image file
        run: docker cp moss-container:/moss/moss.img moss.img
      - name: Upload image as artifact
        uses: actions/upload-artifact@v6
        with:
          name: moss.img
          path: moss.img
