using System.Diagnostics;
using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests;
///
/// lmxopcua#779 — can unbounded recursion in a user script take the whole host process down? A
/// cannot be caught in .NET, so the only safe way to ask is in a
/// separate process: each case re-launches this test assembly (an xunit v3 executable) to run exactly one
/// case, and inspects how that
/// process ended. The test runner itself never executes a recursive script.
///
///
/// Before the guard every case below ended the child with "Stack overflow." and SIGABRT (exit 134 on macOS) — the
/// reproduction recorded in the #779 commit. Now the script fails with
/// , which the run path reports as an ordinary script fault, and the
/// process lives on.
///
public sealed class ScriptRecursionContainmentTests
{
private const string ScenarioVariable = "OTOPCUA_RECURSION_CHILD_SCENARIO";
private const string ResultFileVariable = "OTOPCUA_RECURSION_CHILD_RESULT";
/// Recursive shapes a script can write. None of them is a class member, so the wrapper's
/// single-member check never sees them; the last two have no named self-reference at all.
public static readonly Dictionary Scenarios = new(StringComparer.Ordinal)
{
["local-function"] =
"int Down(int n) => Down(n + 1) + 1; return Down(0);",
["mutual-local-functions"] =
"int Ping(int n) { return Pong(n + 1) + 1; } int Pong(int n) { return Ping(n + 1) + 1; } return Ping(0);",
["self-referencing-lambda"] =
"System.Func f = null!; f = n => f(n + 1) + 1; return f(0);",
["anonymous-self-application"] =
"System.Func