on:
  push:
    branches:
      - main
  pull_request: {}

name: Test

jobs:
  build_and_test:
    name: Test
    runs-on: ubuntu-latest
    permissions:
      checks: write
      contents: read
      pull-requests: write

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v6
        with:
          node-version: 24

      - name: Enable Corepack
        run: |
          corepack enable
          corepack prepare yarn@4.12.0 --activate
          yarn --version

      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: nightly
          components: clippy

      - uses: Swatinem/rust-cache@v2

      - run: cargo test
      - run: cargo run --bin cr calcit/test.cirru

      - name: "try wasm"
        run: >
          cargo build --bin cr-wasm
          && CR_WASM_BIN="./target/debug/cr-wasm" bash scripts/test-wasm.sh

      - uses: giraffate/clippy-action@v1
        with:
          reporter: "github-pr-review"
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: "try js"
        run: >
          yarn
          && yarn tsc
          && cargo run --bin cr calcit/test.cirru js
          && ln -s ../../ node_modules/@calcit/procs
          && cp -v scripts/main.mjs js-out/
          && node js-out/main.mjs

      - name: Deploy CalcitAgent.md to server
        id: deploy
        uses: Pendect/action-rsyncer@v2.0.0
        env:
          DEPLOY_KEY: ${{secrets.rsync_private_key}}
        with:
          flags: "-avzr --progress"
          options: ""
          ssh_options: ""
          src: "docs/*"
          dest: "rsync-user@tiye.me:/web-assets/repo/${{ github.repository }}/docs"

      - name: Display status from deploy
        run: echo "${{ steps.deploy.outputs.status }}"
