Improved the assistants page with categories for assistant plugins (#1211)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-20 11:47:59 +01:00
1 parent ef059ffd15
commit 1db0f85898
74 files changed
+3392 -380

No files matched your search

@@ -0,0 +1,61 @@
using AIStudio.Tools;
using AIStudio.Tools.PluginSystem;
using ComponentKind = AIStudio.Tools.Components;
namespace AIStudio.Tests.Assistants;
/// <summary>
/// Checks that every built-in assistant states its category.
/// </summary>
/// <remarks>
/// The assistants page and everything which offers assistants elsewhere decide by the category, e.g.,
/// when an organization disables one. An assistant without a category would escape such a decision, so a
/// new assistant has to get one, see Components.Category.
/// </remarks>
[TestFixture]
public sealed class AssistantCategoryOfComponentsTests
{
/// <summary>
/// The components which are no assistant of their own.
/// </summary>
private static readonly ComponentKind[] NO_ASSISTANTS =
[
ComponentKind.NONE,
ComponentKind.CHAT,
ComponentKind.DYNAMIC_ASSISTANT,
ComponentKind.WRITER,
ComponentKind.APP_SETTINGS,
ComponentKind.AGENT_TEXT_CONTENT_CLEANER,
ComponentKind.AGENT_DATA_SOURCE_SELECTION,
ComponentKind.AGENT_RETRIEVAL_CONTEXT_VALIDATION,
ComponentKind.AGENT_ASSISTANT_PLUGIN_AUDIT,
];
[Test]
public void EveryAssistantHasACategory()
{
var withoutCategory = Enum.GetValues<ComponentKind>()
.Where(component => !NO_ASSISTANTS.Contains(component))
.Where(component => component.Category is PluginCategory.NONE)
.ToList();
Assert.That(withoutCategory, Is.Empty, "Give each of these assistants its category in Components.Category, or list it here when it is no assistant.");
}
[Test]
public void NoAssistantUsesTheCategoryOfConfigurationPlugins()
{
var withCore = Enum.GetValues<ComponentKind>().Where(component => component.Category is PluginCategory.CORE or PluginCategory.CUSTOM).ToList();
Assert.That(withCore, Is.Empty, "Built-in assistants use COMMON instead of CORE, and never a custom category.");
}
[Test]
public void TheComponentsWhichAreNoAssistantHaveNoCategory()
{
var withCategory = NO_ASSISTANTS.Where(component => component.Category is not PluginCategory.NONE).ToList();
Assert.That(withCategory, Is.Empty);
}
}
@@ -0,0 +1,106 @@
using AIStudio.Settings.DataModel;
using AIStudio.Tools;
using AIStudio.Tools.PluginSystem;
namespace AIStudio.Tests.Assistants;
/// <summary>
/// Checks how the categories users collapse or hide on the assistants page affect their assistants.
/// </summary>
/// <remarks>
/// Collapsing a category is about a tidy page, so its assistants stay in the "Send to ..." menu by default.
/// Hiding one says that its assistants do not matter, so they disappear there as well by default. Two
/// options change both, and a hidden category follows its option even when it is collapsed, too.
/// </remarks>
[TestFixture]
public sealed class AssistantCategoryPageSettingsTests
{
private static readonly AssistantCategoryKey BUSINESS = new(PluginCategory.BUSINESS);
private static readonly AssistantCategoryKey SOFTWARE_DEVELOPMENT = new(PluginCategory.SOFTWARE_DEVELOPMENT);
[Test]
public void TheDefaultsKeepCollapsedCategoriesAndDropHiddenOnes()
{
var page = new DataAssistantsPage();
page.SetCategoryCollapsed(BUSINESS, true);
page.SetCategoryHidden(SOFTWARE_DEVELOPMENT, true);
using (Assert.EnterMultipleScope())
{
Assert.That(page.IsCategoryOffering(BUSINESS), Is.True);
Assert.That(page.IsCategoryOffering(SOFTWARE_DEVELOPMENT), Is.False);
Assert.That(page.IsCategoryOffering(new AssistantCategoryKey(PluginCategory.EDUCATION)), Is.True, "A category the user left alone offers its assistants.");
}
}
[Test]
public void TheOptionsTurnBothAround()
{
var page = new DataAssistantsPage
{
CollapsedCategoryAssistants = AssistantOfferBehavior.TREAT_AS_ABSENT,
HiddenCategoryAssistants = AssistantOfferBehavior.KEEP_OFFERING,
};
page.SetCategoryCollapsed(BUSINESS, true);
page.SetCategoryHidden(SOFTWARE_DEVELOPMENT, true);
using (Assert.EnterMultipleScope())
{
Assert.That(page.IsCategoryOffering(BUSINESS), Is.False);
Assert.That(page.IsCategoryOffering(SOFTWARE_DEVELOPMENT), Is.True);
}
}
[Test]
public void AHiddenCategoryFollowsTheOptionForHiddenOnesAlsoWhenItIsCollapsed()
{
var page = new DataAssistantsPage();
page.SetCategoryCollapsed(BUSINESS, true);
page.SetCategoryHidden(BUSINESS, true);
Assert.That(page.IsCategoryOffering(BUSINESS), Is.False);
}
[Test]
public void TheDefaultsOfTheSettingsAreTheOnesOfThePlan()
{
var page = new Data().AssistantsPage;
using (Assert.EnterMultipleScope())
{
Assert.That(page.CollapsedCategoryAssistants, Is.EqualTo(AssistantOfferBehavior.KEEP_OFFERING));
Assert.That(page.HiddenCategoryAssistants, Is.EqualTo(AssistantOfferBehavior.TREAT_AS_ABSENT));
}
}
[Test]
public void CoreIsStoredAsCommon()
{
var page = new DataAssistantsPage();
page.SetCategoryHidden(new AssistantCategoryKey(PluginCategory.CORE), true);
using (Assert.EnterMultipleScope())
{
Assert.That(page.HiddenCategories, Is.EquivalentTo(new[] { PluginCategory.COMMON }));
Assert.That(page.IsCategoryHidden(new AssistantCategoryKey(PluginCategory.COMMON)), Is.True);
}
}
[Test]
public void CustomCategoriesAreComparedWithoutRegardToCase()
{
var page = new DataAssistantsPage();
page.SetCategoryCollapsed(new AssistantCategoryKey(PluginCategory.CUSTOM, "Space Travel"), true);
page.SetCategoryCollapsed(new AssistantCategoryKey(PluginCategory.CUSTOM, "space travel"), true);
var isCollapsedInAnotherCase = page.IsCategoryCollapsed(new AssistantCategoryKey(PluginCategory.CUSTOM, "SPACE TRAVEL"));
page.SetCategoryCollapsed(new AssistantCategoryKey(PluginCategory.CUSTOM, "SPACE TRAVEL"), false);
using (Assert.EnterMultipleScope())
{
Assert.That(isCollapsedInAnotherCase, Is.True);
Assert.That(page.CollapsedCustomCategories, Is.Empty, "Expanding a category removes every spelling of it.");
}
}
}
@@ -0,0 +1,176 @@
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
using AIStudio.Tools;
using AIStudio.Tools.AssistantLaunch;
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.PluginSystem.Assistants;
using Microsoft.Extensions.Logging.Abstractions;
using ComponentKind = AIStudio.Tools.Components;
namespace AIStudio.Tests.Assistants;
/// <summary>
/// Checks how the categories your organization disabled keep assistants away.
/// </summary>
/// <remarks>
/// Organizations disable categories such as entertainment on their devices. A built-in assistant has one
/// category, a plugin may have several and stays available while one of them is free. Plugins the
/// organization deployed itself are exempt, but only with a proof: a plugin from the internet may declare
/// whatever it likes about itself.
/// </remarks>
[TestFixture]
public sealed class AssistantCategoryRulesTests
{
private static readonly Guid PLUGIN_ID = Guid.Parse("4c8e2a6f-9d1b-4f3a-8e5c-7b2d4f6a8c0e");
private string pluginDirectory = string.Empty;
[SetUp]
public void CreatePluginDirectory()
{
this.pluginDirectory = Path.Combine(Path.GetTempPath(), $"assistant-category-rules-{Guid.NewGuid():N}");
Directory.CreateDirectory(this.pluginDirectory);
}
[TearDown]
public void DeletePluginDirectory()
{
if (Directory.Exists(this.pluginDirectory))
Directory.Delete(this.pluginDirectory, recursive: true);
}
[Test]
public void APluginOfADisabledCategoryIsDisabled()
{
var app = new DataApp { DisabledAssistantCategories = [PluginCategory.ENTERTAINMENT] };
Assert.That(app.DisablesAssistantPlugin(new FakeAssistantPlugin("Quiz", [PluginCategory.ENTERTAINMENT]), isDeployedByOrganization: false), Is.True);
}
[Test]
public void APluginStaysAvailableWhileOneOfItsCategoriesIsFree()
{
var app = new DataApp { DisabledAssistantCategories = [PluginCategory.ENTERTAINMENT] };
Assert.That(app.DisablesAssistantPlugin(new FakeAssistantPlugin("Team quiz", [PluginCategory.ENTERTAINMENT, PluginCategory.BUSINESS]), isDeployedByOrganization: false), Is.False);
}
[Test]
public void APluginYourOrganizationDeployedIsNeverDisabled()
{
var app = new DataApp { DisabledAssistantCategories = [PluginCategory.ENTERTAINMENT] };
Assert.That(app.DisablesAssistantPlugin(new FakeAssistantPlugin("Quiz", [PluginCategory.ENTERTAINMENT]), isDeployedByOrganization: true), Is.False);
}
[TestCase(PluginCategory.CORE, PluginCategory.COMMON)]
[TestCase(PluginCategory.COMMON, PluginCategory.CORE)]
public void CoreAndCommonAreOneCategory(PluginCategory disabledCategory, PluginCategory categoryOfThePlugin)
{
var app = new DataApp { DisabledAssistantCategories = [disabledCategory] };
Assert.That(app.DisablesAssistantPlugin(new FakeAssistantPlugin("Letter writer", [categoryOfThePlugin]), isDeployedByOrganization: false), Is.True, "Both share the heading \"General\", so disabling one disables the other.");
}
[Test]
public void CustomCategoriesAreComparedWithoutRegardToCase()
{
var app = new DataApp { DisabledCustomAssistantCategories = ["fun & games "] };
using (Assert.EnterMultipleScope())
{
Assert.That(app.DisablesAssistantPlugin(new FakeAssistantPlugin("Riddles", [PluginCategory.CUSTOM], ["Fun & Games"]), isDeployedByOrganization: false), Is.True);
Assert.That(app.DisablesAssistantPlugin(new FakeAssistantPlugin("Orbit planner", [PluginCategory.CUSTOM], ["Space Travel"]), isDeployedByOrganization: false), Is.False);
}
}
[Test]
public void DisablingCustomDisablesEveryCustomCategory()
{
var app = new DataApp { DisabledAssistantCategories = [PluginCategory.CUSTOM] };
using (Assert.EnterMultipleScope())
{
Assert.That(app.DisablesAssistantPlugin(new FakeAssistantPlugin("Riddles", [PluginCategory.CUSTOM], ["Fun & Games", "Space Travel"]), isDeployedByOrganization: false), Is.True);
Assert.That(app.DisablesAssistantPlugin(new FakeAssistantPlugin("Orbit planner", [PluginCategory.CUSTOM, PluginCategory.SCIENCE], ["Space Travel"]), isDeployedByOrganization: false), Is.False);
}
}
[Test]
public void ABuiltInAssistantOfADisabledCategoryIsGoneEverywhere()
{
var settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance, null!);
settingsManager.ConfigurationData.App.DisabledAssistantCategories.Add(PluginCategory.SOFTWARE_DEVELOPMENT);
var destinations = AssistantLaunchService.SelectDestinations(ComponentKind.CHAT, component => settingsManager.IsAssistantVisible(component, withLogging: false));
using (Assert.EnterMultipleScope())
{
Assert.That(settingsManager.IsAssistantVisible(ComponentKind.CODING_ASSISTANT, withLogging: false), Is.False);
Assert.That(settingsManager.IsAssistantVisible(ComponentKind.ERI_ASSISTANT, withLogging: false), Is.False);
Assert.That(settingsManager.IsAssistantVisible(ComponentKind.EMAIL_ASSISTANT, withLogging: false), Is.True);
Assert.That(destinations, Does.Not.Contain(ComponentKind.CODING_ASSISTANT), "An assistant of a disabled category must not be offered in \"Send to …\" either.");
Assert.That(destinations, Does.Contain(ComponentKind.EMAIL_ASSISTANT));
}
}
[TestCase(true, true, ExpectedResult = true, TestName = "TheDeclarationWithAnApprovalOfItsCodeIsAProof")]
[TestCase(true, false, ExpectedResult = false, TestName = "TheDeclarationAloneIsNoProof")]
[TestCase(false, true, ExpectedResult = false, TestName = "AnApprovalWithoutTheDeclarationIsNoProof")]
public async Task<bool> APluginOutsideTheDirectoryOfYourOrganizationNeedsAProof(bool declaresDeployment, bool isApproved)
{
using var plugin = await this.LoadLauncherAsync([PluginCategory.ENTERTAINMENT], declaresDeployment);
DataAssistantPluginEnterpriseApproval[] approvals = isApproved ? [ApprovalOf(plugin)] : [];
return PluginFactory.IsAssistantDeployedByOrganization(plugin, approvals);
}
[Test]
public async Task APluginYourOrganizationRolledOutStaysAvailableInADisabledCategory()
{
var settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance, null!);
settingsManager.ConfigurationData.App.DisabledAssistantCategories.Add(PluginCategory.ENTERTAINMENT);
using var plugin = await this.LoadLauncherAsync([PluginCategory.ENTERTAINMENT], declaresDeployment: true);
var isDisabledWithoutApproval = settingsManager.IsDisabledByItsCategories(plugin);
settingsManager.ConfigurationData.AssistantPluginAudit.EnterpriseApprovedPlugins.Add(ApprovalOf(plugin));
var isDisabledWithApproval = settingsManager.IsDisabledByItsCategories(plugin);
using (Assert.EnterMultipleScope())
{
Assert.That(isDisabledWithoutApproval, Is.True);
Assert.That(isDisabledWithApproval, Is.False);
}
}
/// <summary>
/// Approves exactly the code of a plugin, spelled in lowercase, as an organization may write the hash.
/// </summary>
private static DataAssistantPluginEnterpriseApproval ApprovalOf(PluginAssistants plugin) => new() { PluginHash = plugin.ComputeAuditHash().ToLowerInvariant() };
/// <summary>
/// Writes a direct chat launcher into the plugin directory of the test and loads it from there, so that
/// its code has a hash.
/// </summary>
private async Task<PluginAssistants> LoadLauncherAsync(PluginCategory[] categories, bool declaresDeployment)
{
var metadata = new DirectChatLauncherPluginMetadata(PLUGIN_ID, "1.0.0", null, ["MindWork AI"], "MindWork AI", "https://github.com/MindWorkAI/AI-Studio",
categories, [], [PluginTargetGroup.EVERYONE], IsMaintained: true, DeprecationMessage: string.Empty, IsAssistantBuilderGenerated: false);
var definition = new DirectChatLauncherDefinition("Launcher for the tests", "Launcher for the tests", "Opens a disappearing chat.", new AssistantChatLaunchConfiguration(string.Empty, null, null, null, null, null));
var lua = DirectChatLauncherLuaWriter.Write(metadata, definition);
if (declaresDeployment)
lua = lua.Replace("DEPLOYED_USING_CONFIG_SERVER = false", "DEPLOYED_USING_CONFIG_SERVER = true", StringComparison.Ordinal);
await File.WriteAllTextAsync(Path.Combine(this.pluginDirectory, "plugin.lua"), lua);
var plugin = await PluginFactory.Load(this.pluginDirectory, lua);
Assert.That(plugin, Is.InstanceOf<PluginAssistants>(), string.Join(" ", plugin.Issues));
var assistantPlugin = (PluginAssistants)plugin;
assistantPlugin.PluginPath = this.pluginDirectory;
Assert.That(assistantPlugin.IsManagedByConfigServer, Is.EqualTo(declaresDeployment), "The test needs the declaration it asked for.");
return assistantPlugin;
}
}
@@ -0,0 +1,135 @@
using AIStudio.Settings.DataModel;
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.PluginSystem.Assistants;
namespace AIStudio.Tests.Assistants;
/// <summary>
/// Checks how the assistants page groups the assistant plugins by their categories.
/// </summary>
/// <remarks>
/// Every assistant plugin appears under the categories it names, next to the built-in assistants of
/// the same category. The built-in sections lead the page in the order users know, the categories only
/// plugins name follow, and "AI Studio Development" closes the page.
/// </remarks>
[TestFixture]
public sealed class AssistantPageSectionsTests
{
[Test]
public void APluginWithCoreAppearsUnderGeneral()
{
var sections = Build(new FakeAssistantPlugin("Old builder assistant", [PluginCategory.CORE]));
Assert.That(sections.Select(section => section.Category), Is.EqualTo(new[] { new AssistantCategoryKey(PluginCategory.COMMON) }));
}
[Test]
public void APluginAppearsUnderEachOfItsCategories()
{
var plugin = new FakeAssistantPlugin("Market research", [PluginCategory.BUSINESS, PluginCategory.SCIENCE]);
var sections = Build(plugin);
using (Assert.EnterMultipleScope())
{
Assert.That(sections.PluginsOf(new AssistantCategoryKey(PluginCategory.BUSINESS)), Is.EqualTo(new[] { plugin }));
Assert.That(sections.PluginsOf(new AssistantCategoryKey(PluginCategory.SCIENCE)), Is.EqualTo(new[] { plugin }));
}
}
[Test]
public void APluginAppearsOncePerSection()
{
var plugin = new FakeAssistantPlugin("Everyday helper", [PluginCategory.CORE, PluginCategory.COMMON]);
var sections = Build(plugin);
using (Assert.EnterMultipleScope())
{
Assert.That(sections, Has.Count.EqualTo(1));
Assert.That(sections[0].Assistants, Is.EqualTo(new[] { plugin }), "CORE and COMMON share the section \"General\".");
}
}
[Test]
public void CustomCategoriesWhichDifferOnlyInTheirCaseShareOneSection()
{
var first = new FakeAssistantPlugin("Orbit planner", [PluginCategory.CUSTOM], ["Space Travel"]);
var second = new FakeAssistantPlugin("Launch checklist", [PluginCategory.CUSTOM], ["space travel"]);
var sections = Build(first, second);
using (Assert.EnterMultipleScope())
{
Assert.That(sections, Has.Count.EqualTo(1));
Assert.That(sections[0].Category.Name, Is.EqualTo("Space Travel"), "The section keeps the spelling it met first.");
Assert.That(sections[0].Assistants, Is.EqualTo(new[] { second, first }), "The plugins of a section are sorted by title.");
}
}
[Test]
public void TheSectionsFollowTheOrderOfThePage()
{
var sections = Build(
new FakeAssistantPlugin("Log helper", [PluginCategory.AI_STUDIO_DEVELOPMENT]),
new FakeAssistantPlugin("Orbit planner", [PluginCategory.CUSTOM], ["Space Travel"]),
new FakeAssistantPlugin("Quiz", [PluginCategory.ENTERTAINMENT]),
new FakeAssistantPlugin("Code reviewer", [PluginCategory.SOFTWARE_DEVELOPMENT]),
new FakeAssistantPlugin("Lab notes", [PluginCategory.SCIENCE]),
new FakeAssistantPlugin("Flash cards", [PluginCategory.EDUCATION]),
new FakeAssistantPlugin("Invoice checker", [PluginCategory.BUSINESS]),
new FakeAssistantPlugin("Letter writer", [PluginCategory.COMMON]),
new FakeAssistantPlugin("Deep space notes", [PluginCategory.CUSTOM], ["Deep Space"]));
var expected = new[]
{
new AssistantCategoryKey(PluginCategory.COMMON),
new AssistantCategoryKey(PluginCategory.BUSINESS),
new AssistantCategoryKey(PluginCategory.EDUCATION),
new AssistantCategoryKey(PluginCategory.SOFTWARE_DEVELOPMENT),
new AssistantCategoryKey(PluginCategory.ENTERTAINMENT),
new AssistantCategoryKey(PluginCategory.SCIENCE),
new AssistantCategoryKey(PluginCategory.CUSTOM, "Deep Space"),
new AssistantCategoryKey(PluginCategory.CUSTOM, "Space Travel"),
new AssistantCategoryKey(PluginCategory.AI_STUDIO_DEVELOPMENT),
};
Assert.That(sections.Select(section => section.Category), Is.EqualTo(expected));
}
[Test]
public void OnlyTheSectionsOfBuiltInAssistantsCountAsBuiltIn()
{
using (Assert.EnterMultipleScope())
{
Assert.That(AssistantPageSections.IsBuiltIn(new AssistantCategoryKey(PluginCategory.CORE)), Is.True);
Assert.That(AssistantPageSections.IsBuiltIn(new AssistantCategoryKey(PluginCategory.AI_STUDIO_DEVELOPMENT)), Is.True);
Assert.That(AssistantPageSections.IsBuiltIn(new AssistantCategoryKey(PluginCategory.SCIENCE)), Is.False);
Assert.That(AssistantPageSections.IsBuiltIn(new AssistantCategoryKey(PluginCategory.CUSTOM, "General")), Is.False, "A custom category never takes the place of a category of AI Studio, whatever its name.");
}
}
[Test]
public void TheSettingsOfferTheShownAndTheHiddenCategoriesInTheOrderOfThePage()
{
var app = new DataApp { DisabledAssistantCategories = [PluginCategory.ENTERTAINMENT] };
var page = new DataAssistantsPage
{
HiddenCategories = [PluginCategory.SCIENCE, PluginCategory.ENTERTAINMENT, PluginCategory.BUSINESS],
HiddenCustomCategories = ["space travel"],
};
var categories = AssistantPageSections.SelectForSettings(
[new AssistantCategoryKey(PluginCategory.AI_STUDIO_DEVELOPMENT), new AssistantCategoryKey(PluginCategory.CORE), new AssistantCategoryKey(PluginCategory.CUSTOM, "Space Travel")],
app,
page);
Assert.That(categories, Is.EqualTo(new[]
{
new AssistantCategoryKey(PluginCategory.COMMON),
new AssistantCategoryKey(PluginCategory.BUSINESS),
new AssistantCategoryKey(PluginCategory.SCIENCE),
new AssistantCategoryKey(PluginCategory.CUSTOM, "Space Travel"),
new AssistantCategoryKey(PluginCategory.AI_STUDIO_DEVELOPMENT),
}), "A category your organization disabled gets no switch, since no switch of the user brings it back.");
}
private static IReadOnlyList<AssistantPageSection<FakeAssistantPlugin>> Build(params FakeAssistantPlugin[] plugins) => AssistantPageSections.Build(plugins, plugin => plugin.Name);
}
@@ -0,0 +1,198 @@
using System.Text.Json;
using AIStudio.Assistants.Builder;
using AIStudio.Settings.DataModel;
using AIStudio.Tools;
using AIStudio.Tools.PluginSystem;
namespace AIStudio.Tests.Assistants.Builder;
/// <summary>
/// Checks which categories the Assistant Builder offers and how its draft names them.
/// </summary>
/// <remarks>
/// The builder offers every category of AI Studio but CORE, which belongs to configuration and language
/// plugins, in the order of the assistants page. The draft names the choice in a fixed form, so that the
/// step which writes the plugin can take it over.
/// </remarks>
[TestFixture]
public sealed class AssistantBuilderCategoriesTests
{
[Test]
public void TheBuilderOffersEveryCategoryButCore()
{
var expected = Enum.GetValues<PluginCategory>().Where(category => category is not (PluginCategory.NONE or PluginCategory.CORE or PluginCategory.CUSTOM));
Assert.That(AssistantBuilderCategories.Offered, Is.EquivalentTo(expected));
}
[Test]
public void TheCategoriesFollowTheOrderOfTheAssistantsPage()
{
var offered = AssistantBuilderCategories.Offered;
using (Assert.EnterMultipleScope())
{
Assert.That(offered.Take(4), Is.EqualTo(new[] { PluginCategory.COMMON, PluginCategory.BUSINESS, PluginCategory.EDUCATION, PluginCategory.SOFTWARE_DEVELOPMENT }));
Assert.That(offered[^1], Is.EqualTo(PluginCategory.AI_STUDIO_DEVELOPMENT));
}
}
[TestCase(PluginCategory.NONE, "", "")]
[TestCase(PluginCategory.BUSINESS, "", "BUSINESS")]
[TestCase(PluginCategory.SOFTWARE_DEVELOPMENT, "ignored", "SOFTWARE_DEVELOPMENT")]
[TestCase(PluginCategory.CUSTOM, " Space Travel ", "CUSTOM: Space Travel")]
public void TheDraftNamesTheCategoryInItsFixedForm(PluginCategory category, string customName, string expected)
{
Assert.That(AssistantBuilderCategories.FormatForDraft(category, customName), Is.EqualTo(expected));
}
[Test]
public void ThePromptListsEveryOfferedKey()
{
var lines = AssistantBuilderCategories.ListForPrompt(new DataApp()).Split('\n');
Assert.That(lines.Select(line => line.Split(':')[0].TrimStart('-', ' ')), Is.EqualTo(AssistantBuilderCategories.Offered.Select(category => Enum.GetName(category))));
}
[Test]
public void TheBuilderLeavesOutWhatYourOrganizationDisabled()
{
var app = new DataApp { DisabledAssistantCategories = [PluginCategory.ENTERTAINMENT, PluginCategory.CORE, PluginCategory.CUSTOM] };
var offered = AssistantBuilderCategories.OfferedUnder(app);
var listedKeys = AssistantBuilderCategories.ListForPrompt(app).Split('\n').Select(line => line.Split(':')[0].TrimStart('-', ' '));
using (Assert.EnterMultipleScope())
{
Assert.That(offered, Does.Not.Contain(PluginCategory.ENTERTAINMENT));
Assert.That(offered, Does.Not.Contain(PluginCategory.COMMON), "CORE and COMMON are one category.");
Assert.That(offered, Does.Contain(PluginCategory.BUSINESS));
Assert.That(listedKeys, Is.EqualTo(offered.Select(category => Enum.GetName(category))), "The model must not choose a disabled category either.");
Assert.That(AssistantBuilderCategories.IsCustomOfferedUnder(app), Is.False);
Assert.That(AssistantBuilderCategories.IsCustomOfferedUnder(new DataApp()), Is.True);
}
}
[Test]
public void EveryDisabledCategoryOfAnAssistantIsFound()
{
var app = new DataApp
{
DisabledAssistantCategories = [PluginCategory.ENTERTAINMENT],
DisabledCustomAssistantCategories = ["fun & games"],
};
var disabled = AssistantBuilderCategories.FindDisabled(app, [PluginCategory.ENTERTAINMENT, PluginCategory.BUSINESS, PluginCategory.CUSTOM], ["Fun & Games", "Space Travel"]);
Assert.That(disabled, Is.EqualTo(new[] { new AssistantCategoryKey(PluginCategory.ENTERTAINMENT), new AssistantCategoryKey(PluginCategory.CUSTOM, "Fun & Games") }),
"The builder refuses a disabled category even when another one is free: the user would build something the organization does not want.");
}
[Test]
public void ValidCategoriesAreRead()
{
var isRead = AssistantBuilderCategories.TryRead(["BUSINESS", "CUSTOM", "BUSINESS"], [" Space Travel ", "space travel"], out var categories, out var customCategories);
using (Assert.EnterMultipleScope())
{
Assert.That(isRead, Is.True);
Assert.That(categories, Is.EqualTo(new[] { PluginCategory.BUSINESS, PluginCategory.CUSTOM }));
Assert.That(customCategories, Is.EqualTo(new[] { "Space Travel" }));
}
}
[Test]
public void CoreStaysValidForRevisionsOfOlderPlugins()
{
Assert.That(AssistantBuilderCategories.TryRead(["CORE"], [], out _, out _), Is.True);
}
[TestCase(new string[0], new string[0], TestName = "NoCategory")]
[TestCase(new[] { "business" }, new string[0], TestName = "AnotherSpelling")]
[TestCase(new[] { "1" }, new string[0], TestName = "ANumber")]
[TestCase(new[] { "NONE" }, new string[0], TestName = "None")]
[TestCase(new[] { "BUSINESS, SCIENCE" }, new string[0], TestName = "TwoKeysInOne")]
[TestCase(new[] { "MARKETING" }, new string[0], TestName = "AnUnknownKey")]
[TestCase(new[] { "CUSTOM" }, new string[0], TestName = "CustomWithoutAName")]
[TestCase(new[] { "BUSINESS" }, new[] { "Space Travel" }, TestName = "ANameWithoutCustom")]
[TestCase(new[] { "CUSTOM" }, new[] { " " }, TestName = "AnEmptyName")]
public void AnythingTheLoaderWouldNotAcceptIsRefused(string[] categoryNames, string[] customNames)
{
Assert.That(AssistantBuilderCategories.TryRead(categoryNames, customNames, out _, out _), Is.False);
}
[Test]
public async Task TheSchemaListsExactlyTheCategoriesOfAIStudio()
{
var schema = await AppResources.ReadTextAsync("Assistants/Builder/AssistantBuilderLuaResponse.schema.json");
using var document = JsonDocument.Parse(schema);
var keys = document.RootElement.GetProperty("properties").GetProperty("plugin").GetProperty("properties").GetProperty("categories").GetProperty("items").GetProperty("enum")
.EnumerateArray()
.Select(key => key.GetString())
.ToList();
var expected = Enum.GetValues<PluginCategory>().Where(category => category is not PluginCategory.NONE).Select(category => Enum.GetName(category));
Assert.That(keys, Is.EquivalentTo(expected), "Every category of AI Studio but NONE belongs into the schema, so that a model may name it.");
}
[Test]
public void AnAnswerWithAnUnknownCategoryIsRefused()
{
var isParsed = LuaResponse.TryParse($$"""
{
"schema_version": "{{LuaResponse.SCHEMA_VERSION_VALUE}}",
"plugin": { "name": "Orbit planner", "description": "Plans orbits.", "categories": ["SPACE"] },
"assistant": { "kind": "FORM", "title": "Orbit planner", "description": "Plans orbits.", "system_prompt": "Plan orbits.", "submit_text": "Plan", "allow_ai_studio_profiles": true },
"full_lua": "ID = \"00000000-0000-0000-0000-000000000000\""
}
""", out _, out var error, out _);
using (Assert.EnterMultipleScope())
{
Assert.That(isParsed, Is.False);
Assert.That(error, Is.EqualTo(LuaResponseParseError.INVALID_CATEGORIES));
}
}
[Test]
public void ALauncherAnswerWithACustomCategoryWithoutItsNameIsRefused()
{
var isParsed = LauncherTextsResponse.TryParse($$"""
{
"schema_version": "{{LauncherTextsResponse.SCHEMA_VERSION_VALUE}}",
"plugin_name": "Weekly report",
"title": "Weekly report",
"description": "Opens the chat for the weekly report.",
"categories": ["CUSTOM"],
"custom_categories": []
}
""", out _, out var error, out _);
using (Assert.EnterMultipleScope())
{
Assert.That(isParsed, Is.False);
Assert.That(error, Is.EqualTo(LuaResponseParseError.INVALID_CATEGORIES));
}
}
[Test]
public void ALauncherAnswerWithItsCategoryIsRead()
{
var isParsed = LauncherTextsResponse.TryParse($$"""
{
"schema_version": "{{LauncherTextsResponse.SCHEMA_VERSION_VALUE}}",
"plugin_name": "Weekly report",
"title": "Weekly report",
"description": "Opens the chat for the weekly report.",
"categories": ["BUSINESS"],
"custom_categories": []
}
""", out var response, out var error, out _);
using (Assert.EnterMultipleScope())
{
Assert.That(isParsed, Is.True, error.ToString());
Assert.That(response.Categories, Is.EqualTo(new[] { "BUSINESS" }));
}
}
}
@@ -0,0 +1,45 @@
using AIStudio.Tools.PluginSystem;
namespace AIStudio.Tests.Assistants;
/// <summary>
/// An assistant plugin, reduced to its metadata.
/// </summary>
/// <remarks>
/// Enough for everything which decides by the categories of a plugin, e.g., the sections of the
/// assistants page and the categories your organization disabled.
/// </remarks>
internal sealed class FakeAssistantPlugin(string name, PluginCategory[] categories, string[]? customCategories = null) : IPluginMetadata
{
public string IconDataUrl => string.Empty;
public PluginType Type => PluginType.ASSISTANT;
public Guid Id { get; } = Guid.NewGuid();
public string Name => name;
public string Description => string.Empty;
public PluginVersion Version => new(1, 0, 0);
public DateTimeOffset? LastChanged => null;
public string[] Authors => [];
public string SupportContact => string.Empty;
public string SourceURL => string.Empty;
public PluginCategory[] Categories => categories;
public string[] CustomCategories => customCategories ?? [];
public PluginTargetGroup[] TargetGroups => [];
public bool IsMaintained => true;
public string DeprecationMessage => string.Empty;
public bool IsInternal => false;
}
@@ -1,5 +1,7 @@
using AIStudio.Settings.DataModel;
using AIStudio.Tools;
using AIStudio.Tools.AssistantLaunch;
using AIStudio.Tools.PluginSystem;
namespace AIStudio.Tests.Tools.AssistantLaunch;
@@ -55,6 +57,30 @@ public sealed class AssistantLaunchServiceTests
Assert.That(destinations, Does.Not.Contain(AIStudio.Tools.Components.SLIDE_BUILDER_ASSISTANT));
}
[TestCase(false, AssistantOfferBehavior.KEEP_OFFERING, AssistantOfferBehavior.TREAT_AS_ABSENT, ExpectedResult = true, TestName = "ACollapsedCategoryKeepsItsAssistantsByDefault")]
[TestCase(true, AssistantOfferBehavior.KEEP_OFFERING, AssistantOfferBehavior.TREAT_AS_ABSENT, ExpectedResult = false, TestName = "AHiddenCategoryDropsItsAssistantsByDefault")]
[TestCase(false, AssistantOfferBehavior.TREAT_AS_ABSENT, AssistantOfferBehavior.KEEP_OFFERING, ExpectedResult = false, TestName = "ACollapsedCategoryDropsItsAssistantsWhenTheUserSaysSo")]
[TestCase(true, AssistantOfferBehavior.TREAT_AS_ABSENT, AssistantOfferBehavior.KEEP_OFFERING, ExpectedResult = true, TestName = "AHiddenCategoryKeepsItsAssistantsWhenTheUserSaysSo")]
public bool TheMenuFollowsTheOptionsForCollapsedAndHiddenCategories(bool isHidden, AssistantOfferBehavior collapsedCategoryAssistants, AssistantOfferBehavior hiddenCategoryAssistants)
{
var page = new DataAssistantsPage
{
CollapsedCategoryAssistants = collapsedCategoryAssistants,
HiddenCategoryAssistants = hiddenCategoryAssistants
};
var business = new AssistantCategoryKey(PluginCategory.BUSINESS);
if (isHidden)
page.SetCategoryHidden(business, true);
else
page.SetCategoryCollapsed(business, true);
var destinations = AssistantLaunchService.SelectDestinations(AIStudio.Tools.Components.CHAT, page.IsOfferingAssistant);
Assert.That(destinations, Does.Contain(AIStudio.Tools.Components.TRANSLATION_ASSISTANT), "An assistant of another category stays in any case.");
return destinations.Contains(AIStudio.Tools.Components.EMAIL_ASSISTANT);
}
[Test]
public void ShorterNamesComeFirst()
{
@@ -91,6 +91,8 @@ public sealed class DataBackupContentsTests
public PluginCategory[] Categories => [];
public string[] CustomCategories => [];
public PluginTargetGroup[] TargetGroups => [];
public bool IsMaintained => true;
@@ -0,0 +1,64 @@
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.PluginSystem.Assistants;
namespace AIStudio.Tests.Tools.PluginSystem;
/// <summary>
/// Checks that AI Studio writes the categories of a direct chat launcher so that they load back.
/// </summary>
/// <remarks>
/// AI Studio writes the plugin.lua of a launcher itself, when the Assistant Builder creates one and
/// whenever a user changes its chat settings. Its categories decide where the assistants page shows it,
/// so they have to survive both.
/// </remarks>
[TestFixture]
public sealed class DirectChatLauncherCategoriesTests
{
private static readonly Guid PLUGIN_ID = Guid.Parse("5b7d9f1a-3c5e-4a7b-8d9f-1b3d5f7a9c2e");
[Test]
public async Task CustomCategoriesLoadBack()
{
var lua = DirectChatLauncherLuaWriter.Write(CreateMetadata([PluginCategory.BUSINESS, PluginCategory.CUSTOM], ["Space Travel"]), CreateDefinition());
using var plugin = await PluginFactory.Load(null, lua);
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
Assert.That(plugin.Categories, Is.EqualTo(new[] { PluginCategory.BUSINESS, PluginCategory.CUSTOM }));
Assert.That(plugin.CustomCategories, Is.EqualTo(new[] { "Space Travel" }));
}
}
[Test]
public async Task ALauncherWithoutCustomCategoriesWritesNoTableForThem()
{
var lua = DirectChatLauncherLuaWriter.Write(CreateMetadata([PluginCategory.EDUCATION], []), CreateDefinition());
using var plugin = await PluginFactory.Load(null, lua);
using (Assert.EnterMultipleScope())
{
Assert.That(lua, Does.Not.Contain("CUSTOM_CATEGORIES"));
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
Assert.That(plugin.Categories, Is.EqualTo(new[] { PluginCategory.EDUCATION }));
}
}
[Test]
public async Task CustomWithoutItsNamesFallsBackInsteadOfBreakingThePlugin()
{
var lua = DirectChatLauncherLuaWriter.Write(CreateMetadata([PluginCategory.CUSTOM], []), CreateDefinition());
using var plugin = await PluginFactory.Load(null, lua);
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
Assert.That(plugin.Categories, Is.EqualTo(new[] { PluginCategory.COMMON }), "A launcher which lost the names of its custom categories lands under \"General\".");
}
}
private static DirectChatLauncherPluginMetadata CreateMetadata(PluginCategory[] categories, string[] customCategories) => new(PLUGIN_ID, "1.0.0", null, ["MindWork AI"], "MindWork AI",
"https://github.com/MindWorkAI/AI-Studio", categories, customCategories, [PluginTargetGroup.EVERYONE], IsMaintained: true, DeprecationMessage: string.Empty, IsAssistantBuilderGenerated: true);
private static DirectChatLauncherDefinition CreateDefinition() => new("Launcher for the tests", "Launcher for the tests", "Opens a disappearing chat.", new AssistantChatLaunchConfiguration(string.Empty, null, null, null, null, null));
}
@@ -108,6 +108,8 @@ public sealed class LeftOverConfigurationObjectsTests
public PluginCategory[] Categories => [];
public string[] CustomCategories => [];
public PluginTargetGroup[] TargetGroups => [];
public bool IsMaintained => true;
@@ -0,0 +1,179 @@
using AIStudio.Tools.PluginSystem;
namespace AIStudio.Tests.Tools.PluginSystem;
/// <summary>
/// Checks how a plugin states its categories, its custom ones included.
/// </summary>
/// <remarks>
/// The assistants page shows each assistant under its categories. A plugin may also name categories of
/// its own: it declares CUSTOM in CATEGORIES and lists their names in CUSTOM_CATEGORIES. Both belong
/// together, so a plugin which states only one of them is invalid, and its authors notice it right away.
/// </remarks>
[TestFixture]
public sealed class PluginCategoriesTests
{
private static readonly Guid PLUGIN_ID = Guid.Parse("8d2e4b6a-1c3f-4a5e-9b7d-0f2a4c6e8b1d");
[Test]
public async Task TheNewCategoriesAreRead()
{
using var plugin = await LoadModelPlugin("""CATEGORIES = {"COMMON", "AI_STUDIO_DEVELOPMENT"}""");
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
Assert.That(plugin.Categories, Is.EqualTo(new[] { PluginCategory.COMMON, PluginCategory.AI_STUDIO_DEVELOPMENT }));
Assert.That(plugin.CustomCategories, Is.Empty);
}
}
[Test]
public async Task CustomCategoriesAreRead()
{
using var plugin = await LoadModelPlugin("""
CATEGORIES = {"SCIENCE", "CUSTOM"}
CUSTOM_CATEGORIES = {"Space Travel", " Deep Space "}
""");
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
Assert.That(plugin.Categories, Is.EqualTo(new[] { PluginCategory.SCIENCE, PluginCategory.CUSTOM }));
Assert.That(plugin.CustomCategories, Is.EqualTo(new[] { "Space Travel", "Deep Space" }), "Names are trimmed, so that equal names share one heading.");
}
}
[Test]
public async Task ASingleCustomCategoryIsRead()
{
using var plugin = await LoadModelPlugin("""
CATEGORIES = {"CUSTOM"}
CUSTOM_CATEGORIES = {"Space Travel"}
""");
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
Assert.That(plugin.CustomCategories, Is.EqualTo(new[] { "Space Travel" }));
}
}
[Test]
public async Task NamesWhichDifferOnlyInTheirCaseAreMerged()
{
using var plugin = await LoadModelPlugin("""
CATEGORIES = {"CUSTOM"}
CUSTOM_CATEGORIES = {"Space Travel", "space travel", "Deep Space"}
""");
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
Assert.That(plugin.CustomCategories, Is.EqualTo(new[] { "Space Travel", "Deep Space" }), "The first spelling wins.");
}
}
[Test]
public async Task AnEmptyTableWithoutCustomIsNoIssue()
{
using var plugin = await LoadModelPlugin("""
CATEGORIES = {"BUSINESS"}
CUSTOM_CATEGORIES = {}
""");
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
}
[TestCase("""CATEGORIES = {"CUSTOM"}""", TestName = "CustomWithoutTheTable")]
[TestCase("""
CATEGORIES = {"CUSTOM"}
CUSTOM_CATEGORIES = {}
""", TestName = "CustomWithAnEmptyTable")]
[TestCase("""
CATEGORIES = {"BUSINESS"}
CUSTOM_CATEGORIES = {"Space Travel"}
""", TestName = "NamesWithoutCustom")]
[TestCase("""
CATEGORIES = {"CUSTOM"}
CUSTOM_CATEGORIES = {"Space Travel", " "}
""", TestName = "AnEmptyName")]
[TestCase("""
CATEGORIES = {"CUSTOM"}
CUSTOM_CATEGORIES = {"Space Travel", 42}
""", TestName = "ANameWhichIsNoText")]
[TestCase("""
CATEGORIES = {"CUSTOM"}
CUSTOM_CATEGORIES = "Space Travel"
""", TestName = "ANameInsteadOfATable")]
public async Task AnIncompleteDeclarationMakesThePluginInvalid(string categoriesLines)
{
using var plugin = await LoadModelPlugin(categoriesLines);
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.False);
Assert.That(plugin.Issues, Has.Some.Contains("CUSTOM"));
}
}
[TestCase("""CATEGORIES = {"1"}""", TestName = "ANumberIsNoCategory")]
[TestCase("""CATEGORIES = {"BUSINESS, SCIENCE"}""", TestName = "TwoNamesInOneAreNoCategory")]
[TestCase("""CATEGORIES = {"NONE"}""", TestName = "NoneIsNoCategory")]
public async Task OnlyTheNameOfACategoryCounts(string categoriesLine)
{
using var plugin = await LoadModelPlugin(categoriesLine);
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.False, "Enum.TryParse would turn such a value into a category nobody named.");
Assert.That(plugin.Issues, Has.Some.Contains("CATEGORIES"));
}
}
[Test]
public async Task TheCaseOfANameDoesNotMatter()
{
using var plugin = await LoadModelPlugin("""CATEGORIES = {" business "}""");
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.True, string.Join(" ", plugin.Issues));
Assert.That(plugin.Categories, Is.EqualTo(new[] { PluginCategory.BUSINESS }), "Plugins read names like configuration plugins do.");
}
}
[TestCase("""TARGET_GROUPS = {"1"}""", TestName = "ANumberIsNoTargetGroup")]
[TestCase("""TARGET_GROUPS = {"EVERYONE, STUDENTS"}""", TestName = "TwoNamesInOneAreNoTargetGroup")]
public async Task OnlyTheNameOfATargetGroupCounts(string targetGroupsLine)
{
using var plugin = await LoadModelPlugin("""CATEGORIES = {"BUSINESS"}""", targetGroupsLine);
using (Assert.EnterMultipleScope())
{
Assert.That(plugin.IsValid, Is.False);
Assert.That(plugin.Issues, Has.Some.Contains("TARGET_GROUPS"));
}
}
/// <summary>
/// Loads a model plugin, the plugin type with the fewest fields beyond the ones every plugin has.
/// </summary>
/// <param name="categoriesLines">The Lua lines which state CATEGORIES and, optionally, CUSTOM_CATEGORIES.</param>
/// <param name="targetGroupsLine">The Lua line which states TARGET_GROUPS.</param>
private static Task<PluginBase> LoadModelPlugin(string categoriesLines, string targetGroupsLine = """TARGET_GROUPS = {"EVERYONE"}""") => PluginFactory.Load(null, $$"""
ID = "{{PLUGIN_ID}}"
NAME = "Models for the tests"
DESCRIPTION = "A model plugin which declares no models."
VERSION = "1.0.0"
TYPE = "MODEL"
AUTHORS = {"MindWork AI"}
SUPPORT_CONTACT = "MindWork AI"
SOURCE_URL = "https://github.com/MindWorkAI/AI-Studio"
{{categoriesLines}}
{{targetGroupsLine}}
IS_MAINTAINED = true
DEPRECATION_MESSAGE = ""
MODELS = {}
""");
}
@@ -116,7 +116,7 @@ public sealed class PluginLastChangedTests
""");
private static DirectChatLauncherPluginMetadata CreateLauncherMetadata(DateTimeOffset? lastChanged) => new(PLUGIN_ID, "1.0.0", lastChanged, ["MindWork AI"], "MindWork AI",
"https://github.com/MindWorkAI/AI-Studio", [PluginCategory.CORE], [PluginTargetGroup.EVERYONE], IsMaintained: true, DeprecationMessage: string.Empty, IsAssistantBuilderGenerated: false);
"https://github.com/MindWorkAI/AI-Studio", [PluginCategory.CORE], [], [PluginTargetGroup.EVERYONE], IsMaintained: true, DeprecationMessage: string.Empty, IsAssistantBuilderGenerated: false);
private static DirectChatLauncherDefinition CreateLauncherDefinition() => new("Launcher for the tests", "Launcher for the tests", "Opens a disappearing chat.", new AssistantChatLaunchConfiguration(string.Empty, null, null, null, null, null));
}