<%
  topologies = %w( standalone replica-set sharded-cluster )

  # latest_ruby = the most recently released, stable version of Ruby
  #    (make sure this version is being built by 10gen/mongo-ruby-toolchain)
  latest_ruby = "ruby-4.0".inspect # so it gets quoted as a string

  # these are used for testing against a few recent ruby versions
  recent_rubies = %w( ruby-4.0 ruby-3.4 ruby-3.3 ruby-3.2 jruby-9.4 )

  recent_mri_rubies = %w( ruby-4.0 ruby-3.4 )

  # older Ruby versions provided by 10gen/mongo-ruby-toolchain
  older_rubies = %w( ruby-3.0 ruby-2.7  )

  # all supported JRuby versions provided by 10gen/mongo-ruby-toolchain
  jrubies = %w( jruby-9.4 )

  supported_mri_rubies_3 = %w( ruby-4.0 ruby-3.4 ruby-3.3 ruby-3.2 ruby-3.1 ruby-3.0 )

  supported_mri_rubies_3_ubuntu = %w( ruby-4.0 ruby-3.4 ruby-3.3 ruby-3.2 ruby-3.1 )

  supported_mri_ruby_2 = "ruby-2.7".inspect

  supported_rubies = supported_mri_rubies_3 +
                     %w( ruby-2.7 ) +
                     jrubies

  # The latest stable version of MongoDB
  latest_stable_mdb_raw = "8.0"
  latest_stable_mdb = latest_stable_mdb_raw.inspect # so it gets quoted as a string

  stable_and_rapid = [ latest_stable_mdb_raw, "rapid" ]

  recent_mdb = %w( 8.2 8.0 7.0 )
%>

buildvariants:
  # RUBY-3731: test master against the upcoming Ruby (ruby-dev). Runs off-PR
  # (no "pr" tag) on a ~2-week cadence. Failures are reported as RUBY Jira
  # issues via an Evergreen project notification; see .evergreen/README.md.
  - matrix_name: "ruby-dev"
    matrix_spec:
      ruby: "ruby-dev"
      # MongoDB 7.0 enterprise archive is not published for ubuntu2404.
      mongodb-version: <%= latest_stable_mdb %>
      topology: "replica-set"
      os: ubuntu2404
    display_name: "${mongodb-version} ${os} ${topology} ${auth-and-ssl} ${ruby}"
    tasks:
      - name: "run-main-test-suite"
        batchtime: 20160 # run at most once every 14 days

  - matrix_name: DriverBench
    matrix_spec:
      ruby: <%= latest_ruby %>
      mongodb-version: <%= latest_stable_mdb %>
      topology: standalone
      os: ubuntu2204
    display_name: DriverBench
    tasks:
      - name: "driver-bench"

  - matrix_name: "auth/ssl"
    matrix_spec:
      auth-and-ssl: ["auth-and-ssl", "noauth-and-nossl"]
      ruby: <%= latest_ruby %>
      mongodb-version: <%= recent_mdb %>
      topology: <%= topologies %>
      os: ubuntu2204
    display_name: ${auth-and-ssl} ${ruby} db-${mongodb-version} ${topology}
    tags: ["pr"]
    tasks:
      - name: "run-main-test-suite"

  - matrix_name: "mongo-recent"
    matrix_spec:
      ruby: <%= recent_rubies %>
      mongodb-version: <%= recent_mdb %>
      topology: <%= topologies %>
      os: ubuntu2204
    display_name: "${mongodb-version} ${os} ${topology} ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "run-main-test-suite"

  - matrix_name: "mongo-8-arm"
    matrix_spec:
      ruby: <%= latest_ruby %>
      mongodb-version: [ '8.0' ]
      topology: <%= topologies %>
      os: ubuntu2404-arm
    display_name: "${mongodb-version} ${os} ${topology} ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "run-main-test-suite"

  - matrix_name: "mongo-5.x"
    matrix_spec:
      ruby: <%= recent_mri_rubies %>
      mongodb-version: ['5.0']
      topology: <%= topologies %>
      os: ubuntu2004
    display_name: "${mongodb-version} ${topology} ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "run-main-test-suite"

  - matrix_name: "mongo-4.x"
    matrix_spec:
      ruby: <%= older_rubies %>
      mongodb-version: ['4.4']
      topology: <%= topologies %>
      os: ubuntu1804
    display_name: "${mongodb-version} ${topology} ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "run-main-test-suite"

  - matrix_name: "single-lb"
    matrix_spec:
      ruby: <%= latest_ruby %>
      mongodb-version: <%= stable_and_rapid %>
      topology: load-balanced
      single-mongos: single-mongos
      os: ubuntu2204
    display_name: "${mongodb-version} ${topology} single-lb ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test"

  - matrix_name: "mongo-api-version"
    matrix_spec:
      ruby: <%= latest_ruby %>
      mongodb-version: '7.0'
      topology: standalone
      api-version-required: yes
      os: ubuntu2204
    display_name: "${mongodb-version} api-version-required ${topology} ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test"

  - matrix_name: "single-mongos"
    matrix_spec:
      ruby: <%= latest_ruby %>
      mongodb-version: <%= stable_and_rapid %>
      topology: "sharded-cluster"
      single-mongos: single-mongos
      os: ubuntu2204
    display_name: "${mongodb-version} ${topology} single-mongos ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test"

  - matrix_name: CSOT
    matrix_spec:
      ruby: <%= latest_ruby %>
      mongodb-version: <%= stable_and_rapid %>
      topology: replica-set-single-node
      os: ubuntu2204
    display_name: "CSOT - ${mongodb-version}"
    tags: ["pr"]
    tasks:
      - name: test-csot

  - matrix_name: OTel
    matrix_spec:
      ruby: <%= latest_ruby %>
      mongodb-version: <%= latest_stable_mdb %>
      topology: replica-set-single-node
      os: ubuntu2204
    display_name: "OTel - ${mongodb-version}"
    tags: ["pr"]
    tasks:
      - name: test-otel

  - matrix_name: "no-retry-reads"
    matrix_spec:
      retry-reads: no-retry-reads
      ruby: <%= latest_ruby %>
      mongodb-version: <%= stable_and_rapid %>
      topology: <%= topologies %>
      os: ubuntu2204
    display_name: "${mongodb-version} ${topology} ${retry-reads} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test"

  - matrix_name: "no-retry-writes"
    matrix_spec:
      retry-writes: no-retry-writes
      ruby: <%= latest_ruby %>
      mongodb-version: <%= stable_and_rapid %>
      topology: [replica-set, sharded-cluster]
      os: ubuntu2204
    display_name: "${mongodb-version} ${topology} ${retry-writes} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test"

  - matrix_name: "lint"
    matrix_spec:
      lint: on
      ruby: <%= latest_ruby %>
      mongodb-version: <%= latest_stable_mdb %>
      topology: <%= topologies %>
      os: ubuntu2204
    display_name: "${mongodb-version} ${topology} ${lint} ${ruby}"
    tasks:
      - name: "test"

  - matrix_name: "fork"
    matrix_spec:
      fork: on
      ruby: <%= latest_ruby %>
      mongodb-version: <%= latest_stable_mdb %>
      topology: <%= topologies %>
      os: ubuntu2204
    display_name: "${mongodb-version} ${topology} fork ${ruby}"
    tasks:
      - name: "test"

  - matrix_name: "solo"
    matrix_spec:
      solo: on
      ruby: <%= supported_mri_rubies_3_ubuntu %>
      mongodb-version: <%= latest_stable_mdb %>
      topology: <%= topologies %>
      os: ubuntu2204
    display_name: "${mongodb-version} ${topology} solo ${ruby}"
    tasks:
      - name: "test"

  - matrix_name: "stress older"
    matrix_spec:
      stress: on
      ruby: <%= supported_mri_ruby_2 %>
      mongodb-version: ['4.4']
      topology: replica-set
      os: ubuntu1804
    display_name: "${mongodb-version} ${topology} stress ${ruby}"
    tasks:
      - name: "test"

  - matrix_name: "stress"
    matrix_spec:
      stress: on
      ruby: <%= latest_ruby %>
      mongodb-version: <%= recent_mdb %>
      topology: replica-set
      os: ubuntu2204
    display_name: "${mongodb-version} ${topology} stress ${ruby}"
    tasks:
      - name: "test"

#  - matrix_name: "x509-tests"
#    matrix_spec:
#      auth-and-ssl: "x509"
#      ruby: <%= latest_ruby %>
#      mongodb-version: <%= latest_stable_mdb %>
#      topology: standalone
#      os: ubuntu2204
#    display_name: "${mongodb-version} ${topology} ${auth-and-ssl} ${ruby}"
#    tasks:
#      - name: "test"

 <% [
      [latest_ruby, stable_and_rapid, 'ubuntu2204'],
      [supported_mri_ruby_2, '"6.0"', 'ubuntu2004']
    ].each do |rubies, mdb, distro|
 %>
  - matrix_name: <%= "zlib-#{rubies}" %>
    matrix_spec:
      auth-and-ssl: [ "auth-and-ssl", "noauth-and-nossl" ]
      ruby: <%= rubies %>
      mongodb-version: <%= mdb %>
      topology: "replica-set"
      compressor: 'zlib'
      os: <%= distro %>
    display_name: "${compressor} ${mongodb-version} ${topology} ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "run-main-test-suite"

  - matrix_name: <%= "snappy-#{rubies}" %>
    matrix_spec:
      auth-and-ssl: [ "auth-and-ssl", "noauth-and-nossl" ]
      ruby: <%= rubies %>
      mongodb-version: <%= mdb %>
      topology: "replica-set"
      compressor: 'snappy'
      os: <%= distro %>
    display_name: "${compressor} ${mongodb-version} ${topology} ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "run-main-test-suite"

  # the zstd-ruby gem does not support JRuby (explicitly). However, there is
  # apparently a zstd-jni gem for JRuby that we could investigate here; if
  # this test is ever supported to support jruby, the `sample_mri_rubies`
  # reference should be replaced with `sample_rubies`.
  - matrix_name: <%= "zstd-auth-#{rubies}" %>
    matrix_spec:
      auth-and-ssl: [ "auth-and-ssl", "noauth-and-nossl" ]
      ruby: <%= rubies %>
      mongodb-version: <%= mdb %>
      topology: "replica-set"
      compressor: 'zstd'
      os: <%= distro %>
    display_name: "${compressor} ${mongodb-version} ${topology} ${auth-and-ssl} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "run-main-test-suite"

  - matrix_name: <%= "activesupport-#{rubies}" %>
    matrix_spec:
      ruby: <%= rubies %>
      mongodb-version: <%= mdb %>
      topology: replica-set
      as: as
      os: <%= distro %>
    display_name: "AS ${mongodb-version} ${topology} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test"

  - matrix_name: <%= "bson-#{rubies}" %>
    matrix_spec:
      ruby: <%= rubies %>
      mongodb-version: <%= mdb %>
      topology: replica-set
      bson: "*"
      os: <%= distro %>
    display_name: "bson-${bson} ${mongodb-version} ${topology} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test"
  <% end %>

  - matrix_name: "fle"
    matrix_spec:
      auth-and-ssl: "noauth-and-nossl"
      ruby: <%= supported_mri_rubies_3_ubuntu %>
      topology: [replica-set, sharded-cluster]
      mongodb-version: [ '6.0', '7.0', '8.0', 'rapid' ]
      os: ubuntu2204
      fle: helper
    display_name: "FLE: ${mongodb-version} ${topology} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test-fle"

  - matrix_name: "fle-mongocryptd"
    matrix_spec:
      auth-and-ssl: "noauth-and-nossl"
      ruby: <%= latest_ruby %>
      topology: [replica-set, sharded-cluster]
      mongodb-version: <%= latest_stable_mdb %>
      os: ubuntu2204
      fle: mongocryptd
    display_name: "FLE mongocryptd: ${mongodb-version} ${topology} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test-fle"

#  - matrix_name: "fle-latest"
#    matrix_spec:
#      auth-and-ssl: "noauth-and-nossl"
#      ruby: <%#= latest_ruby %>
#      topology: [replica-set, sharded-cluster]
#      mongodb-version: [ 'latest' ]
#      os: ubuntu2204
#      fle: helper
#    display_name: "FLE: ${mongodb-version} ${topology} ${ruby}"
#    tasks:
#      - name: "test-fle"

  # Runs specs against the latest server. Not run on PRs to avoid blocking
  # merges when an unreleased MongoDB build breaks something.
  - name: 'latest-server'
    patchable: false
    display_name: 'Latest MongoDB Server'
    run_on: ubuntu2204-small
    expansions:
      TOPOLOGY: sharded_cluster
      RVM_RUBY: <%= latest_ruby %>
      MONGODB_VERSION: 'latest'
    tasks:
      - name: 'run-main-test-suite'

  - matrix_name: aws-auth-regular
    matrix_spec:
      # https://jira.mongodb.org/browse/RUBY-3311
      # auth-and-ssl: [ aws-regular, aws-assume-role, aws-ec2, aws-ecs, aws-web-identity ]
      # auth-and-ssl: [ aws-regular, aws-assume-role, aws-ecs, aws-web-identity ]
      # https://jira.mongodb.org/browse/RUBY-3659
      auth-and-ssl: [ aws-regular, aws-assume-role, aws-web-identity ]
      ruby: <%= latest_ruby %>
      topology: standalone
      mongodb-version: <%= stable_and_rapid %>
      os: ubuntu2204
    display_name: "AWS ${auth-and-ssl} ${mongodb-version} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: "test-aws-auth"

  - matrix_name: ocsp-verifier
    matrix_spec:
      ocsp-verifier: true
      # No JRuby due to https://github.com/jruby/jruby-openssl/issues/210
      ruby: <%= supported_mri_rubies_3_ubuntu %>
      topology: standalone
      mongodb-version: <%= latest_stable_mdb %>
      os: ubuntu2204
    display_name: "OCSP verifier: ${mongodb-version} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: test

  - matrix_name: ocsp-must-staple
    matrix_spec:
      ocsp-algorithm: ecdsa
      ocsp-must-staple: on
      ocsp-delegate: on
      ruby: <%= latest_ruby %>
      topology: standalone
      mongodb-version: <%= latest_stable_mdb %>
      os: ubuntu2204
      auth-and-ssl: noauth-and-ssl
    display_name: "OCSP integration - must staple: ${mongodb-version} ${ruby}"
    tasks:
      - name: test

  - matrix_name: ocsp-unknown
    matrix_spec:
      ocsp-algorithm: rsa
      ocsp-status: unknown
      ruby: <%= latest_ruby %>
      topology: standalone
      mongodb-version: <%= latest_stable_mdb %>
      os: ubuntu2204
      auth-and-ssl: noauth-and-ssl
    display_name: "OCSP integration - unknown: ${mongodb-version} ${ruby}"
    tasks:
      - name: test

  <% [
    %w(valid none pass),
    %w(unknown none pass),
    %w(revoked none fail),

    %w(valid tlsInsecure=true pass),
    %w(unknown tlsInsecure=true pass),
    %w(revoked tlsInsecure=true pass),

    %w(valid tlsAllowInvalidCertificates=true pass),
    %w(unknown tlsAllowInvalidCertificates=true pass),
    %w(revoked tlsAllowInvalidCertificates=true pass),
  ].each do |status, extra_uri_options, outcome|
  %>
  - matrix_name: ocsp-connectivity
    matrix_spec:
      ocsp-algorithm: '*'
      ocsp-status: <%= status %>
      ocsp-delegate: '*'
      ocsp-connectivity: <%= outcome %>
      extra-uri-options: "<%= extra_uri_options %>"
      ruby: <%= latest_ruby %>
      topology: standalone
      mongodb-version: <%= latest_stable_mdb %>
      os: ubuntu2204
    display_name: "OCSP connectivity: ${ocsp-algorithm} ${ocsp-status} ${ocsp-delegate} ${extra-uri-options} ${mongodb-version} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: test
  <% end %>

  - matrix_name: ocsp-connectivity-jruby
    matrix_spec:
      # ECDSA does not work on JRuby.
      # https://github.com/jruby/jruby-openssl/issues/213
      ocsp-algorithm: rsa
      # We do not perform OCSP verification on JRuby, therefore the revoked
      # configuration fails (connection succeeds due to lack of verification
      # when it is expected to fail).
      # https://github.com/jruby/jruby-openssl/issues/210
      ocsp-status: [valid, unknown]
      ocsp-delegate: '*'
      ocsp-connectivity: pass
      ruby: <%= jrubies.first %>
      topology: standalone
      mongodb-version: <%= latest_stable_mdb %>
      os: ubuntu2204
    display_name: "OCSP connectivity: ${ocsp-algorithm} ${ocsp-status} ${ocsp-delegate} ${mongodb-version} ${ruby}"
    tags: ["pr"]
    tasks:
      - name: test

  # https://jira.mongodb.org/browse/RUBY-3540
  #- matrix_name: testgcpkms-variant
  #  matrix_spec:
  #    ruby: <%= latest_ruby %>
  #    fle: helper
  #    topology: standalone
  #    os: ubuntu2204
  #    mongodb-version: <%= latest_stable_mdb %>
  #  display_name: "GCP KMS"
  #  tasks:
  #    - name: testgcpkms_task_group
  #      batchtime: 20160 # Use a batchtime of 14 days as suggested by the CSFLE test README

  # https://jira.mongodb.org/browse/RUBY-3672
  #- matrix_name: testazurekms-variant
  #  matrix_spec:
  #    ruby: ruby-3.0
  #    fle: helper
  #    topology: standalone
  #    os: debian11 # could eventually look at updating this to rhel80
  #    mongodb-version: 6.0
  #  display_name: "AZURE KMS"
  #  tasks:
  #    - name: testazurekms_task_group
  #      batchtime: 20160 # Use a batchtime of 14 days as suggested by the CSFLE test README

  - matrix_name: atlas-full
    matrix_spec:
      ruby: <%= latest_ruby %>
      os: ubuntu2204
    display_name: "Atlas (Full)"
    tasks:
      - name: testatlas_full_task_group

  - matrix_name: "atlas"
    matrix_spec:
      ruby: <%= supported_mri_rubies_3_ubuntu %>
      os: ubuntu2204
    display_name: "Atlas  connectivity tests ${ruby}"
    tags: ["pr"]
    tasks:
       - name: test-atlas

  - matrix_name: "sfp"
    matrix_spec:
      ruby: <%= supported_mri_rubies_3_ubuntu %>
      os: ubuntu2204
    display_name: "Atlas SFP tests ${ruby}"
    tags: ["pr"]
    tasks:
       - name: test-sfp

# https://jira.mongodb.org/browse/RUBY-3311
#  - matrix_name: "aws-lambda"
#    matrix_spec:
#      ruby: 'ruby-3.2'
#      os: ubuntu2204
#    display_name: "AWS Lambda"
#    tasks:
#       - name: test_aws_lambda_task_group
