using AngleSharp.Dom;
using AngleSharp.Html.Parser;
namespace XFESystem.Browser.Tests;
/// Exercises the official MIT-licensed 0.52.2 core CSS embedded in Browser.
/// Inline sizes represent editor-generated DOM style attributes; the engine has no Monaco branches.
public sealed class MonacoCssLayoutTests
{
private static string OfficialCss()
{
var assembly = typeof(BrowserLayoutEngine).Assembly;
string name = Assert.Single(assembly.GetManifestResourceNames(), name => name.Replace('\\', '/') == "MonacoCore/EditorCore.css");
using var stream = assembly.GetManifestResourceStream(name);
Assert.NotNull(stream); using var reader = new StreamReader(stream); return reader.ReadToEnd();
}
private static IDocument EditorPage(int containerWidth, int containerHeight) => new HtmlParser().ParseDocument($$"""
const answer = 42;
""");
[Theory][InlineData(640, 400)][InlineData(930, 570)]
public void OfficialMonacoCssComputesLiveAbsoluteViewportAndTokens(int width, int height)
{
var page = EditorPage(width, height); var css = OfficialCss(); Assert.Equal(66_405, css.Length);
var result = new BrowserLayoutEngine().Layout(page, new(1200, 800), [new(css, page.GetElementById("Css"))]);
var editor = result.GetBoundingClientRect(page.GetElementById("Editor")!);
Assert.Equal(width, editor.Width); Assert.Equal(height - 28, editor.Height); Assert.Equal(28, editor.Y);
var line = result.GetBoundingClientRect(page.GetElementById("Line")!);
Assert.Equal(60, line.X, 5); Assert.Equal(28, line.Y, 5); Assert.Equal(width - 60, line.Width, 5); Assert.Equal(20, line.Height, 5);
var keyword = page.GetElementById("Keyword")!; Assert.Equal("rgb(86, 156, 214)", result.GetComputedStyle(keyword)!.GetPropertyValue("color"));
var command = Assert.Single(result.Commands, command => command.Text == "const"); Assert.Equal(0x569CD6u, command.Color); Assert.Equal(14, command.Font.Size);
Assert.Equal("nowrap", result.GetComputedStyle(page.GetElementById("Lines")!.QuerySelector(".view-lines")!)!.WhiteSpace);
Assert.Equal("absolute", result.GetComputedStyle(page.GetElementById("Input")!)!.Position);
Assert.Equal(0, result.GetComputedStyle(page.GetElementById("Input")!)!.Color.Alpha);
Assert.Contains(result.Commands, command => command.Kind == BrowserDrawKind.PushClip);
Assert.True(result.Diagnostics.Count < 300); // unsupported upstream features produce bounded diagnostics, not exceptions
}
[Fact] public void SameMonacoDomRelayoutChangesBoxesInsteadOfUsingFixedEditorGeometry()
{
var page = EditorPage(640, 400); var css = OfficialCss(); var engine = new BrowserLayoutEngine();
var sheets = new[] { new BrowserStyleSheet(css, page.GetElementById("Css")) };
var first = engine.Layout(page, new(1200, 800), sheets);
page.GetElementById("Host")!.SetAttribute("style", "width:777px;height:333px");
var next = engine.Layout(page, new(1200, 800), sheets);
Assert.Equal(640, first.GetBoundingClientRect(page.GetElementById("Editor")!).Width);
Assert.Equal(777, next.GetBoundingClientRect(page.GetElementById("Editor")!).Width);
Assert.Equal(305, next.GetBoundingClientRect(page.GetElementById("Editor")!).Height);
Assert.Equal(717, next.GetBoundingClientRect(page.GetElementById("Line")!).Width);
}
}