version: 2.1

jobs:
  build:
    docker:
      - image: registry.example.invalid/ci/node:20
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - deps-v1-{{ checksum "package-lock.json" }}
            - deps-v1-
      - run:
          name: Install
          command: npm ci
      - save_cache:
          key: deps-v1-{{ checksum "package-lock.json" }}
          paths:
            - ~/.npm
      - run:
          name: Build
          command: npm run build
      - persist_to_workspace:
          root: .
          paths:
            - build

  test:
    docker:
      - image: registry.example.invalid/ci/node:20
    steps:
      - checkout
      - attach_workspace:
          at: .
      - run: npm test
      - store_test_results:
          path: reports

workflows:
  build-and-test:
    jobs:
      - build
      - test:
          requires:
            - build
