#!/bin/bash
# phase1883_nested_minimal_vm.sh - Phase 188.3 P1: Nested Loop Minimal smoke test (VM)
# Verifies Pattern6 (NestedLoopMinimal) JoinIR lowering

source "$(dirname "$0")/../../../lib/test_runner.sh"
require_env || exit 2

# Test: Nested loop (3x3 iterations, sum = 9)
FIXTURE="$NYASH_ROOT/apps/tests/phase1883_nested_minimal.hako"
RUN_TIMEOUT_SECS=${RUN_TIMEOUT_SECS:-10}

set +e
OUTPUT=$(timeout "$RUN_TIMEOUT_SECS" env HAKO_JOINIR_STRICT=1 "$NYASH_BIN" --backend vm "$FIXTURE" 2>&1)
EXIT_CODE=$?
set -e

if [ "$EXIT_CODE" -eq 124 ]; then
    log_error "phase1883_nested_minimal_vm: hakorune timed out (>${RUN_TIMEOUT_SECS}s)"
    exit 1
fi

if [ "$EXIT_CODE" -ne 9 ]; then
    log_error "phase1883_nested_minimal_vm: expected exit code 9, got $EXIT_CODE"
    echo "$OUTPUT"
    exit 1
fi

log_success "phase1883_nested_minimal_vm: Pattern6 nested loop test passed (exit=9)"
exit 0
