using System.IO; using Chronos.App.Infrastructure; using Chronos.App.Shell; using Chronos.App.Tests.Editor; using Chronos.App.Tests.Windows; using Chronos.App.Workspace; using Chronos.Core.Archiving; using Chronos.Core.Codecs.Jpeg; using Chronos.Core.Localization; using Chronos.Core.Metadata; using Chronos.Core.Model; using Chronos.Core.Project; using Chronos.Core.Windows.Shell; using Chronos.Data; using Chronos.Data.Archiving; using Chronos.Data.Projects; using Chronos.Data.Workspaces; using Chronos.Testing; namespace Chronos.App.Tests.Workspace; /// /// Phase 5c in Edit: "Open in workspace" offers the originals (Settings › Workspace › Media first, the proxies when the /// drive has no room), and Settings › Workspace › Open project picks the project a sequence with several opens. /// [WindowsOnly] [Serial] [Timeout(300_000)] [Category("workspace")] public sealed class WorkspaceOpenPolicyTests(TestContext context) { private static readonly SequenceProject Main = new() { Id = Guid.CreateVersion7(), SequenceId = Guid.CreateVersion7(), Name = ProjectService.MainName, IsMain = true }; [Test] public void TheOpenDialogStartsWithTheMediaOfTheSettingsWhenTheyFit() { UiLanguage.Apply("en"); var room = new WorkspaceCopyFacts(10_000_000, 100_000_000_000, 4_000_000_000); using var proxies = new OpenInWorkspaceDialogViewModel("Harbour", [Main], _ => { }, copy: room); Assert.True(proxies is { IsProxies: true, IsOriginals: false, CanCopyOriginals: true }); Assert.Equal(BundleMedia.Proxies, proxies.Media); Assert.Contains(proxies.CopyFacts, f => f.Label == Loc.L("Editor.OpenWorkspace.Originals")); using var originals = new OpenInWorkspaceDialogViewModel("Harbour", [Main], _ => { }, copy: room, defaultMedia: BundleMedia.Originals); Assert.True(originals is { IsOriginals: true, IsProxies: false, IsShortOfSpace: false }); Assert.Equal(Loc.L("Tooltips.Editor.OpenWorkspace.OriginalsReady"), originals.OriginalsToolTip); originals.OpenCommand.Execute(null); Assert.Equal(new OpenChoice(Main.Id, null, BundleMedia.Originals), originals.Choice.Result); // The drive has room for the proxies, not the originals: they are offered disabled, why in the tooltip. var tight = new WorkspaceCopyFacts(10_000_000, WorkspaceJobs.CopySpaceReserve + 20_000_000, 4_000_000_000); using var refused = new OpenInWorkspaceDialogViewModel("Harbour", [Main], _ => { }, copy: tight, defaultMedia: BundleMedia.Originals); Assert.True(refused is { IsProxies: true, CanCopyOriginals: false, IsShortOfSpace: false }); refused.IsOriginals = true; Assert.False(refused.IsOriginals, "never chosen without room"); Assert.Equal(Loc.L("Tooltips.Editor.OpenWorkspace.NoRoomOriginals"), refused.OriginalsToolTip); refused.OpenCommand.Execute(null); Assert.Equal(BundleMedia.Proxies, refused.Choice.Result!.Media); // Not measured (a server out of reach): offered, the copy measures again before it writes. using var unmeasured = new OpenInWorkspaceDialogViewModel("Harbour", [Main], _ => { }, server: true); Assert.True(unmeasured.CanCopyOriginals); Assert.Equal(Loc.L("Tooltips.Editor.OpenWorkspace.OriginalsReady.Server"), unmeasured.OriginalsToolTip); unmeasured.IsOriginals = true; Assert.Equal(BundleMedia.Originals, unmeasured.Media); } [Test] public Task TheOriginalsAreCopiedAndTheOpenProjectFollowsTheSettings() => ScreenHost.RunAsync(async () => { var directory = context.CreateTempDirectory("workspace-open-policy"); var environment = AppEnvironment.From(AppPaths.Resolve(Directory.CreateDirectory(Path.Combine(directory, "app")).FullName, Directory.CreateDirectory(Path.Combine(directory, "local")).FullName, [])); UiLanguage.Apply("en"); var settings = new AppSettings { Language = "en", FirstRunCompleted = true, CheckForUpdates = false, WorkspaceDefaultMedia = BundleMedia.Originals, }; var services = new AppServices(environment, settings, () => { }, new NoDialogs()); await services.AttachAsync(await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(environment.DatabasePath)), startEngine: true); var shell = new ShellViewModel(services); try { var sequenceId = await SequenceAsync(services, Path.Combine(directory, "archive")); var section = shell.Sequence; // The originals, as the settings say: copied beside the proxies, and the project's menu offers Proxies only. shell.OpenSequence(sequenceId); await WorkspaceTestBed.UntilAsync(() => section.Dialog is OpenInWorkspaceDialogViewModel, "Open in workspace asks the first time"); var ask = (OpenInWorkspaceDialogViewModel)section.Dialog!; Assert.True(ask.IsOriginals, "the settings' media first"); ask.OpenCommand.Execute(null); await WorkspaceTestBed.UntilAsync(() => section.Editor is not null, "the project opens once its media are copied"); var mainId = section.OpenProjectId!.Value; var menu = Assert.NotNull(section.Editor!.Project); Assert.True(menu.HoldsOriginals); var media = services.Workspace.Store.Find(mainId)!.ReadMedia()!; Assert.True(media.HoldsOriginals && media.Frames.All(f => f.HasOriginal), "every frame has its original"); Assert.True(settings.WorkspaceOpened.ContainsKey(mainId), "the open is recorded for the clean-up"); Assert.True(await section.ConfirmCloseAsync()); await section.CloseAsync(); shell.KeepSequence(null); // A second project here, opened last. var vertical = (await services.Archives!.Projects.CreateAsync(sequenceId, "Vertical", cancellationToken: context.Cancellation)).Id; var checkout = await services.Workspace.CheckoutAsync(sequenceId, vertical, null); await WorkspaceTestBed.UntilAsync(() => services.Database!.Jobs.GetAsync(checkout.Copy.Id).GetAwaiter().GetResult()?.State == JobState.Done, "its media are copied"); services.Workspace.Remember(sequenceId, vertical); // Last used: Vertical. settings.WorkspaceOpenProject = WorkspaceOpenPolicy.LastUsed; Assert.Equal(vertical, await OpenAsync(shell, sequenceId)); // Main, whichever opened last. settings.WorkspaceOpenProject = WorkspaceOpenPolicy.Main; Assert.Equal(mainId, await OpenAsync(shell, sequenceId)); // Ask: the projects here are listed, the one opened last first; closing it opens nothing. settings.WorkspaceOpenProject = WorkspaceOpenPolicy.Ask; shell.OpenSequence(sequenceId); await WorkspaceTestBed.UntilAsync(() => section.Dialog is OpenProjectDialogViewModel, "Open which project? asks"); var which = (OpenProjectDialogViewModel)section.Dialog!; Assert.Equal(2, which.Projects.Count); Assert.Equal(mainId, which.SelectedProject!.Entry.ProjectId); which.CloseCommand.Execute(null); await WorkspaceTestBed.UntilAsync(() => section.Dialog is null, "closed without a choice"); Assert.Null(section.Editor); shell.KeepSequence(null); shell.OpenSequence(sequenceId); await WorkspaceTestBed.UntilAsync(() => section.Dialog is OpenProjectDialogViewModel, "Open which project? asks again"); which = (OpenProjectDialogViewModel)section.Dialog!; which.SelectedProject = which.Projects.Single(p => p.Entry.ProjectId == vertical); which.OpenCommand.Execute(null); await WorkspaceTestBed.UntilAsync(() => section.Editor is not null, "the project chosen opens"); Assert.Equal(vertical, section.OpenProjectId); } finally { await shell.Sequence.CloseAsync(); shell.Dispose(); await services.DisposeAsync(); } }); /// Opens the sequence in Edit as the settings say and returns the project that opened, closed again. private static async Task OpenAsync(ShellViewModel shell, Guid sequenceId) { var section = shell.Sequence; shell.OpenSequence(sequenceId); await WorkspaceTestBed.UntilAsync(() => section.Editor is not null, "the project opens"); Assert.Null(section.Dialog); var opened = section.OpenProjectId!.Value; Assert.True(await section.ConfirmCloseAsync()); await section.CloseAsync(); shell.KeepSequence(null); return opened; } /// An archive with one sequence of six synthetic photos, indexed. private async Task SequenceAsync(AppServices services, string root) { var archives = services.Archives!; var archive = await archives.CreateAsync(root, "Test"); var sequence = EditorFixture.Sequence(6); var folder = Path.Combine(root, "Synthetic"); Directory.CreateDirectory(folder); for (var i = 0; i < sequence.Count; i++) { using var image = sequence.Render(i); var exif = ExifWriter.BuildJpegApp1(sequence.Metadata[i]); File.WriteAllBytes(Path.Combine(folder, $"IMG_{i + 1:0000}.JPG"), JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] })); } await archives.Indexer.ScanAsync(archive, ScanMode.Full, null, _ => ValueTask.CompletedTask, context.Cancellation); return (await services.Database!.Sequences.ListLocationsAsync(archive.Id, context.Cancellation)).Single().Id; } /// Dialogs that pick nothing and decline every confirmation. private sealed class NoDialogs : IAppDialogs { public string? PickFolder(string title, string? initialFolder = null) => null; public string? PickJsonFile(string title) => null; public bool Confirm(ConfirmRequest request) => false; public bool SignIn(string server, string? username, Func> signIn) => false; } }