# CI workflow: one clone, one workspace — install → lint → build → unit test - e2e test.
# Runs on pull requests, push/tag/manual on main, or manual from any branch.
# Deploy workflow depends on this (ci) and runs only on main.
when:
    - event: pull_request
    - event: push
      branch: main
    - event: tag
    - event: manual

steps:
    - name: install
      image: node:20-alpine
      commands:
          - corepack enable && corepack prepare pnpm@10.28.2 --activate
          - pnpm install --frozen-lockfile || pnpm install

    - name: lint
      image: node:20-alpine
      commands:
          - corepack enable && corepack prepare pnpm@10.28.2 --activate
          - pnpm install --frozen-lockfile || pnpm install
          - pnpm run lint
          - pnpm run lint:css
      depends_on:
          - install

    - name: Send Lint Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] Lint failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - lint
      when:
          - status: [failure]

    - name: unit test
      image: node:20-alpine
      commands:
          - corepack enable && corepack prepare pnpm@10.28.2 --activate
          - pnpm install --frozen-lockfile || pnpm install
          - pnpm exec svelte-kit sync
          - pnpm test
      depends_on:
          - lint

    - name: Send Unit Test Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] Unit test failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - unit test
      when:
          - status: [failure]

    - name: build
      image: node:20-alpine
      commands:
          - corepack enable && corepack prepare pnpm@10.28.2 --activate
          - pnpm install --frozen-lockfile || pnpm install
          - pnpm run build
      depends_on:
          - unit test

    - name: Send Test Build Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] Test build failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - build
      when:
          - status: [failure]

    - name: e2e test
      image: mcr.microsoft.com/playwright:v1.58.0-noble
      commands:
          - corepack enable && corepack prepare pnpm@10.28.2 --activate
          - pnpm install --frozen-lockfile || pnpm install
          - pnpm run build
          - npx serve dist -p 4173 &
          - sleep 2
          - CI=1 pnpm run test:e2e
      depends_on:
          - build

    - name: Send E2E Test Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] E2E test failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - e2e test
      when:
          - status: [failure]

    - name: Send CI Pipeline Status Notification (success)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] CI pipeline success 🎉"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - install
          - lint
          - unit test
          - build
          - e2e test
      when:
          - status: [success]
