Files
Chronos/tests/Titano.App.Tests/Editor/ProjectFilesTests.cs
T
Claude 2ffc0268a7 feat(app): .titano project files that open their sequence with its settings
Setup registered .titano with Titano.exe "%1", but nothing wrote such a file
and Titano ignored the argument (BRIEF §18).

- Core ProjectBundle: versioned UTF-8 JSON ("titano-project", format 1) with
  the archive (id, name, path), the sequence (id, folder, title, frame
  count) and the project settings as project.json holds them; newer or
  foreign files are refused, frame positions apply only to the frames they
  were saved for, saving never replaces an earlier file.
- Data ProjectBundles: the file for a sequence of the index, and the sequence
  a file names (by id, else the same folder of the same archive).
- Editor: "Save project file" on the Export tab (sequences of this PC's
  archives); opening a file shows its sequence and applies the settings as
  one undo step with an Undo toast, or says in one sentence why not (not in
  the library, unreadable, a job runs).
- Titano.exe opens its file argument once the index is open and runs once
  per data folder: a later launch hands its file over a current-user named
  pipe, lets the running window come to the front and exits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 10:29:37 +00:00

158 lines
6.9 KiB
C#

using System.IO;
using System.Windows;
using Titano.App.DesignTime;
using Titano.App.Editor;
using Titano.App.Infrastructure;
using Titano.App.Shell;
using Titano.App.Tests.Windows;
using Titano.Core.Localization;
using Titano.Core.Project;
using Titano.Testing;
using Titano.Ui.Controls;
using Titano.Ui.Theming;
namespace Titano.App.Tests.Editor;
/// <summary>
/// <c>.titano</c> project files (BRIEF §18) in the real main window on the editor sample: the editor saves one, and
/// opening one shows its sequence with the file's settings as one undo step, or says in one sentence why not.
/// </summary>
[WindowsOnly]
[Serial]
[Timeout(300_000)]
public sealed class ProjectFilesTests(TestContext context)
{
[Test]
public Task AProjectFileSavesAndOpensItsSequenceWithItsSettings() => ScreenHost.RunAsync(async () =>
{
var folder = context.CreateTempDirectory("project-files");
var environment = AppEnvironment.ForCapture();
UiLanguage.Apply("en");
ThemeManager.Apply(AppTheme.Dark);
var services = new AppServices(environment, new AppSettings { Language = "en", FirstRunCompleted = true }, () => { }, new FolderDialogs(folder));
var sample = SampleData.Configure(services);
var shell = new ShellViewModel(services);
var toasts = new List<Toast>();
shell.PendingToasts.CollectionChanged += (_, e) => toasts.AddRange(e.NewItems?.OfType<Toast>() ?? []);
var window = new MainWindow(shell)
{
WindowStartupLocation = WindowStartupLocation.Manual,
Left = -20000,
Top = -20000,
Width = 1200,
Height = 788,
ShowActivated = false,
ShowInTaskbar = false,
};
window.Show();
try
{
await sample.OpenEmptyAsync();
await shell.StartAsync();
var id = await sample.FillEditorAsync();
var files = new ProjectFiles(shell);
// Saved under the sequence's title, never over an earlier file.
var editor = Assert.NotNull(await shell.Sequence.ShowAsync(id));
await files.SaveAsync(editor.Session);
await files.SaveAsync(editor.Session);
var path = Path.Combine(folder, "Harbour sunset.titano");
Assert.FileExists(path);
Assert.FileExists(Path.Combine(folder, "Harbour sunset (2).titano"));
Assert.Equal(Loc.L("Editor.ProjectFile.Saved"), toasts[^1].Title);
var bundle = ProjectBundle.Load(path);
Assert.Equal(id, bundle.Sequence.Id);
Assert.Equal(editor.FrameCount, bundle.Sequence.Frames);
Assert.Equal(editor.Session.Project.ToJson(), bundle.Project.ToJson());
// Opening it with other settings shows the sequence with them, as one step Undo takes back.
var before = editor.Session.Project;
bundle.ExposureOffset(0.7).Save(path);
await files.OpenAsync(path);
Assert.Same(editor, shell.Sequence.Editor);
Assert.Equal(0.7, editor.Session.Project.Exposure.ExposureOffsetEv);
var applied = toasts[^1];
Assert.Equal(Loc.L("Editor.ProjectFile.Applied"), applied.Title);
Assert.False(applied.IsError);
applied.ActionCommand!.Execute(null);
Assert.Equal(before.ToJson(), editor.Session.Project.ToJson());
// A sequence this library does not have, and a file that is not a project: one sentence each.
var elsewhere = bundle with
{
Archive = bundle.Archive with { Id = Guid.CreateVersion7() },
Sequence = bundle.Sequence with { Id = Guid.CreateVersion7(), Folder = "2026/elsewhere" },
};
var missing = Path.Combine(folder, "Elsewhere.titano");
elsewhere.Save(missing);
await files.OpenAsync(missing);
Assert.Equal(Loc.L("Editor.ProjectFile.NotFound"), toasts[^1].Title);
Assert.True(toasts[^1].IsError);
var text = Path.Combine(folder, "notes.titano");
await File.WriteAllTextAsync(text, "not a project");
await files.OpenAsync(text);
Assert.Equal(Loc.L("Editor.ProjectFile.Unreadable"), toasts[^1].Title);
Assert.True(toasts[^1].IsError);
Assert.Same(editor, shell.Sequence.Editor, "the open sequence stays");
await editor.Session.FlushAsync();
}
finally
{
window.Close();
await shell.Sequence.CloseAsync();
shell.Dispose();
await services.DisposeAsync();
environment.DeleteCaptureData();
}
});
[Test]
public void AHandOverMessageRoundTrips()
{
string[] args = [@"C:\Users\me\Desktop\Harbour sunset.titano"];
Assert.SequenceEqual(args, SingleInstance.Decode(SingleInstance.Encode(args))!);
Assert.Empty(SingleInstance.Decode(SingleInstance.Encode([]))!);
Assert.Null(SingleInstance.Decode("TITANO-UPLOADER/1\nE:\\"u8));
}
[Test]
public void ASecondLaunchOnTheSameDataFolderHandsItsFileToTheFirst()
{
var data = context.CreateTempDirectory("instance");
var first = Assert.NotNull(SingleInstance.TryAcquire(data));
try
{
var received = new TaskCompletionSource<IReadOnlyList<string>>(TaskCreationOptions.RunContinuationsAsynchronously);
first.Listen(args => received.TrySetResult(args));
Assert.Null(SingleInstance.TryAcquire(data), "one Titano per data folder");
using (var other = SingleInstance.TryAcquire(context.CreateTempDirectory("other-copy")))
Assert.NotNull(other, "a portable copy with its own data folder runs on its own");
Assert.True(SingleInstance.HandOver(data, [@"D:\Projects\Roof.titano"]));
Assert.True(received.Task.Wait(TimeSpan.FromSeconds(10)), "the running instance received the hand-over");
Assert.SequenceEqual([@"D:\Projects\Roof.titano"], received.Task.Result);
}
finally
{
first.Dispose();
}
}
/// <summary>Dialogs that pick one folder and cancel everything else.</summary>
private sealed class FolderDialogs(string folder) : IAppDialogs
{
public string? PickFolder(string title, string? initialFolder = null) => folder;
public string? PickJsonFile(string title) => null;
public bool Confirm(ConfirmRequest request) => false;
public bool SignIn(string server, string? username, Func<string, string, Task<string?>> signIn) => false;
}
}
internal static class ProjectBundleTestExtensions
{
/// <summary>The same file with another exposure offset.</summary>
public static ProjectBundle ExposureOffset(this ProjectBundle bundle, double ev) =>
bundle with { Project = bundle.Project with { Exposure = bundle.Project.Exposure with { ExposureOffsetEv = ev } } };
}