# Build and Test Guide

This guide explains how to build, test, and verify the **SignalMirror** and **Simulator Platform** using different engines and configurations.

## SignalMirror Engines

The SignalMirror supports two Lua engines:

1.  **GopherLua (Portable)**: Pure Go implementation. No external dependencies. Ideal for development and cross-compilation.
2.  **LuaJIT (High Performance)**: Uses CGO bindings to LuaJIT. Requires `libluajit-5.1` installed. Ideal for production.

> **Note on Distribution**: The official release package includes **both** engines for all platforms. Binaries built via Docker (e.g., cross-compiled Linux artifacts) are suffixed with `-docker` (e.g., `signalmirror-luajit-linux-amd64-docker`). The release is provided as a unified `.zip` or `.tar.gz`.

## Building SignalMirror

The `Makefile` in `tools/signalmirror/` handles the build process.

### 1. Portable Build (GopherLua)

This is the default build. It produces a static binary (mostly) that works on any system.

```bash
cd tools/signalmirror
make build
# Output: bin/tcp-signalmirror
```

### 2. High Performance Build (LuaJIT)

Requires `CGO_ENABLED=1` and the `luajit` build tag.

```bash
cd tools/signalmirror
CGO_ENABLED=1 go build -tags luajit -o bin/tcp-signalmirror-luajit
```

### 3. Cross-Compilation

To build for all supported platforms (using GopherLua):

```bash
cd tools/signalmirror
make build-all
```

## Lua Plugin Engines (out-of-process)

AlchemyBridge can run its Lua logic out-of-process via independent runner binaries
(one per engine), selected at runtime with `engine_mode: plugin` + `plugin_bin` — no
bridge rebuild and no CGO in the host:

```bash
make build-lua-plugin-engines
# bin/plugin-lua-runner-gopherlua   (GopherLua, Lua 5.1, pure Go)
# bin/plugin-lua-runner-luajit   (LuaJIT 2.1, CGO)
# bin/plugin-lua-runner-lua54    (PUC Lua 5.4.8, CGO)
# bin/plugin-lua-runner-lua55    (PUC Lua 5.5.0, CGO)
```

The CGO variants link the prebuilt static libs under `deps/prebuilt/` (genuine Lua
5.4.8 / 5.5.0 built from source by `scripts/build/build_deps.sh`). Full guide:
[docs/tools/LUA_PLUGIN_ENGINES.md](../tools/LUA_PLUGIN_ENGINES.md).

## Running Tests

### 1. Unit Tests

Run Go unit tests for the backend logic:

```bash
cd tools/signalmirror
go test ./...
```

To test with LuaJIT enabled:

```bash
cd tools/signalmirror
go test -tags luajit ./...
```

### 2. Simulator Logic Tests

These tests verify the Lua Simulator logic (independent of the backend). They run using the standard `lua` interpreter.

```bash
cd tests
lua simulation_test.lua
lua integration/scenario_json.lua
```

### 3. Full Integration Tests (Dual Engine)

To verify the backend works correctly with both engines, use the provided test runner:

```bash
# From project root (Linux/Mac)
./tests/run_backend_tests.sh

# From project root (Windows PowerShell)
.\tests\run_backend_tests.ps1
```

This script will:
1.  Build the backend with GopherLua.
2.  Start the server and run traffic tests.
3.  Build the backend with LuaJIT.
3.  Build the backend with LuaJIT.
4.  Start the server and run traffic tests.

### 4. Containerized Testing

To run the full test suite in an isolated, reproducible Linux environment (using Docker):

```bash
# Runs tests inside a Docker container
./tests/run_docker_tests.sh
```

## Generating Distribution Package

To create a full release package (containing all binaries, SDKs, and docs) similar to the official releases, use the enhanced release script. This script handles:

- Cross-compilation for all platforms.
- Lua bytecode generation (`.luajit`).
- Documentation and Example packaging.

```bash
# Safe to run in restricted environments (noexec) or paths with spaces
bash tools/package_release_enhanced.sh
```

### Modular Distribution (Individual Tools)

If you wish to generate independent packages for each tool (SignalMirror, GlitchWire, NetGraft, etc.), use the `dist-tools` command. This will generate separate `.tar.gz` files in the `dist/` folder.

The process automatically performs cross-compilation for Linux, Windows, and macOS (amd64 and arm64 architectures) and includes the documentation, examples, and relevant educational courses for each tool.

```bash
# Generate packages for all tools
make dist-tools

# Generate for a specific tool (directly with script)
bash scripts/release/package_individual_tools.sh glitchwire
```

### Fast Packaging (Documentation Updates)

If you have only updated documentation or static files and do not need to recompile binaries, use `dist-package-only`. This command skips the cross-compilation step, significantly speeding up the generation process.

```bash
# Regenerate distributions without recompiling (requires previous build)
make dist-package-only
```

The output will be available in the `dist/` directory.

This ensures that tests run in a clean environment with the correct LuaJIT and Go versions, regardless of your local machine's setup.

## Troubleshooting

*   **"lua.h not found"**: Ensure `libluajit-5.1-dev` (Linux) or `luajit` (macOS) is installed when building with `-tags luajit`.
*   **"cgo: exec gcc: executable file not found"**: Ensure `gcc` or `clang` is installed for CGO builds.
*   **"attempt to call global 'pcall' (a nil value)"**: Some GopherLua environments may rename `pcall` to `unsafe_pcall`. The `backend_wrapper.lua` script handles this by aliasing it back, but be aware if running custom scripts directly.

## NetGraft

The **NetGraft** (`tools/proxy`) converts HTTP/JSON requests to TCP/ISO8583 (or other formats).

### Build

```bash
cd tools/proxy
make build
# Output: bin/tcp-http-proxy
```

### Cross-Compilation

```bash
cd tools/proxy
make build-all
```

### Test

```bash
cd tools/proxy
make test
```

## ForgeFlow

The **ForgeFlow** (`tools/generator`) creates simulator configurations from OpenAPI specs.

### Build

```bash
cd tools/generator
make build
# Output: bin/openapi-config-generator
```

### Comprehensive Testing

The generator has a suite of contract tests and example generation:

```bash
cd tools/generator
make test-all
```

This command will:
1.  Build the generator.
2.  Validate sample OpenAPI contracts.
3.  Generate example configurations in `tests/examples/`.

### Other Targets

*   `make build-dev`: Build with race detection.
*   `make build-prod`: Optimized production build (stripped symbols).
*   `make check`: Run code quality checks (vet, fmt, lint).

## Generating Distributions (Release)

To generate the final release packages (Release Artifacts) including the simulator, backend binaries, generator, and SDKs, use the scripts located in `tools/`.

### Prerequisites

*   **Go 1.22+**: A recent version of Go is required (system version might be too old).
    *   If using a manual installation (e.g., `/usr/local/go`), ensure it is in your PATH or known to the scripts.

### Packaging Scripts

The process consists of three main steps:

1.  **Core Distribution**:
    Generates `.tar.gz` and `.zip` files containing the simulator and compiled binaries.
    ```bash
    # Run from project root
    bash tools/package_dist.sh
    ```
    *   Compiles binaries for all architectures (Darwin, Linux, Windows).
    *   Compiles Lua code to bytecode for different versions.
    *   Artifacts are output to the `dist/` folder.

2.  **Third Party Package**:
    Packages external dependencies and libraries.
    ```bash
    bash tools/package_third_party.sh
    ```
    *   Generates `dist/MessagingTools_ThirdParty_vX.tar.gz`.

3.  **Self-Contained PoC Distribution (Optional)**:
    Creates a standalone version for proof-of-concept examples (e.g., `examples/reference/payment_network`).
    ```bash
    bash tools/setup_poc_dist.sh
    ```
    *   Creates a `dist` folder within the specific example containing all necessary binaries and libraries to run without external dependencies.

### Artifact Location

Upon completion, you will find the files in the `dist/` folder:
*   `dist/messageflow-simulator-X.Y.Z.tar.gz`
*   `dist/messageflow-simulator-X.Y.Z.zip`


