# Copyright 2025 Google Inc. All Rights Reserved.
#
# Distributed under MIT license.
# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT

# Workflow for building and running tests with WASM

name: Build/Test WASM

on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, reopened, labeled, unlabeled, synchronize]

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  build_test_wasm:
    name: Build and test with WASM
    runs-on: ubuntu-latest
    env:
      CCACHE_DIR: ${{ github.workspace }}/.ccache
      BUILD_TARGET: wasm32
      EM_VERSION: 3.1.51
      # As of 28.08.2025 ubuntu-latest is 24.04; it is shipped with node 22.18
      NODE_VERSION: 22

    steps:

    - name: Harden Runner
      uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1
      with:
        egress-policy: audit

    - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
      with:
        submodules: true
        fetch-depth: 1

    - name: Install node
      uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
      with:
        node-version: ${{env.NODE_VERSION}}

    - name: Get non-EMSDK node path
      run: which node >> $HOME/.base_node_path

    - name: Install emsdk
      uses: mymindstorm/setup-emsdk@6ab9eb1bda2574c4ddb79809fc9247783eaf9021 # v14
      with:
        version: ${{env.EM_VERSION}}
        no-cache: true

    - name: Set EMSDK node version
      run: |
        echo "NODE_JS='$(cat $HOME/.base_node_path)'" >> $EMSDK/.emscripten
        emsdk construct_env

    - name: Build
      run: |
        LDFLAGS=" -s ALLOW_MEMORY_GROWTH=1 -s NODERAWFS=1 " emcmake cmake -B out .
        cmake --build out
        cd out; ctest --output-on-failure; cd ..
