RynthSuite/Tools/RynthLua.CompatTests/Program.cs
Tom Bohar b48c49f76e RynthSuite as of 2026-11-12 (internal b899791): release 2026.11.12.1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 19:56:37 -05:00

223 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using RynthCore.Plugin.RynthLua;
using RynthCore.PluginCore;
using RynthCore.PluginSdk;
namespace RynthLua.CompatTests;
// Runs every Scripts\*.lua through RynthLua's real Lua runtime against the fake engine
// (FakeHost / FakeWorld), one fresh plugin and world per script, and reports PASS/FAIL.
// A script passes when it called done(), every check() / eq() held, at least one ran, and
// its console shows no "error" line. Files go to a temp folder that is removed afterwards.
//
// Run: dotnet run -c Release [-- filter] (exit 0 = pass, 1 = fail)
internal static unsafe class Program
{
private const int TimeoutMs = 30_000;
private static int Main(string[] args)
{
string filter = args.Length > 0 ? args[0] : string.Empty;
string root = Path.Combine(Path.GetTempPath(), "RynthLuaCompat-" + Guid.NewGuid().ToString("N")[..8]);
RynthLuaPaths.Root = root;
RynthLuaPaths.LegacyScripts = Path.Combine(root, "no-legacy-scripts");
RynthLuaPlugin.ExtraGlobals = Harness.Register;
string scriptDir = ScriptFolder();
var scripts = Directory.GetFiles(scriptDir, "*.lua")
.Where(f => Path.GetFileNameWithoutExtension(f).Contains(filter, StringComparison.OrdinalIgnoreCase))
.OrderBy(f => f, StringComparer.OrdinalIgnoreCase)
.ToList();
Console.WriteLine($"=== RynthLua UtilityBelt compatibility tests ({scripts.Count} scripts) ===");
int passed = 0, checks = 0;
var failed = new List<string>();
if (filter.Length == 0 && !CheckBuiltInVerbsMatchSwitch()) failed.Add("built-in /lua verbs");
if ((filter.Length == 0 || "memoryguard".Contains(filter, StringComparison.OrdinalIgnoreCase)) && !CheckMemoryGuard()) failed.Add("memory guard");
try
{
foreach (string file in scripts)
{
var (ok, n) = RunOne(file);
checks += n;
if (ok) passed++;
else failed.Add(Path.GetFileNameWithoutExtension(file));
}
}
finally
{
try { Directory.Delete(root, recursive: true); } catch { }
}
Console.WriteLine();
Console.WriteLine($"{passed}/{scripts.Count} scripts passed, {checks} checks.");
if (scripts.Count == 0) { Console.WriteLine("ABORT: no scripts ran."); return 1; }
if (failed.Count > 0) Console.WriteLine("FAILED: " + string.Join(", ", failed));
else Console.WriteLine("ALL RYNTHLUA COMPAT TESTS PASSED.");
return failed.Count == 0 ? 0 : 1;
}
private static (bool Ok, int Checks) RunOne(string file)
{
string name = Path.GetFileNameWithoutExtension(file);
FakeWorld.W = new FakeWorld();
Harness.Reset();
RynthCoreApiNative api = FakeHost.Build();
var runtime = new RynthPluginRuntime<RynthLuaPlugin>();
int init = runtime.Init(&api);
RynthLuaPlugin? plugin = runtime.Plugin;
if (init != 0 || plugin == null)
{
Console.WriteLine($"FAIL {name}: the plugin didn't initialize ({init})");
return (false, 0);
}
Harness.Plugin = plugin;
var sw = Stopwatch.StartNew();
string? startError = null;
try
{
plugin.OnLoginComplete();
for (int i = 0; i < 3; i++) Tick(plugin);
Directory.CreateDirectory(RynthLuaPaths.Scripts);
File.Copy(file, Path.Combine(RynthLuaPaths.Scripts, name + ".lua"), overwrite: true);
startError = plugin.StartScript(name);
while (startError == null && !Harness.Done && plugin.ScriptIsLoaded(name) && sw.ElapsedMilliseconds < TimeoutMs)
{
Tick(plugin);
Thread.Sleep(1);
}
}
catch (Exception ex)
{
startError = "harness exception: " + ex;
}
string console = plugin.ScriptConsoleText(name);
// A script may raise an error on purpose; it marks the message "(expected)".
var errors = console.Split('\n')
.Where(l => l.StartsWith("error", StringComparison.OrdinalIgnoreCase) && !l.Contains("(expected)", StringComparison.Ordinal))
.ToList();
var reasons = new List<string>();
if (startError != null) reasons.Add(startError);
if (!Harness.Done) reasons.Add(sw.ElapsedMilliseconds >= TimeoutMs ? "timed out before done()" : "stopped before done()");
if (Harness.Checks == 0) reasons.Add("no checks ran");
reasons.AddRange(Harness.Failures.Select(f => "check: " + f));
reasons.AddRange(errors.Select(e => "console: " + e));
try { plugin.StopScript(name); } catch { }
runtime.Shutdown();
bool ok = reasons.Count == 0;
Console.WriteLine($"{(ok ? "PASS" : "FAIL")} {name} ({Harness.Checks} checks, {sw.ElapsedMilliseconds} ms)");
if (!ok)
{
foreach (string r in reasons.Take(25)) Console.WriteLine(" " + r);
Console.WriteLine(" --- console (last 25 lines) ---");
foreach (string l in console.Split('\n').TakeLast(25)) Console.WriteLine(" | " + l);
}
return (ok, Harness.Checks);
}
/// <summary>
/// RynthLuaPlugin.BuiltInLuaVerbs is kept in step with the switch in HandleLuaChatCommand by hand: a verb in
/// the switch but not the set would be shadowed by a script command of the same name (and listed as a script
/// command in the usage); a verb in the set but not the switch would be unreachable from both. This compares
/// the set with the case labels of that switch, read from the source file.
/// </summary>
private static bool CheckBuiltInVerbsMatchSwitch([System.Runtime.CompilerServices.CallerFilePath] string source = "")
{
string path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(source) ?? string.Empty, "..", "..", "Plugins", "RynthCore.Plugin.RynthLua", "ScriptManager.cs"));
if (!File.Exists(path)) { Console.WriteLine("SKIP built-in /lua verbs: ScriptManager.cs not found beside the project."); return true; }
var field = typeof(RynthLuaPlugin).GetField("BuiltInLuaVerbs", BindingFlags.NonPublic | BindingFlags.Static);
if (field?.GetValue(null) is not HashSet<string> verbs) { Console.WriteLine("FAIL built-in /lua verbs: RynthLuaPlugin.BuiltInLuaVerbs not found."); return false; }
string text = File.ReadAllText(path);
int start = text.IndexOf("switch (sub)", StringComparison.Ordinal);
int end = start < 0 ? -1 : text.IndexOf("default:", start, StringComparison.Ordinal);
if (start < 0 || end < 0) { Console.WriteLine("FAIL built-in /lua verbs: the 'switch (sub)' in HandleLuaChatCommand was not found."); return false; }
var cases = Regex.Matches(text.Substring(start, end - start), @"case\s+""([^""]+)""\s*:")
.Select(m => m.Groups[1].Value).ToHashSet(StringComparer.OrdinalIgnoreCase);
var missingFromSet = cases.Except(verbs, StringComparer.OrdinalIgnoreCase).ToList();
var missingFromSwitch = verbs.Except(cases, StringComparer.OrdinalIgnoreCase).ToList();
bool ok = cases.Count > 0 && missingFromSet.Count == 0 && missingFromSwitch.Count == 0;
Console.WriteLine($"{(ok ? "PASS" : "FAIL")} built-in /lua verbs match the dispatch switch ({cases.Count} verbs)");
if (missingFromSet.Count > 0) Console.WriteLine(" in the switch but not in BuiltInLuaVerbs: " + string.Join(", ", missingFromSet));
if (missingFromSwitch.Count > 0) Console.WriteLine(" in BuiltInLuaVerbs but not in the switch: " + string.Join(", ", missingFromSwitch));
if (cases.Count == 0) Console.WriteLine(" no case labels found (did the switch change shape?)");
return ok;
}
/// <summary>The project's own Scripts folder (so a deleted script can't linger in bin), else the copy in bin.</summary>
private static string ScriptFolder([System.Runtime.CompilerServices.CallerFilePath] string source = "")
{
string beside = Path.Combine(Path.GetDirectoryName(source) ?? string.Empty, "Scripts");
return Directory.Exists(beside) ? beside : Path.Combine(AppContext.BaseDirectory, "Scripts");
}
/// <summary>
/// The memory guard (LuaScriptHost.ChargeAllocation): a script that allocates fast is stopped
/// with a message, and a quiet one keeps running. Three scripts, each in a fresh plugin: one
/// that makes a 400 MB string in one go (the one-run limit), one that keeps 2 MB strings
/// (the 5-second limit), and one that sleeps (must still be running after 3 s).
/// </summary>
private static bool CheckMemoryGuard()
{
var cases = new (string Name, string Code, bool MustStop, string Said)[]
{
// One string.rep can't be stopped part-way, so it has a size cap: a plain Lua error.
("memhog_rep", "local s = string.rep('x', 200000000) print(#s)", true, "string.rep: the result would be"),
("memhog_window", "local t = {} while true do t[#t + 1] = string.rep('y', 1000000) .. #t end", true, "was stopped: it used"),
("memhog_table", "local t = {} local i = 0 while true do i = i + 1 t[i] = { i, i, i } end", true, "was stopped: it used"),
("memquiet", "while true do local s = string.rep('z', 1000) sleep(50) end", false, "was stopped: it used"),
};
bool allOk = true;
foreach (var c in cases)
{
FakeWorld.W = new FakeWorld();
Harness.Reset();
RynthCoreApiNative api = FakeHost.Build();
var runtime = new RynthPluginRuntime<RynthLuaPlugin>();
if (runtime.Init(&api) != 0 || runtime.Plugin == null) { Console.WriteLine($"FAIL memory guard {c.Name}: no plugin"); allOk = false; continue; }
RynthLuaPlugin plugin = runtime.Plugin;
plugin.OnLoginComplete();
for (int i = 0; i < 3; i++) Tick(plugin);
Directory.CreateDirectory(RynthLuaPaths.Scripts);
File.WriteAllText(Path.Combine(RynthLuaPaths.Scripts, c.Name + ".lua"), c.Code);
string? err = plugin.StartScript(c.Name);
var sw = Stopwatch.StartNew();
long limitMs = c.MustStop ? 20_000 : 3_000;
while (err == null && plugin.ScriptIsLoaded(c.Name) && sw.ElapsedMilliseconds < limitMs) { Tick(plugin); Thread.Sleep(1); }
bool loaded = plugin.ScriptIsLoaded(c.Name);
string console = plugin.ScriptConsoleText(c.Name);
bool said = console.Contains(c.Said, StringComparison.Ordinal);
bool ok = err == null && (c.MustStop ? (!loaded && said) : (loaded && !said));
string lastLine = console.Trim().Split('\n').LastOrDefault() ?? string.Empty;
Console.WriteLine($"{(ok ? "PASS" : "FAIL")} memory guard {c.Name} ({sw.ElapsedMilliseconds} ms, {(loaded ? "running" : "stopped")}){(ok ? "" : ": " + (err ?? lastLine))}");
if (ok && c.MustStop) Console.WriteLine(" " + console.Split('\n').First(l => l.Contains(c.Said, StringComparison.Ordinal)).Trim());
try { plugin.StopScript(c.Name); } catch { }
runtime.Shutdown();
GC.Collect();
allOk &= ok;
}
return allOk;
}
private static void Tick(RynthLuaPlugin plugin)
{
FakeWorld.W.Step();
while (FakeWorld.W.Events.Count > 0) FakeWorld.W.Events.Dequeue()(plugin);
plugin.OnTick();
}
}