using System.Net; using System.Text; using System.Text.Json; using Febe.Tests.Support; using Febe.Tests.Web.Support; using Febe.Web.Demo; namespace Febe.Tests.Web; /// /// The commands of the copies to external folders over the demo (tappa E2): an own folder added with the picker and /// taken away, the switch of an account, «Lo stesso post in due account», «Copia di nuovo» of a file «Tolto» and the /// resume of the interrupted job. One demo for the whole sequence, which changes it. /// [TestClass] public sealed class ExportApiDemoCommandTests { [TestMethod] public async Task TheCommandsChangeWhatTheCardTheFilterAndTheJobsShow() { using var temp = new TempFolder("demo-export-commands"); await using var demo = await DemoHost.StartAsync(new DemoOptions(0, TestZones.Rome, temp.Path)); using var client = new HttpClient { BaseAddress = demo.Address, Timeout = TimeSpan.FromSeconds(60) }; var origin = $"{demo.Address.Scheme}://{demo.Address.Authority}"; async Task<(HttpStatusCode Status, JsonElement Body)> SendAsync(HttpMethod method, string path, object? body = null) { using var request = new HttpRequestMessage(method, path); if (body is not null) { request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); } request.Headers.Add("Origin", origin); using var response = await client.SendAsync(request); var text = await response.Content.ReadAsStringAsync(); return (response.StatusCode, text.Length == 0 ? default : JsonDocument.Parse(text).RootElement.Clone()); } async Task OkAsync(HttpMethod method, string path, object? body = null) { var (status, answer) = await SendAsync(method, path, body); Assert.AreEqual(HttpStatusCode.OK, status, $"{method} {path}: {answer}"); return answer; } async Task WaitIdleAsync() { for (var i = 0; i < 300; i++) { if (!(await OkAsync(HttpMethod.Get, "/api/export/progress")).Bool("running")) { return; } await Task.Delay(100); } Assert.Fail("the copy did not end"); } var accounts = (await OkAsync(HttpMethod.Get, "/api/accounts")).Items("accounts").ToList(); long Id(string username) => accounts.First(a => a.Str("username") == username).Long("id"); var marta = Id(DemoExport.CopiedAccount); var giulia = Id("giulia.corre.demo"); // An own folder for @giulia.corre.demo with the demo's picker, then taken away (the copies would stay). var picked = (await OkAsync(HttpMethod.Post, "/api/export/pick-folder", new { })).Str("path"); var saved = await OkAsync(HttpMethod.Post, "/api/export/destinations", new { scope = "account", accountId = giulia, path = picked, label = (string?)null, types = new[] { "postFoto", "storie" }, namingScheme = (string?)null, highlightNamingScheme = (string?)null, profileNamingScheme = (string?)null, }); var destination = saved.GetProperty("destination"); Assert.AreEqual(DemoExport.PickedFolder, destination.Str("label")); Assert.AreEqual("giulia.corre.demo", destination.Str("username")); Assert.IsTrue((await OkAsync(HttpMethod.Get, $"/api/accounts/{giulia}")).GetProperty("export").Bool("hasOwnFolder")); var status = await OkAsync(HttpMethod.Get, $"/api/export/destinations/{destination.Long("id")}/status"); Assert.IsTrue(status.Bool("reachable"), status.ToString()); var (twice, _) = await SendAsync(HttpMethod.Post, "/api/export/destinations", new { scope = "account", accountId = giulia, path = picked, types = new[] { "postFoto" } }); Assert.AreEqual(HttpStatusCode.BadRequest, twice, "one own folder per account"); Assert.Contains("restano", (await OkAsync(HttpMethod.Delete, $"/api/export/destinations/{destination.Long("id")}")).Str("message")); Assert.IsFalse((await OkAsync(HttpMethod.Get, $"/api/accounts/{giulia}")).GetProperty("export").Bool("hasOwnFolder")); // The switch of an account acts at once on its card. await OkAsync(HttpMethod.Post, $"/api/accounts/{giulia}/export-enabled", new { value = false }); Assert.AreEqual(0, (await OkAsync(HttpMethod.Get, $"/api/accounts/{giulia}")).GetProperty("export").Int("due")); await OkAsync(HttpMethod.Post, $"/api/accounts/{giulia}/export-enabled", new { value = true }); Assert.IsGreaterThan(0, (await OkAsync(HttpMethod.Get, $"/api/accounts/{giulia}")).GetProperty("export").Int("due")); // «Lo stesso post in due account». await OkAsync(HttpMethod.Put, "/api/export/settings", new { samePost = "oneOnly" }); Assert.AreEqual("oneOnly", (await OkAsync(HttpMethod.Get, "/api/export")).Str("samePost")); await OkAsync(HttpMethod.Put, "/api/export/settings", new { samePost = "onePerAccount" }); Assert.AreEqual(HttpStatusCode.BadRequest, (await SendAsync(HttpMethod.Put, "/api/export/settings", new { samePost = "tutti" })).Status); // «Copia di nuovo» of the file «Tolto» from the viewer: the copy is written again. var before = (await OkAsync(HttpMethod.Get, $"/api/accounts/{marta}")).GetProperty("export"); var tiles = (await OkAsync(HttpMethod.Get, $"/api/library?accounts={marta}&mode=files&copies=removedOrSuperseded&limit=600")).Items("tiles").ToList(); long? removedCopy = null; foreach (var tile in tiles) { var file = (await OkAsync(HttpMethod.Get, $"/api/entries/{tile.Long("id")}")).Items("media").First(m => m.Long("id") == tile.GetProperty("cover").Long("mediaId")); if (file.Items("copies").FirstOrDefault(c => c.Str("state") == "removed" && c.Bool("canCopyAgain")) is { ValueKind: JsonValueKind.Object } copy) { removedCopy = copy.Long("copyId"); break; } } Assert.IsNotNull(removedCopy, "the demo has a file «Tolto»"); var job = await OkAsync(HttpMethod.Post, "/api/export/copy-again", new { copyId = removedCopy }); Assert.HasCount(1, job.Items("runIds").ToList()); await WaitIdleAsync(); var after = (await OkAsync(HttpMethod.Get, $"/api/accounts/{marta}")).GetProperty("export"); Assert.IsLessThan(before.Int("removedOrSuperseded"), after.Int("removedOrSuperseded")); Assert.AreEqual(HttpStatusCode.BadRequest, (await SendAsync(HttpMethod.Post, "/api/export/copy-again", new { copyId = removedCopy })).Status, "a copied file is not copied again"); // «Riprendi» of the job left interrupted: it ends, and nothing is left to resume. Assert.AreEqual(1, (await OkAsync(HttpMethod.Get, "/api/export")).Int("unfinishedRuns")); await OkAsync(HttpMethod.Post, "/api/export/resume"); await WaitIdleAsync(); Assert.AreEqual(0, (await OkAsync(HttpMethod.Get, "/api/export")).Int("unfinishedRuns")); var runs = (await OkAsync(HttpMethod.Get, "/api/export/runs")).EnumerateArray().ToList(); Assert.IsFalse(runs.Any(r => r.Str("state") is "interrupted" or "running" or "paused")); Assert.IsLessThan(after.Int("notCopied"), (await OkAsync(HttpMethod.Get, $"/api/accounts/{marta}")).GetProperty("export").Int("notCopied")); } }