github.com/soulteary/pocket-bookcase@v0.0.0-20240428065142-0b5a9a0fc98a/internal/core/processing_test.go (about) 1 package core_test 2 3 import ( 4 "bytes" 5 "context" 6 "os" 7 fp "path/filepath" 8 "testing" 9 10 "github.com/sirupsen/logrus" 11 "github.com/soulteary/pocket-bookcase/internal/core" 12 "github.com/soulteary/pocket-bookcase/internal/model" 13 "github.com/soulteary/pocket-bookcase/internal/testutil" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 ) 17 18 func TestDownloadBookImage(t *testing.T) { 19 logger := logrus.New() 20 _, deps := testutil.GetTestConfigurationAndDependencies(t, context.TODO(), logger) 21 22 t.Run("Download Images", func(t *testing.T) { 23 t.Run("fails", func(t *testing.T) { 24 // images is too small with unsupported format with a valid URL 25 imageURL := "https://github.com/soulteary/pocket-bookcase/blob/main/internal/view/assets/res/apple-touch-icon-152x152.png" 26 tmpDir, err := os.MkdirTemp("", "") 27 require.NoError(t, err) 28 dstFile := fp.Join(tmpDir, "image.png") 29 30 // Act 31 err = core.DownloadBookImage(deps, imageURL, dstFile) 32 33 // Assert 34 assert.EqualError(t, err, "unsupported image type") 35 assert.False(t, deps.Domains.Storage.FileExists(dstFile)) 36 }) 37 t.Run("successful download image", func(t *testing.T) { 38 tmpDir, err := os.MkdirTemp("", "") 39 require.NoError(t, err) 40 require.NoError(t, os.Chdir(tmpDir)) 41 // Arrange 42 imageURL := "https://raw.githubusercontent.com/soulteary/pocket-bookcase/main/docs/readme/cover.png" 43 dstFile := "." + string(fp.Separator) + "cover.png" 44 45 // Act 46 err = core.DownloadBookImage(deps, imageURL, dstFile) 47 48 // Assert 49 assert.NoError(t, err) 50 assert.True(t, deps.Domains.Storage.FileExists(dstFile)) 51 }) 52 t.Run("successful download medium size image", func(t *testing.T) { 53 tmpDir, err := os.MkdirTemp("", "") 54 require.NoError(t, err) 55 require.NoError(t, os.Chdir(tmpDir)) 56 57 // Arrange 58 imageURL := "https://raw.githubusercontent.com/soulteary/pocket-bookcase/main/testdata/medium_image.png" 59 dstFile := "." + string(fp.Separator) + "medium_image.png" 60 61 // Act 62 err = core.DownloadBookImage(deps, imageURL, dstFile) 63 64 // Assert 65 assert.NoError(t, err) 66 assert.True(t, deps.Domains.Storage.FileExists(dstFile)) 67 }) 68 }) 69 } 70 71 func TestProcessBookmark(t *testing.T) { 72 logger := logrus.New() 73 _, deps := testutil.GetTestConfigurationAndDependencies(t, context.TODO(), logger) 74 75 t.Run("ProcessRequest with sucssesful result", func(t *testing.T) { 76 tmpDir := t.TempDir() 77 t.Run("Normal without image", func(t *testing.T) { 78 bookmark := model.BookmarkDTO{ 79 ID: 1, 80 URL: "https://example.com", 81 Title: "Example", 82 Excerpt: "This is an example article", 83 CreateEbook: true, 84 CreateArchive: true, 85 } 86 content := bytes.NewBufferString("<html><head></head><body><p>This is an example article</p></body></html>") 87 request := core.ProcessRequest{ 88 Bookmark: bookmark, 89 Content: content, 90 ContentType: "text/html", 91 DataDir: tmpDir, 92 KeepTitle: true, 93 KeepExcerpt: true, 94 } 95 expected, _, _ := core.ProcessBookmark(deps, request) 96 97 if expected.ID != bookmark.ID { 98 t.Errorf("Unexpected ID: got %v, want %v", expected.ID, bookmark.ID) 99 } 100 if expected.URL != bookmark.URL { 101 t.Errorf("Unexpected URL: got %v, want %v", expected.URL, bookmark.URL) 102 } 103 if expected.Title != bookmark.Title { 104 t.Errorf("Unexpected Title: got %v, want %v", expected.Title, bookmark.Title) 105 } 106 if expected.Excerpt != bookmark.Excerpt { 107 t.Errorf("Unexpected Excerpt: got %v, want %v", expected.Excerpt, bookmark.Excerpt) 108 } 109 }) 110 t.Run("Normal with multipleimage", func(t *testing.T) { 111 tmpDir := t.TempDir() 112 html := `html<html> 113 <head> 114 <meta property="og:image" content="http://example.com/image1.jpg"> 115 <meta property="og:image" content="http://example.com/image2.jpg"> 116 <link rel="icon" type="image/png" href="http://example.com/favicon.png"> 117 </head> 118 <body> 119 <p>This is an example article</p> 120 </body> 121 </html>` 122 bookmark := model.BookmarkDTO{ 123 ID: 1, 124 URL: "https://example.com", 125 Title: "Example", 126 Excerpt: "This is an example article", 127 CreateEbook: true, 128 CreateArchive: true, 129 } 130 content := bytes.NewBufferString(html) 131 request := core.ProcessRequest{ 132 Bookmark: bookmark, 133 Content: content, 134 ContentType: "text/html", 135 DataDir: tmpDir, 136 KeepTitle: true, 137 KeepExcerpt: true, 138 } 139 expected, _, _ := core.ProcessBookmark(deps, request) 140 141 if expected.ID != bookmark.ID { 142 t.Errorf("Unexpected ID: got %v, want %v", expected.ID, bookmark.ID) 143 } 144 if expected.URL != bookmark.URL { 145 t.Errorf("Unexpected URL: got %v, want %v", expected.URL, bookmark.URL) 146 } 147 if expected.Title != bookmark.Title { 148 t.Errorf("Unexpected Title: got %v, want %v", expected.Title, bookmark.Title) 149 } 150 if expected.Excerpt != bookmark.Excerpt { 151 t.Errorf("Unexpected Excerpt: got %v, want %v", expected.Excerpt, bookmark.Excerpt) 152 } 153 }) 154 t.Run("ProcessRequest sucssesful with multipleimage included favicon and Thumbnail ", func(t *testing.T) { 155 tmpDir := t.TempDir() 156 html := `html<html> 157 <head> 158 <meta property="og:image" content="http://example.com/image1.jpg"> 159 <meta property="og:image" content="https://raw.githubusercontent.com/soulteary/pocket-bookcase/master/testdata/big_image.png"> 160 <link rel="icon" type="image/svg" href="https://raw.githubusercontent.com/soulteary/pocket-bookcase/master/testdata/favicon.svg"> 161 </head> 162 <body> 163 <p>This is an example article</p> 164 </body> 165 </html>` 166 bookmark := model.BookmarkDTO{ 167 ID: 1, 168 URL: "https://example.com", 169 Title: "Example", 170 Excerpt: "This is an example article", 171 CreateEbook: true, 172 CreateArchive: true, 173 } 174 content := bytes.NewBufferString(html) 175 request := core.ProcessRequest{ 176 Bookmark: bookmark, 177 Content: content, 178 ContentType: "text/html", 179 DataDir: tmpDir, 180 KeepTitle: true, 181 KeepExcerpt: true, 182 } 183 expected, _, _ := core.ProcessBookmark(deps, request) 184 assert.True(t, deps.Domains.Storage.FileExists(fp.Join("thumb", "1"))) 185 if expected.ID != bookmark.ID { 186 t.Errorf("Unexpected ID: got %v, want %v", expected.ID, bookmark.ID) 187 } 188 if expected.URL != bookmark.URL { 189 t.Errorf("Unexpected URL: got %v, want %v", expected.URL, bookmark.URL) 190 } 191 if expected.Title != bookmark.Title { 192 t.Errorf("Unexpected Title: got %v, want %v", expected.Title, bookmark.Title) 193 } 194 if expected.Excerpt != bookmark.Excerpt { 195 t.Errorf("Unexpected Excerpt: got %v, want %v", expected.Excerpt, bookmark.Excerpt) 196 } 197 }) 198 t.Run("ProcessRequest sucssesful with empty title ", func(t *testing.T) { 199 tmpDir := t.TempDir() 200 bookmark := model.BookmarkDTO{ 201 ID: 1, 202 URL: "https://example.com", 203 Title: "", 204 Excerpt: "This is an example article", 205 CreateEbook: true, 206 CreateArchive: true, 207 } 208 content := bytes.NewBufferString("<html><head></head><body><p>This is an example article</p></body></html>") 209 request := core.ProcessRequest{ 210 Bookmark: bookmark, 211 Content: content, 212 ContentType: "text/html", 213 DataDir: tmpDir, 214 KeepTitle: true, 215 KeepExcerpt: true, 216 } 217 expected, _, _ := core.ProcessBookmark(deps, request) 218 219 if expected.ID != bookmark.ID { 220 t.Errorf("Unexpected ID: got %v, want %v", expected.ID, bookmark.ID) 221 } 222 if expected.URL != bookmark.URL { 223 t.Errorf("Unexpected URL: got %v, want %v", expected.URL, bookmark.URL) 224 } 225 if expected.Title != bookmark.URL { 226 t.Errorf("Unexpected Title: got %v, want %v", expected.Title, bookmark.Title) 227 } 228 if expected.Excerpt != bookmark.Excerpt { 229 t.Errorf("Unexpected Excerpt: got %v, want %v", expected.Excerpt, bookmark.Excerpt) 230 } 231 }) 232 t.Run("ProcessRequest sucssesful with empty Excerpt", func(t *testing.T) { 233 tmpDir := t.TempDir() 234 bookmark := model.BookmarkDTO{ 235 ID: 1, 236 URL: "https://example.com", 237 Title: "", 238 Excerpt: "This is an example article", 239 CreateEbook: true, 240 CreateArchive: true, 241 } 242 content := bytes.NewBufferString("<html><head></head><body><p>This is an example article</p></body></html>") 243 request := core.ProcessRequest{ 244 Bookmark: bookmark, 245 Content: content, 246 ContentType: "text/html", 247 DataDir: tmpDir, 248 KeepTitle: true, 249 KeepExcerpt: false, 250 } 251 expected, _, _ := core.ProcessBookmark(deps, request) 252 253 if expected.ID != bookmark.ID { 254 t.Errorf("Unexpected ID: got %v, want %v", expected.ID, bookmark.ID) 255 } 256 if expected.URL != bookmark.URL { 257 t.Errorf("Unexpected URL: got %v, want %v", expected.URL, bookmark.URL) 258 } 259 if expected.Title != bookmark.URL { 260 t.Errorf("Unexpected Title: got %v, want %v", expected.Title, bookmark.Title) 261 } 262 if expected.Excerpt != bookmark.Excerpt { 263 t.Errorf("Unexpected Excerpt: got %v, want %v", expected.Excerpt, bookmark.Excerpt) 264 } 265 }) 266 t.Run("Specific case", func(t *testing.T) { 267 tmpDir := t.TempDir() 268 t.Run("ProcessRequest with ID zero", func(t *testing.T) { 269 270 bookmark := model.BookmarkDTO{ 271 ID: 0, 272 URL: "https://example.com", 273 Title: "Example", 274 Excerpt: "This is an example article", 275 CreateEbook: true, 276 CreateArchive: true, 277 } 278 content := bytes.NewBufferString("<html><head></head><body><p>This is an example article</p></body></html>") 279 request := core.ProcessRequest{ 280 Bookmark: bookmark, 281 Content: content, 282 ContentType: "text/html", 283 DataDir: tmpDir, 284 KeepTitle: true, 285 KeepExcerpt: true, 286 } 287 _, isFatal, err := core.ProcessBookmark(deps, request) 288 assert.Error(t, err) 289 assert.Contains(t, err.Error(), "bookmark ID is not valid") 290 assert.True(t, isFatal) 291 }) 292 293 t.Run("ProcessRequest that content type not zero", func(t *testing.T) { 294 tmpDir := t.TempDir() 295 bookmark := model.BookmarkDTO{ 296 ID: 1, 297 URL: "https://example.com", 298 Title: "Example", 299 Excerpt: "This is an example article", 300 CreateEbook: true, 301 CreateArchive: true, 302 } 303 content := bytes.NewBufferString("<html><head></head><body><p>This is an example article</p></body></html>") 304 request := core.ProcessRequest{ 305 Bookmark: bookmark, 306 Content: content, 307 ContentType: "application/pdf", 308 DataDir: tmpDir, 309 KeepTitle: true, 310 KeepExcerpt: true, 311 } 312 _, _, err := core.ProcessBookmark(deps, request) 313 assert.NoError(t, err) 314 }) 315 }) 316 }) 317 }