---
version: 2.1

commands:
  pre:
    steps:
      - run: node --version
      - run: npm --version
      - run: yarn --version
      - checkout

  enable-eab:
    steps:
      - run:
          name: Enable EAB through environment
          command: |
            echo 'export ACME_CAP_EAB_ENABLED=1' >> $BASH_ENV

  install-cts:
    steps:
      - run:
          name: Install Pebble Challenge Test Server
          command: sudo -E /bin/bash ./scripts/test-suite-install-cts.sh
          environment:
            PEBBLECTS_VERSION: 2.3.1

      - run:
          name: Start Pebble Challenge Test Server
          command: pebble-challtestsrv -dns01 ":8053" -tlsalpn01 ":5001" -http01 ":5002" -https01 ":5003" -defaultIPv4 "127.0.0.1" -defaultIPv6 ""
          background: true

  install-pebble:
    steps:
      - run:
          name: Install Pebble
          command: sudo -E /bin/bash ./scripts/test-suite-install-pebble.sh
          environment:
            PEBBLE_VERSION: 2.3.1

      - run:
          name: Start Pebble
          command: pebble -strict -config /etc/pebble/pebble.json -dnsserver "127.0.0.1:53"
          background: true
          environment:
            PEBBLE_ALTERNATE_ROOTS: 2

      - run:
          name: Set up environment
          command: |
            echo 'export NODE_EXTRA_CA_CERTS="/etc/pebble/ca.cert.pem"' >> $BASH_ENV
            echo 'export ACME_CA_CERT_PATH="/etc/pebble/ca.cert.pem"' >> $BASH_ENV
            echo 'export ACME_DIRECTORY_URL="https://127.0.0.1:14000/dir"' >> $BASH_ENV
            echo 'export ACME_PEBBLE_MANAGEMENT_URL="https://127.0.0.1:15000"' >> $BASH_ENV

      - run:
          name: Wait for Pebble
          command: /bin/bash ./scripts/test-suite-wait-for-ca.sh

  install-step:
    steps:
      - run:
          name: Install Step Certificates
          command: /bin/bash ./scripts/test-suite-install-step.sh
          environment:
            STEPCA_VERSION: 0.18.0
            STEPCLI_VERSION: 0.18.0

      - run:
          name: Start Step CA
          command: /usr/bin/step-ca --resolver="127.0.0.1:53" --password-file="/tmp/password" ~/.step/config/ca.json
          background: true

      - run:
          name: Set up environment
          command: |
            echo 'export NODE_EXTRA_CA_CERTS="/home/circleci/.step/certs/root_ca.crt"' >> $BASH_ENV
            echo 'export ACME_CA_CERT_PATH="/home/circleci/.step/certs/root_ca.crt"' >> $BASH_ENV
            echo 'export ACME_DIRECTORY_URL="https://localhost:8443/acme/acme/directory"' >> $BASH_ENV

            echo 'export ACME_CAP_META_TOS_FIELD=0' >> $BASH_ENV
            echo 'export ACME_CAP_UPDATE_ACCOUNT_KEY=0' >> $BASH_ENV
            echo 'export ACME_CAP_ALTERNATE_CERT_ROOTS=0' >> $BASH_ENV

      - run:
          name: Wait for Step CA
          command: /bin/bash ./scripts/test-suite-wait-for-ca.sh

  install-coredns:
    steps:
      - run:
          name: Install CoreDNS
          command: sudo -E /bin/bash ./scripts/test-suite-install-coredns.sh
          environment:
            COREDNS_VERSION: 1.8.6
            PEBBLECTS_DNS_PORT: 8053

      - run:
          name: Start CoreDNS
          command: sudo coredns -p 53 -conf /etc/coredns/Corefile
          background: true

  test:
    steps:
      - run: yarn --color
      - run: yarn run lint --color
      - run: yarn run lint-types
      - run: yarn run build-docs

      - run:
          command: yarn run test --color
          environment:
            ACME_DOMAIN_NAME: test.example.com
            ACME_CHALLTESTSRV_URL: http://127.0.0.1:8055
            ACME_DNS_RESOLVER: 127.0.0.1
            ACME_TLSALPN_PORT: 5001
            ACME_HTTP_PORT: 5002
            ACME_HTTPS_PORT: 5003

jobs:
  v16: { docker: [{ image: cimg/node:16.16 }], steps: [ pre, install-cts, install-pebble, install-coredns, test ]}
  v18: { docker: [{ image: cimg/node:18.4 }], steps: [ pre, install-cts, install-pebble, install-coredns, test ]}
  eab-v16: { docker: [{ image: cimg/node:16.16 }], steps: [ pre, enable-eab, install-cts, install-pebble, install-coredns, test ]}
  eab-v18: { docker: [{ image: cimg/node:18.4 }], steps: [ pre, enable-eab, install-cts, install-pebble, install-coredns, test ]}
  # step-v12: { docker: [{ image: cimg/node:12.22 }], steps: [ pre, install-cts, install-step, install-coredns, test ]}

workflows:
  test-suite:
    jobs:
      - v16
      - v18
      - eab-v16
      - eab-v18
      # - step-v12
