using Chronos.Core.Diagnostics.Synthetic;
using Chronos.Core.Localization;
using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Processing;
using Chronos.Testing;
namespace Chronos.Data.Tests.Processing;
///
/// The project of a sequence as the engine reads and writes it (owner review 1, phase 4): a format-1 project stays format 1
/// through every edit that uses nothing newer, a transform raises it to 2 and its removal brings it back, and settings of
/// a newer Chronos are refused saying so in the user's language.
///
[Category("processing")]
public sealed class ProjectStoreTests(TestContext context)
{
[Test]
public async Task AnEditNeverRaisesTheFormatOfAProjectThatUsesNothingNewer()
{
await using var fixture = await ProcessingFixture.CreateAsync(context);
var sequence = await fixture.AddAsync(SyntheticSequence.Create(new SyntheticSequenceOptions { Width = 64, Height = 48, FrameCount = 4 }));
var store = new ProjectStore(fixture.Db, new SequenceWriter(new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }));
async Task StoredFormatAsync()
{
var row = (await fixture.Db.Sequences.GetAsync(sequence.Id, fixture.Ct))!;
var file = await File.ReadAllTextAsync(fixture.Folder(sequence).ProjectPath, fixture.Ct);
Assert.Equal(row.SettingsJson, file, "the index and project.json hold the same text");
return ProjectContent.FormatOf(file);
}
Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Exposure = p.Exposure with { ExposureOffsetEv = 0.5 } }, fixture.Ct));
Assert.Equal(ProjectSettings.BaseVersion, await StoredFormatAsync());
// A read project of the base format is never stamped higher, even when it says the highest this Chronos reads.
Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Version = ProjectSettings.CurrentVersion, HeroFrame = 2 }, fixture.Ct));
Assert.Equal(ProjectSettings.BaseVersion, await StoredFormatAsync());
Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Camera = p.Camera with { Rotate = 90 } }, fixture.Ct));
Assert.Equal(2, await StoredFormatAsync());
Assert.True(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Camera = p.Camera with { Rotate = 0 } }, fixture.Ct));
Assert.Equal(ProjectSettings.BaseVersion, await StoredFormatAsync());
Assert.False(await store.UpdateAsync(fixture.Archive.Paths, sequence, p => p with { Version = ProjectSettings.BaseVersion }, fixture.Ct), "nothing changed");
}
[Test]
public void SettingsOfANewerChronosAreRefusedSayingSo()
{
var refused = Assert.Throws(() => ProjectStore.Parse("""{"version":3,"camera":{"rotate":90}}"""));
Assert.Equal(Loc.L("Errors.Render.NewerSettings"), refused.Message);
Assert.Equal(90, ProjectStore.Parse("""{"version":2,"camera":{"rotate":90}}""").Camera.Rotate);
// Text that is not a project keeps its own reason.
Assert.NotEqual(Loc.L("Errors.Render.NewerSettings"), Assert.Throws(() => ProjectStore.Parse("[1]")).Message);
}
}