Files
Chronos/tests/Chronos.Server.Tests/Api/VideoContractTests.cs
T
Alby96andClaude Opus 5.5 728ffa35cb
CI / Build and test (push) Successful in 9m41s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 9s
Release / Build, test and publish (push) Successful in 14m49s
CI / PostgreSQL 16 integration (push) Successful in 8m51s
CI / PostgreSQL 17 integration (push) Successful in 9m48s
CI / Docker image (push) Successful in 54s
Release / Server image (push) Successful in 1m39s
feat: export panel and Render menu, playback options, videos library (owner review 1 phase 5) (#27)
Owner review 1 R3, R8, R9. A grouped Export step and Render menu; encoding, playback (reverse, boomerang, loops, fades, hold) and still frames; one videos library on the desktop and the server with a verified trash and copies to a videos folder.

Closes #21

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-05 09:18:40 +01:00

116 lines
6.9 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Videos;
using Chronos.Protocol;
using Chronos.Testing;
namespace Chronos.Server.Tests.Api;
/// <summary>
/// The wire shapes of the videos library (owner review 1, phase 5): camelCase members, nulls left out, and the members
/// added to existing DTOs trailing and optional, so a client or server of before reads and writes them as it did.
/// </summary>
public sealed class VideoContractTests
{
private static readonly Guid RenderId = Guid.Parse("01890000-0000-7000-8000-000000000001");
private static readonly Guid SequenceId = Guid.Parse("01890000-0000-7000-8000-000000000002");
private static readonly DateTimeOffset Created = new(2026, 11, 5, 18, 30, 0, TimeSpan.Zero);
[Test]
public void AVideoTravelsWithItsSequenceStateAndCopies()
{
var video = new VideoDto(RenderId, SequenceId, "Web 1080p", "h264", 1920, 1080, 25, 12.5, 4_000_000, Created, "PC-A", null, null,
new string('a', 64), RenderKind.Video, "Harbour sunset", new DateTime(2026, 10, 1, 18, 0, 0), "Main", 3, VideoStates.Ok,
[new VideoCopyDto("/videos/Harbour sunset.mp4", Created)], "Harbour sunset.mp4");
var json = JsonNode.Parse(JsonSerializer.Serialize(video, ProtocolJson.Default.VideoDto))!.AsObject();
Assert.SequenceEqual(
["id", "sequenceId", "presetName", "codec", "width", "height", "fps", "durationSeconds", "bytes", "created", "producedBy", "sha256",
"kind", "sequenceTitle", "sequenceFirstCapture", "projectName", "projectVersionNumber", "state", "copies", "fileName"],
json.Select(p => p.Key));
Assert.Equal("video", json["kind"]!.GetValue<string>());
Assert.Equal("/videos/Harbour sunset.mp4", json["copies"]![0]!["path"]!.GetValue<string>());
var back = JsonSerializer.Deserialize(json.ToJsonString(), ProtocolJson.Default.VideoDto)!;
Assert.Equal(video with { Copies = back.Copies }, back);
Assert.Equal(video.Copies[0], back.Copies[0]);
var page = new VideoPageDto([video], "cursor-2");
var pageJson = JsonNode.Parse(JsonSerializer.Serialize(page, ProtocolJson.Default.VideoPageDto))!.AsObject();
Assert.SequenceEqual(["items", "next"], pageJson.Select(p => p.Key));
}
[Test]
public void DeletingIsADryRunThenTheSameFingerprint()
{
var request = JsonSerializer.Serialize(new VideoDeleteRequest([RenderId], DryRun: true), ProtocolJson.Default.VideoDeleteRequest);
Assert.Equal($$"""{"ids":["{{RenderId}}"],"dryRun":true}""", request);
var real = JsonSerializer.Deserialize("""{ "ids": [], "dryRun": false, "fingerprint": "abc" }""", ProtocolJson.Default.VideoDeleteRequest)!;
Assert.Equal((false, "abc"), (real.DryRun, real.Fingerprint));
var plan = new VideoDeletePlanDto([new VideoDeleteItemDto(RenderId, SequenceId, "Harbour sunset", "a.mp4", 10, Missing: false)], 10,
[new VideoBlockedDto(Guid.Empty, VideoBlockReasons.Busy)], "f1", DryRun: true);
var json = JsonNode.Parse(JsonSerializer.Serialize(plan, ProtocolJson.Default.VideoDeletePlanDto))!.AsObject();
Assert.SequenceEqual(["items", "bytes", "blocked", "fingerprint", "dryRun"], json.Select(p => p.Key));
Assert.Equal("busy", json["blocked"]![0]!["reason"]!.GetValue<string>());
var copy = JsonSerializer.Serialize(new VideoCopyRequest([RenderId]), ProtocolJson.Default.VideoCopyRequest);
Assert.Equal($$"""{"ids":["{{RenderId}}"]}""", copy);
Assert.Equal(RenderId, JsonSerializer.Deserialize($$"""{"jobId":"{{RenderId}}"}""", ProtocolJson.Default.VideoCopyResultDto)!.JobId);
Assert.Equal("videos", ServerCapabilities.Videos);
}
[Test]
public void TheMembersAddedToOlderDtosAreLeftOutUntilSet()
{
var render = new RenderDto(RenderId, "MJPEG", "mjpeg", 96, 64, 25, 1, 10, Created, null);
var renderJson = JsonSerializer.Serialize(render, ProtocolJson.Default.RenderDto);
Assert.False(renderJson.Contains("sha256", StringComparison.Ordinal) || renderJson.Contains("kind", StringComparison.Ordinal), renderJson);
var hashed = JsonNode.Parse(JsonSerializer.Serialize(render with { Sha256 = "ab", Kind = RenderKind.Still }, ProtocolJson.Default.RenderDto))!;
Assert.Equal(("ab", "still"), (hashed["sha256"]!.GetValue<string>(), hashed["kind"]!.GetValue<string>()));
var parameters = new RenderJobParameters("MJPEG", "{}", "mjpeg", 96, 64, false, null);
var parametersJson = JsonSerializer.Serialize(parameters, ProtocolJson.Default.RenderJobParameters);
Assert.False(parametersJson.Contains("still", StringComparison.OrdinalIgnoreCase), parametersJson);
Assert.False(parameters.IsStill);
var still = JsonSerializer.Deserialize(
JsonSerializer.Serialize(parameters with { StillFrame = 41, StillPhoto = "cd", RequiredSettingsFormat = 3 }, ProtocolJson.Default.RenderJobParameters),
ProtocolJson.Default.RenderJobParameters)!;
Assert.Equal(((int?)41, "cd", 3, true), (still.StillFrame, still.StillPhoto, still.RequiredSettingsFormat, still.IsStill));
var result = JsonSerializer.Deserialize("""{ "fileName": "a.png", "size": 1, "sha256": "ab", "presetName": "PNG", "width": 1, "height": 1, "fps": 0, "durationSeconds": 0 }""",
ProtocolJson.Default.RenderResultRequest)!;
Assert.Null(result.Kind);
}
[Test]
public void TheLibraryStubAnswersEmptyUntilTheLibraryLands()
{
var stub = NotImplementedVideoLibrary.Instance;
Assert.False(stub.IsAvailable);
Assert.Equal(VideoErrors.FolderUnset, stub.FolderProblem(" "));
Assert.Null(stub.FolderProblem("/videos"));
Assert.Empty(stub.ListAsync(new VideoQuery()).GetAwaiter().GetResult().Items);
Assert.Null(stub.GetAsync(RenderId).GetAwaiter().GetResult());
}
[Test]
public void TrashEntriesAndCopyParametersReadBack()
{
var name = VideoTrashNames.EntryName(RenderId, Created);
Assert.True(name.StartsWith(VideoTrashNames.Prefix, StringComparison.Ordinal));
Assert.True(VideoTrashNames.TryParse(name, out var id, out var ticks));
Assert.Equal((RenderId, Created.UtcTicks), (id, ticks));
Assert.False(VideoTrashNames.IsEntryName($"sequence-{RenderId}"));
Assert.False(VideoTrashNames.IsEntryName(null));
Assert.Equal("videos", TrashKind.Videos.ToString().ToLowerInvariant());
var parameters = new VideoCopyParameters([RenderId], "/videos", FromServer: true, RequestedBy: "alberto");
var back = Assert.NotNull(VideoCopyParameters.Read(parameters.ToJson()));
Assert.Equal((parameters.Folder, parameters.FromServer, parameters.RequestedBy), (back.Folder, back.FromServer, back.RequestedBy));
Assert.SequenceEqual(parameters.RenderIds, back.RenderIds);
Assert.Null(VideoCopyParameters.Read("not json"));
Assert.Equal(24, (int)JobKind.VideoCopy);
}
}