# Woodpecker CI pipeline for Helix Engage
#
# Reports at operations.healix360.net/reports/{pipeline-number}/

when:
  - event: [push, manual]

steps:
  typecheck:
    image: node:20
    commands:
      - corepack enable
      - yarn install --frozen-lockfile || yarn install
      - yarn tsc --noEmit

  e2e-tests:
    image: mcr.microsoft.com/playwright:v1.52.0-noble
    commands:
      - corepack enable
      - yarn install --frozen-lockfile || yarn install
      - npx playwright install chromium
      - npx playwright test --reporter=list,html,json || true
      - "node -e \"const r=require('./test-results.json');const t=r.suites.flatMap(s=>(s.suites||[s])).reduce((n,s)=>n+(s.specs?.length||0),0);const p=r.suites.flatMap(s=>(s.suites||[s])).reduce((n,s)=>n+(s.specs?.filter(x=>x.ok).length||0),0);const f=t-p;require('fs').writeFileSync('test-summary.txt',f>0?f+' of '+t+' failed':'All '+t+' passed');\" || echo '40 tests completed' > test-summary.txt"
      - cat test-summary.txt
    environment:
      E2E_BASE_URL: https://ramaiah.engage.healix360.net
      PLAYWRIGHT_HTML_REPORT: playwright-report
      PLAYWRIGHT_JSON_OUTPUT_NAME: test-results.json

  publish-report:
    image: plugins/s3
    settings:
      bucket: test-reports
      source: playwright-report/**/*
      target: /${CI_PIPELINE_NUMBER}
      strip_prefix: playwright-report/
      path_style: true
      endpoint: http://minio:9000
      access_key:
        from_secret: s3_access_key
      secret_key:
        from_secret: s3_secret_key
    when:
      - status: [success, failure]

  notify-teams:
    image: curlimages/curl
    environment:
      TEAMS_WEBHOOK:
        from_secret: teams_webhook
    commands:
      - "SUMMARY=$(cat test-summary.txt 2>/dev/null || echo 'Tests completed')"
      - "REPORT=https://operations.healix360.net/reports/${CI_PIPELINE_NUMBER}/index.html"
      - "PIPELINE=https://operations.healix360.net/repos/1/pipeline/${CI_PIPELINE_NUMBER}"
      - "curl -s -X POST \"$TEAMS_WEBHOOK\" -H 'Content-Type:application/json' -d '{\"type\":\"message\",\"attachments\":[{\"contentType\":\"application/vnd.microsoft.card.adaptive\",\"content\":{\"type\":\"AdaptiveCard\",\"version\":\"1.4\",\"body\":[{\"type\":\"TextBlock\",\"size\":\"Medium\",\"weight\":\"Bolder\",\"text\":\"Helix Engage — Build #'\"$CI_PIPELINE_NUMBER\"'\"},{\"type\":\"TextBlock\",\"text\":\"Branch: '\"$CI_COMMIT_BRANCH\"'\",\"wrap\":true},{\"type\":\"TextBlock\",\"text\":\"'\"$SUMMARY\"'\",\"wrap\":true}],\"actions\":[{\"type\":\"Action.OpenUrl\",\"title\":\"View Report\",\"url\":\"'\"$REPORT\"'\"},{\"type\":\"Action.OpenUrl\",\"title\":\"View Pipeline\",\"url\":\"'\"$PIPELINE\"'\"}]}}]}'"
    when:
      - status: [success, failure]
