version: 2.1

orbs:
   win: circleci/windows@2.4.0

workflows:
  build-and-test-all:
    jobs:
      # CircleCI's current generation of Node images, cimg/node, allow you to leave the
      # patch version unpinned, but require you to specify the minor version. The one
      # exception is cimg/node:current, which will always give us the latest release in
      # the latest major version-- and the latest major version is where it's most likely
      # that there would be a new minor version, anyway.
      - build-test-linux:
          name: latest Node version
          docker-image: cimg/node:current
          run-lint: true
      - build-test-linux:
          name: Node 17.9
          docker-image: cimg/node:17.9
      - build-test-linux:
          name: Node 16.14
          docker-image: cimg/node:16.14
      - build-test-linux:
          name: Node 15.14
          docker-image: cimg/node:15.14
      - build-test-linux:
          name: Node 14.19
          docker-image: cimg/node:14.19
      - build-test-linux:
          name: Node 13.14
          docker-image: cimg/node:13.14
      - build-test-linux:
          name: Node 12.22
          docker-image: cimg/node:12.22
      - build-test-windows:
          name: Windows

jobs:
  build-test-linux:
    parameters:
      run-lint:
        type: boolean
        default: false
      docker-image:
        type: string
      run-contract-tests:
        type: boolean
        default: true
    docker:
      - image: <<parameters.docker-image>>
    steps:
      - checkout
      - run: echo "Node version:" `node --version`
      - run: npm install
      - run:
          command: npm test
          environment:
            JEST_JUNIT_OUTPUT: "reports/junit/js-test-results.xml"
      - run: npm run check-typescript
      - when:
          condition: <<parameters.run-lint>>
          steps:
            - run: npm run lint
      - when:
          condition: <<parameters.run-contract-tests>>
          steps:
            - run:
                command: npm run contract-test-service
                background: true
            - run: mkdir -p reports/junit
            - run: TEST_HARNESS_PARAMS="-junit reports/junit/contract-test-results.xml -skip-from contract-tests/testharness-suppressions.txt" npm run contract-test-harness
      - run:
          name: dependency audit
          command: ./scripts/better-audit.sh
      - store_test_results:
          path: reports/junit
      - store_artifacts:
          path: reports/junit

  build-test-windows:
    executor:
      name: win/default
      shell: powershell.exe
    steps:
      - checkout
      - run: node --version
      - run: npm install
      - run: npm test
