parameters:
  - name: name
    type: string
  - name: nodeVersion
    type: string

# Steps the for regresion test using autorest-compare.
jobs:
  - job: ${{ parameters.name }}
    displayName: Node ${{ parameters.nodeVersion }}
    variables:
      AUTOREST_HOME: $(Build.SourcesDirectory) # Setting autorest.home to local path to prevent conflict with other parralel jobs in the same machine.
    steps:
      - download: current
        artifact: packages
        displayName: Download packages(.tgz)

      - script: |
          corePkg="$(find $(Pipeline.Workspace)/packages -type f -iname "autorest-core-*.tgz" )"
          modelerfourPkg="$(find $(Pipeline.Workspace)/packages -type f -iname "autorest-modelerfour-*.tgz" )"

          [ -z "$corePkg" ] && echo "Error: Couldn't find autorest core package" && exit 1
          [ -z "$modelerfourPkg" ] && echo "Error: Couldn't find autorest modelerfour package" && exit 1

          echo "Found core package $corePkg"
          echo "Found modelerfour package $modelerfourPkg"

          sed -i "s,--version:../packages/extensions/core,--version:$corePkg," ./regression-tests/regression-compare.yaml
          sed -i "s,--use:../packages/extensions/modelerfour,--use:$modelerfourPkg," ./regression-tests/regression-compare.yaml
        displayName: Find packages path

      - task: NodeTool@0
        inputs:
          versionSpec: ${{ parameters.nodeVersion }}
        displayName: "Install Node.js"

      - task: NodeTool@0
        inputs:
          versionSpec: ${{ parameters.nodeVersion }}
        displayName: "Install Node.js"

      - task: UsePythonVersion@0
        inputs:
          versionSpec: "3.11"
        displayName: "Use Python 3.11"

      - script: |
          npm install -g npm
          npx @microsoft/rush update
          npx @microsoft/rush rebuild
        displayName: Install and build

      - script: |
          echo "Linking autorest-compare"
          cd $(Build.SourcesDirectory)/packages/tools/compare
          npm link --force

          echo "Linking autorest"
          cd $(Build.SourcesDirectory)/packages/apps/autorest
          npm link --force
        displayName: Install autorest-compare

      - script: |
          npm install

          echo "Autorest TestServer Version"
          npm ls "@microsoft.azure/autorest.testserver"

        displayName: Install dependencies
        workingDirectory: regression-tests

      - script: autorest-compare --compare:./regression-tests/regression-compare.yaml --language:python
        displayName: Regression Test - @autorest/python

      - script: autorest-compare --compare:./regression-tests/regression-compare.yaml --language:typescript
        displayName: Regression Test - @autorest/typescript
