using System.Text.Json;
using System.Text.RegularExpressions;
using Febe.Schermate;
using Febe.Tests.Desktop.Support;
namespace Febe.Tests.Web;
///
/// The screenshot tool of spec §10.7 (tools/Febe.Schermate), written in C# like the rest (Alberto's decision of
/// 18 November 2026: no Node, no third-party programs): only Febe's own interface on this computer, every screenshot of
/// docs/schermate and nothing else.
///
[TestClass]
public sealed partial class ScreenshotToolTests
{
[TestMethod]
public void OnlyFebeOnThisComputerIsPhotographed()
{
foreach (var address in new[] { "http://127.0.0.1:8490/libreria", "http://localhost:8480/", "http://[::1]:8480/account", "https://127.0.0.1:8443/" })
{
Assert.IsTrue(Shots.IsOnThisComputer(new Uri(address)), address);
}
foreach (var address in new[]
{
"https://www.instagram.com/", "http://127.0.0.2:8480/", "http://localhost.example.com/", "file:///etc/passwd",
"http://user:pw@127.0.0.1:8480/", "http://192.168.1.10:8480/", "ws://127.0.0.1:8480/",
})
{
Assert.IsFalse(Shots.IsOnThisComputer(new Uri(address)), address);
}
var ex = Assert.ThrowsExactly(() => Request.Parse(["una", "https://www.instagram.com/", "x.png"], Path.GetTempPath()));
StringAssert.Contains(ex.Message, "questo computer");
}
[TestMethod]
public void TheListTakesEveryScreenshotOfTheFolderOnceWithItsStep()
{
var folder = Repository.PathOf("docs", "schermate");
var steps = Repository.PathOf("tools", "Febe.Schermate", "passi");
var files = Shots.All.Select(s => s.File).ToList();
CollectionAssert.AllItemsAreUnique(files);
CollectionAssert.AreEquivalent(Directory.GetFiles(folder, "*.png").Select(Path.GetFileName).ToList(), files, "every screenshot of docs/schermate, no other");
foreach (var shot in Shots.All)
{
Assert.IsTrue(shot.Page.StartsWith('/'), shot.File);
Assert.IsTrue(shot.Width is >= 390 and <= 1440 && shot.Height is >= 844 and <= 1470, shot.File);
if (shot.Step is not null)
{
Assert.IsTrue(File.Exists(Path.Combine(steps, shot.Step)), $"manca il passo {shot.Step}");
}
}
// The steps go next to the program, where it looks for them.
var project = File.ReadAllText(Repository.PathOf("tools", "Febe.Schermate", "Febe.Schermate.csproj"));
StringAssert.Contains(project, "");
}
[TestMethod]
public void TheCommandLineGivesTheDemoTheFolderAndASingleShot()
{
var root = Repository.PathOf();
var all = Request.Parse([], Path.Combine(root, "src"));
Assert.AreEqual(new Uri("http://127.0.0.1:8480/"), all.Base);
Assert.AreEqual(Path.Combine(root, "docs", "schermate"), all.Folder);
Assert.HasCount(Shots.All.Count, all.Shots);
Assert.AreEqual(new Uri("http://127.0.0.1:8480/libreria?tema=chiaro"), all.AddressOf(all.Shots[0]));
var other = Request.Parse(["8490", "uscita"], root);
Assert.AreEqual(8490, other.Base.Port);
Assert.AreEqual(Path.Combine(root, "uscita"), other.Folder);
var one = Request.Parse(["una", "http://localhost:8490/attivita?scena=fermo", "prova.png", "390", "844", "500", "passo.js"], root);
var shot = one.Shots.Single();
Assert.AreEqual(new Uri("http://localhost:8490/attivita?scena=fermo"), one.AddressOf(shot));
Assert.AreEqual(root, one.Folder);
Assert.AreEqual(new Shot(string.Empty, "prova.png", 390, 844, 500, Path.Combine(root, "passo.js")), shot);
Assert.AreEqual(Path.Combine(root, "passo.js"), one.StepPath(shot.Step!));
foreach (var wrong in new[] { new[] { "porta" }, ["0"], ["70000"], ["8480", "a", "b"], ["una", "http://127.0.0.1/"], ["una", "http://127.0.0.1/", "x.jpg"],
["una", "http://127.0.0.1/", "x.png", "10"], ["una", "http://127.0.0.1/", "x.png", "800", "600", "-1"] })
{
Assert.ThrowsExactly(() => Request.Parse(wrong, root), string.Join(' ', wrong));
}
Assert.IsNull(Request.RepositoryRoot(Path.GetPathRoot(Path.GetTempPath())!));
}
[TestMethod]
public void TheBrowserHasNoWindowAPrivateProfileAndTheDebugPortOnlyHere()
{
var arguments = Screenshotter.Arguments("profilo", 390, 844, noSandbox: false);
CollectionAssert.Contains(arguments, "--headless");
CollectionAssert.Contains(arguments, "--user-data-dir=profilo");
CollectionAssert.Contains(arguments, "--remote-debugging-port=0");
CollectionAssert.Contains(arguments, "--remote-debugging-address=127.0.0.1");
CollectionAssert.Contains(arguments, "--no-proxy-server");
CollectionAssert.Contains(arguments, "--window-size=390,844");
CollectionAssert.DoesNotContain(arguments, "--no-sandbox");
Assert.AreEqual("about:blank", arguments[^1]);
CollectionAssert.Contains(Screenshotter.Arguments("profilo", 1440, 900, noSandbox: true), "--no-sandbox");
// Only a page target on 127.0.0.1 is driven.
Assert.AreEqual(
"ws://127.0.0.1:9333/devtools/page/A",
Screenshotter.PageDebugger("""[{"type":"service_worker","webSocketDebuggerUrl":"ws://127.0.0.1:9333/devtools/page/W"},{"type":"page","webSocketDebuggerUrl":"ws://127.0.0.1:9333/devtools/page/A"}]"""));
Assert.IsNull(Screenshotter.PageDebugger("""[{"type":"page","webSocketDebuggerUrl":"ws://10.0.0.5:9333/devtools/page/A"}]"""));
Assert.IsNull(Screenshotter.PageDebugger("[]"));
using var hover = JsonDocument.Parse("""{"hover":[640,212.5],"label":"x"}""");
Assert.AreEqual((640d, 212.5d), Screenshotter.HoverPoint(hover.RootElement));
using var none = JsonDocument.Parse("""{"hover":[1]}""");
Assert.IsNull(Screenshotter.HoverPoint(none.RootElement));
}
[TestMethod]
public void NoToolNeedsNode()
{
var tools = Repository.PathOf("tools");
foreach (var file in Directory.EnumerateFiles(tools, "*", SearchOption.AllDirectories))
{
var relative = Path.GetRelativePath(tools, file);
if (relative.Split(Path.DirectorySeparatorChar).Any(part => part is "bin" or "obj"))
{
continue;
}
Assert.IsFalse(Path.GetExtension(file) is ".mjs" or ".cjs" or ".ts", $"script Node: {relative}");
Assert.AreNotEqual("package.json", Path.GetFileName(file), relative);
if (Path.GetExtension(file) is ".sh" or ".ps1" or ".cmd" or ".bat" or ".cs" or ".csproj")
{
Assert.IsFalse(NodeCommand().IsMatch(File.ReadAllText(file)), $"comando node in {relative}");
}
}
Assert.IsFalse(Directory.Exists(Path.Combine(tools, "schermate")), "tools/schermate became tools/Febe.Schermate");
StringAssert.Contains(File.ReadAllText(Repository.PathOf("Febe.slnx")), "");
}
/// «node» or «npx» run as a command (at the start of a line or after a separator), not inside a word.
[GeneratedRegex(@"(?m)(?:^|[;&|(`]\s*|\bexec\s+|\btimeout\s+\d+\s+)(?:node|npx|npm)\s")]
private static partial Regex NodeCommand();
}