From c9ffdeb1be77afb61b043fe9e4faa37f2acd038c Mon Sep 17 00:00:00 2001 From: MarcT1me Date: Thu, 26 Nov 2026 02:45:43 +0700 Subject: [PATCH] [SFS] [TESTS] adding entry point and entry point tests --- Stellar.FileSystem.stellar.module | 2 +- .../FileSystemEntryPoint.cs | 63 ++++++ .../EntryPointTests.cs | 188 ++++++++++++++++++ 3 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 src/Managed/Stellar.FileSystem/FileSystemEntryPoint.cs create mode 100644 tests/Stellar.FileSystem.Tests/EntryPointTests.cs diff --git a/Stellar.FileSystem.stellar.module b/Stellar.FileSystem.stellar.module index 98e579d..f0d3a2a 100644 --- a/Stellar.FileSystem.stellar.module +++ b/Stellar.FileSystem.stellar.module @@ -1,7 +1,7 @@ { "$schema": "https://quantumdevteam.github.io/StellarEngine-Core/schemas/stellar.module.schema.json", "Id": "Stellar.FileSystem", - "Version": "0.9.0", + "Version": "0.11.0", "Level": "CoreModule", "Description": "File system of the engine", "Dependencies": [] diff --git a/src/Managed/Stellar.FileSystem/FileSystemEntryPoint.cs b/src/Managed/Stellar.FileSystem/FileSystemEntryPoint.cs new file mode 100644 index 0000000..81c727a --- /dev/null +++ b/src/Managed/Stellar.FileSystem/FileSystemEntryPoint.cs @@ -0,0 +1,63 @@ +using Stellar.Kernel.Configuration; +using Stellar.Kernel.EntryPoint; +using Stellar.Kernel.FileSystem; +using Stellar.Kernel.Services; + +namespace Stellar.FileSystem; + +/// +/// Publishes the file system of the engine: +/// app:// is the folder of the application, read-only, +/// user:// the folder of the user. +/// +[StellarEntryPoint] +public sealed class FileSystemEntryPoint(RuntimeConfiguration configuration) + : StellarEntryPoint(FileSystemModule.Id, configuration) +{ + private IServiceCatalog? _services; + private VirtualFileSystem? _fileSystem; + + /// + public override int Run(ref readonly TContext context) + { + if (context.Services is not { } services) return 1; + + _services = services; + _fileSystem = VirtualFileSystem.CreateDefault(); + + _fileSystem.Mount(new DomainMount( + "app", DomainScheme.Directory, AppContext.BaseDirectory, + new Dictionary { ["readonly"] = "true" } + )); + + if (!string.IsNullOrWhiteSpace(RuntimeConfiguration.ProjectName)) + { + string company = RuntimeConfiguration.CompanyName; + if (string.IsNullOrWhiteSpace(company) || company == "Unknown") company = "Starlet"; + _fileSystem.Mount(new DomainMount( + "user", DomainScheme.Directory, Path.Combine(Environment.GetFolderPath( + Environment.SpecialFolder.ApplicationData), + company, RuntimeConfiguration.ProjectName) + )); + } + + services.Set(_fileSystem); + + return 0; + } + + /// + public override void RequestStop(ref readonly TContext context) + { + if (_services is not { } services) return; + _services = null; + + if (ReferenceEquals(services.Find(), _fileSystem)) services.Remove(); + } + + /// + public override void Dispose() + { + _fileSystem?.Dispose(); + } +} diff --git a/tests/Stellar.FileSystem.Tests/EntryPointTests.cs b/tests/Stellar.FileSystem.Tests/EntryPointTests.cs new file mode 100644 index 0000000..14ba044 --- /dev/null +++ b/tests/Stellar.FileSystem.Tests/EntryPointTests.cs @@ -0,0 +1,188 @@ +using Stellar.Core.EntryPoints; +using Stellar.Core.Services; +using Stellar.Kernel.Configuration; +using Stellar.Kernel.EntryPoint; +using Stellar.Kernel.FileSystem; +using Stellar.Kernel.Services; + +namespace Stellar.FileSystem.Tests; + +public sealed class EntryPointTests + : IDisposable +{ + private static readonly string AppData = + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + + // Names nobody else has: what the tests leave in the real folder of the user is deleted whole + private readonly string _company = "StellarTests-" + Guid.NewGuid().ToString("N"); + private readonly string _project = "Project-" + Guid.NewGuid().ToString("N"); + private readonly ServiceCatalog _services = new(); + + public void Dispose() + { + Delete(Path.Combine(AppData, _company)); + Delete(Path.Combine(AppData, _project)); + Delete(Path.Combine(AppData, "Starlet", _project)); + Delete(Path.Combine(AppData, "Unknown", _project)); + + // Only if it is ours: a folder of somebody else is not empty + string shared = Path.Combine(AppData, "Starlet"); + if (Directory.Exists(shared) && !Directory.EnumerateFileSystemEntries(shared).Any()) Directory.Delete(shared); + } + + private static void Delete(string path) + { + if (Directory.Exists(path)) Directory.Delete(path, true); + } + + private static FileSystemEntryPoint Make(string company, string project) + { + return new FileSystemEntryPoint(new RuntimeConfiguration { CompanyName = company, ProjectName = project }); + } + + private static int Run(FileSystemEntryPoint entryPoint, IServiceCatalog? services) + { + var context = new RunContext(null, services: services); + return entryPoint.Run(in context); + } + + private static void RequestStop(FileSystemEntryPoint entryPoint) + { + var context = new StopContext(null, StopReason.Regular); + entryPoint.RequestStop(in context); + } + + private static void Stop(FileSystemEntryPoint entryPoint) + { + RequestStop(entryPoint); + entryPoint.Dispose(); + } + + [Fact] + public void Run_PublishesTheFileSystem_AndStopTakesItBack() + { + FileSystemEntryPoint entryPoint = Make(_company, _project); + Run(entryPoint, _services).Should().Be(0); + + var files = _services.Find()!; + files.Should().BeOfType(); + files.Domains.Keys.Should().BeEquivalentTo("app", "user"); + + Stop(entryPoint); + _services.Find().Should().BeNull(); + files.Domains.Keys.Should().BeEmpty(); + } + + [Fact] + public void User_IsTheFolderOfTheProject_OfTheCompany() + { + FileSystemEntryPoint entryPoint = Make(_company, _project); + Run(entryPoint, _services); + + _services.Find()!.WriteAllText(FilePath.Parse("user://a.txt"), "text"); + File.ReadAllText(Path.Combine(AppData, _company, _project, "a.txt")).Should().Be("text"); + + Stop(entryPoint); + File.Exists(Path.Combine(AppData, _company, _project, "a.txt")).Should().BeTrue("the files outlive the engine"); + } + + [Theory] + [InlineData("Unknown")] + [InlineData("")] + [InlineData(null)] + public void User_OfNoCompany_IsInStarlet(string? company) + { + FileSystemEntryPoint entryPoint = Make(company!, _project); + Run(entryPoint, _services); + Stop(entryPoint); + + Directory.Exists(Path.Combine(AppData, "Starlet", _project)).Should().BeTrue(); + } + + [Theory] + [InlineData("")] + [InlineData(null)] + public void User_OfNoProjectName_IsNotMounted(string? project) + { + FileSystemEntryPoint entryPoint = Make(_company, project!); + Run(entryPoint, _services); + Stop(entryPoint); + + Directory.Exists(Path.Combine(AppData, _company)).Should().BeFalse(); + } + + [Fact] + public void User_OfNoProject_IsNotMounted() + { + FileSystemEntryPoint entryPoint = Make(_company, ""); + Run(entryPoint, _services).Should().Be(0); + + _services.Find()!.Domains.Keys.Should().BeEquivalentTo("app"); + + Stop(entryPoint); + Directory.Exists(Path.Combine(AppData, _company)).Should().BeFalse(); + } + + [Fact] + public void App_CannotBeWritten() + { + FileSystemEntryPoint entryPoint = Make(_company, _project); + Run(entryPoint, _services); + + var files = _services.Find()!; + files.Invoking(it => it.WriteAllText(FilePath.Parse("app://a.txt"), "text")) + .Should().Throw(); + File.Exists(Path.Combine(AppContext.BaseDirectory, "a.txt")).Should().BeFalse(); + + Stop(entryPoint); + } + + [Fact] + public void WithoutServices_DoesNotStart() + { + FileSystemEntryPoint entryPoint = Make(_company, _project); + Run(entryPoint, null).Should().NotBe(0); + + entryPoint.Invoking(Stop).Should().NotThrow(); + Directory.Exists(Path.Combine(AppData, _company)).Should().BeFalse(); + } + + [Fact] + public void Stop_LeavesTheFileSystemOfAnotherModule() + { + FileSystemEntryPoint entryPoint = Make(_company, _project); + Run(entryPoint, _services); + + using var other = new VirtualFileSystem(); + _services.Set(other); + + Stop(entryPoint); + _services.Find().Should().BeSameAs(other); + } + + [Fact] + public void TwoEngines_HaveTheirOwnFileSystems() + { + var otherServices = new ServiceCatalog(); + FileSystemEntryPoint first = Make(_company, _project); + FileSystemEntryPoint second = Make(_company, _project); + Run(first, _services); + Run(second, otherServices); + + Stop(first); + otherServices.Find()!.Domains.Keys.Should().BeEquivalentTo("app", "user"); + + Stop(second); + } + + [Fact] + public void Dispose_MayBeRepeated_AndCalledWithoutRun() + { + FileSystemEntryPoint entryPoint = Make(_company, _project); + entryPoint.Invoking(it => it.Dispose()).Should().NotThrow(); + + Run(entryPoint, _services); + Stop(entryPoint); + entryPoint.Invoking(Stop).Should().NotThrow(); + } +}