parameters:
  - name: SDKType
    type: string
    default: client
  - name: ServiceDirectory
    type: string
    default: 'not-specified' # Set a default that breaks in obvious ways.
  - name: TestPipeline
    type: boolean
    default: false
  - name: Artifacts
    type: object
    default: []
  - name: AdditionalModules
    type: object
    default: []
  - name: PreTestSteps
    type: object
    default: []
  - name: Matrix
    type: object
  - name: CloudConfig
    type: object
    default: {}
  - name: DependsOn
    type: string
    default: ''
  - name: UsePlatformContainer
    type: boolean
    default: false
  - name: PreBuildSteps
    type: object
    default: []
  - name: TestGoals
    type: string
    default: $(TestGoals)
  - name: TestOptions
    type: string
    default: $(TestOptions)
  - name: TestParallelization
    type: string
    default: '1C'
  - name: BuildParallelization
    type: string
    default: '2C'

jobs:
  - job: 'Test'
    dependsOn: ${{ parameters.DependsOn }}
    condition: and(succeeded(), ne(variables['Skip.Test'], 'true'))

    strategy:
      matrix: $[ ${{ parameters.Matrix }} ]

    pool:
      name: $(Pool)
      vmImage: $(OSVmImage)

    ${{ if eq(parameters.UsePlatformContainer, 'true') }}:
      # Add a default so the job doesn't fail when the matrix is empty
      container: $[ variables['Container'] ]

    variables:
      - template: /eng/pipelines/templates/variables/globals.yml

    steps:
      - template: /eng/pipelines/templates/steps/initialize-test-environment.yml
        parameters:
          Artifacts: ${{ parameters.Artifacts }}
          AdditionalModules: ${{ parameters.AdditionalModules }}
          ServiceDirectory: ${{ parameters.ServiceDirectory }}
          CheckoutRecordings: true
          SDKType: ${{ parameters.SDKType }}

      - template: /eng/common/testproxy/test-proxy-tool.yml
        parameters:
          runProxy: true

      - template: /eng/pipelines/templates/steps/restore-test-proxy-recordings.yml
        parameters:
          Paths: $(SparseCheckoutDirectories)

      - pwsh: |
          $files = Get-ChildItem -Path $(Build.SourcesDirectory) -Filter test-proxy.log
          foreach($file in $files){
              Write-Host "##[group]$file"
              cat $file
              Write-Host "##[endgroup]"
          }
        displayName: Dump Test-Proxy Logs
        condition: succeededOrFailed()

      - template: /eng/pipelines/templates/steps/install-reporting-tools.yml
        parameters:
          JdkVersion: $(JavaTestVersion)

      - ${{ parameters.PreBuildSteps }}

      - ${{ parameters.PreTestSteps }}

      - template: /eng/pipelines/templates/steps/build-and-test.yml
        parameters:
          SDKType: ${{ parameters.SDKType }}
          ServiceDirectory: ${{ parameters.ServiceDirectory }}
          ParallelTestPlayback: 'true'
          TestGoals: ${{ parameters.TestGoals }}
          TestOptions: ${{ parameters.TestOptions }}
          TestParallelization: ${{ parameters.TestParallelization }}
          BuildParallelization: ${{ parameters.BuildParallelization }}

      - template: /eng/pipelines/templates/steps/upload-repository-on-failure.yml

      - template: /eng/common/testproxy/publish-proxy-logs.yml
