mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-04 09:27:21 +00:00
182 lines
6.1 KiB
C#
182 lines
6.1 KiB
C#
using AIStudio.Components;
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace AIStudio.Tests.Components;
|
|
|
|
public sealed class FileExclusionEditorTests
|
|
{
|
|
[TestCase(true, "*.pdf")]
|
|
[TestCase(true, "Dockerfile")]
|
|
[TestCase(true, "node_modules")]
|
|
[TestCase(false, "bin/")]
|
|
[TestCase(false, "*.pdf;obj/")]
|
|
[TestCase(true, ":text:")]
|
|
[TestCase(false, ":documents:")]
|
|
[TestCase(true, "src/bin/")]
|
|
[TestCase(false, "**/*.tmp")]
|
|
public async Task WrongFieldAndInvalidRulesAreNotPublished(bool folders, string patterns)
|
|
{
|
|
var published = new List<string>();
|
|
var editor = Create(published);
|
|
|
|
await editor.EditAsync(patterns, folders);
|
|
|
|
Assert.That(published, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public async Task EditingTheOtherFieldCannotPublishAnInvalidDraft()
|
|
{
|
|
var published = new List<string>();
|
|
var editor = Create(published);
|
|
|
|
await editor.EditAsync("*.pdf", folders: true);
|
|
await editor.EditAsync("*.bak", folders: false);
|
|
Assert.That(published, Is.Empty);
|
|
|
|
await editor.EditAsync("build/", folders: true);
|
|
Assert.That(published, Is.EqualTo(new[] { "build/; *.bak" }));
|
|
}
|
|
|
|
[Test]
|
|
public async Task MovingARuleToItsCorrectFieldPublishesItEvenIfTheCombinedTextDoesNotChange()
|
|
{
|
|
var published = new List<string>();
|
|
var editor = Create(published, "bin/");
|
|
|
|
await editor.EditAsync("bin/;*.pdf", folders: true);
|
|
await editor.EditAsync("*.pdf", folders: false);
|
|
Assert.That(published, Is.Empty);
|
|
|
|
await editor.EditAsync("bin/", folders: true);
|
|
Assert.That(published, Is.EqualTo(new[] { "bin/; *.pdf" }));
|
|
}
|
|
|
|
[TestCase(true, true)]
|
|
[TestCase(true, false)]
|
|
[TestCase(false, true)]
|
|
[TestCase(false, false)]
|
|
public async Task ResettingOneFieldCannotPersistInvalidRulesInTheOtherField(bool folders, bool explicitReset)
|
|
{
|
|
var published = new List<string>();
|
|
var editor = Create(published);
|
|
var resets = new List<string>();
|
|
if (explicitReset)
|
|
editor.ResetRequested = EventCallback.Factory.Create<string>(new object(), (string value) => resets.Add(value));
|
|
|
|
await editor.EditAsync(folders ? "*.keep;wrong/" : "*.wrong", folders: !folders);
|
|
await editor.ResetAsync(folders);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(published, Is.Empty);
|
|
Assert.That(resets, Is.Empty);
|
|
});
|
|
|
|
await editor.EditAsync(folders ? "*.keep" : "keep/", folders: !folders);
|
|
Assert.That(published, Is.EqualTo(new[] { folders ? "bin/; *.keep" : "keep/; *.tmp" }));
|
|
}
|
|
|
|
[TestCase(true)]
|
|
[TestCase(false)]
|
|
public async Task ResettingTheInvalidFieldRestoresAValidValue(bool folders)
|
|
{
|
|
var published = new List<string>();
|
|
var editor = Create(published);
|
|
var resets = new List<string>();
|
|
editor.ResetRequested = EventCallback.Factory.Create<string>(new object(), (string value) => resets.Add(value));
|
|
|
|
await editor.EditAsync(folders ? "*.wrong" : "wrong/", folders);
|
|
await editor.ResetAsync(folders);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(published, Is.Empty);
|
|
Assert.That(resets, Is.EqualTo(new[] { folders ? "bin/; *.custom" : "custom/; *.tmp" }));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task DisabledEditorRejectsEditsAndResets()
|
|
{
|
|
var published = new List<string>();
|
|
var editor = Create(published);
|
|
editor.Disabled = true;
|
|
|
|
await editor.EditAsync("changed/", folders: true);
|
|
await editor.EditAsync("*.changed", folders: false);
|
|
await editor.ResetAsync(folders: true);
|
|
await editor.ResetAsync(folders: false);
|
|
|
|
Assert.That(published, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ReadOnlyEditorRejectsEditsAndBothResetCallbacks()
|
|
{
|
|
var published = new List<string>();
|
|
var resets = new List<string>();
|
|
var editor = Create(published);
|
|
editor.ReadOnly = true;
|
|
editor.ResetRequested = EventCallback.Factory.Create<string>(new object(), (string value) => resets.Add(value));
|
|
editor.LoadParameters();
|
|
|
|
await editor.EditAsync("changed/", folders: true);
|
|
await editor.EditAsync("*.changed", folders: false);
|
|
await editor.ResetAsync(folders: true);
|
|
await editor.ResetAsync(folders: false);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(published, Is.Empty);
|
|
Assert.That(resets, Is.Empty);
|
|
});
|
|
|
|
editor.ResetRequested = default;
|
|
await editor.ResetAsync(folders: true);
|
|
await editor.ResetAsync(folders: false);
|
|
Assert.That(published, Is.Empty);
|
|
|
|
// Returning to editing must reveal the unchanged source, not either attempted edit.
|
|
editor.ReadOnly = false;
|
|
editor.LoadParameters();
|
|
await editor.EditAsync("*.other", folders: false);
|
|
Assert.That(published, Is.EqualTo(new[] { "custom/; *.other" }));
|
|
}
|
|
|
|
[Test]
|
|
public async Task HiddenResetButtonsCannotChangeTheRules()
|
|
{
|
|
var published = new List<string>();
|
|
var editor = Create(published);
|
|
editor.ShowReset = false;
|
|
|
|
await editor.ResetAsync(folders: true);
|
|
await editor.ResetAsync(folders: false);
|
|
|
|
Assert.That(published, Is.Empty);
|
|
}
|
|
|
|
private static TestableFileExclusionEditor Create(List<string> published, string initial = "custom/;*.custom")
|
|
{
|
|
var editor = new TestableFileExclusionEditor
|
|
{
|
|
Text = initial,
|
|
ResetValue = "bin/;*.tmp",
|
|
TextChanged = EventCallback.Factory.Create<string>(new object(), (string value) => published.Add(value)),
|
|
};
|
|
editor.LoadParameters();
|
|
return editor;
|
|
}
|
|
|
|
private sealed class TestableFileExclusionEditor : FileExclusionEditor
|
|
{
|
|
public void LoadParameters() => this.OnParametersSet();
|
|
|
|
public Task EditAsync(string patterns, bool folders) => folders ? this.ChangeFoldersAsync(patterns) : this.ChangeFilesAsync(patterns);
|
|
|
|
public Task ResetAsync(bool folders) => this.RestoreDefaultsAsync(folders);
|
|
}
|
|
}
|