|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | +using IPCamLapse.Models; |
| 4 | +using IPCamLapse.Services; |
| 5 | +using Microsoft.AspNetCore.Hosting; |
| 6 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 9 | + |
| 10 | +namespace IPCamLapse.Tests; |
| 11 | + |
| 12 | +public sealed class CapturePipelineIntegrationTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public async Task DemoCaptureRendersAndDownloadsVideo() |
| 16 | + { |
| 17 | + var root = Path.Combine(Path.GetTempPath(), $"ipcamlapse-integration-{Guid.NewGuid():N}"); |
| 18 | + Directory.CreateDirectory(root); |
| 19 | + try |
| 20 | + { |
| 21 | + await using var factory = new IPCamLapseFactory(root); |
| 22 | + using var client = factory.CreateClient(new WebApplicationFactoryClientOptions |
| 23 | + { |
| 24 | + AllowAutoRedirect = false, |
| 25 | + HandleCookies = true |
| 26 | + }); |
| 27 | + var sessions = factory.Services.GetRequiredService<ICaptureSessionService>(); |
| 28 | + var session = await sessions.CreateSessionAsync(new CaptureSession |
| 29 | + { |
| 30 | + Id = "1a2b3c4d", |
| 31 | + Name = "Integration demo", |
| 32 | + Configuration = new CaptureConfiguration |
| 33 | + { |
| 34 | + CameraProfileId = CameraProfileService.DemoProfileId, |
| 35 | + CaptureIntervalSeconds = 1, |
| 36 | + CaptureDurationSeconds = 2, |
| 37 | + VideoTargetDurationSeconds = 1, |
| 38 | + MaxCaptureRetries = 0, |
| 39 | + MaxConsecutiveFailures = 1 |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + var page = await client.GetStringAsync($"/Sessions/Details/{session.Id}"); |
| 44 | + var token = Regex.Match(page, "<meta name=\"csrf-token\" content=\"([^\"]+)\"").Groups[1].Value; |
| 45 | + Assert.NotEmpty(token); |
| 46 | + client.DefaultRequestHeaders.Add("X-CSRF-TOKEN", token); |
| 47 | + |
| 48 | + var started = await client.PostAsync($"/api/sessions/{session.Id}/start", null); |
| 49 | + started.EnsureSuccessStatusCode(); |
| 50 | + |
| 51 | + var stopwatch = Stopwatch.StartNew(); |
| 52 | + CaptureSession? completed = null; |
| 53 | + while (stopwatch.Elapsed < TimeSpan.FromSeconds(15)) |
| 54 | + { |
| 55 | + completed = await sessions.GetSessionAsync(session.Id); |
| 56 | + if (completed?.Status is SessionStatus.Completed or SessionStatus.Failed) |
| 57 | + break; |
| 58 | + await Task.Delay(100); |
| 59 | + } |
| 60 | + |
| 61 | + Assert.NotNull(completed); |
| 62 | + Assert.Equal(SessionStatus.Completed, completed.Status); |
| 63 | + Assert.True(completed.CapturedFrameCount >= 2); |
| 64 | + Assert.NotNull(completed.VideoPath); |
| 65 | + |
| 66 | + var download = await client.GetAsync($"/api/sessions/{session.Id}/video"); |
| 67 | + download.EnsureSuccessStatusCode(); |
| 68 | + Assert.Equal("video/mp4", download.Content.Headers.ContentType?.MediaType); |
| 69 | + Assert.Equal(FakeVideoService.Content, await download.Content.ReadAsByteArrayAsync()); |
| 70 | + } |
| 71 | + finally |
| 72 | + { |
| 73 | + if (Directory.Exists(root)) |
| 74 | + Directory.Delete(root, true); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private sealed class IPCamLapseFactory : WebApplicationFactory<Program> |
| 79 | + { |
| 80 | + private readonly string _root; |
| 81 | + |
| 82 | + public IPCamLapseFactory(string root) |
| 83 | + { |
| 84 | + _root = root; |
| 85 | + } |
| 86 | + |
| 87 | + protected override void ConfigureWebHost(IWebHostBuilder builder) |
| 88 | + { |
| 89 | + builder.UseSetting("Storage:DataPath", Path.Combine(_root, "data")); |
| 90 | + builder.ConfigureServices(services => |
| 91 | + { |
| 92 | + services.RemoveAll<IVideoService>(); |
| 93 | + services.AddSingleton<IVideoService, FakeVideoService>(); |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private sealed class FakeVideoService : IVideoService |
| 99 | + { |
| 100 | + public static readonly byte[] Content = "integration-video"u8.ToArray(); |
| 101 | + |
| 102 | + public async Task<VideoRenderResult> CreateTimeLapseAsync( |
| 103 | + VideoRenderRequest request, |
| 104 | + CancellationToken cancellationToken = default) |
| 105 | + { |
| 106 | + await File.WriteAllBytesAsync(request.OutputPath, Content, cancellationToken); |
| 107 | + return new VideoRenderResult(true, request.OutputPath, null); |
| 108 | + } |
| 109 | + |
| 110 | + public Task<bool> IsFfmpegAvailableAsync() => Task.FromResult(true); |
| 111 | + } |
| 112 | +} |
0 commit comments