using System.Net; using System.Net.Http.Json; using System.Text.RegularExpressions; using Chronos.Protocol; using Chronos.Testing; namespace Chronos.Server.Tests.Hosting; /// /// The Videos page of the admin panel (owner review 1, R9, decision 22; Admin/web/pages/videos.js): in the menu after /// the Library, what it imports and calls (the videos API, never an edit or a render: owner review 2, R6), the delete's dry /// run before the move with the plan's fingerprint, Copy's reasons when it is off, the player, and its strings; and what the /// Trash, a sequence and Settings show of the videos library. /// [Category("server")] public sealed partial class AdminVideosTests(TestContext context) { [Test] public async Task TheVideosPageIsInTheMenuAfterTheLibraryAndLoads() { await using var server = await TestServer.StartAsync(context); var app = await server.Client.GetStringAsync("/admin/app.js"); var top = Regex.Match(app, @"const topPages = \[([^\]]*)\]").Groups[1].Value.Split(',').Select(p => p.Trim().Trim('"')).ToList(); Assert.Equal(top.IndexOf("library") + 1, top.IndexOf("videos")); Assert.Contains(" videos: ` m.Groups[1].Value).ToList(); Assert.SequenceEqual(["../i18n.js", "../ui.js", "../icons.js", "./library/shared.js"], imports); // The panel runs under a strict CSP: no inline handlers or styles. Assert.True(!Regex.IsMatch(page, @"\son[a-z]+="""), "inline handler"); Assert.True(!page.Contains(" style=", StringComparison.Ordinal), "inline style"); // A sequence's renders name their kind and hash as the Videos page does. foreach (var helper in new[] { "videoKind(v)", "kindText(kind)", "codecLabel(codec)", "shortHash(sha256)" }) Assert.Contains($"export function {helper}", page); var sequence = await server.Client.GetStringAsync("/admin/pages/library/sequence.js"); Assert.Contains(@"import { videoKind, kindText, shortHash } from ""../videos.js"";", sequence); } [Test] public async Task TheVideosPageManagesAndNeverEdits() { await using var server = await TestServer.StartAsync(context); var page = await server.Client.GetStringAsync("/admin/pages/videos.js"); var calls = ApiCall().Matches(page).Select(m => (Method: m.Groups[1].Value, Target: m.Groups[2].Value)).ToList(); Assert.True(calls.Count >= 4, string.Join(", ", calls)); // Reads the list, the server's videos folder and a filtered sequence's title; writes only the videos' delete and copy: // no settings, no project, no render, no job of its own. foreach (var (method, target) in calls) { Assert.True(method is "get" or "post", $"api.{method}({target}"); if (method == "get") Assert.True(target is "listQuery(" or @"""/api/v1/server/settings""" or "`/api/v1/sequences/${encodeURIComponent(id)}`", target); else Assert.True(target is @"""/api/v1/videos/copy""" or @"""/api/v1/videos/delete""", target); } Assert.Contains("return `/api/v1/videos?${p}`;", page); foreach (var never in new[] { "settingsJson", "/projects", "/jobs\"", "api.put(", "api.patch(", "api.del(" }) Assert.False(page.Contains(never, StringComparison.Ordinal), never); // A render's file is read (play, download); nothing asks for a render. Assert.False(Regex.IsMatch(page, "/render(?!s/)"), "a render request"); // Delete: the dry run, the confirmation (copies stay), then the delete with the plan's fingerprint; a changed plan reloads. var dryRun = page.IndexOf(@"api.post(""/api/v1/videos/delete"", { ids, dryRun: true, fingerprint: null })", StringComparison.Ordinal); var confirm = page.IndexOf("const confirmed = await confirmDialog({", Math.Max(0, dryRun), StringComparison.Ordinal); var delete = page.IndexOf(@"api.post(""/api/v1/videos/delete"", { ids, dryRun: false, fingerprint: plan.fingerprint })", StringComparison.Ordinal); Assert.True(dryRun > 0 && confirm > dryRun && delete > confirm, "dry run, confirmation, delete"); Assert.Contains(@"t(""Admin.Videos.Delete.CopiesStay"")", page); Assert.Contains("list: listed([...blocked, ...going]),", page); Assert.Contains(@"if (err.code === ""plan_changed"") await load();", page); // Copy is off, saying why, without the right or without a videos folder; a folder unset since says so from then on. Assert.Contains(@"const copyWhy = !may(EDIT) ? ""Admin.Videos.Copy.NoRight.Tip"" : folder === """" ? ""Admin.Videos.Copy.NoFolder.Tip"" : null;", page); Assert.Contains(@"const deleteWhy = may(DELETE) ? null : ""Admin.Videos.Delete.NoRight.Tip"";", page); Assert.Contains(@"if (err.code === ""videos_folder_unset"") folder = """";", page); Assert.Contains("const EDIT = 4;", page); Assert.Contains("const DELETE = 8;", page); Assert.Equal(4L, (long)Chronos.Core.Model.Permissions.Edit); Assert.Equal(8L, (long)Chronos.Core.Model.Permissions.Delete); // The player: a