using System.IO.Compression;
using AIStudio.Tests.Tools.OpenXml;
using AIStudio.Tests.Tools.PowerPoint;
using AIStudio.Tools.DocumentSegments;
using AIStudio.Tools.DocumentSegments.PowerPoint;
using AIStudio.Tools.PowerPoint;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using Microsoft.Extensions.Logging.Abstractions;
using A = DocumentFormat.OpenXml.Drawing;
using P = DocumentFormat.OpenXml.Presentation;
namespace AIStudio.Tests.Tools.DocumentSegments.PowerPoint;
///
/// Checks which parts of a presentation a model works on, and that everything else stays as it is.
///
[TestFixture]
public sealed class PowerPointSegmentFormatTests
{
private const string LINK_ID = "rIdMars";
private const string RICH_PARAGRAPH = "We fly <1>very1> far,<2/><3>see the plan3> on slide <4/>";
private const int VIDEO_SIZE = 8 * 1024 * 1024;
private static readonly SlotBounds TEXT_BOX_BOUNDS = new(838_200, 1_825_625, 10_515_600, 1_000_000);
private static readonly SlotBounds TABLE_BOUNDS = new(838_200, 3_000_000, 10_515_600, 741_680);
private readonly PowerPointSegmentFormat format = new();
private string testFolder = string.Empty;
private string source = string.Empty;
private string target = string.Empty;
[SetUp]
public void CreatePresentation()
{
this.testFolder = Directory.CreateDirectory(Path.Join(Path.GetTempPath(), "mindwork-ai-studio-tests", Guid.NewGuid().ToString("N"))).FullName;
this.source = Path.Join(this.testFolder, "source.pptx");
this.target = Path.Join(this.testFolder, "target.pptx");
var slide = new TestSlide(0, (TestPlaceholder.Title(), "Mission to Mars"))
{
Elements = [TestElement.TextBox(TEXT_BOX_BOUNDS, "rich"), TestElement.Table(TABLE_BOUNDS, ["Name", "Value"], ["Speed", "42"])],
};
using (var stream = TestTemplate.Create(new TestTemplateOptions(Slides: [slide]), new TestLayout("Title only", TestPlaceholder.Title())))
File.WriteAllBytes(this.source, stream.ToArray());
using var document = PresentationDocument.Open(this.source, true);
var slidePart = document.PresentationPart!.SlideParts.Single();
slidePart.AddHyperlinkRelationship(new Uri("https://example.org/mars"), true, LINK_ID);
// A paragraph as PowerPoint writes one: runs in their language, a word in bold, a line break, a link, and the number of the slide:
var paragraph = slidePart.Slide!.Descendants().Single(candidate => candidate.InnerText == "rich");
paragraph.RemoveAllChildren();
paragraph.Append(
new A.Run(new A.RunProperties { Language = "en-US", Dirty = false }, new A.Text("We fly ")),
new A.Run(new A.RunProperties { Language = "en-US", Bold = true }, new A.Text("very")),
new A.Run(new A.RunProperties { Language = "en-US", SpellingError = true }, new A.Text(" far,")),
new A.Break(new A.RunProperties { Language = "en-US" }),
new A.Run(new A.RunProperties(new A.HyperlinkOnClick { Id = LINK_ID }) { Language = "en-US" }, new A.Text("see the plan")),
new A.Run(new A.RunProperties { Language = "en-US" }, new A.Text(" on slide ")),
new A.Field(new A.RunProperties { Language = "en-US" }, new A.Text("1")) { Id = "{B6F15528-21DE-4FAA-801E-634DDDAF4B2B}", Type = "slidenum" },
new A.EndParagraphRunProperties { Language = "en-US" });
var notesPart = slidePart.AddNewPart();
notesPart.NotesSlide = new P.NotesSlide(new P.CommonSlideData(new P.ShapeTree(
new P.NonVisualGroupShapeProperties(
new P.NonVisualDrawingProperties { Id = 1U, Name = string.Empty },
new P.NonVisualGroupShapeDrawingProperties(),
new P.ApplicationNonVisualDrawingProperties()),
new P.GroupShapeProperties(new A.TransformGroup()),
new P.Shape(
new P.NonVisualShapeProperties(
new P.NonVisualDrawingProperties { Id = 2U, Name = "Notes" },
new P.NonVisualShapeDrawingProperties(),
new P.ApplicationNonVisualDrawingProperties(new P.PlaceholderShape { Type = P.PlaceholderValues.Body, Index = 1U })),
new P.ShapeProperties(),
new P.TextBody(new A.BodyProperties(), new A.ListStyle(), new A.Paragraph(new A.Run(new A.Text("Speak slowly."))))))));
}
[TearDown]
public void RemoveFolder() => Directory.Delete(this.testFolder, true);
[Test]
public async Task TheTextsOfSlidesAndNotesBecomeSegmentsWithTags()
{
var keys = (await this.format.ReadAsync(this.source, NullLogger.Instance, CancellationToken.None)).Select(segment => segment.Key).ToList();
using (Assert.EnterMultipleScope())
{
Assert.That(keys, Does.Contain("Mission to Mars"));
Assert.That(keys, Does.Contain(RICH_PARAGRAPH), "Bold and a link are pairs, a line break and a field tags of their own; language and spell check make no difference.");
Assert.That(keys, Is.SupersetOf(new[] { "Name", "Value", "Speed", "42" }), "The cells of tables are read.");
Assert.That(keys, Does.Contain("Speak slowly."), "The notes are read.");
Assert.That(keys, Has.None.Contains("Click"), "The layouts stay out.");
}
}
[Test]
public async Task WritingTheSourceBackChangesNothing()
{
var result = await this.format.WriteAsync(this.source, this.target, await this.ResultsAsync(key => key), "de-DE", NullLogger.Instance, CancellationToken.None);
using var sourceDocument = PresentationDocument.Open(this.source, false);
using var targetDocument = PresentationDocument.Open(this.target, false);
using (Assert.EnterMultipleScope())
{
Assert.That(result.Outcome, Is.EqualTo(SegmentWriteOutcome.WRITTEN));
Assert.That(SlideOf(targetDocument).OuterXml, Is.EqualTo(SlideOf(sourceDocument).OuterXml));
}
}
[Test]
public async Task NewWordsKeepTheStructure()
{
var result = await this.format.WriteAsync(this.source, this.target, await this.ResultsAsync(key => key.ToUpperInvariant()), "de-DE", NullLogger.Instance, CancellationToken.None);
using var sourceDocument = PresentationDocument.Open(this.source, false);
using var document = PresentationDocument.Open(this.target, false);
var slide = SlideOf(document);
var paragraph = slide.Descendants().Single(candidate => candidate.Descendants().Any());
var runs = paragraph.Elements().ToList();
using (Assert.EnterMultipleScope())
{
Assert.That(ValidationErrorsOf(document), Is.EqualTo(ValidationErrorsOf(sourceDocument)), "The new presentation is as valid as the source.");
Assert.That(result.SimplifiedCount, Is.Zero);
Assert.That(slide.Descendants().Select(candidate => candidate.InnerText), Is.SupersetOf(new[] { "MISSION TO MARS", "NAME", "VALUE", "SPEED", "42" }));
Assert.That(runs.Select(run => run.Text!.Text), Is.EqualTo(new[] { "WE FLY ", "VERY", " FAR,", "SEE THE PLAN", " ON SLIDE " }));
Assert.That(runs.Where(run => run.RunProperties?.Bold?.Value == true).Select(run => run.Text!.Text), Is.EqualTo(new[] { "VERY" }), "The word in bold stays bold.");
Assert.That(runs.Where(run => run.RunProperties?.GetFirstChild()?.Id?.Value == LINK_ID).Select(run => run.Text!.Text), Is.EqualTo(new[] { "SEE THE PLAN" }), "The link keeps its words and its address.");
Assert.That(runs.Select(run => run.RunProperties?.Language?.Value), Has.All.EqualTo("de-DE"), "New words carry their language.");
Assert.That(runs.Select(run => run.RunProperties?.SpellingError), Has.All.Null, "New words carry no mark of an earlier spell check.");
Assert.That(paragraph.Elements().Count(), Is.EqualTo(1), "The line break stays.");
Assert.That(paragraph.Elements().Single().Type?.Value, Is.EqualTo("slidenum"), "The number of the slide stays a field.");
Assert.That(paragraph.ChildElements.Last(), Is.TypeOf(), "The properties of the end of the paragraph come last.");
Assert.That(document.PresentationPart!.SlideParts.Single().NotesSlidePart!.NotesSlide!.Descendants().Single().InnerText, Is.EqualTo("SPEAK SLOWLY."));
}
}
[Test]
public async Task TheFormattingFollowsItsWords()
{
var results = await this.ResultsAsync(key => key == RICH_PARAGRAPH ? "<3>Siehe den Plan3> auf Folie <4/>:<2/>Wir fliegen <1>sehr1> weit." : key);
await this.format.WriteAsync(this.source, this.target, results, "de-DE", NullLogger.Instance, CancellationToken.None);
using var document = PresentationDocument.Open(this.target, false);
var paragraph = SlideOf(document).Descendants().Single(candidate => candidate.Descendants().Any());
using (Assert.EnterMultipleScope())
{
Assert.That(paragraph.ChildElements.Select(child => child.LocalName), Is.EqualTo(new[] { "r", "r", "fld", "r", "br", "r", "r", "r", "endParaRPr" }));
Assert.That(paragraph.Elements().Where(run => run.RunProperties?.Bold?.Value == true).Select(run => run.Text!.Text), Is.EqualTo(new[] { "sehr" }));
Assert.That(paragraph.Elements().First().RunProperties?.GetFirstChild(), Is.Not.Null, "The link moved to the front with its words.");
}
}
///
/// The format skips a paragraph whose text stays as it was, so this test hands every key to the writer itself:
/// a writer which puts a paragraph together again from its own tags has to yield the same presentation.
///
[Test]
public void WritingEveryKeyThroughTheWriterKeepsThePresentation()
{
File.Copy(this.source, this.target);
using (var document = PresentationDocument.Open(this.target, true))
{
foreach (var paragraph in SlideSegmentReader.Read(document))
Assert.That(SlideParagraphWriter.Write(paragraph, paragraph.Segment.Key, string.Empty), Is.True, $"'{paragraph.Segment.Key}' keeps its inline structure and formatting.");
}
using var sourceDocument = PresentationDocument.Open(this.source, false);
using var targetDocument = PresentationDocument.Open(this.target, false);
using (Assert.EnterMultipleScope())
{
Assert.That(ValidationErrorsOf(targetDocument), Is.EqualTo(ValidationErrorsOf(sourceDocument)), "The new presentation is as valid as the source.");
foreach (var (sourceRoot, targetRoot) in RootsOf(sourceDocument).Zip(RootsOf(targetDocument)))
{
Assert.That(StructureOf(targetRoot), Is.EqualTo(StructureOf(sourceRoot)), $"Every element of {sourceRoot.LocalName} besides the runs stays, in its order.");
Assert.That(FormattedTextOf(targetRoot), Is.EqualTo(FormattedTextOf(sourceRoot)), $"Every word of {sourceRoot.LocalName} keeps its formatting.");
}
}
}
[Test]
public async Task WhatTheModelDroppedFollowsItsWords()
{
// The model left out the line break and the number of the slide:
var results = await this.ResultsAsync(key => key == RICH_PARAGRAPH ? "Wir fliegen <1>sehr1> weit, <3>siehe den Plan3> auf Folie" : key);
var result = await this.format.WriteAsync(this.source, this.target, results, "de-DE", NullLogger.Instance, CancellationToken.None);
using var sourceDocument = PresentationDocument.Open(this.source, false);
using var document = PresentationDocument.Open(this.target, false);
var paragraph = SlideOf(document).Descendants().Single(candidate => candidate.Descendants().Any());
using (Assert.EnterMultipleScope())
{
Assert.That(ValidationErrorsOf(document), Is.EqualTo(ValidationErrorsOf(sourceDocument)), "The new presentation is as valid as the source.");
Assert.That(result.SimplifiedCount, Is.EqualTo(1));
Assert.That(paragraph.ChildElements.Select(child => child.LocalName).TakeLast(3), Is.EqualTo(new[] { "br", "fld", "endParaRPr" }), "The line break and the field follow the words rather than getting lost.");
}
}
[Test]
public async Task TooManyChangesOfFormattingKeepTheMostCommonFormatting()
{
// A glossary in the title: each term in bold comes first, but most letters are those of the plain explanations:
using (var document = PresentationDocument.Open(this.source, true))
{
var title = SlideOf(document).Descendants().Single(candidate => candidate.InnerText == "Mission to Mars");
title.RemoveAllChildren();
var end = title.GetFirstChild();
foreach (var run in Enumerable.Range(0, 13).SelectMany(_ => new[]
{
new A.Run(new A.RunProperties { Language = "en-US", Bold = true }, new A.Text("Term ")),
new A.Run(new A.RunProperties { Language = "en-US" }, new A.Text("explained in plain words. ")),
}))
{
if (end is null)
title.AppendChild(run);
else
title.InsertBefore(run, end);
}
}
var results = await this.ResultsAsync(key => key.StartsWith("Term ", StringComparison.Ordinal) ? key.ToUpperInvariant() : key);
var result = await this.format.WriteAsync(this.source, this.target, results, string.Empty, NullLogger.Instance, CancellationToken.None);
using var written = PresentationDocument.Open(this.target, false);
var paragraph = SlideOf(written).Descendants().Single(candidate => candidate.InnerText.StartsWith("TERM EXPLAINED", StringComparison.Ordinal));
using (Assert.EnterMultipleScope())
{
Assert.That(result.SimplifiedCount, Is.EqualTo(1), "Thirteen terms in bold are more pairs than a model keeps in place reliably.");
Assert.That(paragraph.Elements().Where(run => run.RunProperties?.Bold?.Value == true), Is.Empty, "The new words get the formatting of most letters, not the one of the first word.");
}
}
[Test]
public async Task AVideoBehindTheSlidesIsStreamedThroughAsItIs()
{
this.AddVideo();
var results = await this.ResultsAsync(key => key.ToUpperInvariant());
var stagedBefore = StagingFolder.Files();
var allocatedBefore = GC.GetTotalAllocatedBytes(true);
var result = await this.format.WriteAsync(this.source, this.target, results, "de-DE", NullLogger.Instance, CancellationToken.None);
var allocated = GC.GetTotalAllocatedBytes(true) - allocatedBefore;
using var sourceArchive = ZipFile.OpenRead(this.source);
using var targetArchive = ZipFile.OpenRead(this.target);
var changedEntries = sourceArchive.Entries.Where(entry => !ContentOf(entry).SequenceEqual(ContentOf(targetArchive.GetEntry(entry.FullName)!))).Select(entry => entry.FullName).ToList();
var targetVideo = targetArchive.GetEntry("ppt/media/video1.mp4")!;
using var document = PresentationDocument.Open(this.target, false);
using (Assert.EnterMultipleScope())
{
Assert.That(result.Outcome, Is.EqualTo(SegmentWriteOutcome.WRITTEN));
Assert.That(targetArchive.Entries.Select(entry => entry.FullName), Is.EqualTo(sourceArchive.Entries.Select(entry => entry.FullName)), "Every entry keeps its place.");
Assert.That(changedEntries, Is.EquivalentTo(new[] { "ppt/slides/slide1.xml", "ppt/notesSlides/notesSlide1.xml" }), "Only the parts with new words change; every other entry keeps its bytes.");
Assert.That(targetVideo.CompressedLength, Is.EqualTo(targetVideo.Length), "The video stays uncompressed.");
Assert.That(allocated, Is.LessThan(VIDEO_SIZE / 2), "The video goes through the stream, not through memory.");
Assert.That(SlideOf(document).Descendants().Select(paragraph => paragraph.InnerText), Has.Member("MISSION TO MARS"), "The new words reach the new document.");
Assert.That(StagingFolder.Files().Except(stagedBefore), Is.Empty, "The new document leaves nothing behind in the staging folder.");
}
}
[Test]
public void TheTextOfAPresentationLeavesItsVideosOut()
{
this.AddVideo();
long xmlSize;
using (var archive = ZipFile.OpenRead(this.source))
xmlSize = archive.Entries.Where(entry => entry.FullName.EndsWith(".xml", StringComparison.Ordinal) || entry.FullName.EndsWith(".rels", StringComparison.Ordinal)).Sum(entry => entry.Length);
Assert.That(this.format.TextSizeOf(this.source), Is.EqualTo(xmlSize).And.LessThan(VIDEO_SIZE), "Only the XML comes into memory, unpacked, never the pictures and videos.");
}
[Test]
public async Task ReadingThroughTheSkeletonLeavesNothingBehind()
{
this.AddVideo();
var stagedBefore = StagingFolder.Files();
var keys = (await this.format.ReadAsync(this.source, NullLogger.Instance, CancellationToken.None)).Select(segment => segment.Key).ToList();
using (Assert.EnterMultipleScope())
{
Assert.That(keys, Does.Contain("Mission to Mars"), "The skeleton holds every text of the presentation.");
Assert.That(StagingFolder.Files().Except(stagedBefore), Is.Empty, "The skeleton is gone once the presentation is read.");
}
}
[Test]
public async Task AChangedSourceWritesNothing()
{
var result = await this.format.WriteAsync(this.source, this.target, new SegmentResults(new Dictionary()), "de-DE", NullLogger.Instance, CancellationToken.None);
using (Assert.EnterMultipleScope())
{
Assert.That(result.Outcome, Is.EqualTo(SegmentWriteOutcome.SOURCE_CHANGED));
Assert.That(File.Exists(this.target), Is.False);
}
}
private async Task ResultsAsync(Func transform)
{
var segments = await this.format.ReadAsync(this.source, NullLogger.Instance, CancellationToken.None);
return new SegmentResults(segments
.Where(segment => segment.HasWords)
.DistinctBy(segment => segment.Key)
.ToDictionary(segment => segment.Key, segment => SegmentOutcome.Transformed(transform(segment.Key))));
}
private static P.Slide SlideOf(PresentationDocument document) => document.PresentationPart!.SlideParts.Single().Slide!;
///
/// Adds a video as PowerPoint stores it: uncompressed, behind the slides, which the update mode of ZipArchive would load into memory.
///
private void AddVideo() => TestTemplate.AddVideo(this.source, 1, TestTemplate.VideoOf(VIDEO_SIZE));
private static byte[] ContentOf(ZipArchiveEntry entry)
{
using var stream = entry.Open();
using var content = new MemoryStream();
stream.CopyTo(content);
return content.ToArray();
}
private static IEnumerable RootsOf(PresentationDocument document)
{
var slidePart = document.PresentationPart!.SlideParts.Single();
yield return slidePart.Slide!;
yield return slidePart.NotesSlidePart!.NotesSlide!;
}
///
/// The elements of a part in their order, apart from runs and their properties, which a writer may split or join.
///
private static List StructureOf(OpenXmlElement root) => root
.Descendants()
.Where(element => element is not (A.Run or A.Text or A.RunProperties) && !element.Ancestors().Any())
.Select(element => element.LocalName)
.ToList();
///
/// The words of a part with their formatting, joined where the formatting stays the same; language and spell check are no formatting, see SlideSegmentReader.
///
private static List FormattedTextOf(OpenXmlElement root)
{
var spans = new List<(string Format, string Text)>();
foreach (var text in root.Descendants())
{
var format = string.Empty;
if (text.Parent?.GetFirstChild() is { } properties)
{
var copy = (A.RunProperties)properties.CloneNode(true);
copy.Language = null;
copy.AlternativeLanguage = null;
copy.SpellingError = null;
copy.Dirty = null;
copy.NoProof = null;
copy.SmartTagClean = null;
// Properties which change nothing count as none, as for the reader:
format = copy.HasAttributes || copy.HasChildren ? copy.OuterXml : string.Empty;
}
if (spans.Count > 0 && spans[^1].Format == format)
spans[^1] = (format, spans[^1].Text + text.Text);
else
spans.Add((format, text.Text));
}
return spans.Select(span => $"{span.Format}: {span.Text}").ToList();
}
///
/// What the validator finds, without where it finds it: new runs move the places of what the source already had.
///
private static List ValidationErrorsOf(PresentationDocument document) => new OpenXmlValidator(FileFormatVersions.Office2019)
.Validate(document)
.Select(error => $"{error.Part?.Uri}: {error.Description}")
.Order(StringComparer.Ordinal)
.ToList();
}