AI-Studio/app/Tests/Tools/MarkdownTests.cs
Thorsten Sommer 65a1683b95
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Verify (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Fixed exports and the clipboard losing the sources of an answer (#966)
2026-09-13 19:37:15 +02:00

66 lines
2.5 KiB
C#

using AIStudio.Tools;
namespace AIStudio.Tests.Tools;
/// <summary>
/// Checks that an answer which opens a code fence without closing it ends where it ends.
/// </summary>
/// <remarks>
/// A fence without its counterpart runs to the end of the document, so whatever is appended below an
/// answer is read as code instead of as Markdown. The chat never shows this, because it renders the
/// answer and the sources below it as two texts. A document is one text, and there an answer which
/// ends in an open fence takes the source list with it into a grey box.
/// </remarks>
[TestFixture]
public sealed class MarkdownTests
{
[Test]
public void AnOpenFenceGetsItsCounterpart()
{
var answer = Lines("Here is the code:", string.Empty, "```csharp", "var answer = 42;");
Assert.That(Markdown.CloseOpenCodeFence(answer), Is.EqualTo(Lines(answer, "```")), "The fence is closed with the same three backticks which opened it.");
}
[Test]
public void ALongerFenceIsClosedAtItsOwnLength()
{
// Four backticks are what a model writes when the block itself holds Markdown with code in
// it. The three backticks inside are content then, not the end of the block:
var answer = Lines("````markdown", "```csharp", "var answer = 42;", "```");
Assert.That(Markdown.CloseOpenCodeFence(answer), Is.EqualTo(Lines(answer, "````")), "Only a fence of at least the opening length closes the block.");
}
[Test]
public void ATildeFenceIsClosedWithTildes()
{
var answer = Lines("~~~", "var answer = 42;");
Assert.That(Markdown.CloseOpenCodeFence(answer), Is.EqualTo(Lines(answer, "~~~")), "A block opened with tildes cannot be closed with backticks.");
}
[Test]
public void AClosedFenceIsLeftAlone()
{
var answer = Lines("Here is the code:", string.Empty, "```csharp", "var answer = 42;", "```");
Assert.That(Markdown.CloseOpenCodeFence(answer), Is.EqualTo(answer), "The usual case: the model closed its block itself.");
}
[Test]
public void TextWithoutAnyFenceIsLeftAlone()
{
const string ANSWER = "Nothing in this answer opens a code block.";
Assert.That(Markdown.CloseOpenCodeFence(ANSWER), Is.EqualTo(ANSWER));
}
[Test]
public void AnEmptyTextIsLeftAlone()
{
Assert.That(Markdown.CloseOpenCodeFence(string.Empty), Is.Empty);
}
private static string Lines(params string[] lines) => string.Join(Environment.NewLine, lines);
}