using System.Collections.Concurrent;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.DependencyInjection;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Archiving.Jobs;
using Chronos.Data.Jobs;
using Chronos.Data.Videos;
using Chronos.Protocol;
using Chronos.Protocol.Client;
using Chronos.Server.Api;
using Chronos.Server.Configuration;
using Chronos.Server.Storage;
using Chronos.Testing;
namespace Chronos.Server.Tests.Api;
///
/// /api/v1/videos (owner review 1, phase 5, R9) against a library that answers what each test sets: who may read,
/// delete and copy, the dry run and its fingerprint, the videos folder, the audit entries, a video restored through the
/// trash routes, and the protocol client's calls.
///
[Category("server")]
public sealed class VideoEndpointTests(TestContext context)
{
[Test]
public async Task ReadersListAndPlayEditorsCopyAndDeleters()
{
var library = new FakeVideoLibrary();
await using var server = await StartAsync(library);
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
var (uploader, _, _) = await server.UserAsync("ugo", "uploader");
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var render = await RenderAsync(server, editor, library);
using (var anonymous = await server.Client.GetAsync("/api/v1/videos")) await anonymous.ExpectAsync(HttpStatusCode.Unauthorized);
using (var refused = await uploader.GetAsync("/api/v1/videos")) await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var listed = await viewer.GetAsync("/api/v1/videos"))
{
var page = await listed.ReadAsync(ProtocolJson.Default.VideoPageDto);
var video = page.Items.Single();
Assert.Equal((render.Id, "Harbour sunset", RenderKind.Video, VideoStates.Ok, "Harbour sunset - 4K.mp4"),
(video.Id, video.SequenceTitle, video.Kind, video.State, video.FileName));
Assert.Equal("/videos/Harbour sunset - 4K.mp4", video.Copies.Single().Path);
}
using (var one = await viewer.GetAsync($"/api/v1/videos/{render.Id}"))
Assert.Equal(render.Sha256, (await one.ReadAsync(ProtocolJson.Default.VideoDto)).Sha256);
using (var unknown = await viewer.GetAsync($"/api/v1/videos/{Guid.CreateVersion7()}"))
Assert.Equal("render", (await unknown.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details);
// A reader plays the file: a