94 lines
3.6 KiB
C#
94 lines
3.6 KiB
C#
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);
|
|
}
|