// test_nop_safepoint.hako — Phase 2 Day 6 Nop + Safepoint tests
// Expected: Nop/Safepoint instructions always succeed (no-op)

using selfhost.vm.hakorune-vm.hakorune_vm_core as HakoruneVmCore
using selfhost.shared.common.string_helpers as StringHelpers

static box Main {
  main() {
    // Test 1: Nop instruction alone
    local mir1 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"nop"},{"op":"const","dst":1,"value":{"type":"i64","value":42}},{"op":"ret","value":1}],"terminator":{"op":"ret","value":1}}]}]}"#

    local result1 = HakoruneVmCore.run(mir1)
    print("Test 1: nop + const 42 + ret → " + StringHelpers.int_to_str(result1))
    if result1 != 42 {
      print("[FAIL] Test 1: expected 42, got " + StringHelpers.int_to_str(result1))
      return 1
    }

    // Test 2: Safepoint instruction alone
    local mir2 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"safepoint"},{"op":"const","dst":1,"value":{"type":"i64","value":100}},{"op":"ret","value":1}],"terminator":{"op":"ret","value":1}}]}]}"#

    local result2 = HakoruneVmCore.run(mir2)
    print("Test 2: safepoint + const 100 + ret → " + StringHelpers.int_to_str(result2))
    if result2 != 100 {
      print("[FAIL] Test 2: expected 100, got " + StringHelpers.int_to_str(result2))
      return 1
    }

    // Test 3: Mixed instructions (const + nop + safepoint + binop + ret)
    local mir3 = r#"{"functions":[{"name":"test","blocks":[{"id":0,"instructions":[{"op":"const","dst":1,"value":{"type":"i64","value":10}},{"op":"nop"},{"op":"const","dst":2,"value":{"type":"i64","value":32}},{"op":"safepoint"},{"op":"binop","op_kind":"Add","dst":3,"lhs":1,"rhs":2},{"op":"nop"},{"op":"ret","value":3}],"terminator":{"op":"ret","value":3}}]}]}"#

    local result3 = HakoruneVmCore.run(mir3)
    print("Test 3: const 10, nop, const 32, safepoint, add, nop, ret → " + StringHelpers.int_to_str(result3))
    if result3 != 42 {
      print("[FAIL] Test 3: expected 42, got " + StringHelpers.int_to_str(result3))
      return 1
    }

    print("✅ All Nop + Safepoint tests PASSED!")
    return 0
  }
}
