version: 2.1
jobs:
  test-common:
    docker:
      - image: penpotapp/devenv:latest

    working_directory: ~/repo
    resource_class: medium+

    environment:
      JAVA_OPTS: -Xmx4g -Xms100m -XX:+UseSerialGC
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "common/deps.edn"}}

      - run:
          name: "fmt check & linter"
          working_directory: "./common"
          command: |
            yarn install
            yarn run fmt:clj:check
            yarn run lint:clj

      - run:
          name: "JVM tests"
          working_directory: "./common"
          command: |
            clojure -M:dev:test

      - run:
          name: "NODE tests"
          working_directory: "./common"
          command: |
            yarn run test

      - save_cache:
          paths:
            - ~/.m2
          key: v1-dependencies-{{ checksum "common/deps.edn"}}


  test-frontend:
    docker:
      - image: penpotapp/devenv:latest

    working_directory: ~/repo
    resource_class: medium+

    environment:
      JAVA_OPTS: -Xmx4g -Xms100m -XX:+UseSerialGC
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "frontend/deps.edn"}}

      - run:
          name: "prepopulate linter cache"
          working_directory: "./common"
          command: |
            yarn install
            yarn run lint:clj

      - run:
          name: "fmt check & linter"
          working_directory: "./frontend"
          command: |
            yarn install
            yarn run fmt:clj:check
            yarn run fmt:js:check
            yarn run lint:scss
            yarn run lint:clj

      - run:
          name: "unit tests"
          working_directory: "./frontend"
          command: |
            yarn install
            yarn run test

      - save_cache:
          paths:
            - ~/.m2
          key: v1-dependencies-{{ checksum "frontend/deps.edn"}}


  test-integration:
    docker:
      - image: penpotapp/devenv:latest

    working_directory: ~/repo
    resource_class: large

    environment:
      JAVA_OPTS: -Xmx6g -Xms2g
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "frontend/deps.edn"}}

      - run:
          name: "integration tests"
          working_directory: "./frontend"
          command: |
            yarn install
            yarn run build:app:assets
            yarn run build:app
            yarn run build:app:libs
            yarn run playwright install --with-deps chromium
            yarn run test:e2e -x --workers=4

  test-backend:
    docker:
      - image: penpotapp/devenv:latest
      - image: cimg/postgres:14.5
        environment:
          POSTGRES_USER: penpot_test
          POSTGRES_PASSWORD: penpot_test
          POSTGRES_DB: penpot_test
      - image: cimg/redis:7.0.5

    working_directory: ~/repo
    resource_class: medium+

    environment:
      JAVA_OPTS: -Xmx4g -Xms100m -XX:+UseSerialGC
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - checkout

      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "backend/deps.edn" }}

      - run:
          name: "prepopulate linter cache"
          working_directory: "./common"
          command: |
            yarn install
            yarn run lint:clj

      - run:
          name: "fmt check & linter"
          working_directory: "./backend"
          command: |
            yarn install
            yarn run fmt:clj:check
            yarn run lint:clj

      - run:
          name: "tests"
          working_directory: "./backend"
          command: |
            clojure -M:dev:test --reporter kaocha.report/documentation

          environment:
            PENPOT_TEST_DATABASE_URI: "postgresql://localhost/penpot_test"
            PENPOT_TEST_DATABASE_USERNAME: penpot_test
            PENPOT_TEST_DATABASE_PASSWORD: penpot_test
            PENPOT_TEST_REDIS_URI: "redis://localhost/1"

      - save_cache:
          paths:
            - ~/.m2
          key: v1-dependencies-{{ checksum "backend/deps.edn" }}


  test-exporter:
    docker:
      - image: penpotapp/devenv:latest

    working_directory: ~/repo
    resource_class: medium+

    environment:
      JAVA_OPTS: -Xmx4g -Xms100m -XX:+UseSerialGC
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - checkout

      - run:
          name: "prepopulate linter cache"
          working_directory: "./common"
          command: |
            yarn install
            yarn run lint:clj

      - run:
          name: "fmt check & linter"
          working_directory: "./exporter"
          command: |
            yarn install
            yarn run fmt:clj:check
            yarn run lint:clj

workflows:
  penpot:
    jobs:
      - test-frontend
      - test-integration
      - test-backend
      - test-common
      - test-exporter