|
1 | 1 | using System.Collections.Generic; |
| 2 | +using System.Globalization; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
2 | 5 | using System.Text; |
| 6 | +using System.Text.Json; |
3 | 7 | using MarkItDown.Converters; |
4 | 8 | using MarkItDown.Intelligence.Models; |
5 | 9 | using MarkItDown.YouTube; |
6 | 10 | using Shouldly; |
7 | 11 | using Xunit; |
| 12 | +using MarkItDown.Tests; |
8 | 13 |
|
9 | 14 | namespace MarkItDown.Tests.Converters; |
10 | 15 |
|
@@ -50,6 +55,72 @@ private sealed class NullYouTubeMetadataProvider : IYouTubeMetadataProvider |
50 | 55 | } |
51 | 56 | } |
52 | 57 |
|
| 58 | + [Fact] |
| 59 | + public async Task ConvertAsync_WithRecordedMetadata_RendersVideoDetails() |
| 60 | + { |
| 61 | + var metadata = LoadRecordedMetadata(); |
| 62 | + var provider = new FixtureYouTubeMetadataProvider(metadata); |
| 63 | + var converter = new YouTubeUrlConverter(provider); |
| 64 | + var streamInfo = new StreamInfo(url: "https://www.youtube.com/watch?v=8hnpIIamb6k"); |
| 65 | + |
| 66 | + var result = await converter.ConvertAsync(Stream.Null, streamInfo); |
| 67 | + |
| 68 | + result.Title.ShouldBe(metadata.Title); |
| 69 | + result.Markdown.ShouldContain(metadata.Title); |
| 70 | + result.Markdown.ShouldContain("Managed Code"); |
| 71 | + result.Markdown.ShouldContain("**Views:** 483"); |
| 72 | + result.Markdown.ShouldContain("SOLID Principles"); |
| 73 | + result.Markdown.ShouldContain("## Captions"); |
| 74 | + result.Segments.ShouldContain(segment => segment.Type == SegmentType.Metadata); |
| 75 | + result.Segments.Count(s => s.Type == SegmentType.Audio).ShouldBe(metadata.Captions.Count); |
| 76 | + |
| 77 | + var firstCaption = result.Segments.First(s => s.Type == SegmentType.Audio); |
| 78 | + firstCaption.StartTime.ShouldBe(TimeSpan.FromSeconds(0)); |
| 79 | + firstCaption.Markdown.ShouldContain("SOLID principles"); |
| 80 | + } |
| 81 | + |
| 82 | + private static YouTubeMetadata LoadRecordedMetadata() |
| 83 | + { |
| 84 | + var jsonPath = TestAssetLoader.GetAssetPath(TestAssetCatalog.YoutubeSolidPrinciplesJson); |
| 85 | + using var stream = File.OpenRead(jsonPath); |
| 86 | + var fixture = JsonSerializer.Deserialize<YouTubeMetadataFixture>(stream, new JsonSerializerOptions |
| 87 | + { |
| 88 | + PropertyNameCaseInsensitive = true |
| 89 | + }); |
| 90 | + |
| 91 | + if (fixture is null) |
| 92 | + { |
| 93 | + throw new InvalidOperationException("Failed to deserialize recorded YouTube metadata fixture."); |
| 94 | + } |
| 95 | + |
| 96 | + var captions = fixture.Captions.Select(c => new YouTubeCaptionSegment( |
| 97 | + c.Text, |
| 98 | + c.Start is not null ? TimeSpan.FromSeconds(c.Start.Value) : null, |
| 99 | + c.End is not null ? TimeSpan.FromSeconds(c.End.Value) : null, |
| 100 | + c.Metadata ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 101 | + )).ToList(); |
| 102 | + |
| 103 | + var thumbnails = fixture.Thumbnails.Select(uri => new Uri(uri)).ToList(); |
| 104 | + var additional = fixture.AdditionalMetadata ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 105 | + |
| 106 | + return new YouTubeMetadata( |
| 107 | + VideoId: fixture.VideoId, |
| 108 | + Title: fixture.Title, |
| 109 | + ChannelTitle: fixture.ChannelTitle, |
| 110 | + WatchUrl: new Uri(fixture.WatchUrl), |
| 111 | + ChannelUrl: new Uri(fixture.ChannelUrl), |
| 112 | + Duration: fixture.DurationSeconds is not null ? TimeSpan.FromSeconds(fixture.DurationSeconds.Value) : null, |
| 113 | + UploadDate: fixture.UploadDate is not null ? DateTimeOffset.Parse(fixture.UploadDate, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) : null, |
| 114 | + ViewCount: fixture.ViewCount, |
| 115 | + LikeCount: fixture.LikeCount, |
| 116 | + Tags: fixture.Tags ?? Array.Empty<string>(), |
| 117 | + Description: fixture.Description, |
| 118 | + Thumbnails: thumbnails, |
| 119 | + Captions: captions, |
| 120 | + AdditionalMetadata: additional |
| 121 | + ); |
| 122 | + } |
| 123 | + |
53 | 124 | private sealed class StubYouTubeMetadataProvider : IYouTubeMetadataProvider |
54 | 125 | { |
55 | 126 | public Task<YouTubeMetadata?> GetVideoAsync(string videoId, CancellationToken cancellationToken = default) |
@@ -81,4 +152,45 @@ private sealed class StubYouTubeMetadataProvider : IYouTubeMetadataProvider |
81 | 152 | return Task.FromResult<YouTubeMetadata?>(metadata); |
82 | 153 | } |
83 | 154 | } |
| 155 | + |
| 156 | + private sealed class FixtureYouTubeMetadataProvider : IYouTubeMetadataProvider |
| 157 | + { |
| 158 | + private readonly YouTubeMetadata metadata; |
| 159 | + |
| 160 | + public FixtureYouTubeMetadataProvider(YouTubeMetadata metadata) |
| 161 | + { |
| 162 | + this.metadata = metadata; |
| 163 | + } |
| 164 | + |
| 165 | + public Task<YouTubeMetadata?> GetVideoAsync(string videoId, CancellationToken cancellationToken = default) |
| 166 | + { |
| 167 | + return Task.FromResult<YouTubeMetadata?>(metadata); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private sealed class YouTubeMetadataFixture |
| 172 | + { |
| 173 | + public string VideoId { get; init; } = string.Empty; |
| 174 | + public string Title { get; init; } = string.Empty; |
| 175 | + public string ChannelTitle { get; init; } = string.Empty; |
| 176 | + public string WatchUrl { get; init; } = string.Empty; |
| 177 | + public string ChannelUrl { get; init; } = string.Empty; |
| 178 | + public double? DurationSeconds { get; init; } |
| 179 | + public string? UploadDate { get; init; } |
| 180 | + public long? ViewCount { get; init; } |
| 181 | + public long? LikeCount { get; init; } |
| 182 | + public IReadOnlyList<string>? Tags { get; init; } |
| 183 | + public string? Description { get; init; } |
| 184 | + public IReadOnlyList<string> Thumbnails { get; init; } = Array.Empty<string>(); |
| 185 | + public IReadOnlyList<YouTubeCaptionFixture> Captions { get; init; } = Array.Empty<YouTubeCaptionFixture>(); |
| 186 | + public IReadOnlyDictionary<string, string>? AdditionalMetadata { get; init; } |
| 187 | + } |
| 188 | + |
| 189 | + private sealed class YouTubeCaptionFixture |
| 190 | + { |
| 191 | + public string Text { get; init; } = string.Empty; |
| 192 | + public double? Start { get; init; } |
| 193 | + public double? End { get; init; } |
| 194 | + public IReadOnlyDictionary<string, string>? Metadata { get; init; } |
| 195 | + } |
84 | 196 | } |
0 commit comments