mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-04 18:07:21 +00:00
Added PowerPoint templates to the Slide Assistant (#1055)
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
1 parent
8c5cfa78b6
commit
6eb23ebbcb
136 files changed
+16535
-522
No files matched your search
@@ -0,0 +1,121 @@
|
||||
using AIStudio.Settings;
|
||||
|
||||
namespace AIStudio.Tests.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Checks how a file a configuration plugin names in a setting is found, e.g., the PowerPoint
|
||||
/// template it ships for the Slide Assistant.
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public sealed class PluginFilePathTests
|
||||
{
|
||||
private string pluginDirectory = string.Empty;
|
||||
|
||||
[SetUp]
|
||||
public void CreatePluginDirectory()
|
||||
{
|
||||
this.pluginDirectory = Path.Combine(Path.GetTempPath(), $"plugin-file-path-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(Path.Combine(this.pluginDirectory, "templates"));
|
||||
File.WriteAllBytes(Path.Combine(this.pluginDirectory, "Corporate.pptx"), [1]);
|
||||
File.WriteAllBytes(Path.Combine(this.pluginDirectory, "templates", "Corporate.potx"), [1]);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void DeletePluginDirectory() => Directory.Delete(this.pluginDirectory, true);
|
||||
|
||||
[Test]
|
||||
public void AFileOfThePluginIsFoundByItsRelativePath()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(PluginFilePath.TryResolve("Corporate.pptx", this.pluginDirectory, out var nextToPlugin, out var issue), Is.True, issue);
|
||||
Assert.That(nextToPlugin, Is.EqualTo(Path.Combine(Path.GetFullPath(this.pluginDirectory), "Corporate.pptx")));
|
||||
Assert.That(PluginFilePath.TryResolve("templates/Corporate.potx", this.pluginDirectory, out var inFolder, out issue), Is.True, issue);
|
||||
Assert.That(inFolder, Is.EqualTo(Path.Combine(Path.GetFullPath(this.pluginDirectory), "templates", "Corporate.potx")), "Both kinds of slashes work on every system.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnEmptyOrAbsolutePathStaysAsItIs()
|
||||
{
|
||||
var absolutePath = Path.Combine(Path.GetTempPath(), "somewhere", "Corporate.potx");
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(PluginFilePath.TryResolve(string.Empty, this.pluginDirectory, out var empty, out _), Is.True, "An organization may decide on no file at all.");
|
||||
Assert.That(empty, Is.Empty);
|
||||
Assert.That(PluginFilePath.TryResolve(absolutePath, this.pluginDirectory, out var absolute, out _), Is.True, "A file on a network share is the organization's business.");
|
||||
Assert.That(absolute, Is.EqualTo(absolutePath));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APathLeavingThePluginDirectoryIsRejected()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(PluginFilePath.TryResolve("../Corporate.pptx", this.pluginDirectory, out var resolved, out var issue), Is.False);
|
||||
Assert.That(issue, Does.Contain(".."));
|
||||
Assert.That(resolved, Is.Empty);
|
||||
Assert.That(PluginFilePath.TryResolve("templates/../../Corporate.pptx", this.pluginDirectory, out _, out _), Is.False);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AMissingFileIsRejected()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(PluginFilePath.TryResolve("Missing.pptx", this.pluginDirectory, out var resolved, out var issue), Is.False);
|
||||
Assert.That(issue, Does.Contain("does not exist"));
|
||||
Assert.That(resolved, Is.Empty);
|
||||
Assert.That(PluginFilePath.TryResolve("Corporate.pptx", string.Empty, out _, out _), Is.False, "Without the plugin directory, a relative path means nothing.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APathWhichHasToStayInsideThePluginLeadsToNothingElse()
|
||||
{
|
||||
var absolutePath = Path.Combine(Path.GetTempPath(), "somewhere", "Corporate.potx");
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(PluginFilePath.TryResolveInside("templates\\Corporate.potx", this.pluginDirectory, out var inFolder, out var issue), Is.True, issue);
|
||||
Assert.That(inFolder, Is.EqualTo(Path.Combine(Path.GetFullPath(this.pluginDirectory), "templates", "Corporate.potx")));
|
||||
Assert.That(PluginFilePath.TryResolveInside(absolutePath, this.pluginDirectory, out var absolute, out issue), Is.False, "Where only a file of the plugin will do, an absolute path is no way out.");
|
||||
Assert.That(issue, Does.Contain("outside"));
|
||||
Assert.That(absolute, Is.Empty);
|
||||
Assert.That(PluginFilePath.TryResolveInside(string.Empty, this.pluginDirectory, out _, out _), Is.False, "An empty path names no file.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AFolderOfThePluginIsFoundByItsRelativePath()
|
||||
{
|
||||
var absolutePath = Path.Combine(Path.GetTempPath(), "somewhere", "Pictures");
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(PluginFilePath.TryResolveDirectory("templates", this.pluginDirectory, out var inPlugin, out var issue), Is.True, issue);
|
||||
Assert.That(inPlugin, Is.EqualTo(Path.Combine(Path.GetFullPath(this.pluginDirectory), "templates")));
|
||||
Assert.That(PluginFilePath.TryResolveDirectory(string.Empty, this.pluginDirectory, out var empty, out _), Is.True, "An organization may decide on no folder at all.");
|
||||
Assert.That(empty, Is.Empty);
|
||||
Assert.That(PluginFilePath.TryResolveDirectory(absolutePath, this.pluginDirectory, out var absolute, out _), Is.True, "A folder on a network share is the organization's business.");
|
||||
Assert.That(absolute, Is.EqualTo(absolutePath));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AFolderFollowsTheRulesOfAFile()
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(PluginFilePath.TryResolveDirectory("templates/../..", this.pluginDirectory, out var resolved, out var issue), Is.False);
|
||||
Assert.That(issue, Does.Contain(".."));
|
||||
Assert.That(resolved, Is.Empty);
|
||||
Assert.That(PluginFilePath.TryResolveDirectory("Corporate.pptx", this.pluginDirectory, out _, out issue), Is.False, "A file is no folder.");
|
||||
Assert.That(issue, Does.Contain("does not exist"));
|
||||
Assert.That(PluginFilePath.TryResolve("templates", this.pluginDirectory, out _, out _), Is.False, "A folder is no file.");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.PowerPoint;
|
||||
|
||||
using Lua;
|
||||
using Lua.Standard;
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace AIStudio.Tests.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Checks the layouts for kinds of slides which an organization preselects through a configuration plugin.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The setting maps the members of an enum to texts, a shape which ManagedConfiguration reads with an
|
||||
/// overload of its own. The settings are reached through Program.SERVICE_PROVIDER, as in the app,
|
||||
/// because a managed setting writes its value and its lock there.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class PreselectedLayoutsTests
|
||||
{
|
||||
private static readonly Guid PLUGIN_ID = Guid.Parse("5c2e8a71-3f4d-4b6e-9d0a-8e1f7b3c6a52");
|
||||
|
||||
private SettingsManager settingsManager = null!;
|
||||
private ServiceProvider serviceProvider = null!;
|
||||
private IServiceProvider previousServiceProvider = null!;
|
||||
|
||||
[SetUp]
|
||||
public void CreateSettings()
|
||||
{
|
||||
// Creating the settings data registers every managed setting:
|
||||
this.settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance, null!);
|
||||
|
||||
this.previousServiceProvider = Program.SERVICE_PROVIDER;
|
||||
this.serviceProvider = new ServiceCollection().AddSingleton(this.settingsManager).BuildServiceProvider();
|
||||
Program.SERVICE_PROVIDER = this.serviceProvider;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void RestoreApplicationState()
|
||||
{
|
||||
// The managed state of a setting outlives this fixture, so we release what the plugin took over:
|
||||
ManagedConfiguration.CleanupLeftOverManagedConfigurations([], new HashSet<Guid>());
|
||||
|
||||
Program.SERVICE_PROVIDER = this.previousServiceProvider;
|
||||
this.serviceProvider.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APluginPreselectsTheLayoutsItNames()
|
||||
{
|
||||
await ProcessAsync("""{ ["CHAPTER"] = "Section Header", ["content"] = "Title and Content" }""");
|
||||
|
||||
Assert.That(this.PreselectedLayouts, Is.EquivalentTo(new Dictionary<LayoutPurpose, string>
|
||||
{
|
||||
[LayoutPurpose.CHAPTER] = "Section Header",
|
||||
[LayoutPurpose.CONTENT] = "Title and Content",
|
||||
}), "Kinds of slides count whatever their case, as everywhere in a configuration, and the names of the layouts stay as written.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks that a kind of slide which does not exist preselects nothing and leaves the rest of the table intact.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// "CHAPTERS" is the typo an administrator would make, and a newer plugin might name a kind of slide
|
||||
/// which this version does not know yet. Neither may take the valid entries down with it.
|
||||
/// </remarks>
|
||||
[Test]
|
||||
public async Task AnUnknownKindOfSlideIsPassedOver()
|
||||
{
|
||||
await ProcessAsync("""{ ["CHAPTERS"] = "Section Header", ["TITLE"] = "Title Slide", [3] = "Two Content" }""");
|
||||
|
||||
Assert.That(this.PreselectedLayouts, Is.EquivalentTo(new Dictionary<LayoutPurpose, string> { [LayoutPurpose.TITLE] = "Title Slide" }), "Only the valid kind of slide may count, and a number is no kind of slide.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ThePreselectionIsLockedWithoutUserOverride()
|
||||
{
|
||||
await ProcessAsync("""{ ["CHAPTER"] = "Section Header" }""");
|
||||
|
||||
var isManaged = ManagedConfiguration.TryGet(x => x.SlideBuilder, x => x.PreselectedLayouts, out var configMeta);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(isManaged, Is.True);
|
||||
Assert.That(configMeta.IsLocked, Is.True);
|
||||
Assert.That(configMeta.LockedByConfigPluginId, Is.EqualTo(PLUGIN_ID));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEditableDefaultKeepsTheChoicesOfTheUser()
|
||||
{
|
||||
await ProcessAsync("""{ ["CHAPTER"] = "Section Header" }""", allowUserOverride: true);
|
||||
|
||||
var isManaged = ManagedConfiguration.TryGet(x => x.SlideBuilder, x => x.PreselectedLayouts, out var configMeta);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(isManaged, Is.True);
|
||||
Assert.That(configMeta.IsLocked, Is.False, "With AllowUserOverride, users may change the layouts.");
|
||||
Assert.That(configMeta.EditableDefaultByConfigPluginId, Is.EqualTo(PLUGIN_ID));
|
||||
Assert.That(this.PreselectedLayouts, Is.EquivalentTo(new Dictionary<LayoutPurpose, string> { [LayoutPurpose.CHAPTER] = "Section Header" }), "The first time, the default of the organization applies.");
|
||||
});
|
||||
|
||||
this.PreselectedLayouts[LayoutPurpose.CHAPTER] = "Chapter";
|
||||
await ProcessAsync("""{ ["CHAPTER"] = "Section Header" }""", allowUserOverride: true);
|
||||
|
||||
Assert.That(this.PreselectedLayouts[LayoutPurpose.CHAPTER], Is.EqualTo("Chapter"), "Once the user changed the layouts, the next start of the plugin must not overwrite them.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RemovingThePluginHandsBackTheLayoutsOfTheUser()
|
||||
{
|
||||
this.PreselectedLayouts[LayoutPurpose.CONTENT] = "Content";
|
||||
await ProcessAsync("""{ ["CHAPTER"] = "Section Header" }""");
|
||||
|
||||
ManagedConfiguration.CleanupLeftOverManagedConfigurations([], new HashSet<Guid>());
|
||||
|
||||
Assert.That(this.PreselectedLayouts, Is.EquivalentTo(new Dictionary<LayoutPurpose, string> { [LayoutPurpose.CONTENT] = "Content" }), "Without its plugin, the layouts the user chose before apply again.");
|
||||
}
|
||||
|
||||
private Dictionary<LayoutPurpose, string> PreselectedLayouts => this.settingsManager.ConfigurationData.SlideBuilder.PreselectedLayouts;
|
||||
|
||||
/// <summary>
|
||||
/// Lets a configuration plugin set the preselected layouts.
|
||||
/// </summary>
|
||||
/// <param name="luaTable">The Lua table of layout names by kind of slide.</param>
|
||||
/// <param name="allowUserOverride">Whether users may change the layouts.</param>
|
||||
private static async Task ProcessAsync(string luaTable, bool allowUserOverride = false)
|
||||
{
|
||||
var state = LuaState.Create();
|
||||
state.OpenBasicLibrary();
|
||||
state.OpenTableLibrary();
|
||||
|
||||
await state.DoStringAsync($$"""
|
||||
SETTINGS = {}
|
||||
SETTINGS["DataSlideBuilder.PreselectedLayouts"] = {{luaTable}}
|
||||
SETTINGS["DataSlideBuilder.PreselectedLayouts.AllowUserOverride"] = {{(allowUserOverride ? "true" : "false")}}
|
||||
""");
|
||||
|
||||
if (!state.Environment["SETTINGS"].TryRead<LuaTable>(out var settingsTable))
|
||||
throw new InvalidOperationException("The settings of this test are not a Lua table.");
|
||||
|
||||
var processed = ManagedConfiguration.TryProcessConfiguration(x => x.SlideBuilder, x => x.PreselectedLayouts, PLUGIN_ID, settingsTable, false);
|
||||
Assert.That(processed, Is.True, "A table of layout names by kind of slide is a valid configuration of this setting.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Settings.DataModel;
|
||||
|
||||
using Lua;
|
||||
using Lua.Standard;
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace AIStudio.Tests.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Checks what an organization preselects through a configuration plugin for the maximum number of bullet points per slide.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The settings are reached through Program.SERVICE_PROVIDER, as in the app, because a managed setting
|
||||
/// writes its value and its lock there. The plugin passes the range check of DataSlideBuilder, as
|
||||
/// PluginConfiguration does.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class PreselectedMaxBulletPointsTests
|
||||
{
|
||||
private static readonly Guid PLUGIN_ID = Guid.Parse("4c81e2a6-0f3b-4d97-a52e-7b19c6d3f084");
|
||||
|
||||
private SettingsManager settingsManager = null!;
|
||||
private ServiceProvider serviceProvider = null!;
|
||||
private IServiceProvider previousServiceProvider = null!;
|
||||
|
||||
[SetUp]
|
||||
public void CreateSettings()
|
||||
{
|
||||
// Creating the settings data registers every managed setting:
|
||||
this.settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance, null!);
|
||||
|
||||
this.previousServiceProvider = Program.SERVICE_PROVIDER;
|
||||
this.serviceProvider = new ServiceCollection().AddSingleton(this.settingsManager).BuildServiceProvider();
|
||||
Program.SERVICE_PROVIDER = this.serviceProvider;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void RestoreApplicationState()
|
||||
{
|
||||
// The managed state of a setting outlives this fixture, so we release what the plugin took over:
|
||||
ManagedConfiguration.CleanupLeftOverManagedConfigurations([], new HashSet<Guid>());
|
||||
|
||||
Program.SERVICE_PROVIDER = this.previousServiceProvider;
|
||||
this.serviceProvider.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APluginPreselectsTheMaximum()
|
||||
{
|
||||
var processed = await ProcessAsync("5");
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(processed, Is.True);
|
||||
Assert.That(this.PreselectedMaximum, Is.EqualTo(5));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task NoMaximumIsAValidPreselection()
|
||||
{
|
||||
this.PreselectedMaximum = 3;
|
||||
var processed = await ProcessAsync("0");
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(processed, Is.True, "An organization may decide on no maximum at all.");
|
||||
Assert.That(this.PreselectedMaximum, Is.Zero);
|
||||
});
|
||||
}
|
||||
|
||||
[TestCase("9")]
|
||||
[TestCase("-1")]
|
||||
public async Task AMaximumOutsideTheRangeIsRejected(string configuredMaximum)
|
||||
{
|
||||
this.PreselectedMaximum = 3;
|
||||
var processed = await ProcessAsync(configuredMaximum);
|
||||
|
||||
ManagedConfiguration.TryGet(x => x.SlideBuilder, x => x.PreselectedMaxBulletPointsPerSlide, out var configMeta);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(processed, Is.False);
|
||||
Assert.That(this.PreselectedMaximum, Is.EqualTo(3), "A rejected value leaves the choice of the user untouched.");
|
||||
Assert.That(configMeta.IsLocked, Is.False, "A rejected value locks nothing.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ThePreselectionIsLockedWithoutUserOverride()
|
||||
{
|
||||
await ProcessAsync("5");
|
||||
|
||||
var isManaged = ManagedConfiguration.TryGet(x => x.SlideBuilder, x => x.PreselectedMaxBulletPointsPerSlide, out var configMeta);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(isManaged, Is.True);
|
||||
Assert.That(configMeta.IsLocked, Is.True);
|
||||
Assert.That(configMeta.LockedByConfigPluginId, Is.EqualTo(PLUGIN_ID));
|
||||
});
|
||||
}
|
||||
|
||||
private int PreselectedMaximum
|
||||
{
|
||||
get => this.settingsManager.ConfigurationData.SlideBuilder.PreselectedMaxBulletPointsPerSlide;
|
||||
set => this.settingsManager.ConfigurationData.SlideBuilder.PreselectedMaxBulletPointsPerSlide = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lets a configuration plugin preselect the maximum number of bullet points per slide.
|
||||
/// </summary>
|
||||
/// <param name="luaValue">The maximum as the plugin writes it in Lua.</param>
|
||||
/// <returns>Whether the plugin's value was taken.</returns>
|
||||
private static async Task<bool> ProcessAsync(string luaValue)
|
||||
{
|
||||
var state = LuaState.Create();
|
||||
state.OpenBasicLibrary();
|
||||
|
||||
await state.DoStringAsync($"""
|
||||
SETTINGS = {"{}"}
|
||||
SETTINGS["DataSlideBuilder.PreselectedMaxBulletPointsPerSlide"] = {luaValue}
|
||||
""");
|
||||
|
||||
if (!state.Environment["SETTINGS"].TryRead<LuaTable>(out var settingsTable))
|
||||
throw new InvalidOperationException("The settings of this test are not a Lua table.");
|
||||
|
||||
return ManagedConfiguration.TryProcessConfiguration(x => x.SlideBuilder, x => x.PreselectedMaxBulletPointsPerSlide, PLUGIN_ID, settingsTable, false, validator: DataSlideBuilder.IsValidMaxBulletPointsPerSlide);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.PowerPoint;
|
||||
|
||||
using Lua;
|
||||
using Lua.Standard;
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace AIStudio.Tests.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Checks what an organization preselects through a configuration plugin for the slides of its template.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The setting maps the numbers of the slides to the members of an enum, a shape which ManagedConfiguration
|
||||
/// reads with an overload of its own. The settings are reached through Program.SERVICE_PROVIDER, as in the
|
||||
/// app, because a managed setting writes its value and its lock there.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class PreselectedTemplateSlideUsesTests
|
||||
{
|
||||
private static readonly Guid PLUGIN_ID = Guid.Parse("9e4b2d17-6a3c-4f58-b1e0-2c7d8a5f3e91");
|
||||
|
||||
private SettingsManager settingsManager = null!;
|
||||
private ServiceProvider serviceProvider = null!;
|
||||
private IServiceProvider previousServiceProvider = null!;
|
||||
|
||||
[SetUp]
|
||||
public void CreateSettings()
|
||||
{
|
||||
// Creating the settings data registers every managed setting:
|
||||
this.settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance, null!);
|
||||
|
||||
this.previousServiceProvider = Program.SERVICE_PROVIDER;
|
||||
this.serviceProvider = new ServiceCollection().AddSingleton(this.settingsManager).BuildServiceProvider();
|
||||
Program.SERVICE_PROVIDER = this.serviceProvider;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void RestoreApplicationState()
|
||||
{
|
||||
// The managed state of a setting outlives this fixture, so we release what the plugin took over:
|
||||
ManagedConfiguration.CleanupLeftOverManagedConfigurations([], new HashSet<Guid>());
|
||||
|
||||
Program.SERVICE_PROVIDER = this.previousServiceProvider;
|
||||
this.serviceProvider.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APluginPreselectsTheSlidesItNumbers()
|
||||
{
|
||||
await ProcessAsync("""{ [1] = "KEEP", [4] = "edit", [7] = "OMIT" }""");
|
||||
|
||||
Assert.That(this.PreselectedUses, Is.EquivalentTo(new Dictionary<int, TemplateSlideUse>
|
||||
{
|
||||
[1] = TemplateSlideUse.KEEP,
|
||||
[4] = TemplateSlideUse.EDIT,
|
||||
[7] = TemplateSlideUse.OMIT,
|
||||
}), "Options count whatever their case, as everywhere in a configuration.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks that an entry which names no slide or no option preselects nothing and leaves the rest of the table intact.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Slides count from 1, so 0 and negative numbers name no slide, and a number in quotes is a text in
|
||||
/// Lua. "KEEPS" is the typo an administrator would make. None may take the valid entries down with it.
|
||||
/// </remarks>
|
||||
[Test]
|
||||
public async Task AnEntryWhichNamesNoSlideOrNoOptionIsPassedOver()
|
||||
{
|
||||
await ProcessAsync("""{ [0] = "KEEP", [-2] = "KEEP", ["3"] = "KEEP", [5] = "KEEPS", ["TITLE"] = "EDIT", [2] = "EDIT" }""");
|
||||
|
||||
Assert.That(this.PreselectedUses, Is.EquivalentTo(new Dictionary<int, TemplateSlideUse> { [2] = TemplateSlideUse.EDIT }), "Only the valid entry may count.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ThePreselectionIsLockedWithoutUserOverride()
|
||||
{
|
||||
await ProcessAsync("""{ [1] = "KEEP" }""");
|
||||
|
||||
var isManaged = ManagedConfiguration.TryGet(x => x.SlideBuilder, x => x.PreselectedTemplateSlideUses, out var configMeta);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(isManaged, Is.True);
|
||||
Assert.That(configMeta.IsLocked, Is.True);
|
||||
Assert.That(configMeta.LockedByConfigPluginId, Is.EqualTo(PLUGIN_ID));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEditableDefaultKeepsTheChoicesOfTheUser()
|
||||
{
|
||||
await ProcessAsync("""{ [1] = "KEEP" }""", allowUserOverride: true);
|
||||
|
||||
var isManaged = ManagedConfiguration.TryGet(x => x.SlideBuilder, x => x.PreselectedTemplateSlideUses, out var configMeta);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(isManaged, Is.True);
|
||||
Assert.That(configMeta.IsLocked, Is.False, "With AllowUserOverride, users may change the choices.");
|
||||
Assert.That(configMeta.EditableDefaultByConfigPluginId, Is.EqualTo(PLUGIN_ID));
|
||||
Assert.That(this.PreselectedUses, Is.EquivalentTo(new Dictionary<int, TemplateSlideUse> { [1] = TemplateSlideUse.KEEP }), "The first time, the default of the organization applies.");
|
||||
});
|
||||
|
||||
this.PreselectedUses[1] = TemplateSlideUse.EDIT;
|
||||
await ProcessAsync("""{ [1] = "KEEP" }""", allowUserOverride: true);
|
||||
|
||||
Assert.That(this.PreselectedUses[1], Is.EqualTo(TemplateSlideUse.EDIT), "Once the user changed the choices, the next start of the plugin must not overwrite them.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RemovingThePluginHandsBackTheChoicesOfTheUser()
|
||||
{
|
||||
this.PreselectedUses[3] = TemplateSlideUse.EDIT;
|
||||
await ProcessAsync("""{ [1] = "KEEP" }""");
|
||||
|
||||
ManagedConfiguration.CleanupLeftOverManagedConfigurations([], new HashSet<Guid>());
|
||||
|
||||
Assert.That(this.PreselectedUses, Is.EquivalentTo(new Dictionary<int, TemplateSlideUse> { [3] = TemplateSlideUse.EDIT }), "Without its plugin, the choices the user made before apply again.");
|
||||
}
|
||||
|
||||
private Dictionary<int, TemplateSlideUse> PreselectedUses => this.settingsManager.ConfigurationData.SlideBuilder.PreselectedTemplateSlideUses;
|
||||
|
||||
/// <summary>
|
||||
/// Lets a configuration plugin preselect what the presentation does with the slides of the template.
|
||||
/// </summary>
|
||||
/// <param name="luaTable">The Lua table of options by number of slide.</param>
|
||||
/// <param name="allowUserOverride">Whether users may change the choices.</param>
|
||||
private static async Task ProcessAsync(string luaTable, bool allowUserOverride = false)
|
||||
{
|
||||
var state = LuaState.Create();
|
||||
state.OpenBasicLibrary();
|
||||
state.OpenTableLibrary();
|
||||
|
||||
await state.DoStringAsync($$"""
|
||||
SETTINGS = {}
|
||||
SETTINGS["DataSlideBuilder.PreselectedTemplateSlideUses"] = {{luaTable}}
|
||||
SETTINGS["DataSlideBuilder.PreselectedTemplateSlideUses.AllowUserOverride"] = {{(allowUserOverride ? "true" : "false")}}
|
||||
""");
|
||||
|
||||
if (!state.Environment["SETTINGS"].TryRead<LuaTable>(out var settingsTable))
|
||||
throw new InvalidOperationException("The settings of this test are not a Lua table.");
|
||||
|
||||
var processed = ManagedConfiguration.TryProcessConfiguration(x => x.SlideBuilder, x => x.PreselectedTemplateSlideUses, PLUGIN_ID, settingsTable, false);
|
||||
Assert.That(processed, Is.True, "A table of options by number of slide is a valid configuration of this setting.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user