# Conflicts: # LehrerApp.TemplateDesigner.Tests/ProjectLifecycleTests.cs # LehrerApp.TemplateDesigner/DesignerViewModel.cs # LehrerApp.TemplateDesigner/MainWindow.axaml # LehrerApp.Templating/LayoutParser.cs # LehrerApp.Templating/QuestTemplateRenderer.cs
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
using LehrerApp.TemplateDesigner;
|
||||
using LehrerApp.Templating;
|
||||
using Xunit;
|
||||
using System.Runtime.Versioning;
|
||||
using QuestPDF.Fluent;
|
||||
using QuestPDF.Infrastructure;
|
||||
|
||||
namespace LehrerApp.TemplateDesigner.Tests;
|
||||
|
||||
[SupportedOSPlatform("windows")]
|
||||
[SupportedOSPlatform("linux")]
|
||||
[SupportedOSPlatform("macos")]
|
||||
public sealed class PdfImportPipelineTests
|
||||
{
|
||||
[Fact]
|
||||
public void TemplateDiff_LaesstIdentischenTextStatischUndMarkiertAbweichung()
|
||||
{
|
||||
var pipeline = new PdfImportPipeline();
|
||||
var template = Document(
|
||||
Block("a", 40, 30, "Schule am Park"),
|
||||
Block("b", 40, 100, "Max Mustermann"));
|
||||
var example = Document(
|
||||
Block("a2", 40, 30, "Schule am Park"),
|
||||
Block("b2", 40, 100, "Erika Beispiel"));
|
||||
|
||||
var candidate = Assert.Single(pipeline.FindCandidates(example, template));
|
||||
|
||||
Assert.Equal("Erika Beispiel", candidate.OriginalText);
|
||||
Assert.Equal(PdfImportConfidence.High, candidate.Confidence);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EinzelPdf_ErkenntDatumTypDeterministisch()
|
||||
{
|
||||
var pipeline = new PdfImportPipeline();
|
||||
|
||||
var candidate = Assert.Single(pipeline.FindCandidates(Document(Block("d", 400, 60, "31.08.2026")), null));
|
||||
|
||||
Assert.Equal("Datum", candidate.Name);
|
||||
Assert.Equal(PlaceholderType.Date, candidate.Type);
|
||||
Assert.Equal(PdfImportConfidence.High, candidate.Confidence);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UebernahmeErsetztDasAktuelleDesignerprojekt()
|
||||
{
|
||||
var result = new PdfImportResult("PAGE 595 842 pt\nTEXT 40 100 $Name\n",
|
||||
new TemplateManifest
|
||||
{
|
||||
Id = "pdf-import", Name = "PDF-Import", PageSize = new(595, 842, "pt"),
|
||||
Placeholders = [new("Name", PlaceholderType.Text)],
|
||||
}, new Dictionary<string, byte[]>(),
|
||||
[new PdfImportCandidate { Id = "x", BlockIds = ["x"], OriginalText = "Erika Beispiel", Name = "Name", Confidence = PdfImportConfidence.High }]);
|
||||
var viewModel = new DesignerViewModel();
|
||||
|
||||
viewModel.ApplyPdfImport(result);
|
||||
|
||||
Assert.Equal("pt", viewModel.Unit);
|
||||
Assert.Equal("Erika Beispiel", Assert.Single(viewModel.Placeholders).Sample);
|
||||
Assert.Contains("$Name", viewModel.LayoutSource);
|
||||
Assert.False(viewModel.CanExport);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EchtesPdf_WirdExtrahiertUndAlsRenderbareVorlageErzeugt()
|
||||
{
|
||||
QuestPDF.Settings.License = LicenseType.Community;
|
||||
var path = Path.Combine(Path.GetTempPath(), $"pdf-import-{Guid.NewGuid():N}.pdf");
|
||||
try
|
||||
{
|
||||
QuestPDF.Fluent.Document.Create(document => document.Page(page =>
|
||||
{
|
||||
page.Size(595, 842); page.Margin(40); page.Content().Text("Erika Beispiel").FontSize(11);
|
||||
})).GeneratePdf(path);
|
||||
|
||||
var result = await new PdfImportPipeline().BuildAsync(path, null, null, null);
|
||||
var viewModel = new DesignerViewModel();
|
||||
viewModel.ApplyPdfImport(result);
|
||||
var png = new QuestTemplateRenderer().RenderFirstPageToPng(viewModel.BuildLoaded(), viewModel.BuildDataProvider());
|
||||
|
||||
Assert.NotEmpty(result.Candidates);
|
||||
Assert.Contains("pdf-import-background.png", result.Assets.Keys);
|
||||
Assert.True(png.Length > 1_000);
|
||||
}
|
||||
finally { if (File.Exists(path)) File.Delete(path); }
|
||||
}
|
||||
|
||||
private static PdfImportDocument Document(params PdfTextBlock[] blocks) =>
|
||||
new([new PdfImportPage(1, 595, 842, blocks)]);
|
||||
|
||||
private static PdfTextBlock Block(string id, double x, double y, string text) =>
|
||||
new(id, 1, x, y, 120, 12, 11, "Arial", false, false, text);
|
||||
}
|
||||
@@ -108,6 +108,15 @@ public sealed class ProjectLifecycleTests
|
||||
Assert.Empty(parsed.ContentFlows.Single().Elements);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AktuelleAnsicht_KannAlsPdfGerendertWerden()
|
||||
{
|
||||
var pdf = new DesignerViewModel().RenderCurrentPdf();
|
||||
|
||||
Assert.True(pdf.Length > 100);
|
||||
Assert.Equal("%PDF-", System.Text.Encoding.ASCII.GetString(pdf, 0, 5));
|
||||
}
|
||||
|
||||
private static readonly byte[] OnePixelPng = Convert.FromBase64String(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user