|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseHeadlinesFromHTML(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + input string |
| 11 | + expectedHTML string |
| 12 | + expectedHeadlines []HTMLHeadline |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "empty string", |
| 16 | + input: "", |
| 17 | + expectedHTML: "", |
| 18 | + expectedHeadlines: []HTMLHeadline{}, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "no headlines", |
| 22 | + input: "<p>Just a paragraph</p>", |
| 23 | + expectedHTML: "<p>Just a paragraph</p>", |
| 24 | + expectedHeadlines: []HTMLHeadline{}, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "simple h3 without span", |
| 28 | + input: "<h3>Links</h3>", |
| 29 | + expectedHTML: `<h3 id="links">Links</h3>`, |
| 30 | + expectedHeadlines: []HTMLHeadline{ |
| 31 | + {Slug: "links", Text: "Links"}, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "h3 with span wrapper", |
| 36 | + input: "<h3><span>Sprungmarken</span></h3>", |
| 37 | + expectedHTML: `<h3 id="sprungmarken">Sprungmarken</h3>`, |
| 38 | + expectedHeadlines: []HTMLHeadline{ |
| 39 | + {Slug: "sprungmarken", Text: "Sprungmarken"}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "multiple headlines", |
| 44 | + input: "<h3>Links</h3><p>Some content</p><h3>Hosts</h3>", |
| 45 | + expectedHTML: `<h3 id="links">Links</h3><p>Some content</p><h3 id="hosts">Hosts</h3>`, |
| 46 | + expectedHeadlines: []HTMLHeadline{ |
| 47 | + {Slug: "links", Text: "Links"}, |
| 48 | + {Slug: "hosts", Text: "Hosts"}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "h3 containing HTML tags should be skipped", |
| 53 | + input: "<h3>Text with <br> line break</h3>", |
| 54 | + expectedHTML: "<h3>Text with <br> line break</h3>", |
| 55 | + expectedHeadlines: []HTMLHeadline{}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "headline with surrounding content preserved", |
| 59 | + input: "<p>Before</p><h3>Title</h3><p>After</p>", |
| 60 | + expectedHTML: `<p>Before</p><h3 id="title">Title</h3><p>After</p>`, |
| 61 | + expectedHeadlines: []HTMLHeadline{ |
| 62 | + {Slug: "title", Text: "Title"}, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + gotHTML, gotHeadlines := parseHeadlinesFromHTML(tt.input) |
| 70 | + if gotHTML != tt.expectedHTML { |
| 71 | + t.Errorf("parseHeadlinesFromHTML(%q)\n got HTML: %q\n want HTML: %q", tt.input, gotHTML, tt.expectedHTML) |
| 72 | + } |
| 73 | + if len(gotHeadlines) != len(tt.expectedHeadlines) { |
| 74 | + t.Fatalf("parseHeadlinesFromHTML(%q)\n got %d headlines, want %d", tt.input, len(gotHeadlines), len(tt.expectedHeadlines)) |
| 75 | + } |
| 76 | + for i, want := range tt.expectedHeadlines { |
| 77 | + got := gotHeadlines[i] |
| 78 | + if got.Slug != want.Slug || got.Text != want.Text { |
| 79 | + t.Errorf("headline[%d] = {Slug: %q, Text: %q}, want {Slug: %q, Text: %q}", i, got.Slug, got.Text, want.Slug, want.Text) |
| 80 | + } |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestGetChapterFromDescription(t *testing.T) { |
| 87 | + tests := []struct { |
| 88 | + name string |
| 89 | + input string |
| 90 | + expected []map[string]string |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "empty string", |
| 94 | + input: "", |
| 95 | + expected: nil, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "no chapters", |
| 99 | + input: "<p>Just a description without timestamps</p>", |
| 100 | + expected: nil, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "pattern 1 with span wrapper", |
| 104 | + input: "<p><span>(00:01:30) Introduction</span></p><p><span>(00:15:00) Main Topic</span></p>", |
| 105 | + expected: []map[string]string{ |
| 106 | + {"start": "00:01:30", "title": "Introduction"}, |
| 107 | + {"start": "00:15:00", "title": "Main Topic"}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "pattern 2 without span", |
| 112 | + input: "<p>(00:00:00) Intro</p><p>(00:05:30) Topic</p>", |
| 113 | + expected: []map[string]string{ |
| 114 | + {"start": "00:00:00", "title": "Intro"}, |
| 115 | + {"start": "00:05:30", "title": "Topic"}, |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "MM:SS format gets prepended with 00:", |
| 120 | + input: "<p>(05:30) Short Format</p>", |
| 121 | + expected: []map[string]string{ |
| 122 | + {"start": "00:05:30", "title": "Short Format"}, |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "HTML entities in title are unescaped", |
| 127 | + input: "<p><span>(00:10:00) Q&A Session</span></p>", |
| 128 | + expected: []map[string]string{ |
| 129 | + {"start": "00:10:00", "title": "Q&A Session"}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "single chapter", |
| 134 | + input: "<p><span>(00:00:00) Only Chapter</span></p>", |
| 135 | + expected: []map[string]string{ |
| 136 | + {"start": "00:00:00", "title": "Only Chapter"}, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tt := range tests { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + got := getChapterFromDescription(tt.input) |
| 144 | + if len(got) != len(tt.expected) { |
| 145 | + t.Fatalf("getChapterFromDescription()\n got %d chapters, want %d\n got: %v", len(got), len(tt.expected), got) |
| 146 | + } |
| 147 | + for i, want := range tt.expected { |
| 148 | + if got[i]["start"] != want["start"] || got[i]["title"] != want["title"] { |
| 149 | + t.Errorf("chapter[%d] = %v, want %v", i, got[i], want) |
| 150 | + } |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments