Refused JSON contracts built by reflection and checked all of them in the trimmed build (#1152)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-03 18:29:50 +01:00
1 parent f864359290
commit 0b42c0288e
23 files changed
+535 -99

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
using System.Text.Json.Nodes;
using AIStudio.Components;
namespace AIStudio.Tests.Components;
/// <summary>
/// Checks how the tree of a tool result in the chat writes its keys and values.
/// </summary>
/// <remarks>
/// The tree writes each key and each value as JSON. Escaping for a web page, as the default encoder of
/// System.Text.Json does, would show every umlaut and every quotation mark as a code of six characters,
/// although Blazor encodes the text for the page anyway. A value parsed from JSON goes through the same
/// writer as one created in code, so both get checked.
/// </remarks>
[TestFixture]
public sealed class JsonTreeViewTests
{
[TestCase("Größe der Übersicht")]
[TestCase("if (a < b && c > d) return 'x' + y;")]
[TestCase("日本語のテキスト")]
public void ValuesAppearAsWritten(string text)
{
var parsed = JsonNode.Parse(new JsonObject { ["text_content"] = text }.ToJsonString())!["text_content"];
using (Assert.EnterMultipleScope())
{
Assert.That(JsonTreeView.FormatValue(JsonValue.Create(text)), Is.EqualTo($"\"{text}\""), "A value created in code.");
Assert.That(JsonTreeView.FormatValue(parsed), Is.EqualTo($"\"{text}\""), "A value parsed from JSON.");
}
}
[TestCase("Größe")]
[TestCase("a < b && c > d")]
public void KeysAppearAsWritten(string key) => Assert.That(JsonTreeView.FormatKey(key), Is.EqualTo($"\"{key}\""));
[Test]
public void WhatJsonHasToEscapeStaysEscaped()
{
const string TEXT = "A \"quoted\" line\nand a backslash \\ at its end.";
using (Assert.EnterMultipleScope())
{
Assert.That(JsonNode.Parse(JsonTreeView.FormatValue(JsonValue.Create(TEXT)))?.GetValue<string>(), Is.EqualTo(TEXT), "A value.");
Assert.That(JsonNode.Parse(JsonTreeView.FormatKey(TEXT))?.GetValue<string>(), Is.EqualTo(TEXT), "A key.");
}
}
}