diff --git a/backend/FwLite/FwDataMiniLcmBridge.Tests/Fixtures/MockFwProjectLoader.cs b/backend/FwLite/FwDataMiniLcmBridge.Tests/Fixtures/MockFwProjectLoader.cs index 890bca1624..4123cdd39f 100644 --- a/backend/FwLite/FwDataMiniLcmBridge.Tests/Fixtures/MockFwProjectLoader.cs +++ b/backend/FwLite/FwDataMiniLcmBridge.Tests/Fixtures/MockFwProjectLoader.cs @@ -19,7 +19,7 @@ public override LcmCache LoadCache(FwDataProject project) return cache; } - public override LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs) + public override LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs, string uiWs = "en") { Init(); var lcmDirectories = new LcmDirectories(project.ProjectsPath, TemplatesFolder); @@ -28,7 +28,7 @@ public override LcmCache NewProject(FwDataProject project, string analysisWs, st new SimpleProjectId(BackendProviderType.kMemoryOnly, Path.GetFullPath(project.FilePath)), analysisWs, vernacularWs, - null, + uiWs, new LfLcmUi(progress.SynchronizeInvoke), lcmDirectories, new LcmSettings()); diff --git a/backend/FwLite/FwDataMiniLcmBridge/LcmUtils/ProjectLoader.cs b/backend/FwLite/FwDataMiniLcmBridge/LcmUtils/ProjectLoader.cs index 3d64f8016e..db3b5b83f6 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/LcmUtils/ProjectLoader.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/LcmUtils/ProjectLoader.cs @@ -19,7 +19,13 @@ public interface IProjectLoader /// LcmCache LoadCache(FwDataProject project); - LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs); + LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs, string uiWs = "en"); + + /// + /// Like the single-WS overload, but the new project also gets every analysis/vernacular writing + /// system in the given lists. The first item of each list is the default of that type. + /// + LcmCache NewProject(FwDataProject project, IReadOnlyList analysisWss, IReadOnlyList vernacularWss, string uiWs = "en"); } public class ProjectLoader(IOptions config) : IProjectLoader @@ -75,27 +81,58 @@ public virtual LcmCache LoadCache(FwDataProject project) return cache; } - public virtual LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs) + public virtual LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs, string uiWs = "en") + { + return NewProject(project, [analysisWs], [vernacularWs], uiWs); + } + + public virtual LcmCache NewProject(FwDataProject project, + IReadOnlyList analysisWss, + IReadOnlyList vernacularWss, + string uiWs = "en") { Init(); var lcmDirectories = new LcmDirectories(project.ProjectsPath, TemplatesFolder); var progress = new LcmThreadedProgress(); + var analysisDefinitions = analysisWss.Select(CreateWritingSystemDefinition).ToList(); + var vernacularDefinitions = vernacularWss.Select(CreateWritingSystemDefinition).ToList(); NewProject(progress, project.Name, lcmDirectories, progress.SynchronizeInvoke, - new CoreWritingSystemDefinition(analysisWs) { Id = analysisWs }, - new CoreWritingSystemDefinition(vernacularWs) { Id = vernacularWs }); + analysisDefinitions[0], + vernacularDefinitions[0], + uiWs, + AdditionalWritingSystems(analysisDefinitions), + AdditionalWritingSystems(vernacularDefinitions)); return LoadCache(project); } + private static CoreWritingSystemDefinition CreateWritingSystemDefinition(string ws) => new(ws) { Id = ws }; + + // The default writing system (the first of the list) is passed to CreateNewLangProj on its own, so + // the "additional" set is the whole list as a hash set with that default removed. The new project + // ends up with them in list order (NewProjectWritingSystemsTests checks this). + private static HashSet AdditionalWritingSystems(List definitions) + { + var additional = definitions.ToHashSet(); + additional.Remove(definitions[0]); + return additional; + } + private static void NewProject(IThreadedProgress progress, string projectName, ILcmDirectories lcmDirectories, ISynchronizeInvoke syncInvoke, CoreWritingSystemDefinition analysisWs, - CoreWritingSystemDefinition vernacularWs) + CoreWritingSystemDefinition vernacularWs, + string uiWs, + HashSet additionalAnalysisWss, + HashSet additionalVernacularWss) { - LcmCache.CreateNewLangProj(progress, [projectName, lcmDirectories, syncInvoke, analysisWs, vernacularWs]); + // After the default analysis/vernacular definitions and the string UI writing system, + // CreateNewLangProj takes the additional analysis then additional vernacular writing systems. + LcmCache.CreateNewLangProj(progress, + [projectName, lcmDirectories, syncInvoke, analysisWs, vernacularWs, uiWs, additionalAnalysisWss, additionalVernacularWss]); } } diff --git a/backend/FwLite/FwLiteProjectSync.Tests/NewProjectWritingSystemsTests.cs b/backend/FwLite/FwLiteProjectSync.Tests/NewProjectWritingSystemsTests.cs new file mode 100644 index 0000000000..aaa4e80d39 --- /dev/null +++ b/backend/FwLite/FwLiteProjectSync.Tests/NewProjectWritingSystemsTests.cs @@ -0,0 +1,60 @@ +using FwDataMiniLcmBridge; +using FwDataMiniLcmBridge.LcmUtils; +using FwLiteProjectSync.Tests.Fixtures; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace FwLiteProjectSync.Tests; + +/// +/// ProjectLoader.NewProject with several writing systems of each type, against the real SIL.LCModel template. +/// +public class NewProjectWritingSystemsTests : IAsyncLifetime +{ + private const string ProjectFolder = nameof(NewProjectWritingSystemsTests); + private readonly ServiceProvider _rootServiceProvider; + private readonly AsyncServiceScope _scope; + private IServiceProvider Services => _scope.ServiceProvider; + + public NewProjectWritingSystemsTests() + { + _rootServiceProvider = new ServiceCollection() + .AddSyncServices(ProjectFolder, mockFwProjectLoader: false) + .BuildServiceProvider(); + _scope = _rootServiceProvider.CreateAsyncScope(); + } + + public Task InitializeAsync() + { + if (Directory.Exists(ProjectFolder)) Directory.Delete(ProjectFolder, true); + Directory.CreateDirectory(Services.GetRequiredService>().Value.ProjectsFolder); + return Task.CompletedTask; + } + + public async Task DisposeAsync() + { + await _scope.DisposeAsync(); + await _rootServiceProvider.DisposeAsync(); + } + + [Fact] + public async Task NewProjectAddsEveryRequestedWritingSystemToTheCorrectListInOrder() + { + var fwDataBridgeConfig = Services.GetRequiredService>().Value; + // The SIL.LCModel template ships next to the test assembly; point liblcm at it (this project, + // unlike FwDataMiniLcmBridge.Tests, doesn't otherwise configure TemplatesFolder). + fwDataBridgeConfig.TemplatesFolder = Path.Combine(AppContext.BaseDirectory, "Templates"); + var fwDataProject = new FwDataProject("multi-ws-source", fwDataBridgeConfig.ProjectsFolder); + // First of each list is the default; the rest go through CreateNewLangProj's additional-WS sets. + // Distinct codes per type so a swapped analysis/vernacular argument would fail this test, and + // several additional ones per type because CreateNewLangProj takes those as an unordered set. + string[] analysis = ["en", "de", "pt", "ru"]; + string[] vernacular = ["fr", "es", "it", "nl"]; + Services.GetRequiredService().NewProject(fwDataProject, analysis, vernacular, uiWs: "en").Dispose(); + + using var api = Services.GetRequiredService().GetFwDataMiniLcmApi(fwDataProject, false); + var writingSystems = await api.GetWritingSystems(); + writingSystems.Analysis.Select(ws => ws.WsId.Code).Should().Equal(analysis); + writingSystems.Vernacular.Select(ws => ws.WsId.Code).Should().Equal(vernacular); + } +}