name: solana-js-test
on:
  push:
    branches:
      - main
    paths:
      - ".github/workflows/**"
      - "javascript/solana.js/src/**"
      - "javascript/solana.js/test/**"
      - "javascript/solana.js/package.json"
      - "javascript/solana.js/package-lock.json"

env:
  ANCHOR_VERSION: 0.25.0
  CARGO_TERM_COLOR: always
  SOLANA_VERSION: 1.13.5

jobs:
  job1:
    name: Build @switchboard-xyz/solana.js
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: "npm"
          cache-dependency-path: "javascript/solana.js/package-lock.json"
      - name: Install Dependencies
        working-directory: javascript/solana.js
        run: npm ci
      - name: Setup Solana SDK Cache
        id: cache-solana-sdk-build
        uses: actions/cache@v3
        with:
          key:
            ${{ runner.os }}-Solana.js-${{
            hashFiles('javascript/solana.js/src/**') }}
          path: |
            javascript/solana.js/lib
      - name: Build solana.js
        working-directory: javascript/solana.js
        run: npm run build

  job2:
    name: "@switchboard-xyz/solana.js Devnet Tests"
    needs: job1
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: "npm"
          cache-dependency-path: "javascript/solana.js/package-lock.json"
      - name: Install Dependencies
        working-directory: javascript/solana.js
        run: npm ci
      - name: Cache Build For Next Jobs
        id: cache-solana-sdk-build
        uses: actions/cache@v3
        with:
          key:
            ${{ runner.os }}-Solana.js-${{
            hashFiles('javascript/solana.js/src/**') }}
          path: |
            javascript/solana.js/lib
      - name: Build solana.js
        if: steps.cache-solana-sdk-build.outputs.cache-hit != 'true'
        working-directory: javascript/solana.js
        run: npm run build
      - name: Start Local Validator
        uses: switchboard-xyz/solana-local-validator@v0.1
        with:
          solana-version: v1.14.10
          cluster: devnet
          args:
            "--url https://api.devnet.solana.com  --clone
            2TfB33aLaneQb5TNVwyDz3jSZXS6jdW2ARw1Dgf84XCG --clone
            J4CArpsbrZqu1axqQ4AnrqREs3jwoyA1M5LMiQQmAzB9 --clone
            CKwZcshn4XDvhaWVH9EXnk3iu19t6t5xP2Sy2pD6TRDp --clone
            BYM81n8HvTJuqZU1PmTVcwZ9G8uoji7FKM6EaPkwphPt --clone
            FVLfR6C2ckZhbSwBzZY4CX7YBcddUSge5BNeGQv5eKhy"
      - name: Run Tests
        working-directory: javascript/solana.js
        env:
          SOLANA_LOCALNET: 1
        run: npm run test:localnet

  job3:
    name: "@switchboard-xyz/solana.js Mainnet Tests"
    needs: job1
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16
          cache: "npm"
          cache-dependency-path: "javascript/solana.js/package-lock.json"
      - name: Install Dependencies
        working-directory: javascript/solana.js
        run: npm ci
      - name: Cache Build For Next Jobs
        id: cache-solana-sdk-build
        uses: actions/cache@v3
        with:
          key:
            ${{ runner.os }}-Solana.js-${{
            hashFiles('javascript/solana.js/src/**') }}
          path: |
            javascript/solana.js/lib
      - name: Build solana.js
        if: steps.cache-solana-sdk-build.outputs.cache-hit != 'true'
        working-directory: javascript/solana.js
        run: npm run build
      - name: Start Local Validator
        uses: switchboard-xyz/solana-local-validator@v0.1
        with:
          solana-version: v1.14.10
          cluster: mainnet
          args:
            "--url https://api.mainnet-beta.solana.com  --clone
            SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f --clone
            7nYabs9dUhvxYwdTnrWVBL9MYviKSfrEbdWCUbcnwkpF --clone
            Fi8vncGpNKbq62gPo56G4toCehWNy77GgqGkTaAF5Lkk --clone
            CyZuD7RPDcrqCGbNvLCyqk6Py9cEZTKmNKujfPi3ynDd --clone
            J7nSEX8ADf3pVVicd6yKy2Skvg8iLePEmkLUisAAaioD"
      - name: Run Tests
        working-directory: javascript/solana.js
        env:
          SOLANA_LOCALNET: 1
          SWITCHBOARD_PROGRAM_ID: SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f
        run: npm run test:localnet:mainnet
