steps:
  - bash: "rm -rf $(Agent.WorkFolder)/_tools/node"
    displayName: "Clear Node cache"

  - bash: |
      rm -rf /tmp/metro-bundler-cache-*
      rm -rf /tmp/haste-map-react-native-packager-*
      rm -rf ~/.cache/yarn
    displayName: Clear RN, Yarn cache
    condition: and(succeeded(), or(eq(variables['Agent.OS'], 'Linux'), eq(variables['Agent.OS'], 'Darwin')))

  - task: NodeTool@0
    displayName: Use Node 16.x
    inputs:
      versionSpec: 16.x
      checkLatest: true

  - task: Npm@1
    displayName: "npm ci"
    inputs:
      command: custom
      verbose: false
      customCommand: ci

  - bash: npx gulp release
    displayName: "gulp release"

  - task: CopyFiles@2
    displayName: "Copy Files to: test/smoke/package/resources/drop-win"
    inputs:
      Contents: "*.vsix"
      TargetFolder: "test/smoke/package/resources/drop-win"

  - bash: |
      /usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
      echo ">>> Started xvfb"
    displayName: Start xvfb
    condition: and(succeeded(), eq(variables['Agent.OS'], 'Linux'))

  - bash: |
      npm install -g yarn react-native-cli
      npm install -g appium@$(appiumVersion) --force
      npm install -g expo-cli@$(expoCLIVersion)
      npm install -g @expo/ngrok@^4.1.0
    displayName: "npm install -g yarn react-native-cli expo-cli @expo/ngrok appium"

  - bash: npm install --global windows-build-tools --vs2017
    displayName: "npm install --global windows-build-tools --vs2017"
    condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))

  - bash: |
      export MOCHA_FILE=./ReactNativeSmokeTests.xml
      export BASIC_ONLY=""
      export RESET_CACHE=""
      export SKIP_UNSTABLE_TESTS=""
      if [ -z "$(runAllTest)" ]; then
        SKIP_UNSTABLE_TESTS="--skip-unstable-tests"
      fi
      if [ -n "$(basicOnly)" ]; then
        BASIC_ONLY="--basic-only"
      fi
      if [ -n "$(resetApplicationCache)" ]; then
        RESET_CACHE="--reset-cache"
      fi
      yarn smoke-tests $BASIC_ONLY $RESET_CACHE $SKIP_UNSTABLE_TESTS
    displayName: "Run smoke tests"
    env:
      DISPLAY: ":99.0"

  - powershell: Get-AppxPackage | where-object {$_.InstallLocation –Match "RNWApp"} | Remove-AppxPackage
    displayName: "Clean up RNW app"
    condition: and(always(), eq(variables['Agent.OS'], 'Windows_NT'))

  - task: PublishTestResults@2
    displayName: "Publish Test Results"
    inputs:
      testResultsFiles: "./$(smokeTestDirectory)/vscode/test/smoke/ReactNativeSmokeTests.xml"
      failTaskOnFailedTests: true
      testRunTitle: $(Agent.OS) React Native extension smoke tests - Attempt №$(System.JobAttempt)
    condition: always()

  - task: ArchiveFiles@2
    displayName: "Archive SmokeTestLogs"
    inputs:
      rootFolderOrFile: SmokeTestLogs
      archiveFile: "$(Build.ArtifactStagingDirectory)/SmokeTestLogs_$(Agent.JobName)"
    condition: always()

  - task: PublishBuildArtifacts@1
    displayName: "Publish Artifact: SmokeTestLogs"
    inputs:
      ArtifactName: SmokeTestLogs
    condition: always()
