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; /// /// 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. /// 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()); Assert.Equal("/videos/Harbour sunset.mp4", json["copies"]![0]!["path"]!.GetValue()); 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()); 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(), hashed["kind"]!.GetValue())); 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); } }