mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-03 03:54:24 +00:00
211 lines
9.9 KiB
C#
211 lines
9.9 KiB
C#
using System.IO.Compression;
|
|
|
|
using AIStudio.Tests.Tools.PowerPoint;
|
|
using AIStudio.Tools.OpenXml;
|
|
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
using A = DocumentFormat.OpenXml.Drawing;
|
|
using P = DocumentFormat.OpenXml.Presentation;
|
|
|
|
namespace AIStudio.Tests.Tools.OpenXml;
|
|
|
|
/// <summary>
|
|
/// Checks that the skeleton of an Office document holds only its XML, and that a new document takes each entry from
|
|
/// where it lies: the XML from the skeleton or from a DOM, the pictures and videos from the source, and new parts from
|
|
/// their files.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Most tests build their documents as plain archives, since the skeleton works on entries and knows nothing of their
|
|
/// meaning; a writer which edits the skeleton is played by the update mode of ZipArchive.
|
|
/// </remarks>
|
|
[TestFixture]
|
|
public sealed class PackageSkeletonTests
|
|
{
|
|
private const string CONTENT_TYPES = "[Content_Types].xml";
|
|
private const string RELATIONSHIPS = "_rels/.rels";
|
|
private const string SLIDE = "ppt/slides/slide1.xml";
|
|
private const string PICTURE = "ppt/media/image1.png";
|
|
private const string VIDEO = "ppt/media/video1.mp4";
|
|
private const string THUMBNAIL = "docProps/thumbnail.jpeg";
|
|
|
|
private static readonly string[] ENTRY_NAMES = [CONTENT_TYPES, RELATIONSHIPS, SLIDE, PICTURE, VIDEO, THUMBNAIL];
|
|
|
|
private string testFolder = string.Empty;
|
|
private string sourcePath = string.Empty;
|
|
private string documentPath = string.Empty;
|
|
|
|
[SetUp]
|
|
public void CreateSource()
|
|
{
|
|
this.testFolder = Directory.CreateDirectory(Path.Join(Path.GetTempPath(), "mindwork-ai-studio-tests", Guid.NewGuid().ToString("N"))).FullName;
|
|
this.sourcePath = Path.Join(this.testFolder, "source.pptx");
|
|
this.documentPath = Path.Join(this.testFolder, "document.pptx");
|
|
WriteArchive(this.sourcePath, VideoOf(1));
|
|
}
|
|
|
|
[TearDown]
|
|
public void RemoveFolder() => Directory.Delete(this.testFolder, true);
|
|
|
|
[Test]
|
|
public void TheSkeletonKeepsOnlyTheXml()
|
|
{
|
|
var stagedBefore = StagingFolder.Files();
|
|
using (var skeleton = PackageSkeleton.Create(this.sourcePath, ".pptx", NullLogger.Instance, CancellationToken.None))
|
|
{
|
|
using var skeletonFile = skeleton.OpenRead();
|
|
using var archive = new ZipArchive(skeletonFile, ZipArchiveMode.Read);
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(archive.Entries.Select(entry => entry.FullName), Is.EqualTo(ENTRY_NAMES), "Every entry keeps its place.");
|
|
Assert.That(ContentOf(archive.GetEntry(SLIDE)!), Is.EqualTo(ContentOf(this.sourcePath, SLIDE)), "The XML keeps its content.");
|
|
Assert.That(ContentOf(archive.GetEntry(RELATIONSHIPS)!), Is.EqualTo(ContentOf(this.sourcePath, RELATIONSHIPS)), "The relationships keep their content.");
|
|
Assert.That(archive.Entries.Where(entry => !PackageSkeleton.HoldsXml(entry)).Select(entry => entry.Length), Has.Exactly(3).Items.And.All.Zero, "Pictures and videos stay empty.");
|
|
}
|
|
}
|
|
|
|
Assert.That(StagingFolder.Files().Except(stagedBefore), Is.Empty, "Disposing the skeleton removes it.");
|
|
}
|
|
|
|
[Test]
|
|
public void TheDocumentTakesEachEntryFromWhereItLies()
|
|
{
|
|
var newPicture = Path.Join(this.testFolder, "new.png");
|
|
File.WriteAllBytes(newPicture, [7, 8, 9]);
|
|
|
|
bool isWritten;
|
|
using (var skeleton = PackageSkeleton.Create(this.sourcePath, ".pptx", NullLogger.Instance, CancellationToken.None))
|
|
{
|
|
// The writer changes a slide, removes the thumbnail, and adds a picture and a slide:
|
|
using (var skeletonFile = skeleton.OpenForEditing())
|
|
using (var archive = new ZipArchive(skeletonFile, ZipArchiveMode.Update))
|
|
{
|
|
Overwrite(archive, SLIDE, "<slide>changed</slide>"u8.ToArray());
|
|
archive.GetEntry(THUMBNAIL)!.Delete();
|
|
archive.CreateEntry("ppt/media/image2.png");
|
|
Add(archive, "ppt/slides/slide2.xml", "<slide>new</slide>"u8.ToArray(), CompressionLevel.Optimal);
|
|
}
|
|
|
|
isWritten = skeleton.TryWriteDocument([], new Dictionary<string, string> { ["ppt/media/image2.png"] = newPicture }, this.documentPath, CancellationToken.None);
|
|
}
|
|
|
|
using var document = ZipFile.OpenRead(this.documentPath);
|
|
var video = document.GetEntry(VIDEO)!;
|
|
using (Assert.EnterMultipleScope())
|
|
{
|
|
Assert.That(isWritten, Is.True);
|
|
Assert.That(document.Entries.Select(entry => entry.FullName), Is.EqualTo(new[] { CONTENT_TYPES, RELATIONSHIPS, SLIDE, PICTURE, VIDEO, "ppt/media/image2.png", "ppt/slides/slide2.xml" }), "What the writer removed stays out, and what it added comes along.");
|
|
Assert.That(ContentOf(document.GetEntry(SLIDE)!), Is.EqualTo("<slide>changed</slide>"u8.ToArray()), "The XML comes from the skeleton.");
|
|
Assert.That(ContentOf(document.GetEntry(PICTURE)!), Is.EqualTo(ContentOf(this.sourcePath, PICTURE)), "A picture of the source comes from the source.");
|
|
Assert.That(ContentOf(video), Is.EqualTo(VideoOf(1)), "A video of the source comes from the source.");
|
|
Assert.That(video.CompressedLength, Is.EqualTo(video.Length), "A stored video stays stored.");
|
|
Assert.That(ContentOf(document.GetEntry("ppt/media/image2.png")!), Is.EqualTo(new byte[] { 7, 8, 9 }), "A new picture comes from its file.");
|
|
Assert.That(ContentOf(document.GetEntry("ppt/slides/slide2.xml")!), Is.EqualTo("<slide>new</slide>"u8.ToArray()));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void ANewPartUnderTheNameOfARemovedOneComesFromItsFile()
|
|
{
|
|
var newPicture = Path.Join(this.testFolder, "new.png");
|
|
File.WriteAllBytes(newPicture, [7, 8, 9]);
|
|
|
|
using (var skeleton = PackageSkeleton.Create(this.sourcePath, ".pptx", NullLogger.Instance, CancellationToken.None))
|
|
{
|
|
// The SDK may give a new part the name of a part it removed:
|
|
using (var skeletonFile = skeleton.OpenForEditing())
|
|
using (var archive = new ZipArchive(skeletonFile, ZipArchiveMode.Update))
|
|
{
|
|
archive.GetEntry(PICTURE)!.Delete();
|
|
archive.CreateEntry(PICTURE);
|
|
}
|
|
|
|
skeleton.TryWriteDocument([], new Dictionary<string, string> { [PICTURE] = newPicture }, this.documentPath, CancellationToken.None);
|
|
}
|
|
|
|
using var document = ZipFile.OpenRead(this.documentPath);
|
|
Assert.That(ContentOf(document.GetEntry(PICTURE)!), Is.EqualTo(new byte[] { 7, 8, 9 }));
|
|
}
|
|
|
|
[Test]
|
|
public void AChangedPartComesFromItsDom()
|
|
{
|
|
using (var template = TestTemplate.Create(new TestTemplateOptions(Slides: [new TestSlide(0, (TestPlaceholder.Title(), "News"))]), new TestLayout("Title", TestPlaceholder.Title())))
|
|
File.WriteAllBytes(this.sourcePath, template.ToArray());
|
|
|
|
using (var skeleton = PackageSkeleton.Create(this.sourcePath, ".pptx", NullLogger.Instance, CancellationToken.None))
|
|
{
|
|
using var skeletonFile = skeleton.OpenRead();
|
|
using var presentation = PresentationDocument.Open(skeletonFile, false);
|
|
var slidePart = presentation.PresentationPart!.SlideParts.Single();
|
|
slidePart.Slide!.Descendants<A.Text>().Single().Text = "Breaking news";
|
|
|
|
skeleton.TryWriteDocument([slidePart], new Dictionary<string, string>(), this.documentPath, CancellationToken.None);
|
|
}
|
|
|
|
using var document = PresentationDocument.Open(this.documentPath, false);
|
|
Assert.That(document.PresentationPart!.SlideParts.Single().Slide!.Descendants<P.Shape>().Single().InnerText, Is.EqualTo("Breaking news"));
|
|
}
|
|
|
|
[Test]
|
|
public void AChangedSourceWritesNoDocument()
|
|
{
|
|
using var skeleton = PackageSkeleton.Create(this.sourcePath, ".pptx", NullLogger.Instance, CancellationToken.None);
|
|
|
|
// PowerPoint saved the source with another video meanwhile:
|
|
File.Delete(this.sourcePath);
|
|
WriteArchive(this.sourcePath, VideoOf(2));
|
|
|
|
Assert.That(skeleton.TryWriteDocument([], new Dictionary<string, string>(), this.documentPath, CancellationToken.None), Is.False);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a document as PowerPoint stores it: the XML deflated, the picture deflated, and the video stored.
|
|
/// </summary>
|
|
private static void WriteArchive(string path, byte[] video)
|
|
{
|
|
using var archive = ZipFile.Open(path, ZipArchiveMode.Create);
|
|
Add(archive, CONTENT_TYPES, "<Types />"u8.ToArray(), CompressionLevel.Optimal);
|
|
Add(archive, RELATIONSHIPS, "<Relationships />"u8.ToArray(), CompressionLevel.Optimal);
|
|
Add(archive, SLIDE, "<slide>original</slide>"u8.ToArray(), CompressionLevel.Optimal);
|
|
Add(archive, PICTURE, [.. Enumerable.Repeat((byte)1, 4096)], CompressionLevel.Optimal);
|
|
Add(archive, VIDEO, video, CompressionLevel.NoCompression);
|
|
Add(archive, THUMBNAIL, [4, 5, 6], CompressionLevel.Optimal);
|
|
}
|
|
|
|
private static void Add(ZipArchive archive, string name, byte[] content, CompressionLevel compressionLevel)
|
|
{
|
|
using var stream = archive.CreateEntry(name, compressionLevel).Open();
|
|
stream.Write(content);
|
|
}
|
|
|
|
private static void Overwrite(ZipArchive archive, string name, byte[] content)
|
|
{
|
|
using var stream = archive.GetEntry(name)!.Open();
|
|
stream.SetLength(0);
|
|
stream.Write(content);
|
|
}
|
|
|
|
private static byte[] VideoOf(int seed)
|
|
{
|
|
var video = new byte[64 * 1024];
|
|
new Random(seed).NextBytes(video);
|
|
return video;
|
|
}
|
|
|
|
private static byte[] ContentOf(string path, string name)
|
|
{
|
|
using var archive = ZipFile.OpenRead(path);
|
|
return ContentOf(archive.GetEntry(name)!);
|
|
}
|
|
|
|
private static byte[] ContentOf(ZipArchiveEntry entry)
|
|
{
|
|
using var stream = entry.Open();
|
|
using var content = new MemoryStream();
|
|
stream.CopyTo(content);
|
|
return content.ToArray();
|
|
}
|
|
} |