RynthServers as of 2026-11-10 (internal 5a8f64b): release 2026.11.10.1

This commit is contained in:
Tom Bohar 2026-11-10 15:28:13 -05:00
commit 8547236e48
27 changed files with 2961 additions and 16 deletions

View file

@ -0,0 +1,951 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using RynthCore.Engine.UI.Sections;
using RynthCore.Plugin.RynthDreamWeave;
using RynthCore.Plugin.ServerPack;
using RynthCore.RynthAiTests.Harness;
namespace RynthServerPack.Tests;
// DreamWeave's web buttons, the pure part: the destinations and the "/weblogin" reply's wire text.
internal static class DreamWeaveWebTests
{
public static void Register(Runner r)
{
r.Add("dreamweave web: a player's copy of the launch line is never taken", PlayerCopy);
r.Add("dreamweave web: the launch URL is built from constants and a clean code", UrlFromConstants);
r.Add("dreamweave web: destination ids are verbs no one else uses", IdsAreFree);
r.Add("dreamweave web: LaunchWire: code alphabet is strict", Alphabet);
r.Add("dreamweave web: ACE's refusals parse as refusals, other lines are ignored", Refusals);
r.Add("dreamweave web: replies count only while asked, once", RepliesOnlyWhileAsked);
r.Add("dreamweave web: one request at a time, never stuck", OneAtATime);
r.Add("dreamweave web: the code never reaches a status or message", CodeNeverInStatus);
r.Add("dreamweave web: a code-shaped refusal is replaced, never shown", CodeShapedRefusal);
r.Add("dreamweave web: an expired request frees itself", SelfExpiry);
r.Add("dreamweave web: reset keeps the throttle", ResetKeepsThrottle);
r.Add("dreamweave web: a late code is eaten while it could still be valid", LateCodeIsStale);
r.Add("dreamweave web: overlapping requests never leak a code to chat", OverlappingRequests);
r.Add("dreamweave web: profile keys separate accounts", ProfileKeys);
r.Add("dreamweave web: ImGui only in WebButtons, no context switching, draw makes no host calls (its catch logs via LogTagged)", ThreadRules);
r.Add("dreamweave web: an open's answer lands only on its own request", ResultGeneration);
r.Add("dreamweave web: a malformed launch-code line never reaches chat", MalformedSuccess);
r.Add("dreamweave web: the source scan's helpers read code, not comments or strings", ScanHelpers);
r.Add("dreamweave web: the plugin's shared hooks hand off to the web tools first", HooksHandOff);
r.Add("dreamweave web: the bar icon's menu lists the destinations, in the engine's own terms", BarMenuItems);
r.Add("dreamweave web: the engine draws the bar menu from API 95", EngineDrawsMenu);
r.Add("dreamweave web: the card keeps its buttons on every engine", CardKeepsButtons);
}
private static void BarMenuItems()
{
List<ServerBarMenuItem> items = DreamWeaveWeb.BarMenu();
string[][] want =
{
new[] { "abilities", "Class Abilities", ((char)0xE6A2).ToString(), "/rserver abilities" },
new[] { "market", "Market", ((char)0xE470).ToString(), "/rserver market" },
new[] { "suit", "Suit Builder", ((char)0xE40A).ToString(), "/rserver suit" },
};
Check.Eq(items.Count, want.Length, "one item per destination");
for (int i = 0; i < want.Length && i < items.Count; i++)
{
Check.Eq(items[i].Id, want[i][0], "id " + i);
Check.Eq(items[i].Tooltip, want[i][1], "tooltip " + i);
Check.Eq(items[i].Icon, want[i][2], "icon " + i);
Check.Eq(items[i].Command, want[i][3], "command " + i);
Check.Eq(SectionManifestParser.Command(items[i].Command), items[i].Command, "the engine keeps the command " + i);
Check.Eq(SectionManifestParser.Icon(items[i].Icon), items[i].Icon, "the engine keeps the icon " + i);
Check.True(DreamWeaveWeb.ById(items[i].Id) != null, "the command's verb is a destination: " + items[i].Id);
}
Check.True(items.Count <= SectionManifestParser.MaxBarMenuItems, "within the engine's cap");
// And through the manifest, as the engine reads it.
var info = new ServerPackInfo("dreamweave", "DreamWeave", new[] { "play.acdreamweave.com" }, Array.Empty<string>());
SectionManifest m = SectionManifestParser.Parse("RynthDreamWeave", ServerPackModel.BuildSectionsJson(info, "RynthDreamWeave", null, barMenu: items), out string error);
Check.Eq(error, string.Empty, "parses clean: " + error);
Check.Eq(m.Windows.Single().Menu.Length, 3, "the engine reads three items");
}
private static void EngineDrawsMenu()
{
// The plugin's const is pinned to the engine's own (the linked PluginContractVersion), so a renumber fails here.
uint bar = RynthCore.Engine.Plugins.PluginContractVersion.BarMenu;
Check.Eq(DreamWeaveWeb.BarMenuEngineApi, bar, "the engine API that draws the bar menu equals the engine's BarMenu");
Check.False(DreamWeaveWeb.EngineDrawsBarMenu(bar - 1), "one before: the card keeps its buttons");
Check.True(DreamWeaveWeb.EngineDrawsBarMenu(bar), "at BarMenu: the engine draws the menu");
Check.True(DreamWeaveWeb.EngineDrawsBarMenu(bar + 1), "after: the engine draws the menu");
Check.False(DreamWeaveWeb.EngineDrawsBarMenu(0), "0: unknown version keeps the buttons");
}
private static void CardKeepsButtons()
{
string hostPath = Path.Combine(ServerProjectsTests.PluginsDir(), "RynthCore.Plugin.RynthDreamWeave", "DreamWeaveWebTools.cs");
Check.True(File.Exists(hostPath), "DreamWeaveWebTools.cs exists");
if (!File.Exists(hostPath)) return;
string host = StripComments(File.ReadAllText(hostPath));
List<string> card = StatementsAfter(host, "partial void ServerCardActions()");
Check.Eq(card.Count, 1, "ServerCardActions implemented once");
// 2026-11-09: the card keeps its buttons on every engine (buttons in the feature's own window).
Check.True(card.Count == 1 && card[0].Contains("WebButtons.Draw(", StringComparison.Ordinal),
"ServerCardActions draws the web buttons");
Check.False(card.Count == 1 && card[0].Contains("EngineDrawsBarMenu", StringComparison.Ordinal),
"ServerCardActions never hides the buttons for an engine with the bar menu");
Check.False(host.Contains("CurrentApiVersion", StringComparison.Ordinal), "the plugin never reads the compile-time CurrentApiVersion");
Check.True(host.Contains("DreamWeaveWeb.BarMenu()", StringComparison.Ordinal), "ServerBarMenu supplies DreamWeaveWeb.BarMenu()");
}
// -- Host wiring (source scan: the plugin is NativeAOT, so its host code can't run here) --
// The rules are allow-lists: what may appear is named, so a new spelling of a forbidden call fails.
private static IEnumerable<string> SourceFiles(string root)
=> Directory.GetFiles(root, "*.cs", SearchOption.AllDirectories)
.Where(p => !p.Contains(Path.DirectorySeparatorChar + "bin" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)
&& !p.Contains(Path.DirectorySeparatorChar + "obj" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase));
/// <summary>
/// The website feature's own files, the ones its rules hold to: DreamWeaveWeb*.cs, Web*.cs and LaunchWire.cs.
/// The plugin's other features (the Skills and Character panels, the Bank page) draw with ImGui themselves.
/// </summary>
private static List<string> WebFiles(string dw)
{
var files = SourceFiles(dw).Where(p =>
{
string n = Path.GetFileName(p);
return n.StartsWith("DreamWeaveWeb", StringComparison.Ordinal) || n.StartsWith("Web", StringComparison.Ordinal) || n == "LaunchWire.cs";
}).ToList();
Check.True(files.Count >= 6, "the website feature's files are found (" + files.Count + ")");
return files;
}
/// <summary>
/// The text with // and /* */ comments removed. String literals ("...", @"...", $"...") and char
/// literals are kept as they are, so a "//" inside one is not a comment.
/// </summary>
internal static string StripComments(string text)
{
var sb = new System.Text.StringBuilder(text.Length);
int i = 0;
while (i < text.Length)
{
char c = text[i];
char next = i + 1 < text.Length ? text[i + 1] : '\0';
if (c == '/' && next == '/')
{
while (i < text.Length && text[i] != '\n') i++;
continue;
}
if (c == '/' && next == '*')
{
int end = text.IndexOf("*/", i + 2, StringComparison.Ordinal);
i = end < 0 ? text.Length : end + 2;
sb.Append(' ');
continue;
}
if (c == '"' || (c == '@' && next == '"') || (c == '$' && next == '"') || (c == '$' && next == '@') || (c == '@' && next == '$'))
{
int start = i;
bool verbatim = false;
while (i < text.Length && text[i] != '"') { if (text[i] == '@') verbatim = true; i++; }
i++; // the opening quote
while (i < text.Length)
{
if (verbatim)
{
if (text[i] == '"' && i + 1 < text.Length && text[i + 1] == '"') { i += 2; continue; }
if (text[i] == '"') { i++; break; }
}
else
{
if (text[i] == '\\') { i += 2; continue; }
if (text[i] == '"' || text[i] == '\n') { i++; break; }
}
i++;
}
sb.Append(text, start, Math.Min(i, text.Length) - start);
continue;
}
if (c == '\'')
{
int start = i++;
while (i < text.Length && text[i] != '\'' && text[i] != '\n') i += text[i] == '\\' ? 2 : 1;
i++;
sb.Append(text, start, Math.Min(i, text.Length) - start);
continue;
}
sb.Append(c);
i++;
}
return sb.ToString();
}
/// <summary>The statement at <paramref name="i"/>: a balanced { } block, or up to the next ';'.</summary>
private static string StatementAt(string code, int i)
{
while (i < code.Length && char.IsWhiteSpace(code[i])) i++;
if (i < code.Length && code[i] == '{')
{
int depth = 0, j = i;
for (; j < code.Length; j++)
{
if (code[j] == '{') depth++;
else if (code[j] == '}' && --depth == 0) break;
}
return code.Substring(i, Math.Min(j + 1, code.Length) - i);
}
int semi = code.IndexOf(';', i);
return code.Substring(i, (semi < 0 ? code.Length : semi + 1) - i);
}
/// <summary>The statement after each <paramref name="head"/>.</summary>
private static List<string> StatementsAfter(string code, string head)
{
var found = new List<string>();
int at = 0;
while ((at = code.IndexOf(head, at, StringComparison.Ordinal)) >= 0)
{
found.Add(StatementAt(code, at + head.Length));
at += head.Length;
}
return found;
}
/// <summary>Every lock statement: its target (trimmed) and its body.</summary>
internal static List<(string Target, string Body)> Locks(string code)
{
var found = new List<(string, string)>();
foreach (System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(code, @"\block\s*\("))
{
int open = m.Index + m.Length - 1, depth = 0, j = open;
for (; j < code.Length; j++)
{
if (code[j] == '(') depth++;
else if (code[j] == ')' && --depth == 0) break;
}
if (j >= code.Length) { found.Add(("<unclosed>", "")); continue; }
found.Add((code.Substring(open + 1, j - open - 1).Trim(), StatementAt(code, j + 1)));
}
return found;
}
/// <summary>
/// A call: an identifier or a member chain (".", "?."), an optional type-argument list, then "(".
/// Group 1 is the chain; the type arguments are left out of it ("Notify<int>(" is "Notify").
/// </summary>
private static readonly System.Text.RegularExpressions.Regex CallPattern = new(
@"(?<![A-Za-z0-9_.?])([A-Za-z_][A-Za-z0-9_]*(?:\s*\??\.\s*[A-Za-z_][A-Za-z0-9_]*)*)\s*(?:<[^<>();{}]*>)?\s*\(");
/// <summary>
/// Every call in <paramref name="body"/> that is not a member call on <paramref name="receiver"/>
/// ("_web.Begin(" is allowed; "Send(", "Helper(", "if (", "x._web.Begin(", "a?.Invoke(", "Notify<int>(" are not).
/// </summary>
internal static List<string> CallsNotOn(string body, string receiver)
{
var bad = new List<string>();
var allowed = new System.Text.RegularExpressions.Regex("^" + System.Text.RegularExpressions.Regex.Escape(receiver) + @"\.[A-Za-z_][A-Za-z0-9_]*$");
foreach (System.Text.RegularExpressions.Match m in CallPattern.Matches(body))
{
string chain = System.Text.RegularExpressions.Regex.Replace(m.Groups[1].Value, @"\s+", "");
if (!allowed.IsMatch(chain)) bad.Add(chain + "(");
}
return bad;
}
private static string Collapse(string s) => System.Text.RegularExpressions.Regex.Replace(s, @"\s+", "");
private static int CountOf(string text, string needle)
{
int n = 0, at = 0;
while ((at = text.IndexOf(needle, at, StringComparison.Ordinal)) >= 0) { n++; at += needle.Length; }
return n;
}
// DreamWeavePlugin.cs and DreamWeaveSkills.cs own the hooks the web tools share with the bank and the Skills
// panel: each hands off to its Web* method before its own code, so no gate of theirs comes first.
private static void HooksHandOff()
{
string dw = Path.Combine(ServerProjectsTests.PluginsDir(), "RynthCore.Plugin.RynthDreamWeave");
string plugin = StripComments(File.ReadAllText(Path.Combine(dw, "DreamWeavePlugin.cs")));
string skills = StripComments(File.ReadAllText(Path.Combine(dw, "DreamWeaveSkills.cs")));
(string Code, string Head, string Call)[] hooks =
{
(plugin, "partial void ServerLoginComplete()", "WebLoginComplete();"),
(plugin, "partial void ServerLogout()", "WebLogout();"),
(plugin, "partial void ServerShutdown()", "WebShutdown();"),
(plugin, "partial void ServerChatText(string text, int chatType, ref bool eat)", "WebChatText(text, chatType, ref eat);"),
(plugin, "partial void ServerTick(bool active)", "WebTick();"),
(skills, "partial void ServerChatCommand(string[] parts, ref bool handled)", "WebChatCommand(parts, ref handled);"),
(skills, "partial void ServerCommandVerbs(List<string> verbs)", null!),
};
foreach (var (code, head, call) in hooks)
{
List<string> body = StatementsAfter(code, head);
Check.Eq(body.Count, 1, head + ": implemented once");
if (body.Count != 1 || call == null) continue;
Check.True(body[0].TrimStart('{', ' ', '\n', '\r').StartsWith(call, StringComparison.Ordinal), head + ": " + call + " comes first");
}
List<string> verbs = StatementsAfter(skills, "partial void ServerCommandVerbs(List<string> verbs)");
Check.True(verbs.Count == 1 && verbs[0].Contains("WebCommandVerbs(verbs);", StringComparison.Ordinal), "ServerCommandVerbs adds the web verbs");
List<string> info = StatementsAfter(plugin, "partial void ServerInfoLines(Action<string> line)");
Check.True(info.Count == 1 && info[0].Contains("WebInfoLines(line);", StringComparison.Ordinal), "ServerInfoLines adds the web tools' line");
// The web tools implement none of the hooks they are handed: one implementation each, in the hub files.
string web = StripComments(File.ReadAllText(Path.Combine(dw, "DreamWeaveWebTools.cs")));
foreach (string hook in new[] { "ServerLoginComplete", "ServerLogout", "ServerShutdown", "ServerChatText", "ServerTick", "ServerChatCommand", "ServerCommandVerbs", "ServerInfoLines" })
Check.False(web.Contains("partial void " + hook + "(", StringComparison.Ordinal), "DreamWeaveWebTools.cs leaves " + hook + " to the hub");
}
// The draw path makes no host calls. ServerCardActions' catch (render thread) does log through
// LogTagged once per failure, the shared pattern RenderEmbedded's own catch already follows.
private static void ThreadRules()
{
string plugins = ServerProjectsTests.PluginsDir();
string dw = Path.Combine(plugins, "RynthCore.Plugin.RynthDreamWeave");
string buttonsPath = Path.Combine(dw, "DreamWeave", "WebButtons.cs");
string hostPath = Path.Combine(dw, "DreamWeaveWebTools.cs");
Check.True(File.Exists(buttonsPath), "DreamWeave\\WebButtons.cs exists");
Check.True(File.Exists(hostPath), "DreamWeaveWebTools.cs exists");
if (!File.Exists(buttonsPath) || !File.Exists(hostPath)) return;
var bareImGui = new System.Text.RegularExpressions.Regex(@"(?<![A-Za-z0-9_])ImGui\.");
foreach (string f in WebFiles(dw))
{
string code = StripComments(File.ReadAllText(f));
string name = Path.GetFileName(f);
Check.False(code.Contains("SetCurrentContext", StringComparison.Ordinal), name + ": no SetCurrentContext (RenderEmbedded/OnRender own the context)");
bool isButtons = string.Equals(Path.GetFullPath(f), Path.GetFullPath(buttonsPath), StringComparison.OrdinalIgnoreCase);
if (!isButtons)
{
Check.False(bareImGui.IsMatch(code), name + ": no ImGui. calls outside WebButtons.cs");
Check.False(code.Contains("SafeImGui.", StringComparison.Ordinal), name + ": no SafeImGui. calls outside WebButtons.cs");
}
}
// StripComments does not model raw string literals: a DreamWeave file with one must fail, not scan
// wrongly. Only the files this scan parses in depth are held to it (the web tools' files and the two hub
// files HooksHandOff reads), so another server, or another DreamWeave feature, can't break it.
foreach (string f in WebFiles(dw).Concat(new[] { Path.Combine(dw, "DreamWeavePlugin.cs"), Path.Combine(dw, "DreamWeaveSkills.cs") }))
Check.False(File.ReadAllText(f).Contains("\"\"\"", StringComparison.Ordinal), Path.GetFileName(f) + ": no raw string literal (the scan does not support them)");
var processStart = new System.Text.RegularExpressions.Regex(@"\bProcess\s*\.\s*Start\b");
foreach (string f in SourceFiles(plugins))
Check.False(processStart.IsMatch(StripComments(File.ReadAllText(f))), Path.GetFileName(f) + ": no Process.Start under Plugins (ShellExecuteW only)");
// Monitor would take the launcher's lock without a lock statement, out of the scan's sight.
var monitor = new System.Text.RegularExpressions.Regex(@"\bMonitor\s*\.");
foreach (string f in new[] { buttonsPath, hostPath })
Check.False(monitor.IsMatch(StripComments(File.ReadAllText(f))), Path.GetFileName(f) + ": no Monitor (lock statements only)");
// The launcher holds no callbacks, so nothing can run host code from inside its lock.
string launcher = StripComments(File.ReadAllText(Path.Combine(dw, "DreamWeave", "WebLauncher.cs")));
Check.False(System.Text.RegularExpressions.Regex.IsMatch(launcher, @"\b(Action|Func|delegate)\b"), "WebLauncher.cs: no Action, Func or delegate");
// WebButtons.cs calls only ImGui/SafeImGui, the launcher (under its lock) and the click it was handed.
string buttons = StripComments(File.ReadAllText(buttonsPath));
int draw = buttons.IndexOf("public static void Draw(", StringComparison.Ordinal);
Check.True(draw >= 0, "WebButtons.Draw exists");
string drawBody = draw < 0 ? "" : StatementAt(buttons, buttons.IndexOf(')', draw) + 1);
var drawAllowed = new System.Text.RegularExpressions.Regex(@"^(ImGui\.[A-Za-z]+|SafeImGui\.[A-Za-z]+|click|lock|if|foreach)\($");
var drawCalls = new List<string>();
foreach (System.Text.RegularExpressions.Match m in CallPattern.Matches(drawBody))
{
string chain = Collapse(m.Groups[1].Value) + "(";
if (!drawAllowed.IsMatch(chain)) drawCalls.Add(chain);
}
Check.Eq(string.Join(" ", drawCalls), "", "WebButtons.Draw calls only ImGui, SafeImGui and click (draw only draws)");
Check.True(buttons.Contains("ImGui.SmallButton(d.Label + \"##dw_web_\" + d.Id)", StringComparison.Ordinal), "WebButtons.cs: one SmallButton per destination, id-suffixed");
foreach (string pair in new[] { "Begin", "End", "Push", "Pop" })
Check.False(System.Text.RegularExpressions.Regex.IsMatch(buttons, @"ImGui\." + pair), "WebButtons.cs: no ImGui." + pair + "* (no paired calls)");
List<(string Target, string Body)> drawLocks = Locks(buttons);
Check.Eq(drawLocks.Count, 1, "WebButtons.cs: one lock, to copy Pending and Status out");
foreach (var (target, body) in drawLocks)
{
Check.Eq(target, "l.Gate", "WebButtons.cs: the lock is the launcher's Gate");
Check.Eq(string.Join(" ", CallsNotOn(body, "l")), "", "WebButtons.cs: only launcher members under the lock");
Check.Eq(Collapse(body), "{pending=l.Pending!=null;status=l.Status;}", "WebButtons.cs: the lock copies Pending and Status, nothing else");
}
string host = StripComments(File.ReadAllText(hostPath));
List<string> cardHook = StatementsAfter(host, "partial void ServerCardActions()");
Check.Eq(cardHook.Count, 1, "DreamWeaveWebTools.cs implements ServerCardActions once");
string clickPath = cardHook.Count == 1 ? cardHook[0] : "";
Check.True(clickPath.Contains("WebButtons.Draw(_web, Active, _webClick ??= d => RunOnTick(() => StartLaunch(d)));", StringComparison.Ordinal),
"the card hook draws through WebButtons.Draw, and a click only queues StartLaunch with RunOnTick");
Check.Eq(CountOf(host, "StartLaunch("), 3, "StartLaunch: defined, queued by the click, called by the verb (nothing else)");
// Every lock in the host is on the launcher and holds launcher member calls only.
List<(string Target, string Body)> hostLocks = Locks(host);
Check.Eq(hostLocks.Count, 9, "the host takes the launcher's lock in exactly 9 places");
foreach (var (target, body) in hostLocks)
{
Check.Eq(target, "_web.Gate", "every lock in the host is lock (_web.Gate)");
Check.Eq(string.Join(" ", CallsNotOn(body, "_web")), "", "only _web members under lock (_web.Gate): " + Collapse(body));
}
// Each lock body, exactly: launcher calls and reads, nothing else (no host property read either).
string[] lockBodies =
{
"{begun=_web.Begin(d,Environment.TickCount64);status=_web.Status;}", // StartLaunch
"_web.Cancel(notSent);", // StartLaunch, send failed
"{kind=_web.OnChatLine(text,chatType,Environment.TickCount64,outd,outlaunchCode,out_);status=_web.Status;generation=_web.Generation;}", // ServerChatText
"_web.SetResult(generation,text);", // SetWebResult
"{timedOut=_web.TakeTimedOut(Environment.TickCount64);status=_web.Status;}", // ServerTick
"status=_web.Status;", // ServerInfoLines
"_web.Reset();", // ServerLoginComplete
"_web.Reset();", // ServerLogout
"_web.Reset();", // ServerShutdown
};
Check.Eq(string.Join("\n", hostLocks.Select(l => Collapse(l.Body))), string.Join("\n", lockBodies), "the host's lock bodies, exactly");
Check.False(host.Contains("DateTime", StringComparison.Ordinal) || host.Contains("Stopwatch", StringComparison.Ordinal), "nowMs comes from Environment.TickCount64 only");
Check.True(host.Contains("Environment.TickCount64", StringComparison.Ordinal), "the host uses Environment.TickCount64");
// The code goes into the launch URL and nowhere else.
int uses = System.Text.RegularExpressions.Regex.Matches(host, @"\blaunchCode\b").Count;
Check.Eq(uses, 3, "launchCode: declared, filled by OnChatLine, used once (DreamWeaveWeb.LaunchUrl)");
Check.Eq(CountOf(host, "out launchCode"), 1, "launchCode is filled only by OnChatLine");
Check.True(host.Contains("DreamWeaveWeb.LaunchUrl(launchCode, ", StringComparison.Ordinal), "the code's one use is the launch URL");
// The chat hook reads every line through the launcher, whatever the world: no Active before OnChatLine.
List<string> chatHook = StatementsAfter(host, "private void WebChatText(string text, int chatType, ref bool eat)");
Check.Eq(chatHook.Count, 1, "DreamWeaveWebTools.cs implements WebChatText once");
string chat = chatHook.Count == 1 ? chatHook[0] : "";
int onChat = chat.IndexOf("OnChatLine(", StringComparison.Ordinal);
Check.True(onChat > 0, "ServerChatText calls OnChatLine");
// Everything before the lock, exactly: declarations only, so no gate, return or throw comes first.
System.Text.RegularExpressions.Match firstLock = System.Text.RegularExpressions.Regex.Match(chat, @"\block\s*\(");
Check.Eq(firstLock.Success ? Collapse(chat.Substring(0, firstLock.Index)) : "<no lock>",
"{LaunchLinekind=LaunchLine.NotOurs;try{WebDestination?d;stringlaunchCode;stringstatus;longgeneration;",
"ServerChatText: nothing but declarations before the launcher reads the line (a code must be eaten whatever the world is now)");
// Stale eats the line and does nothing else.
int stale = host.IndexOf("case LaunchLine.Stale:", StringComparison.Ordinal);
Check.True(stale >= 0, "the chat hook handles LaunchLine.Stale");
if (stale >= 0)
{
int from = stale + "case LaunchLine.Stale:".Length;
int end = host.IndexOf("return;", from, StringComparison.Ordinal);
Check.Eq(end < 0 ? "<no return>" : Collapse(host.Substring(from, end - from)), "eat=true;", "the Stale arm is exactly 'eat = true; return;'");
}
}
private static void MalformedSuccess()
{
string head = "[WEB] Launch code: ";
string long44 = head + Code + "A (single use, expires in 60 seconds)";
string dotted = head + Code + ".";
foreach (string l in new[] { long44, dotted })
{
Check.True(!Parse(l, 0x05, out _, out _), "TryParse still rejects it: " + l.Length + " chars");
Check.True(LaunchWire.IsSuccessShaped(l, 0x05), "success-shaped: " + l.Length + " chars");
Check.False(LaunchWire.IsSuccessShaped(l, 0x02), "a player's copy is not success-shaped");
}
Check.True(LaunchWire.IsSuccessShaped(" [WEB] Launch code", 0x05), "the stem alone, past leading space");
Check.False(LaunchWire.IsSuccessShaped("[WEB] Signing in is turned off.", 0x05), "a refusal is not success-shaped");
Check.False(LaunchWire.IsSuccessShaped(null, 0x05), "null is not success-shaped");
foreach (string l in new[] { long44, dotted })
{
// Pending: the request takes the line as an unexpected reply; nothing of the code survives.
var w = new WebLauncher();
w.Begin(Market, 0);
var k = w.OnChatLine(l, 0x05, 100, out var dest, out string code, out string msg);
Check.True(k == LaunchLine.Refused, "pending: a malformed code line is Refused (eaten), not NotOurs");
Check.True(ReferenceEquals(dest, Market), "pending: the destination is handed back");
Check.Eq(code, "", "pending: no code");
Check.Eq(msg, "an unexpected reply (is RynthDreamWeave up to date?)", "pending: the fixed message");
Check.Eq(w.Status, "Market: DreamWeave sent an unexpected reply (is RynthDreamWeave up to date?)", "pending: the status");
Check.True(w.Pending == null, "pending: cleared");
Check.False(w.Status.Contains(Code.Substring(0, 12), StringComparison.Ordinal) || msg.Contains(Code.Substring(0, 12), StringComparison.Ordinal), "pending: no code in status or message");
// A player's copy while pending is still not ours.
var p = new WebLauncher();
p.Begin(Market, 0);
Check.True(p.OnChatLine(l, 0x02, 100, out _, out _, out _) == LaunchLine.NotOurs, "player copy (0x02): not ours");
Check.True(p.Pending != null, "player copy leaves the request pending");
// Not pending: Stale inside the window, NotOurs after it.
var s = new WebLauncher();
s.Begin(Market, 0);
s.TakeTimedOut(10_000);
Check.True(s.OnChatLine(l, 0x05, 20_000, out dest, out code, out msg) == LaunchLine.Stale, "not pending, inside the window: Stale");
Check.True(dest == null && code == "" && msg == "", "Stale carries nothing");
Check.True(s.OnChatLine(l, 0x05, 70_001, out _, out _, out _) == LaunchLine.NotOurs, "after the window: NotOurs");
Check.True(new WebLauncher().OnChatLine(l, 0x05, 5, out _, out _, out _) == LaunchLine.NotOurs, "no Begin ever: NotOurs");
}
// An empty refusal is an unexpected reply too.
foreach (string l in new[] { "[WEB] ", "[WEB] " })
{
var e = new WebLauncher();
e.Begin(Market, 0);
Check.True(e.OnChatLine(l, 0x05, 10, out _, out _, out string msg) == LaunchLine.Refused, "empty refusal: Refused");
Check.Eq(msg, "an unexpected reply (is RynthDreamWeave up to date?)", "empty refusal: the fixed message");
}
}
private static void ResultGeneration()
{
var w = new WebLauncher();
w.Begin(Market, 0);
Check.True(w.OnChatLine(Line, 0x05, 100, out _, out _, out _) == LaunchLine.Code, "request 1: code");
long gen1 = w.Generation;
Check.True(w.Begin(DreamWeaveWeb.ById("suit")!, 3_100) == LaunchBegin.Started, "request 2, 3 s later");
Check.True(w.OnChatLine(Line, 0x05, 3_200, out _, out _, out _) == LaunchLine.Code, "request 2: code");
long gen2 = w.Generation;
Check.True(gen2 != gen1, "each request has its own generation");
Check.Eq(w.Status, "Opening Suit Builder...", "request 2 is opening");
w.SetResult(gen1, "x");
Check.Eq(w.Status, "Opening Suit Builder...", "request 1's late answer leaves the status alone");
w.SetResult(gen2, "y");
Check.Eq(w.Status, "y", "request 2's answer sets it");
// Pending, or after a Reset (a relog), an answer is ignored too.
var p = new WebLauncher();
p.Begin(Market, 0);
long g = p.Generation;
p.SetResult(g, "x");
Check.Eq(p.Status, "Asking DreamWeave for a login code for Market...", "ignored while a request is pending");
p.OnChatLine(Line, 0x05, 100, out _, out _, out _);
g = p.Generation;
p.Reset();
p.SetResult(g, "x");
Check.Eq(p.Status, "", "an answer from before a Reset is ignored");
// A refused Begin moves nothing on.
var r = new WebLauncher();
r.Begin(Market, 0);
r.OnChatLine(Line, 0x05, 100, out _, out _, out _);
long rg = r.Generation;
Check.True(r.Begin(Market, 200) == LaunchBegin.TooSoon, "too soon");
r.SetResult(rg, "opened");
Check.Eq(r.Status, "opened", "a TooSoon Begin keeps the open's generation");
}
private static void ScanHelpers()
{
string src = "int a = 1; // lock (x) { Send(); }\nstring u = \"https://x//y\"; /* Reply.Ok() */ char q = '\"'; string v = @\"a\"\"//b\"; lock (_web.Gate) b = 2;\n";
string code = StripComments(src);
Check.False(code.Contains("Send", StringComparison.Ordinal) || code.Contains("Reply", StringComparison.Ordinal), "comments removed");
Check.True(code.Contains("\"https://x//y\"", StringComparison.Ordinal), "// inside a string kept");
Check.True(code.Contains("@\"a\"\"//b\"", StringComparison.Ordinal), "// inside a verbatim string kept");
Check.True(code.Contains("lock (_web.Gate) b = 2;", StringComparison.Ordinal), "code after a char literal holding a quote kept");
var locks = Locks("lock(_web.Gate){ Send(\"x\"); } lock ( other ) _web.Begin(); lock (_web.Gate) { _web.Begin(d, Environment.TickCount64); if (a) b(); }");
Check.Eq(locks.Count, 3, "every lock found, with or without a space");
Check.Eq(locks[0].Target, "_web.Gate", "target");
Check.Eq(string.Join(" ", CallsNotOn(locks[0].Body, "_web")), "Send(", "a bare call under the lock is caught");
Check.Eq(locks[1].Target, "other", "another target is reported as it is");
Check.Eq(string.Join(" ", CallsNotOn(locks[2].Body, "_web")), "if( b(", "keywords and helpers are not launcher calls");
Check.Eq(string.Join(" ", CallsNotOn("x._web.Begin(); _web.Begin(); _web.Gate.Foo();", "_web")), "x._web.Begin( _web.Gate.Foo(", "only a direct _web member call is allowed");
Check.Eq(string.Join(" ", CallsNotOn("a?.Invoke(x); Notify<int>(y); _web.Begin();", "_web")), "a?.Invoke( Notify(", "null-conditional and generic calls are calls");
Check.Eq(string.Join(" ", CallsNotOn("_web?.Begin(); _web.Begin<int>();", "_web")), "_web?.Begin(", "_web?. is not the allowed form; a generic _web member call is");
}
private const string Code = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde"; // 43 chars
private const string Line = "[WEB] Launch code: " + Code + " (single use, expires in 60 seconds)";
private static bool Parse(string? text, int type, out string code, out string refusal)
=> LaunchWire.TryParse(text, type, out code, out refusal);
private static void PlayerCopy()
{
Check.Eq(Code.Length, LaunchWire.CodeLength, "fixture code is 43 chars");
foreach (int type in new[] { 0x02, 0x03, 0x08, 0x0C, 0x13 })
Check.True(!Parse(Line, type, out _, out _), "player-authored chat type 0x" + type.ToString("X2") + " refused");
Check.True(!Parse("Bob tells you, " + Line, 0x05, out _, out _), "a tell wrapper with a system type refused");
Check.True(!Parse("hello " + Line, 0x05, out _, out _), "the lead mid-line refused");
Check.True(Parse(Line, 0x05, out string code, out string refusal), "valid line, system type");
Check.Eq(code, Code, "the code");
Check.Eq(refusal, "", "no refusal on success");
Check.True(Parse(Line, -1, out code, out _), "unknown type: the anchor alone protects");
Check.Eq(code, Code, "the code (type -1)");
Check.True(!Parse(null, 0x05, out _, out _), "null refused");
Check.True(!Parse("", 0x05, out _, out _), "empty refused");
}
private static void UrlFromConstants()
{
string head = "[WEB] Launch code: ";
Check.True(!Parse(head + Code.Substring(1) + " (single use)", 0x05, out _, out _), "42-char code refused");
Check.True(!Parse(head + Code + "A (single use)", 0x05, out _, out _), "44-char run (no space after 43) refused");
Check.True(!Parse(head + Code + "&to=//evil.com", 0x05, out _, out _), "code followed by &to= refused");
Check.True(!Parse(head + Code, 0x05, out _, out _), "no trailing space refused");
Check.True(!Parse(head + Code.Substring(0, 42) + "! (single use)", 0x05, out _, out _), "bad code char refused");
Check.True(!Parse(head + Code.Substring(0, 42) + "% (single use)", 0x05, out _, out _), "percent in code refused");
// None of the malformed success lines may leak out as a refusal either.
Check.True(!Parse(head + Code.Substring(1) + " x", 0x05, out _, out string r1) && r1 == "", "malformed success is not a refusal");
WebDestination suit = DreamWeaveWeb.ById("suit")!;
string url = DreamWeaveWeb.LaunchUrl(Code, suit);
Check.True(url.StartsWith(DreamWeaveWeb.SiteBase + "/launch?code=", StringComparison.Ordinal), "url starts with the base");
Check.Eq(url, DreamWeaveWeb.SiteBase + "/launch?code=" + Code + "&to=%2Ftools%2Fsuit", "exact suit url");
Check.True(url.EndsWith("%2Ftools%2Fsuit", StringComparison.Ordinal), "ends with the escaped path");
Check.Eq(url.Split("&to=").Length - 1, 1, "exactly one &to=");
Check.Eq(DreamWeaveWeb.LaunchUrl(Code, DreamWeaveWeb.ById("abilities")!),
DreamWeaveWeb.SiteBase + "/launch?code=" + Code + "&to=%2Fplay%2Fabilities", "abilities opens the live class ability page");
foreach (WebDestination d in DreamWeaveWeb.Destinations)
{
string u = DreamWeaveWeb.LaunchUrl(Code, d);
Check.True(ServerWebOpen.IsAllowedUrl(u, DreamWeaveWeb.AllowedHosts), d.Id + ": url passes the allowlist");
Check.True(d.Path.StartsWith("/", StringComparison.Ordinal) && !d.Path.StartsWith("//", StringComparison.Ordinal), d.Id + ": path is rooted, not //");
Check.True(d.Path.IndexOfAny(new[] { '?', '#', '\\' }) < 0, d.Id + ": no ? # or backslash in path");
Check.True(d.Label.Length > 0 && d.Tooltip.Length > 0, d.Id + ": label and tooltip");
}
Check.True(DreamWeaveWeb.ById("SUIT") == suit, "ById is case-insensitive");
Check.True(DreamWeaveWeb.ById("nope") == null && DreamWeaveWeb.ById(null) == null && DreamWeaveWeb.ById("") == null, "unknown id -> null");
Check.Eq(LaunchWire.RequestFor(suit), "/weblogin", "request is /weblogin");
}
private static void IdsAreFree()
{
var seen = new HashSet<string>();
string[] serverIds = ServerPluginIds();
Check.True(serverIds.Length > 0, "found the server plugin ids");
foreach (WebDestination d in DreamWeaveWeb.Destinations)
{
Check.True(d.Id.Length >= 1 && d.Id.Length <= 16 && d.Id.All(c => c >= 'a' && c <= 'z'), d.Id + ": 1-16 chars [a-z]");
Check.True(Array.IndexOf(ServerPackCommands.SharedVerbs, d.Id) < 0, d.Id + ": not a shared verb");
Check.True(Array.IndexOf(serverIds, d.Id) < 0, d.Id + ": not a server plugin id");
Check.True(seen.Add(d.Id), d.Id + ": unique");
}
}
private static void Alphabet()
{
const string b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
Check.Eq(b64.Length, 64, "64 base64url chars");
foreach (char c in b64)
Check.True(LaunchWire.IsCodeChar(c), "accepted: " + c);
char[] bad = { '+', '/', '=', '&', '?', '#', '%', '.', ' ', '\t', '!', '~', '\u00e9', '\u0000' };
foreach (char c in bad)
{
string name = "U+" + ((int)c).ToString("X4");
Check.True(!LaunchWire.IsCodeChar(c), "IsCodeChar rejects " + name);
foreach (int idx in new[] { 0, 21, 42 })
{
char[] chars = Code.ToCharArray();
chars[idx] = c;
string line = "[WEB] Launch code: " + new string(chars) + " (single use, expires in 60 seconds)";
Check.True(!Parse(line, 0x05, out string code, out string refusal), "refused: " + name + " at " + idx);
Check.Eq(code, "", "no code: " + name + " at " + idx);
Check.Eq(refusal, "", "no refusal: " + name + " at " + idx);
}
}
// Round trip of a code holding both '-' and '_'.
string dashed = "-_" + Code.Substring(2);
Check.True(Parse("[WEB] Launch code: " + dashed + " (single use, expires in 60 seconds)", 0x05, out string got, out _), "dash/underscore code parses");
Check.Eq(got, dashed, "dash/underscore code intact");
Check.True(DreamWeaveWeb.LaunchUrl(got, DreamWeaveWeb.ById("market")!).Contains("code=" + dashed + "&to=", StringComparison.Ordinal), "url carries the code verbatim");
// The stem alone marks the success shape: malformed, it is dropped, never a refusal.
foreach (string l in new[] { "[WEB] Launch code:" + Code, "[WEB] Launch code " + Code, "[WEB] Launch code", "[WEB] Launch codes " + Code })
{
Check.True(!Parse(l, 0x05, out string c2, out string r2), "malformed success-shaped line dropped: " + l);
Check.Eq(c2, "", "no code: " + l);
Check.Eq(r2, "", "no refusal text: " + l);
}
}
private static void Refusals()
{
string[] lines =
{
"[WEB] Signing in to the website from the game is turned off on this server.",
"[WEB] The website sign-in service is not available right now.",
"[WEB] You are requesting website sign-in codes too quickly. Wait a minute and try again.",
};
foreach (string l in lines)
{
Check.True(Parse(l, 0x05, out string code, out string refusal), "refusal parses: " + l);
Check.Eq(code, "", "no code in a refusal");
Check.Eq(refusal, l.Substring(LaunchWire.ReplyLead.Length), "refusal text after the lead");
Check.True(!Parse(l, 0x02, out _, out _), "a player's copy of a refusal is ignored");
}
Check.True(!Parse("You say, \"hi\"", 0x05, out _, out _), "other chat ignored");
Check.True(!Parse("[WEBX] Launch code: " + Code, 0x05, out _, out _), "lookalike lead ignored");
}
private static WebDestination Market => DreamWeaveWeb.ById("market")!;
private static void RepliesOnlyWhileAsked()
{
var w = new WebLauncher();
Check.True(w.OnChatLine(Line, 0x05, 0, out _, out _, out _) == LaunchLine.NotOurs, "nothing pending: not ours");
Check.True(w.Begin(Market, 1000) == LaunchBegin.Started, "begin");
Check.Eq(w.Status, "Asking DreamWeave for a login code for Market...", "asking status");
var k = w.OnChatLine(Line, 0x05, 1000 + 9_999, out var dest, out string code, out string msg);
Check.True(k == LaunchLine.Code, "line at +9,999 ms is the code");
Check.Eq(code, Code, "the code");
Check.Eq(msg, "", "no message on a code");
Check.True(ReferenceEquals(dest, Market), "destination handed back");
Check.True(w.Pending == null, "code clears pending");
Check.True(w.OnChatLine(Line, 0x05, 1000 + 9_999, out dest, out code, out msg) == LaunchLine.Stale, "an identical second line is stale: eaten, opens nothing");
Check.True(dest == null && code == "" && msg == "", "no outputs on Stale");
// Timeout at exactly +10,000.
var t = new WebLauncher();
t.Begin(Market, 50_000);
Check.True(t.OnChatLine(Line, 0x05, 60_000, out _, out _, out _) == LaunchLine.Stale, "a valid code at +10,000 ms is late: stale, not a code");
Check.True(t.TakeTimedOut(59_999) == null, "not timed out at +9,999");
Check.True(ReferenceEquals(t.TakeTimedOut(60_000), Market), "timed out once at +10,000");
Check.True(t.TakeTimedOut(60_001) == null, "and only once");
Check.True(t.Pending == null, "timeout clears pending");
Check.Eq(t.Status, "No reply from DreamWeave to /weblogin within 10 s, so nothing was opened.", "timeout status");
// Refusal clears pending.
var f = new WebLauncher();
f.Begin(Market, 0);
string refusal = "[WEB] The website sign-in service is not available right now.";
Check.True(f.OnChatLine(refusal, 0x05, 10, out dest, out code, out msg) == LaunchLine.Refused, "refusal");
Check.Eq(msg, "The website sign-in service is not available right now.", "refusal message");
Check.Eq(code, "", "no code on refusal");
Check.Eq(f.Status, "Market: DreamWeave said: The website sign-in service is not available right now.", "refusal status");
Check.True(f.Pending == null, "refusal clears pending");
// A player's copy and unrelated chat leave the request pending.
var p = new WebLauncher();
p.Begin(Market, 0);
Check.True(p.OnChatLine(Line, 0x02, 5, out _, out _, out _) == LaunchLine.NotOurs, "player copy not ours");
Check.True(p.OnChatLine("You say, \"hi\"", 0x05, 5, out _, out _, out _) == LaunchLine.NotOurs, "other chat not ours");
Check.True(p.Pending != null, "still pending");
// Reset clears pending and status.
p.Reset();
Check.True(p.Pending == null, "reset clears pending");
Check.Eq(p.Status, "", "reset clears status");
Check.True(p.OnChatLine(Line, 0x05, 5, out _, out _, out _) == LaunchLine.Stale, "after reset: a code is stale, still eaten");
}
private static void OneAtATime()
{
var w = new WebLauncher();
Check.True(w.Begin(Market, 100_000) == LaunchBegin.Started, "first begin");
Check.True(w.Begin(DreamWeaveWeb.ById("suit")!, 100_001) == LaunchBegin.Busy, "second while pending: busy");
Check.True(w.Begin(DreamWeaveWeb.ById("suit")!, 100_000 + 5_000) == LaunchBegin.Busy, "busy wins over too-soon/free");
Check.True(ReferenceEquals(w.Pending, Market), "first request still the pending one");
w.Cancel("Could not send /weblogin.");
Check.True(w.Pending == null, "cancel clears pending");
Check.Eq(w.Status, "Could not send /weblogin.", "cancel status");
Check.True(w.Begin(Market, 100_000 + 2_999) == LaunchBegin.TooSoon, "within 3 s of the last begin: too soon");
Check.True(w.Pending == null, "too soon starts nothing");
Check.True(w.Begin(Market, 100_000 + 3_000) == LaunchBegin.Started, "at 3 s: started");
// Never stuck: a timeout frees the slot.
Check.True(w.TakeTimedOut(103_000 + 10_000) != null, "timeout");
Check.True(w.Begin(Market, 113_000) == LaunchBegin.Started, "begin after timeout");
}
private static void CodeNeverInStatus()
{
var w = new WebLauncher();
w.Begin(Market, 0);
Check.True(!w.Status.Contains(Code), "status while asking");
var k = w.OnChatLine(Line, 0x05, 100, out _, out string code, out string msg);
Check.True(k == LaunchLine.Code, "code flow");
Check.Eq(code.Length, 43, "43-char code returned");
Check.True(!w.Status.Contains(Code), "status after code");
Check.True(!msg.Contains(Code), "message after code");
Check.True(!w.Status.Contains(code.Substring(0, 12)), "no code fragment in status");
// Timeout and refusal paths too.
var t = new WebLauncher();
t.Begin(Market, 0);
t.TakeTimedOut(10_000);
Check.True(!t.Status.Contains(Code), "timeout status");
var f = new WebLauncher();
f.Begin(Market, 0);
f.OnChatLine("[WEB] nope", 0x05, 1, out _, out _, out msg);
Check.True(!f.Status.Contains(Code) && !msg.Contains(Code), "refusal status and message");
}
private static void CodeShapedRefusal()
{
var w = new WebLauncher();
w.Begin(Market, 0);
var k = w.OnChatLine("[WEB] Try again later: " + Code, 0x05, 10, out var dest, out string code, out string msg);
Check.True(k == LaunchLine.Refused, "still a refusal, so the caller eats the line");
Check.True(ReferenceEquals(dest, Market), "destination handed back");
Check.Eq(code, "", "no code");
Check.True(!msg.Contains(Code), "message has no code");
Check.True(!w.Status.Contains(Code), "status has no code");
Check.Eq(msg, "an unexpected reply (is RynthDreamWeave up to date?)", "fixed replacement text");
Check.Eq(w.Status, "Market: DreamWeave sent an unexpected reply (is RynthDreamWeave up to date?)", "status from the replacement");
Check.True(w.Pending == null, "pending cleared");
// A long run of code chars anywhere, and a 42-char run (below the limit) left alone.
var w2 = new WebLauncher();
w2.Begin(Market, 0);
w2.OnChatLine("[WEB] x " + Code + Code + " y", 0x05, 10, out _, out _, out msg);
Check.Eq(msg, "an unexpected reply (is RynthDreamWeave up to date?)", "longer run replaced");
var w3 = new WebLauncher();
w3.Begin(Market, 0);
w3.OnChatLine("[WEB] id " + Code.Substring(1) + " ok", 0x05, 10, out _, out _, out msg);
Check.Eq(msg, "id " + Code.Substring(1) + " ok", "42-char run is ordinary text");
}
private static void SelfExpiry()
{
var w = new WebLauncher();
w.Begin(Market, 0);
Check.True(w.Begin(Market, 9_999) == LaunchBegin.Busy, "before the deadline: busy");
Check.True(w.Begin(Market, 10_001) != LaunchBegin.Busy, "past the deadline: not busy");
var x = new WebLauncher();
x.Begin(Market, 0);
Check.True(x.Begin(DreamWeaveWeb.ById("suit")!, 10_001) == LaunchBegin.Started, "expired, then started as normal");
Check.True(x.Pending!.Id == "suit", "the new request is the pending one");
Check.True(x.TakeTimedOut(10_001) == null, "the new request is not timed out yet");
Check.Eq(x.TakeTimedOut(20_001)?.Id ?? "none", "suit", "and it times out at its own deadline");
}
private static void ResetKeepsThrottle()
{
var w = new WebLauncher();
Check.True(w.Begin(Market, 1_000) == LaunchBegin.Started, "begin");
w.Reset();
Check.True(w.Pending == null, "reset clears pending");
Check.True(w.Begin(Market, 1_001) == LaunchBegin.TooSoon, "throttle survives a relog");
}
private static void LateCodeIsStale()
{
var w = new WebLauncher();
w.Begin(Market, 0);
var k = w.OnChatLine(Line, 0x05, 10_500, out var dest, out string code, out string msg);
Check.True(k == LaunchLine.Stale, "valid code at +10,500 ms is stale");
Check.True(dest == null && code == "" && msg == "", "stale carries nothing");
Check.True(w.Pending != null, "stale does not clear pending");
Check.True(!w.Status.Contains(Code), "status has no code");
Check.True(ReferenceEquals(w.TakeTimedOut(10_500), Market), "timeout still fires once");
Check.True(w.TakeTimedOut(10_501) == null, "and only once");
Check.Eq(w.Status, "No reply from DreamWeave to /weblogin within 10 s, so nothing was opened.", "timeout status intact");
Check.True(w.OnChatLine(Line, 0x05, 20_000, out dest, out code, out _) == LaunchLine.Stale, "still stale after the timeout was taken");
Check.True(dest == null && code == "", "no destination or code");
Check.True(w.OnChatLine(Line, 0x05, 69_999, out _, out _, out _) == LaunchLine.Stale, "last stale ms (+69,999)");
Check.True(w.OnChatLine(Line, 0x05, 70_001, out _, out _, out _) == LaunchLine.NotOurs, "at +70,001 ms: not ours");
Check.True(w.OnChatLine("[WEB] some refusal", 0x05, 20_000, out _, out _, out _) == LaunchLine.NotOurs, "a late refusal is not ours");
Check.True(w.OnChatLine(Line, 0x02, 20_000, out _, out _, out _) == LaunchLine.NotOurs, "a late player copy is not ours");
var n = new WebLauncher();
Check.True(n.OnChatLine(Line, 0x05, 10_500, out _, out _, out _) == LaunchLine.NotOurs, "no Begin ever: not ours");
// A consumed request's window persists: a repeat is eaten.
var c = new WebLauncher();
c.Begin(Market, 0);
c.OnChatLine(Line, 0x05, 100, out _, out _, out _);
Check.True(c.OnChatLine(Line, 0x05, 10_500, out _, out _, out _) == LaunchLine.Stale, "after a consumed code: a repeat is stale");
// A new Begin never ends the old stale window: the new request takes a code while pending,
// and the window still covers the old code once the new request is gone.
var b = new WebLauncher();
b.Begin(Market, 0);
b.TakeTimedOut(10_000);
b.Begin(Market, 20_000);
Check.True(b.OnChatLine(Line, 0x05, 20_100, out _, out _, out _) == LaunchLine.Code, "new request takes the code while pending");
Check.True(b.OnChatLine(Line, 0x05, 20_200, out _, out _, out _) == LaunchLine.Stale, "then a further valid code is stale");
// A refused Begin does not extend the stale window.
var t = new WebLauncher();
t.Begin(Market, 0);
t.Cancel("send failed"); // Pending would answer Busy; TooSoon needs a free slot
Check.True(t.Begin(Market, 2_999) == LaunchBegin.TooSoon, "second begin inside the throttle is refused");
Check.True(t.OnChatLine(Line, 0x05, 70_000, out _, out _, out _) == LaunchLine.NotOurs, "a refused Begin leaves the window anchored at the first");
// A Busy Begin does not extend the stale window either.
var y = new WebLauncher();
y.Begin(Market, 0);
Check.True(y.Begin(Market, 9_999) == LaunchBegin.Busy, "second begin while pending is Busy");
y.Cancel("send failed");
Check.True(y.OnChatLine(Line, 0x05, 70_000, out _, out _, out _) == LaunchLine.NotOurs, "a Busy Begin leaves the window anchored at the first");
// Window upper boundary.
Check.Eq(LaunchWire.CodeLifetimeMs, 60_000L, "code lifetime");
var u = new WebLauncher();
u.Begin(Market, 0);
Check.True(u.OnChatLine(Line, 0x05, 69_999, out _, out _, out _) == LaunchLine.Stale, "+69,999 stale");
Check.True(u.OnChatLine(Line, 0x05, 70_000, out _, out _, out _) == LaunchLine.NotOurs, "+70,000 not ours");
}
private static void ProfileKeys()
{
string? a = WebProfile.Key("dreamweave", "Alice");
string? b = WebProfile.Key("dreamweave", "Bob");
Check.True(a != null && b != null, "keys exist");
Check.True(a != b, "different accounts -> different keys");
Check.Eq(WebProfile.Key("dreamweave", " alice "), a, "trimmed and case-folded");
Check.Eq(WebProfile.Key("dreamweave", "ALICE"), a, "case-folded");
Check.Eq(WebProfile.Key("dreamweave", "Alice"), a, "stable across calls");
Check.True(WebProfile.Key("coldeve", "Alice") != a, "same account, other server -> different key");
Check.True(a!.StartsWith("dreamweave/", StringComparison.Ordinal), "prefixed by the server id");
string[] accounts = { "Alice", "Bob", "a", "x y", "caf" + (char)0xE9, "A-very-long-account-name-0123456789-0123456789-0123456789" };
foreach (string acct in accounts)
{
string? k = WebProfile.Key("dreamweave", acct);
Check.True(ServerWebOpen.IsValidProfileKey(k), "valid key for " + acct.Length + " chars");
// A 1-2 char account could appear in the server id or the hex by chance; the rest cannot.
if (acct.Length >= 3) Check.True(!k!.Contains(acct.Trim().ToLowerInvariant()), "key never contains the account text");
}
Check.Eq(WebProfile.Key("dreamweave", "alice"), "dreamweave/67b613ccd746148225de235baae58eab", "key format is frozen (changing it orphans saved web logins)");
Check.Eq(WebProfile.Key("coldeve", "alice"), "coldeve/1101ebeaa4456aba9ca33cd292d7b99c", "key format is frozen (changing it orphans saved web logins)");
Check.True(WebProfile.Key("DreamWeave", "alice") == null, "uppercase server id -> null");
Check.True(WebProfile.Key("dream/weave", "alice") == null, "slash in server id -> null");
Check.True(WebProfile.Key("", "alice") == null, "empty server id -> null");
Check.True(WebProfile.Key("dreamweave", null) == null, "null account -> null");
Check.True(WebProfile.Key("dreamweave", "") == null, "empty account -> null");
Check.True(WebProfile.Key("dreamweave", " ") == null, "blank account -> null");
}
private static void OverlappingRequests()
{
// Scenario 1: request 1 times out, request 2 begins, request 1's late code arrives.
var w = new WebLauncher();
w.Begin(Market, 0);
w.TakeTimedOut(10_000);
Check.True(w.Begin(Market, 13_000) == LaunchBegin.Started, "request 2");
Check.True(w.OnChatLine(Line, 0x05, 14_000, out _, out _, out _) == LaunchLine.Code, "the late code is taken by request 2 (indistinguishable)");
Check.True(w.OnChatLine(Line, 0x05, 15_000, out var dest, out string code, out string msg) == LaunchLine.Stale, "request 2's own reply is stale, never chat");
Check.True(dest == null && code == "" && msg == "", "stale carries nothing");
// Scenario 2: request 1 expires, request 2 begins and is cancelled, the late code arrives.
var c = new WebLauncher();
c.Begin(Market, 0);
c.TakeTimedOut(10_000);
c.Begin(Market, 13_000);
c.Cancel("Could not send /weblogin.");
Check.True(c.OnChatLine(Line, 0x05, 14_000, out _, out _, out _) == LaunchLine.Stale, "after cancel: stale");
// Reset while pending, then the code.
var r = new WebLauncher();
r.Begin(Market, 0);
r.Reset();
Check.True(r.OnChatLine(Line, 0x05, 5_000, out _, out _, out _) == LaunchLine.Stale, "after reset while pending: stale");
Check.True(r.OnChatLine("[WEB] some refusal", 0x05, 5_000, out _, out _, out _) == LaunchLine.NotOurs, "refusals outside a request stay not ours");
// Cancel right after Begin: the ask may have been sent anyway.
var k = new WebLauncher();
k.Begin(Market, 0);
k.Cancel("x");
Check.True(k.OnChatLine(Line, 0x05, 2_000, out _, out _, out _) == LaunchLine.Stale, "cancelled request's code is stale");
}
private static string[] ServerPluginIds()
{
var ids = new List<string>();
foreach (string p in Directory.GetDirectories(ServerProjectsTests.PluginsDir(), "RynthCore.Plugin.Rynth*"))
{
string csproj = Path.Combine(p, Path.GetFileName(p) + ".csproj");
if (!File.Exists(csproj)) continue;
string id = XDocument.Load(csproj).Descendants("RynthPluginServerId").FirstOrDefault()?.Value.Trim() ?? "";
if (id.Length > 0) ids.Add(id);
}
return ids.ToArray();
}
}

View file

@ -22,6 +22,9 @@ internal static class Program
ServerPackCommandsTests.Register(runner);
DreamWeaveSkillsTests.Register(runner);
DreamWeaveTests.Register(runner);
ServerWebOpenTests.Register(runner);
ServerWebPanelTests.Register(runner);
DreamWeaveWebTests.Register(runner);
return runner.Run("RynthServerPack tests", args.Length > 0 ? args[0] : null);
}
}

View file

@ -32,6 +32,15 @@
<!-- "/rserver" words and answers (2026-10-28) and the shared reply queue. -->
<Compile Include="..\..\Plugins\Shared\ServerPack\ServerPackCommands.cs" Link="Plugin\ServerPackCommands.cs" />
<Compile Include="$(RynthSuiteRoot)Plugins\Shared\CommandReply.cs" Link="Shared\CommandReply.cs" />
<!-- The web opener's pure rules (allowlist, route). -->
<Compile Include="..\..\Plugins\Shared\ServerPack\ServerWebOpen.cs" Link="Plugin\ServerWebOpen.cs" />
<!-- The engine web panel's answers and the one open the tick waits on (pure: the engine is a seam). -->
<Compile Include="..\..\Plugins\Shared\ServerPack\ServerWebPanel.cs" Link="Plugin\ServerWebPanel.cs" />
<!-- DreamWeave's web destinations and the launch reply's wire text (pure). -->
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthDreamWeave\DreamWeave\DreamWeaveWeb.cs" Link="DreamWeave\DreamWeaveWeb.cs" />
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthDreamWeave\DreamWeave\LaunchWire.cs" Link="DreamWeave\LaunchWire.cs" />
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthDreamWeave\DreamWeave\WebLauncher.cs" Link="DreamWeave\WebLauncher.cs" />
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthDreamWeave\DreamWeave\WebProfile.cs" Link="DreamWeave\WebProfile.cs" />
<!-- RynthConquest's own features (pure parts) and the RynthOracle sources it links. -->
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthConquest\Conquest\ConquestData.cs" Link="Conquest\ConquestData.cs" />
<Compile Include="..\..\Plugins\RynthCore.Plugin.RynthConquest\Conquest\ConquestFeature.cs" Link="Conquest\ConquestFeature.cs" />
@ -53,6 +62,8 @@
<Compile Include="$(RynthSuiteRoot)Plugins\Shared\StorageFeed.cs" Link="Shared\StorageFeed.cs" />
<!-- RynthCore's own sections-manifest parser (pure), so the manifest is checked the way the engine reads it. -->
<Compile Include="..\..\..\RynthCore\src\RynthCore.Engine\UI\Sections\SectionManifest.cs" Link="Engine\SectionManifest.cs" />
<!-- The engine's API numbers (pure const file): the plugin's own copy of the barMenu version is pinned to it. -->
<Compile Include="..\..\..\RynthCore\src\RynthCore.Engine\Plugins\PluginContractVersion.cs" Link="Engine\PluginContractVersion.cs" />
<!-- "RynthServer.Profile": the profile JSON, and the shared reader's parser that consumes it. -->
<Compile Include="..\..\Plugins\Shared\ServerPack\ServerPackProfile.cs" Link="Plugin\ServerPackProfile.cs" />
<!-- "RynthServer.QuestPack": a server plugin's own quest list (the info JSON; the pack itself is data). -->

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using RynthCore.Plugin.ServerPack;
using RynthCore.Plugin.Shared;
using RynthCore.RynthAiTests.Harness;
@ -50,6 +51,31 @@ internal static class ServerPackCommandsTests
Check.Eq(usage.Length, 2, "two usage lines");
Check.True(usage[0].Contains("/rserver [show|hide|toggle]") && usage[1].Contains("/rserver auto|on|off"), "the usage names every verb");
Check.Eq(ServerPackCommands.Unknown("wobble"), "Unknown '/rserver wobble'.", "an unknown word");
// A server without ServerUsageLines (Coldeve) prints exactly the two shared lines, byte for byte.
string[] pinned =
{
"/rserver [show|hide|toggle] - the Coldeve server card (also in the Mini Remote's Char section).",
"/rserver info - what is known about Coldeve; /rserver auto|on|off - this character's override.",
};
Check.Eq(string.Join("\n", usage), string.Join("\n", pinned), "hookless usage is unchanged");
Check.Eq(string.Join("\n", ServerPackCommands.Usage("Coldeve", new List<string>())), string.Join("\n", pinned), "no own lines: unchanged");
Check.Eq(string.Join("\n", ServerPackCommands.Usage("Coldeve", new List<string> { "", " " })), string.Join("\n", pinned), "blank own lines are dropped");
string coldeve = string.Join("\n", System.IO.Directory.GetFiles(System.IO.Path.Combine(ServerProjectsTests.PluginsDir(), "RynthCore.Plugin.RynthColdeve"), "*.cs", System.IO.SearchOption.AllDirectories)
.Where(f => !f.Contains("\\obj\\") && !f.Contains("\\bin\\")).Select(System.IO.File.ReadAllText));
Check.False(coldeve.Contains("ServerUsageLines", StringComparison.Ordinal), "Coldeve has no ServerUsageLines (so the pin above is what it prints)");
// A server's own lines follow the shared two, which stay first and unchanged.
string[] own = ServerPackCommands.Usage("Coldeve", new List<string> { " /rserver x - one more line " });
Check.Eq(own.Length, 3, "an own line is appended");
Check.Eq(own[2], "/rserver x - one more line", "trimmed");
Check.True(own[0] == pinned[0] && own[1] == pinned[1], "the shared two come first, unchanged");
// DreamWeave's verbs follow the shared two.
string[] dw = ServerPackCommands.Usage("DreamWeave", new List<string> { RynthCore.Plugin.RynthDreamWeave.DreamWeaveWeb.UsageLine() });
Check.Eq(dw.Length, 3, "DreamWeave: three usage lines");
Check.Eq(dw[2], "/rserver abilities | market | suit - open the website's Class Abilities, Market or Suit Builder, logged in as this account.", "DreamWeave's own line");
Check.True(dw[0] == ServerPackCommands.Usage("DreamWeave")[0] && dw[1] == ServerPackCommands.Usage("DreamWeave")[1], "the shared two come first, unchanged");
}
private static void Queue()

View file

@ -24,6 +24,7 @@ internal static class ServerPackHooksTests
r.Add("hooks: settings migration runs once, and a broken file means defaults", SettingsMigration);
r.Add("hooks: profile values are published, validated and read back", ProfileValues);
r.Add("hooks: the own-reply window opens, stretches and closes", ReplyWindow);
r.Add("hooks: the card draws server actions under the verdict, once", CardActions);
}
internal static string TempDir()
@ -199,4 +200,45 @@ internal static class ServerPackHooksTests
req.Clear();
Check.False(req.Awaiting(1001), "cleared");
}
// Source scan: the card hands the server's own buttons a spot right under RynthCore's verdict.
private static string ServerPackDir()
{
string? dir = AppContext.BaseDirectory;
while (dir != null && !Directory.Exists(Path.Combine(dir, "Plugins", "Shared", "ServerPack")))
dir = Path.GetDirectoryName(dir);
if (dir == null) throw new DirectoryNotFoundException("Plugins\\Shared\\ServerPack not found above " + AppContext.BaseDirectory);
return Path.Combine(dir, "Plugins", "Shared", "ServerPack");
}
private static int CountOf(string text, string needle)
{
int n = 0, at = 0;
while ((at = text.IndexOf(needle, at, StringComparison.Ordinal)) >= 0) { n++; at += needle.Length; }
return n;
}
private static void CardActions()
{
string ui = File.ReadAllText(Path.Combine(ServerPackDir(), "ServerPackUi.cs"));
string plugin = File.ReadAllText(Path.Combine(ServerPackDir(), "ServerPackPlugin.cs"));
int body = ui.IndexOf("public void RenderBody()", StringComparison.Ordinal);
Check.True(body >= 0, "RenderBody exists");
int verdict = ui.IndexOf("RenderVerdict(info);", body, StringComparison.Ordinal);
int facts = ui.IndexOf("RenderFacts(info);", body, StringComparison.Ordinal);
Check.True(verdict > body && facts > verdict, "RenderBody draws the verdict, then the facts");
Check.Eq(CountOf(ui, "_cardActions?.Invoke()"), 1, "the card actions are invoked exactly once");
int call = ui.IndexOf("_cardActions?.Invoke()", StringComparison.Ordinal);
Check.True(call > verdict && call < facts, "the actions are drawn after the verdict and before the facts");
// Hookless servers must render exactly as before: no separator was added to the card (base RenderBody has 5).
int bodyEnd = ui.IndexOf("private void RenderVerdict", body, StringComparison.Ordinal);
Check.Eq(CountOf(ui.Substring(body, bodyEnd - body), "ImGui.Separator()"), 5, "RenderBody separator count is unchanged from base");
Check.False(ui.Substring(verdict, call - verdict).Contains("Separator", StringComparison.Ordinal), "no separator between the verdict and the actions");
Check.True(plugin.Contains("DrawCardActions", StringComparison.Ordinal) && plugin.Contains("partial void ServerCardActions();", StringComparison.Ordinal),
"the plugin declares the ServerCardActions hook and DrawCardActions");
Check.True(plugin.Contains("_pages, DrawCardActions)", StringComparison.Ordinal), "the plugin passes DrawCardActions to ServerPackUi");
}
}

View file

@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.Json;
using RynthCore.Engine.UI.Sections;
using RynthCore.Plugin.ServerPack;
using RynthCore.RynthAiTests.Harness;
@ -18,6 +20,9 @@ internal static class ServerPackModelTests
r.Add("server pack: before any push the world name decides", WorldFallback);
r.Add("server pack: override commands are checked before sending", ForceCommands);
r.Add("server pack: the sections manifest declares the server and a server-only card", SectionsJson);
r.Add("server pack: no bar menu writes no barMenu key", NoBarMenuKey);
r.Add("server pack: the bar menu items reach the engine parser as given", BarMenuRoundTrip);
r.Add("server pack: a bar menu tooltip with quotes and backslashes round-trips", BarMenuEscapes);
r.Add("server pack: logout clears the verdict", ClearOnLogout);
r.Add("server pack: the card's botting line and address", InfoText);
r.Add("server pack: the RynthServer.Profile JSON carries the server's settings", ProfileJson);
@ -217,6 +222,57 @@ internal static class ServerPackModelTests
Check.Eq(odd.RootElement.GetProperty("servers")[0].GetProperty("hosts")[0].GetString(), "a\\b", "escaped");
}
private static ServerBarMenuItem[] MenuItems() => new[]
{
new ServerBarMenuItem("market", "Market", ((char)0xE470).ToString(), "/rserver market"),
new ServerBarMenuItem("suit", "Suit Builder", ((char)0xE40A).ToString(), "/rserver suit"),
new ServerBarMenuItem("abilities", "Class Abilities", ((char)0xE6A2).ToString(), "/rserver abilities"),
};
private static void NoBarMenuKey()
{
string plain = ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve");
Check.Eq(ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, barMenu: null), plain, "null pages and null menu are the default");
Check.Eq(ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, barMenu: new List<ServerBarMenuItem>()), plain, "an empty menu is the default");
Check.False(plain.Contains("barMenu"), "no barMenu key");
}
private static void BarMenuRoundTrip()
{
ServerBarMenuItem[] items = MenuItems();
string json = ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, barMenu: items);
SectionManifest m = SectionManifestParser.Parse("RynthColdeve", json, out string error);
Check.Eq(error, string.Empty, "parses clean: " + error);
SectionBarMenuItem[] menu = m.Windows.Single().Menu;
Check.Eq(menu.Length, 3, "three items");
for (int i = 0; i < items.Length; i++)
{
Check.Eq(menu[i].Id, items[i].Id, "id " + i);
Check.Eq(menu[i].Tooltip, items[i].Tooltip, "tooltip " + i);
Check.Eq(menu[i].Icon, items[i].Icon, "icon " + i);
Check.Eq(menu[i].Command, items[i].Command, "command " + i);
}
// With windows of the server's own (ServerWindows), the menu stays on the server's window only.
var own = new[] { new ServerWindow("skills", "Skills", "x", 300, 400, 200, 200, false, _ => default) };
SectionManifest both = SectionManifestParser.Parse("RynthColdeve",
ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, own, barMenu: items), out error);
Check.Eq(error, string.Empty, "with a window of its own: parses clean: " + error);
Check.Eq(both.Windows.Length, 2, "with a window of its own: two windows");
Check.Eq(both.Windows[0].Menu.Length, 3, "with a window of its own: the server window has the menu");
Check.Eq(both.Windows[1].Menu.Length, 0, "with a window of its own: that window has none");
}
private static void BarMenuEscapes()
{
const string tip = "Say \"hi\" \\ there";
var items = new[] { new ServerBarMenuItem("odd", tip, ((char)0xE470).ToString(), "/rserver market") };
string json = ServerPackModel.BuildSectionsJson(Coldeve(), "RynthColdeve", null, barMenu: items);
SectionManifest m = SectionManifestParser.Parse("RynthColdeve", json, out string error);
Check.Eq(error, string.Empty, "parses clean: " + error);
Check.Eq(m.Windows.Single().Menu.Single().Tooltip, tip, "tooltip survives escaping");
}
private static void ClearOnLogout()
{
var model = new ServerPackModel(Coldeve());

View file

@ -35,7 +35,7 @@ internal static class ServerProjectsTests
"levistras",
};
private static string PluginsDir()
internal static string PluginsDir()
{
string? dir = AppContext.BaseDirectory;
while (dir != null && !Directory.Exists(Path.Combine(dir, "Plugins", "Shared", "ServerPack")))

View file

@ -0,0 +1,72 @@
using System;
using RynthCore.Plugin.ServerPack;
using RynthCore.RynthAiTests.Harness;
namespace RynthServerPack.Tests;
// The web opener's pure part: which URLs a server plugin may open, and where the page goes.
internal static class ServerWebOpenTests
{
public static void Register(Runner r)
{
r.Add("web open: only https on an allowed host, and the route follows the engine", Rules);
r.Add("web open: a request never prints its URL, and the browser's error says Windows", NoUrlInText);
}
private static readonly string[] Hosts = { "www.acdreamweave.com" };
private static readonly string[] Wild = { "*.acdreamweave.com" };
private const string Key = "dreamweave/0123456789abcdef0123456789abcdef";
private static void NoUrlInText()
{
const string secret = "AbCdEfGhIjKlMnOpQrStUvWxYz0123456789-_AbCde";
var r = new WebOpenRequest("https://www.acdreamweave.com/launch?code=" + secret + "&to=%2Fmarket", Hosts, Key, "DreamWeave");
Check.Eq(r.ToString(), "WebOpenRequest(DreamWeave)", "ToString names the title only");
Check.False(r.ToString().Contains(secret, StringComparison.Ordinal), "ToString never carries the code");
Check.False(("" + r).Contains("acdreamweave", StringComparison.Ordinal), "nor the URL");
Check.False((r with { Title = "x" }).ToString().Contains(secret, StringComparison.Ordinal), "a copy neither");
string web = System.IO.File.ReadAllText(System.IO.Path.Combine(ServerProjectsTests.PluginsDir(), "Shared", "ServerPack", "ServerPackWeb.cs"));
Check.True(web.Contains("$\"Couldn't open your browser (Windows error {code}).\"", StringComparison.Ordinal), "the browser failure names the Windows error");
Check.True(web.Contains("\"Couldn't open your browser (Windows error 0).\"", StringComparison.Ordinal), "and its exception path");
Check.False(web.Contains("(code ", StringComparison.Ordinal), "the old \"(code N)\" wording is gone");
}
private static void Rules()
{
Check.True(ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com/x", Hosts), "https on the host");
Check.True(ServerWebOpen.IsAllowedUrl("https://WWW.AcDreamWeave.com/x", Hosts), "host case-insensitive");
Check.True(!ServerWebOpen.IsAllowedUrl("http://www.acdreamweave.com/x", Hosts), "http refused");
Check.True(!ServerWebOpen.IsAllowedUrl("https://user@www.acdreamweave.com/x", Hosts), "userinfo refused");
Check.True(!ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com:444/x", Hosts), "other port refused");
Check.True(!ServerWebOpen.IsAllowedUrl("https://www.acdreamweave.com.evil.com/x", Hosts), "suffix host refused");
Check.True(!ServerWebOpen.IsAllowedUrl("javascript:alert(1)", Hosts), "javascript refused");
Check.True(!ServerWebOpen.IsAllowedUrl(null, Hosts), "null refused");
Check.True(!ServerWebOpen.IsAllowedUrl("/relative", Hosts), "relative refused");
Check.True(ServerWebOpen.IsAllowedUrl("https://wiki.acdreamweave.com/", Wild), "wildcard matches a subdomain");
Check.True(!ServerWebOpen.IsAllowedUrl("https://acdreamweave.com/", Wild), "wildcard excludes the bare domain");
Check.True(!ServerWebOpen.IsAllowedUrl("https://evilacdreamweave.com/", Wild), "wildcard needs the dot");
Check.True(ServerWebOpen.IsValidProfileKey(Key), "dreamweave/ + 32 hex");
Check.True(!ServerWebOpen.IsValidProfileKey("dreamweave/0123456789ABCDEF0123456789abcdef"), "uppercase refused");
Check.True(!ServerWebOpen.IsValidProfileKey("../0123456789abcdef0123456789abcdef"), "dotdot refused");
Check.True(!ServerWebOpen.IsValidProfileKey(Key + "0"), "longer refused");
Check.True(!ServerWebOpen.IsValidProfileKey(""), "empty refused");
Check.True(!ServerWebOpen.IsValidProfileKey(null), "null refused");
var ok = new WebOpenRequest("https://www.acdreamweave.com/x", Hosts, Key, "t");
Check.Eq(ServerWebOpen.Route(ok, true), WebOpenRoute.Panel, "panel");
Check.Eq(ServerWebOpen.Route(ok, false), WebOpenRoute.Browser, "no panel -> browser");
Check.Eq(ServerWebOpen.Route(ok with { ProfileKey = "" }, true), WebOpenRoute.Browser, "empty key -> browser");
Check.Eq(ServerWebOpen.Route(ok with { ProfileKey = "Bad/Key" }, true), WebOpenRoute.Browser, "bad key -> browser");
Check.Eq(ServerWebOpen.Route(ok with { Url = "http://www.acdreamweave.com/x" }, true), WebOpenRoute.Refused, "bad url refused");
Check.True(ServerWebOpen.FallbackToBrowser(PanelResult.RefusedBeforeNavigate), "refused before navigate -> browser");
Check.True(!ServerWebOpen.FallbackToBrowser(PanelResult.Opened), "opened -> no browser");
Check.True(!ServerWebOpen.FallbackToBrowser(PanelResult.UrlNotAllowed), "engine allowlist is not bypassed");
Check.True(!ServerWebOpen.FallbackToBrowser(PanelResult.FailedAfterNavigate), "spent code -> no browser");
Check.True(ServerWebOpen.TryGetAllowedUri("https://www.acdreamweave.com/x?a=1", Hosts, out string abs) && abs == "https://www.acdreamweave.com/x?a=1", "validated absolute uri");
Check.True(!ServerWebOpen.TryGetAllowedUri("http://www.acdreamweave.com/", Hosts, out string none) && none.Length == 0, "refused gives no uri");
}
}

View file

@ -0,0 +1,403 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using RynthCore.Plugin.ServerPack;
using RynthCore.RynthAiTests.Harness;
namespace RynthServerPack.Tests;
// The engine web panel's answers as PanelResult, and the one open the tick waits on (ServerWebPanel.cs),
// against a fake engine. The plugin's own engine calls (ServerPackWeb.cs) are checked by source scan.
internal static class ServerWebPanelTests
{
public static void Register(Runner r)
{
r.Add("web panel: every Open return code maps to a PanelResult", OpenCodes);
r.Add("web panel: every state maps to a PanelResult", States);
r.Add("web panel: only a refusal before navigating falls back to the browser", Fallback);
r.Add("web panel: a refusal answers at once, exactly once", SyncAnswers);
r.Add("web panel: a queued open is answered by the tick's poll, exactly once", PollAnswers);
r.Add("web panel: a queued open that never settles times out, neutral", Timeout);
r.Add("web panel: Close closes only our own handle and answers a waiting open", CloseOwnHandle);
r.Add("web panel: engine exceptions and a throwing answer never escape", Exceptions);
r.Add("web panel: SDK references sit in NoInlining helpers behind both catches, no locks", SourceRules);
}
private const int S_None = 0, S_Starting = 1, S_Loading = 2, S_Ready = 3, S_Closed = 4, S_Failed = 5, S_Superseded = 6;
private const int F_Visible = 1, F_Focused = 2, F_OpenedInBrowser = 4;
private sealed class FakeEngine : IWebPanelEngine
{
public bool Has = true;
public int OpenReturns = 7;
public int Status = S_Starting, Flags, Error;
public uint Nav;
public bool ThrowOnOpen, ThrowOnState, ThrowOnHas;
public readonly List<int> Closed = new();
public readonly List<string> Opens = new();
public int StateCalls;
public bool HasPanel() => ThrowOnHas ? throw new InvalidOperationException("has") : Has;
public int Open(string url, string[] allowlist, string profileKey, string title)
{
if (ThrowOnOpen) throw new MissingMethodException("open");
Opens.Add(string.Join(",", allowlist) + "|" + profileKey + "|" + title);
return OpenReturns;
}
public int GetState(int handle, out int flags, out int error, out uint navigationCount)
{
StateCalls++;
if (ThrowOnState) throw new TypeLoadException("state");
flags = Flags; error = Error; navigationCount = Nav;
return Status;
}
public int Close(int handle) { Closed.Add(handle); return 1; }
}
private static readonly WebOpenRequest Req = new("https://www.acdreamweave.com/x?code=secret",
new[] { "www.acdreamweave.com" }, "dreamweave/0123456789abcdef0123456789abcdef", "DreamWeave");
private static void OpenCodes()
{
Check.Eq(ServerWebPanel.FromOpenCode(1), PanelResult.Pending, "handle 1 is queued, not opened");
Check.Eq(ServerWebPanel.FromOpenCode(12345), PanelResult.Pending, "any handle > 0 is queued");
Check.Eq(ServerWebPanel.FromOpenCode(-1), PanelResult.BadArgs, "BadArgs");
Check.Eq(ServerWebPanel.FromOpenCode(-2), PanelResult.UrlNotAllowed, "UrlNotAllowed");
Check.Eq(ServerWebPanel.FromOpenCode(-3), PanelResult.RefusedBeforeNavigate, "NoGameWindow refuses before navigating");
Check.Eq(ServerWebPanel.FromOpenCode(-4), PanelResult.RefusedBeforeNavigate, "Unavailable refuses before navigating");
Check.Eq(ServerWebPanel.FromOpenCode(-5), PanelResult.RefusedBeforeNavigate, "ShuttingDown refuses before navigating");
// No handle was issued, so nothing navigated: an undocumented code is a refusal too.
foreach (int c in new[] { 0, -6, -7, -8, -9, -10, int.MinValue })
Check.Eq(ServerWebPanel.FromOpenCode(c), PanelResult.RefusedBeforeNavigate, "undocumented open code " + c + ": no handle, refusal");
}
private static void States()
{
Check.Eq(ServerWebPanel.FromState(S_Starting, 0, 0, 0), PanelResult.Pending, "Starting: keep polling");
Check.Eq(ServerWebPanel.FromState(S_Loading, F_Visible, 0, 1), PanelResult.Opened, "Loading: opened");
Check.Eq(ServerWebPanel.FromState(S_Ready, F_Visible | F_Focused, 0, 1), PanelResult.Opened, "Ready: opened");
Check.Eq(ServerWebPanel.FromState(S_Failed, F_OpenedInBrowser, -7, 0), PanelResult.OpenedInBrowserByEngine, "Failed+OpenedInBrowser (RuntimeMissing): the engine opened it");
Check.Eq(ServerWebPanel.FromState(S_Failed, F_OpenedInBrowser, -8, 0), PanelResult.OpenedInBrowserByEngine, "Failed+OpenedInBrowser (CreateFailed): the engine opened it");
Check.Eq(ServerWebPanel.FromState(S_Failed, 0, -9, 1), PanelResult.FailedAfterNavigate, "Failed+ProcessFailed: the code is spent");
Check.Eq(ServerWebPanel.FromState(S_Failed, 0, -9, 0), PanelResult.FailedAfterNavigate, "ProcessFailed never falls back, whatever the count");
Check.Eq(ServerWebPanel.FromState(S_Failed, 0, -7, 0), PanelResult.RefusedBeforeNavigate, "RuntimeMissing, no browser, no navigation: fall back ourselves");
Check.Eq(ServerWebPanel.FromState(S_Failed, 0, -8, 0), PanelResult.RefusedBeforeNavigate, "CreateFailed, no browser, no navigation: fall back ourselves");
Check.Eq(ServerWebPanel.FromState(S_Failed, 0, -7, 1), PanelResult.FailedAfterNavigate, "RuntimeMissing after a navigation: spent, no fallback");
Check.Eq(ServerWebPanel.FromState(S_Failed, 0, -8, 2), PanelResult.FailedAfterNavigate, "CreateFailed after a navigation: spent, no fallback");
Check.Eq(ServerWebPanel.FromState(S_Failed, 0, 0, 0), PanelResult.FailedAfterNavigate, "Failed with an unknown error: no fallback");
Check.Eq(ServerWebPanel.FromState(S_Failed, 0, -4, 0), PanelResult.FailedAfterNavigate, "Failed with an undocumented error: no fallback");
Check.Eq(ServerWebPanel.FromState(S_Closed, 0, 0, 0), PanelResult.Closed, "Closed before it loaded");
Check.Eq(ServerWebPanel.FromState(S_Superseded, 0, 0, 0), PanelResult.Closed, "Superseded: replaced");
Check.Eq(ServerWebPanel.FromState(S_None, 0, 0, 0), PanelResult.NoAnswer, "None (older engine, shutting down): neutral");
Check.Eq(ServerWebPanel.FromState(99, 0, 0, 0), PanelResult.NoAnswer, "an undocumented status: neutral");
Check.Eq(ServerWebPanel.FromState(-4, 0, 0, 0), PanelResult.NoAnswer, "a negative status: neutral");
}
private static void Fallback()
{
foreach (PanelResult p in Enum.GetValues<PanelResult>())
Check.Eq(ServerWebOpen.FallbackToBrowser(p), p == PanelResult.RefusedBeforeNavigate, "fallback for " + p);
}
private static List<PanelResult> Answers(out Action<PanelResult> then)
{
var list = new List<PanelResult>();
then = list.Add;
return list;
}
private static void SyncAnswers()
{
foreach ((int code, PanelResult want) in new[] { (-1, PanelResult.BadArgs), (-2, PanelResult.UrlNotAllowed), (-3, PanelResult.RefusedBeforeNavigate), (-4, PanelResult.RefusedBeforeNavigate), (-5, PanelResult.RefusedBeforeNavigate) })
{
var e = new FakeEngine { OpenReturns = code };
var p = new ServerWebPanel(e);
var got = Answers(out var then);
p.Open(Req, 0, then);
Check.Eq(string.Join(",", got), want.ToString(), "open code " + code + ": answered at once");
Check.False(p.Waiting, "open code " + code + ": nothing waits");
p.Poll(1);
p.Poll(ServerWebPanel.PollTimeoutMs + 5);
Check.Eq(got.Count, 1, "open code " + code + ": the poll never answers again");
Check.Eq(e.StateCalls, 0, "open code " + code + ": no state is read");
p.Close();
Check.Eq(e.Closed.Count, 0, "open code " + code + ": no handle, nothing to close");
}
var o = new FakeEngine();
new ServerWebPanel(o).Open(Req, 0, _ => { });
Check.Eq(string.Join(" ", o.Opens), "www.acdreamweave.com|dreamweave/0123456789abcdef0123456789abcdef|DreamWeave", "the request's allowlist, profile key and title reach the engine");
}
private static void PollAnswers()
{
var cases = new (int Status, int Flags, int Error, uint Nav, PanelResult Want)[]
{
(S_Loading, F_Visible, 0, 1, PanelResult.Opened),
(S_Ready, F_Visible, 0, 1, PanelResult.Opened),
(S_Failed, F_OpenedInBrowser, -7, 0, PanelResult.OpenedInBrowserByEngine),
(S_Failed, 0, -9, 1, PanelResult.FailedAfterNavigate),
(S_Failed, 0, -7, 0, PanelResult.RefusedBeforeNavigate),
(S_Failed, 0, -8, 0, PanelResult.RefusedBeforeNavigate),
(S_Closed, 0, 0, 0, PanelResult.Closed),
(S_Superseded, 0, 0, 0, PanelResult.Closed),
(S_None, 0, 0, 0, PanelResult.NoAnswer),
};
foreach (var c in cases)
{
var e = new FakeEngine { Status = S_Starting };
var p = new ServerWebPanel(e);
var got = Answers(out var then);
p.Open(Req, 1_000, then);
Check.Eq(got.Count, 0, c.Want + ": a handle is not an answer");
Check.True(p.Waiting, c.Want + ": waiting after a handle");
p.Poll(1_100);
p.Poll(1_200);
Check.Eq(got.Count, 0, c.Want + ": Starting keeps waiting");
e.Status = c.Status; e.Flags = c.Flags; e.Error = c.Error; e.Nav = c.Nav;
p.Poll(1_300);
Check.Eq(string.Join(",", got), c.Want.ToString(), "state " + c.Status + "/" + c.Flags + "/" + c.Error + "/" + c.Nav + ": one poll decides");
Check.False(p.Waiting, c.Want + ": nothing waits after the answer");
int calls = e.StateCalls;
p.Poll(1_400);
p.Poll(1_000 + ServerWebPanel.PollTimeoutMs + 1);
Check.Eq(got.Count, 1, c.Want + ": answered exactly once");
Check.Eq(e.StateCalls, calls, c.Want + ": no polling after the answer");
}
}
private static void Timeout()
{
Check.True(ServerWebPanel.PollTimeoutMs >= 5_000 && ServerWebPanel.PollTimeoutMs <= 60_000, "the poll timeout is between 5 and 60 seconds");
var e = new FakeEngine { Status = S_Starting };
var p = new ServerWebPanel(e);
var got = Answers(out var then);
p.Open(Req, 10_000, then);
p.Poll(10_000 + ServerWebPanel.PollTimeoutMs - 1);
Check.Eq(got.Count, 0, "still Starting just before the timeout: waiting");
p.Poll(10_000 + ServerWebPanel.PollTimeoutMs);
Check.Eq(string.Join(",", got), PanelResult.NoAnswer.ToString(), "at the timeout: NoAnswer");
Check.False(ServerWebOpen.FallbackToBrowser(PanelResult.NoAnswer), "a timeout never opens the browser");
p.Poll(10_000 + ServerWebPanel.PollTimeoutMs + 50);
Check.Eq(got.Count, 1, "the timeout answers once");
// The handle is kept: the panel may still show, and logout closes it.
p.Close();
Check.Eq(string.Join(",", e.Closed), "7", "a timed-out open's handle is still ours to close");
}
private static void CloseOwnHandle()
{
var none = new FakeEngine();
new ServerWebPanel(none).Close();
Check.Eq(none.Closed.Count, 0, "never opened: Close calls nothing");
var e = new FakeEngine { OpenReturns = 7, Status = S_Starting };
var p = new ServerWebPanel(e);
var got = Answers(out var then);
p.Open(Req, 0, then);
p.Close();
Check.Eq(string.Join(",", got), PanelResult.Closed.ToString(), "Close answers the waiting open as Closed");
Check.Eq(string.Join(",", e.Closed), "7", "Close closes our handle");
p.Close();
Check.Eq(string.Join(",", e.Closed), "7", "a second Close closes nothing");
Check.Eq(got.Count, 1, "and answers nothing again");
// A newer open replaces the one we waited on; Close then closes the newer handle only.
var e2 = new FakeEngine { OpenReturns = 7, Status = S_Starting };
var p2 = new ServerWebPanel(e2);
var first = Answers(out var then1);
var second = Answers(out var then2);
p2.Open(Req, 0, then1);
e2.OpenReturns = 8;
p2.Open(Req, 10, then2);
Check.Eq(string.Join(",", first), PanelResult.Closed.ToString(), "a newer open answers the older one as Closed");
Check.Eq(second.Count, 0, "the newer one still waits");
e2.Status = S_Ready;
p2.Poll(20);
Check.Eq(string.Join(",", second), PanelResult.Opened.ToString(), "the newer one opens");
p2.Close();
Check.Eq(string.Join(",", e2.Closed), "8", "Close closes the newest handle only");
Check.Eq(first.Count, 1, "the older answer is not repeated");
// An opened panel (answered) is still closed by Close; a refused one gives nothing to close.
var e3 = new FakeEngine { OpenReturns = 9, Status = S_Ready };
var p3 = new ServerWebPanel(e3);
p3.Open(Req, 0, _ => { });
p3.Poll(1);
e3.OpenReturns = -4;
p3.Open(Req, 2, _ => { });
p3.Close();
Check.Eq(string.Join(",", e3.Closed), "9", "a refused open keeps the earlier handle ours to close");
// A refused second open leaves the first one waiting; the tick still answers it.
foreach (int refusal in new[] { -1, -2, -3, -4, -5 })
{
var e4 = new FakeEngine { OpenReturns = 7, Status = S_Starting };
var p4 = new ServerWebPanel(e4);
var a1 = Answers(out var t1);
var a2 = Answers(out var t2);
p4.Open(Req, 0, t1);
e4.OpenReturns = refusal;
p4.Open(Req, 10, t2);
Check.Eq(a1.Count, 0, "refused second open (" + refusal + "): the first is not answered");
Check.True(p4.Waiting, "refused second open (" + refusal + "): the first still waits");
Check.Eq(a2.Count, 1, "refused second open (" + refusal + "): answered at once");
e4.Status = S_Ready;
p4.Poll(20);
Check.Eq(string.Join(",", a1), PanelResult.Opened.ToString(), "refused second open (" + refusal + "): the first opens on the tick");
}
// Thrown second open: the same.
var e5 = new FakeEngine { OpenReturns = 7, Status = S_Starting };
var p5 = new ServerWebPanel(e5);
var b1 = Answers(out var u1);
p5.Open(Req, 0, u1);
e5.ThrowOnOpen = true;
p5.Open(Req, 10, _ => { });
Check.Eq(b1.Count, 0, "a second open that throws leaves the first unanswered");
Check.True(p5.Waiting, "and waiting");
}
private static void Exceptions()
{
var e = new FakeEngine { ThrowOnOpen = true };
var p = new ServerWebPanel(e);
var got = Answers(out var then);
p.Open(Req, 0, then);
Check.Eq(string.Join(",", got), PanelResult.RefusedBeforeNavigate.ToString(), "Open throwing: refused before navigating");
var s = new FakeEngine { Status = S_Starting };
var ps = new ServerWebPanel(s);
var sg = Answers(out var sthen);
ps.Open(Req, 0, sthen);
s.ThrowOnState = true;
ps.Poll(1);
Check.Eq(string.Join(",", sg), PanelResult.NoAnswer.ToString(), "GetState throwing after a handle: neutral, never the browser");
var h = new FakeEngine { ThrowOnHas = true };
Check.False(new ServerWebPanel(h).Available, "HasPanel throwing: not available");
Check.True(new ServerWebPanel(new FakeEngine()).Available, "HasPanel true: available");
Check.False(new ServerWebPanel(new FakeEngine { Has = false }).Available, "HasPanel false: not available");
var t = new FakeEngine { OpenReturns = -4 };
bool threw = false;
try { new ServerWebPanel(t).Open(Req, 0, _ => throw new InvalidOperationException("answer")); }
catch (Exception) { threw = true; }
Check.False(threw, "a throwing answer (sync) never escapes Open");
var tp = new FakeEngine { Status = S_Starting };
var ptp = new ServerWebPanel(tp);
ptp.Open(Req, 0, _ => throw new InvalidOperationException("answer"));
tp.Status = S_Ready;
threw = false;
try { ptp.Poll(1); } catch (Exception) { threw = true; }
Check.False(threw, "a throwing answer (polled) never escapes Poll");
Check.False(ptp.Waiting, "and the open is still answered");
}
// ServerPackWeb.cs runs inside acclient against whatever SDK the installed core ships.
private static void SourceRules()
{
string shared = Path.Combine(ServerProjectsTests.PluginsDir(), "Shared", "ServerPack");
string webPath = Path.Combine(shared, "ServerPackWeb.cs");
string panelPath = Path.Combine(shared, "ServerWebPanel.cs");
string pluginPath = Path.Combine(shared, "ServerPackPlugin.cs");
string web = DreamWeaveWebTests.StripComments(File.ReadAllText(webPath));
string panel = DreamWeaveWebTests.StripComments(File.ReadAllText(panelPath));
// The SDK web-panel names, and the only methods allowed to name them.
var sdkName = new System.Text.RegularExpressions.Regex(@"\b(HasWebPanel|OpenWebPanel|NavigateWebPanel|CloseWebPanel|GetWebPanelState|WebPanelOptions|WebPanelState|WebPanelStatus|WebPanelFlags|WebPanelResult|WebPanelStateFlags|WebPanelOpenNative|WebPanelStateNative|WebPanelOpenFn|WebPanelNavigateFn|WebPanelCloseFn|WebPanelGetStateFn)\b");
string[] helpers = { "WebPanelHasCore", "WebPanelOpenCore", "WebPanelStateCore", "WebPanelCloseCore" };
// Every server plugin source other than ServerPackWeb.cs names none of them (the pure seam uses ints).
foreach (string f in Directory.GetFiles(ServerProjectsTests.PluginsDir(), "*.cs", SearchOption.AllDirectories)
.Where(p => !p.Contains(Path.DirectorySeparatorChar + "obj" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)
&& !p.Contains(Path.DirectorySeparatorChar + "bin" + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)))
{
if (string.Equals(Path.GetFullPath(f), Path.GetFullPath(webPath), StringComparison.OrdinalIgnoreCase)) continue;
Check.False(sdkName.IsMatch(DreamWeaveWebTests.StripComments(File.ReadAllText(f))), Path.GetFileName(f) + ": no SDK web-panel names outside ServerPackWeb.cs");
}
// In ServerPackWeb.cs, each SDK name sits inside one of the helpers, and each helper is NoInlining
// and takes and returns primitives and strings only.
var methodHead = new System.Text.RegularExpressions.Regex(
@"\[MethodImpl\(MethodImplOptions\.NoInlining\)\]\s*private\s+(bool|int)\s+(" + string.Join("|", helpers) + @")\(([^)]*)\)");
var spans = new List<(int Start, int End)>();
var found = new List<string>();
foreach (System.Text.RegularExpressions.Match m in methodHead.Matches(web))
{
found.Add(m.Groups[2].Value);
foreach (string param in m.Groups[3].Value.Split(',', StringSplitOptions.RemoveEmptyEntries))
{
string type = param.Trim().Split(' ')[0] == "out" ? param.Trim().Split(' ')[1] : param.Trim().Split(' ')[0];
Check.True(type is "int" or "uint" or "string" or "string[]", m.Groups[2].Value + ": parameter type " + type + " is a primitive or string");
}
int bodyStart = m.Index + m.Length;
int end = BodyEnd(web, bodyStart);
spans.Add((m.Index, end));
}
Check.Eq(string.Join(",", found.OrderBy(x => x)), string.Join(",", helpers.OrderBy(x => x)), "the four helpers, each NoInlining, returning bool or int");
var stray = new List<string>();
foreach (System.Text.RegularExpressions.Match m in sdkName.Matches(web))
if (!spans.Any(s => m.Index >= s.Start && m.Index < s.End)) stray.Add(m.Value + "@" + m.Index);
Check.Eq(string.Join(" ", stray), "", "ServerPackWeb.cs: SDK web-panel names only inside the NoInlining helpers");
// Each helper is called exactly once, inside a try with both catches.
foreach (string h in helpers)
{
var call = new System.Text.RegularExpressions.Regex(@"try\s*\{\s*return\s+_plugin\." + h + @"\([^;]*\);\s*\}\s*catch\s*\(MissingMethodException\)\s*\{[^{}]*\}\s*catch\s*\(TypeLoadException\)\s*\{[^{}]*\}");
Check.Eq(call.Matches(web).Count, 1, h + ": called once, as try { return _plugin." + h + "(...); } catch (MissingMethodException) { } catch (TypeLoadException) { }");
Check.Eq(System.Text.RegularExpressions.Regex.Matches(web, @"\b" + h + @"\(").Count, 2, h + ": declared and called once (nothing else calls it)");
}
Check.True(web.Contains("Flags = WebPanelFlags.FallbackToBrowser", StringComparison.Ordinal), "the open asks the engine to fall back to the browser");
// The browser opens from two places only: OpenWeb's browser route, and right after AfterPanel's
// FallbackToBrowser guard. A browser open in any switch arm would spend the code twice.
string collapsed = System.Text.RegularExpressions.Regex.Replace(web, @"\s+", "");
Check.Eq(CountOf(web, "OpenInBrowser(r, done)"), 2, "ServerPackWeb.cs: OpenInBrowser(r, done) appears exactly twice");
Check.Eq(CountOf(collapsed, "if(ServerWebOpen.FallbackToBrowser(result)){LogTagged(\"webpanel:\"+result+\",usingthebrowser\");OpenInBrowser(r,done);return;}"), 1,
"AfterPanel: the browser open sits right after the FallbackToBrowser guard");
Check.Eq(CountOf(collapsed, "}OpenInBrowser(r,done);}"), 1, "OpenWeb: the other is the browser route at its end");
Check.Eq(CountOf(web, "ServerWebOpen.FallbackToBrowser("), 1, "AfterPanel asks FallbackToBrowser once");
// No locks in the web code: the waiting open and the handle move by Interlocked only.
Check.Eq(DreamWeaveWebTests.Locks(web).Count, 0, "ServerPackWeb.cs takes no lock");
Check.Eq(DreamWeaveWebTests.Locks(panel).Count, 0, "ServerWebPanel.cs takes no lock");
Check.False(System.Text.RegularExpressions.Regex.IsMatch(web + panel, @"\bMonitor\s*\."), "no Monitor in the web code");
// The poll runs from the plugin's OnTick once, after the tick's queued actions and before ServerTick.
string plugin = DreamWeaveWebTests.StripComments(File.ReadAllText(pluginPath));
Check.Eq(System.Text.RegularExpressions.Regex.Matches(plugin, @"\bPollWeb\(\);").Count, 1, "ServerPackPlugin.cs calls PollWeb() once");
int tick = plugin.IndexOf("public override void OnTick()", StringComparison.Ordinal);
int poll = plugin.IndexOf("PollWeb();", StringComparison.Ordinal);
int login = plugin.IndexOf("if (!_loginComplete) return;", tick < 0 ? 0 : tick, StringComparison.Ordinal);
int serverTick = plugin.IndexOf("ServerTick(active);", tick < 0 ? 0 : tick, StringComparison.Ordinal);
Check.True(tick >= 0 && login > tick && poll > login && poll < serverTick, "PollWeb runs in OnTick, logged in, before ServerTick");
Check.Eq(System.Text.RegularExpressions.Regex.Matches(web, @"\b_webPanel\?\.Poll\(Environment\.TickCount64\)").Count, 1, "PollWeb polls with Environment.TickCount64");
}
private static int CountOf(string text, string needle)
{
int n = 0, at = 0;
while ((at = text.IndexOf(needle, at, StringComparison.Ordinal)) >= 0) { n++; at += needle.Length; }
return n;
}
private static int BodyEnd(string code, int from)
{
int open = code.IndexOf('{', from);
int arrow = code.IndexOf("=>", from, StringComparison.Ordinal);
if (arrow >= 0 && (open < 0 || arrow < open)) { int semi = code.IndexOf(';', arrow); return semi < 0 ? code.Length : semi + 1; }
if (open < 0) return code.Length;
int depth = 0;
for (int j = open; j < code.Length; j++)
{
if (code[j] == '{') depth++;
else if (code[j] == '}' && --depth == 0) return j + 1;
}
return code.Length;
}
}