| 1 | import type { InternalAxiosRequestConfig } from 'axios' |
| 2 | import { readdir } from 'node:fs/promises' |
| 3 | import { tmpdir } from 'node:os' |
| 4 | import { Readable } from 'node:stream' |
| 5 | import { AccountType } from '@yikart/common' |
| 6 | import { AxiosError } from 'axios' |
| 7 | import sharp from 'sharp' |
| 8 | import { describe, expect, it, vi } from 'vitest' |
| 9 | import { PlatformErrorCategory } from '../platforms/platforms.exception' |
| 10 | import { PublishMediaAdaptationImageFormat } from '../platforms/publish-media-adaptation.schema' |
| 11 | import { PublishValidationIssueCode } from '../platforms/publish.schema' |
| 12 | import { MediaService } from './media.service' |
| 13 | |
| 14 | const PUBLIC_MEDIA_URL = 'https://93.184.216.34/video.mp4' |
| 15 | |
| 16 | vi.mock('@yikart/assets', () => ({ |
| 17 | AssetsService: class AssetsService {}, |
| 18 | VideoMetadataService: class VideoMetadataService {}, |
| 19 | })) |
| 20 | |
| 21 | vi.mock('@yikart/mongodb', () => ({ |
| 22 | AssetType: { |
| 23 | PublishMedia: 'publishMedia', |
| 24 | }, |
| 25 | })) |
| 26 | |
| 27 | function createService(assetsService = { uploadFromBuffer: vi.fn() }) { |
| 28 | return new MediaService({} as never, assetsService as never) |
| 29 | } |
| 30 | |
| 31 | function setHttpAdapter(service: MediaService, adapter: (config: InternalAxiosRequestConfig) => unknown) { |
| 32 | const serviceWithHttp = service as unknown as { http: { defaults: { adapter: unknown } } } |
| 33 | serviceWithHttp.http.defaults.adapter = adapter |
| 34 | } |
| 35 | |
| 36 | async function readStream(stream: Readable): Promise<Buffer> { |
| 37 | const chunks: Buffer[] = [] |
| 38 | for await (const chunk of stream) { |
| 39 | chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)) |
| 40 | } |
| 41 | return Buffer.concat(chunks) |
| 42 | } |
| 43 | |
| 44 | async function listMediaTempDirs(): Promise<string[]> { |
| 45 | return (await readdir(tmpdir())).filter(name => name.startsWith('aitoearn-media-')) |
| 46 | } |
| 47 | |
| 48 | describe('media service http downloads', () => { |
| 49 | it('returns media downloads as Buffer', async () => { |
| 50 | const service = createService() |
| 51 | setHttpAdapter(service, async config => ({ |
| 52 | data: Buffer.from('media-bytes'), |
| 53 | status: 200, |
| 54 | statusText: 'OK', |
| 55 | headers: {}, |
| 56 | config, |
| 57 | })) |
| 58 | |
| 59 | const buffer = await service.getBuffer({ |
| 60 | platform: AccountType.TikTok, |
| 61 | endpoint: 'downloadVideo', |
| 62 | url: PUBLIC_MEDIA_URL, |
| 63 | }) |
| 64 | |
| 65 | expect(Buffer.isBuffer(buffer)).toBe(true) |
| 66 | expect(buffer.toString()).toBe('media-bytes') |
| 67 | }) |
| 68 | |
| 69 | it('creates upload sources with range streams and blobs without retaining temp files', async () => { |
| 70 | const service = createService() |
| 71 | setHttpAdapter(service, async config => ({ |
| 72 | data: Readable.from(Buffer.from('0123456789')), |
| 73 | status: 200, |
| 74 | statusText: 'OK', |
| 75 | headers: { 'content-type': 'video/mp4' }, |
| 76 | config, |
| 77 | })) |
| 78 | const before = new Set(await listMediaTempDirs()) |
| 79 | |
| 80 | const result = await service.withUploadSource({ |
| 81 | platform: AccountType.TikTok, |
| 82 | endpoint: 'downloadVideo', |
| 83 | url: 'https://cdn.example.test/video.mp4', |
| 84 | }, async source => ({ |
| 85 | sizeBytes: source.sizeBytes, |
| 86 | contentType: source.contentType, |
| 87 | filename: source.filename, |
| 88 | streamBytes: (await readStream(source.stream({ start: 2, end: 5 }))).toString(), |
| 89 | blobBytes: Buffer.from(await (await source.blob({ start: 6, end: 8 })).arrayBuffer()).toString(), |
| 90 | })) |
| 91 | const after = new Set(await listMediaTempDirs()) |
| 92 | |
| 93 | expect(result).toEqual({ |
| 94 | sizeBytes: 10, |
| 95 | contentType: 'video/mp4', |
| 96 | filename: 'video.mp4', |
| 97 | streamBytes: '2345', |
| 98 | blobBytes: '678', |
| 99 | }) |
| 100 | expect([...after].filter(name => !before.has(name))).toEqual([]) |
| 101 | }) |
| 102 | |
| 103 | it('cleans upload source temp files when the handler fails', async () => { |
| 104 | const service = createService() |
| 105 | setHttpAdapter(service, async config => ({ |
| 106 | data: Readable.from(Buffer.from('video-data')), |
| 107 | status: 200, |
| 108 | statusText: 'OK', |
| 109 | headers: { 'content-type': 'video/mp4' }, |
| 110 | config, |
| 111 | })) |
| 112 | const before = new Set(await listMediaTempDirs()) |
| 113 | |
| 114 | await expect(service.withUploadSource({ |
| 115 | platform: AccountType.TikTok, |
| 116 | endpoint: 'downloadVideo', |
| 117 | url: 'https://cdn.example.test/video.mp4', |
| 118 | }, async () => { |
| 119 | throw new Error('handler failed') |
| 120 | })).rejects.toThrow('handler failed') |
| 121 | const after = new Set(await listMediaTempDirs()) |
| 122 | |
| 123 | expect([...after].filter(name => !before.has(name))).toEqual([]) |
| 124 | }) |
| 125 | |
| 126 | it('converts platform media download errors through the response interceptor', async () => { |
| 127 | const service = createService() |
| 128 | setHttpAdapter(service, async (config) => { |
| 129 | throw new AxiosError('connect failed', 'ECONNRESET', config) |
| 130 | }) |
| 131 | |
| 132 | await expect(service.getBuffer({ |
| 133 | platform: AccountType.TikTok, |
| 134 | endpoint: 'downloadVideo', |
| 135 | url: PUBLIC_MEDIA_URL, |
| 136 | taskId: 'task-id', |
| 137 | })).rejects.toMatchObject({ |
| 138 | category: PlatformErrorCategory.Network, |
| 139 | context: { |
| 140 | endpoint: 'downloadVideo', |
| 141 | taskId: 'task-id', |
| 142 | metadata: { url: PUBLIC_MEDIA_URL }, |
| 143 | }, |
| 144 | platformCause: { |
| 145 | platformMessage: 'connect failed', |
| 146 | }, |
| 147 | }) |
| 148 | }) |
| 149 | |
| 150 | it('keeps probe and conversion http errors unwrapped without platform context', async () => { |
| 151 | const service = createService() |
| 152 | const error = new AxiosError('probe failed', 'ECONNRESET') |
| 153 | setHttpAdapter(service, async () => { |
| 154 | throw error |
| 155 | }) |
| 156 | |
| 157 | await expect(service.probeImage('https://93.184.216.34/image.png')).rejects.toBe(error) |
| 158 | }) |
| 159 | }) |
| 160 | |
| 161 | describe('media service publish validation', () => { |
| 162 | it('uses server metadata type for media probing and validates covers', async () => { |
| 163 | const service = createService() |
| 164 | const probeVideo = vi.spyOn(service, 'probeVideo').mockResolvedValue({ |
| 165 | width: 1920, |
| 166 | height: 1080, |
| 167 | format: 'mp4', |
| 168 | durationSec: 30, |
| 169 | codec: 'unknown', |
| 170 | sizeBytes: 1024, |
| 171 | }) |
| 172 | const probeImage = vi.spyOn(service, 'probeImage').mockResolvedValue({ |
| 173 | width: 1200, |
| 174 | height: 900, |
| 175 | format: 'jpeg', |
| 176 | sizeBytes: 512, |
| 177 | }) |
| 178 | |
| 179 | await expect(service.validateMedia({ |
| 180 | media: [{ |
| 181 | url: 'https://93.184.216.34/media?id=video', |
| 182 | metadata: { type: 'video' }, |
| 183 | }], |
| 184 | cover: { |
| 185 | url: 'https://93.184.216.34/cover.jpeg', |
| 186 | }, |
| 187 | }, { |
| 188 | videoFormats: ['mp4'], |
| 189 | imageFormats: ['jpeg'], |
| 190 | })).resolves.toEqual([]) |
| 191 | |
| 192 | expect(probeVideo).toHaveBeenCalledWith('https://93.184.216.34/media?id=video') |
| 193 | expect(probeImage).toHaveBeenCalledWith('https://93.184.216.34/cover.jpeg') |
| 194 | }) |
| 195 | |
| 196 | it('reports why the server could not fetch or probe a video', async () => { |
| 197 | const service = createService() |
| 198 | setHttpAdapter(service, async (config) => { |
| 199 | throw new AxiosError('forbidden', 'ERR_BAD_REQUEST', config, undefined, { |
| 200 | status: 403, |
| 201 | statusText: 'Forbidden', |
| 202 | headers: {}, |
| 203 | config, |
| 204 | data: '', |
| 205 | }) |
| 206 | }) |
| 207 | |
| 208 | await expect(service.validateMedia({ |
| 209 | media: [{ url: 'https://93.184.216.34/video.mp4', metadata: { type: 'video' } }], |
| 210 | }, {})).resolves.toEqual([{ |
| 211 | code: PublishValidationIssueCode.MediaUnreachable, |
| 212 | path: ['content', 'media', 0], |
| 213 | params: { field: 'video', url: 'https://93.184.216.34/video.mp4', reason: 'HEAD returned HTTP 403' }, |
| 214 | }]) |
| 215 | }) |
| 216 | |
| 217 | it('surfaces ffprobe failures as the unreachable reason', async () => { |
| 218 | const service = createService() |
| 219 | setHttpAdapter(service, async config => ({ data: '', status: 200, statusText: 'OK', headers: {}, config })) |
| 220 | const serviceWithMetadata = service as unknown as { videoMetadataService: { probeVideoMetadata: () => Promise<never> } } |
| 221 | serviceWithMetadata.videoMetadataService = { |
| 222 | probeVideoMetadata: vi.fn().mockRejectedValue(new Error('ffprobe failed: Server returned 403 Forbidden (access denied)')), |
| 223 | } |
| 224 | |
| 225 | await expect(service.validateMedia({ |
| 226 | media: [{ url: 'https://93.184.216.34/video.mp4', metadata: { type: 'video' } }], |
| 227 | }, {})).resolves.toEqual([expect.objectContaining({ |
| 228 | code: PublishValidationIssueCode.MediaUnreachable, |
| 229 | params: expect.objectContaining({ reason: 'ffprobe failed: Server returned 403 Forbidden (access denied)' }), |
| 230 | })]) |
| 231 | }) |
| 232 | |
| 233 | it('accepts extensionless video media normalized with metadata when probe format is unknown', async () => { |
| 234 | const service = createService() |
| 235 | vi.spyOn(service, 'probeVideo').mockResolvedValue({ |
| 236 | width: 1920, |
| 237 | height: 1080, |
| 238 | format: 'unknown', |
| 239 | durationSec: 30, |
| 240 | codec: 'unknown', |
| 241 | sizeBytes: 1024, |
| 242 | }) |
| 243 | |
| 244 | await expect(service.validateMedia({ |
| 245 | media: [{ |
| 246 | url: 'https://93.184.216.34/signed-video', |
| 247 | metadata: { type: 'video' }, |
| 248 | }], |
| 249 | }, { |
| 250 | videoFormats: ['mp4'], |
| 251 | maxVideoDuration: 90, |
| 252 | })).resolves.toEqual([]) |
| 253 | }) |
| 254 | |
| 255 | it('keeps feed image aspect validation separate from reel and story cover rules', () => { |
| 256 | const service = createService() |
| 257 | const portraitImage = { |
| 258 | width: 1080, |
| 259 | height: 1920, |
| 260 | format: 'jpeg', |
| 261 | sizeBytes: 1024, |
| 262 | } |
| 263 | |
| 264 | expect(service.validateImage(portraitImage, { |
| 265 | imageFormats: ['jpeg'], |
| 266 | aspectRatio: { min: 0.8, max: 1.91 }, |
| 267 | }, ['content', 'media', 0])).toEqual([expect.objectContaining({ |
| 268 | code: PublishValidationIssueCode.TooSmall, |
| 269 | params: expect.objectContaining({ |
| 270 | current: 0.56, |
| 271 | minimum: 0.8, |
| 272 | }), |
| 273 | })]) |
| 274 | |
| 275 | expect(service.validateImage(portraitImage, { |
| 276 | imageFormats: ['jpeg'], |
| 277 | }, ['content', 'cover'])).toEqual([]) |
| 278 | }) |
| 279 | }) |
| 280 | |
| 281 | describe('media service publish media preparation', () => { |
| 282 | it('keeps media URLs unchanged and strips media adaptation options when adaptation is off', async () => { |
| 283 | const assetsService = { uploadFromBuffer: vi.fn() } |
| 284 | const service = createService(assetsService) |
| 285 | vi.spyOn(service, 'probeImage').mockImplementation(async url => ({ |
| 286 | width: url.includes('cover') ? 1200 : 800, |
| 287 | height: url.includes('cover') ? 900 : 600, |
| 288 | format: 'png', |
| 289 | sizeBytes: 512, |
| 290 | })) |
| 291 | const content = { |
| 292 | media: [{ |
| 293 | url: 'https://cdn.example.test/source.png', |
| 294 | options: { adaptation: { imageFormat: PublishMediaAdaptationImageFormat.Off } }, |
| 295 | }], |
| 296 | cover: { |
| 297 | url: 'https://cdn.example.test/cover.png', |
| 298 | options: { adaptation: { imageFormat: PublishMediaAdaptationImageFormat.Off } }, |
| 299 | }, |
| 300 | } |
| 301 | |
| 302 | await expect(service.preparePublishContentMedia({ |
| 303 | userId: 'user-1', |
| 304 | content, |
| 305 | mediaRules: { imageFormats: ['jpg', 'jpeg', 'png'] }, |
| 306 | })).resolves.toEqual({ |
| 307 | content: { |
| 308 | media: [{ |
| 309 | url: 'https://cdn.example.test/source.png', |
| 310 | metadata: { |
| 311 | type: 'image', |
| 312 | width: 800, |
| 313 | height: 600, |
| 314 | format: 'png', |
| 315 | sizeBytes: 512, |
| 316 | }, |
| 317 | }], |
| 318 | cover: { |
| 319 | url: 'https://cdn.example.test/cover.png', |
| 320 | metadata: { |
| 321 | type: 'image', |
| 322 | width: 1200, |
| 323 | height: 900, |
| 324 | format: 'png', |
| 325 | sizeBytes: 512, |
| 326 | }, |
| 327 | }, |
| 328 | }, |
| 329 | issues: [], |
| 330 | }) |
| 331 | expect(assetsService.uploadFromBuffer).not.toHaveBeenCalled() |
| 332 | }) |
| 333 | |
| 334 | it('keeps auto media unchanged when the source format already matches platform rules', async () => { |
| 335 | const assetsService = { uploadFromBuffer: vi.fn() } |
| 336 | const service = createService(assetsService) |
| 337 | vi.spyOn(service, 'probeImage').mockResolvedValue({ |
| 338 | width: 800, |
| 339 | height: 600, |
| 340 | format: 'jpeg', |
| 341 | sizeBytes: 512, |
| 342 | }) |
| 343 | |
| 344 | await expect(service.preparePublishContentMedia({ |
| 345 | userId: 'user-1', |
| 346 | content: { |
| 347 | media: [{ |
| 348 | url: 'https://cdn.example.test/source.jpg', |
| 349 | options: { adaptation: { imageFormat: PublishMediaAdaptationImageFormat.Auto } }, |
| 350 | }], |
| 351 | }, |
| 352 | mediaRules: { imageFormats: ['jpg', 'jpeg', 'png'] }, |
| 353 | })).resolves.toEqual({ |
| 354 | content: { |
| 355 | media: [{ |
| 356 | url: 'https://cdn.example.test/source.jpg', |
| 357 | metadata: { |
| 358 | type: 'image', |
| 359 | width: 800, |
| 360 | height: 600, |
| 361 | format: 'jpeg', |
| 362 | sizeBytes: 512, |
| 363 | }, |
| 364 | }], |
| 365 | }, |
| 366 | issues: [], |
| 367 | }) |
| 368 | expect(assetsService.uploadFromBuffer).not.toHaveBeenCalled() |
| 369 | }) |
| 370 | |
| 371 | it('normalizes extensionless video media from backend probe metadata', async () => { |
| 372 | const assetsService = { uploadFromBuffer: vi.fn() } |
| 373 | const service = createService(assetsService) |
| 374 | const probeVideo = vi.spyOn(service, 'probeVideo').mockResolvedValue({ |
| 375 | width: 1920, |
| 376 | height: 1080, |
| 377 | format: 'video/mp4', |
| 378 | durationSec: 30, |
| 379 | codec: 'unknown', |
| 380 | sizeBytes: 1024, |
| 381 | }) |
| 382 | setHttpAdapter(service, async config => ({ |
| 383 | data: undefined, |
| 384 | status: 200, |
| 385 | statusText: 'OK', |
| 386 | headers: { 'content-type': 'video/mp4' }, |
| 387 | config, |
| 388 | })) |
| 389 | |
| 390 | await expect(service.preparePublishContentMedia({ |
| 391 | userId: 'user-1', |
| 392 | content: { |
| 393 | media: [{ url: 'https://cdn.example.test/signed-media' }], |
| 394 | }, |
| 395 | mediaRules: { videoFormats: ['mp4'] }, |
| 396 | })).resolves.toEqual({ |
| 397 | content: { |
| 398 | media: [{ |
| 399 | url: 'https://cdn.example.test/signed-media', |
| 400 | metadata: { |
| 401 | type: 'video', |
| 402 | width: 1920, |
| 403 | height: 1080, |
| 404 | durationSec: 30, |
| 405 | codec: 'unknown', |
| 406 | format: 'video/mp4', |
| 407 | sizeBytes: 1024, |
| 408 | }, |
| 409 | }], |
| 410 | }, |
| 411 | issues: [], |
| 412 | }) |
| 413 | expect(probeVideo).toHaveBeenCalledWith('https://cdn.example.test/signed-media') |
| 414 | expect(assetsService.uploadFromBuffer).not.toHaveBeenCalled() |
| 415 | }) |
| 416 | |
| 417 | it('normalizes extensionless image media from backend probe metadata', async () => { |
| 418 | const assetsService = { uploadFromBuffer: vi.fn() } |
| 419 | const service = createService(assetsService) |
| 420 | const source = await sharp({ |
| 421 | create: { |
| 422 | width: 4, |
| 423 | height: 2, |
| 424 | channels: 3, |
| 425 | background: '#ffffff', |
| 426 | }, |
| 427 | }).png().toBuffer() |
| 428 | setHttpAdapter(service, async config => ({ |
| 429 | data: config.method === 'head' ? undefined : source, |
| 430 | status: 200, |
| 431 | statusText: 'OK', |
| 432 | headers: { 'content-type': 'image/png', 'content-length': String(source.length) }, |
| 433 | config, |
| 434 | })) |
| 435 | |
| 436 | await expect(service.preparePublishContentMedia({ |
| 437 | userId: 'user-1', |
| 438 | content: { |
| 439 | media: [{ url: 'https://cdn.example.test/signed-image' }], |
| 440 | }, |
| 441 | mediaRules: { imageFormats: ['png'] }, |
| 442 | })).resolves.toEqual({ |
| 443 | content: { |
| 444 | media: [{ |
| 445 | url: 'https://cdn.example.test/signed-image', |
| 446 | metadata: { |
| 447 | type: 'image', |
| 448 | width: 4, |
| 449 | height: 2, |
| 450 | format: 'png', |
| 451 | sizeBytes: source.length, |
| 452 | }, |
| 453 | }], |
| 454 | }, |
| 455 | issues: [], |
| 456 | }) |
| 457 | expect(assetsService.uploadFromBuffer).not.toHaveBeenCalled() |
| 458 | }) |
| 459 | |
| 460 | it('converts auto media to the first platform-supported target format when source format is unsupported', async () => { |
| 461 | const source = await sharp({ |
| 462 | create: { |
| 463 | width: 4, |
| 464 | height: 2, |
| 465 | channels: 3, |
| 466 | background: '#ffffff', |
| 467 | }, |
| 468 | }).png().toBuffer() |
| 469 | const assetsService = { |
| 470 | uploadFromBuffer: vi.fn(async () => ({ |
| 471 | url: 'https://assets.example.test/publish/media/source.jpeg', |
| 472 | })), |
| 473 | } |
| 474 | const service = createService(assetsService) |
| 475 | setHttpAdapter(service, async config => ({ |
| 476 | data: config.method === 'head' ? undefined : source, |
| 477 | status: 200, |
| 478 | statusText: 'OK', |
| 479 | headers: { 'content-length': String(source.length) }, |
| 480 | config, |
| 481 | })) |
| 482 | |
| 483 | const result = await service.preparePublishContentMedia({ |
| 484 | userId: 'user-1', |
| 485 | content: { |
| 486 | media: [{ |
| 487 | url: 'https://cdn.example.test/source.gif', |
| 488 | options: { adaptation: { imageFormat: PublishMediaAdaptationImageFormat.Auto } }, |
| 489 | }], |
| 490 | }, |
| 491 | mediaRules: { imageFormats: ['jpeg'], maxImageSize: 1024 * 1024 }, |
| 492 | }) |
| 493 | |
| 494 | expect(result.issues).toEqual([]) |
| 495 | expect(result.content).toEqual({ |
| 496 | media: [{ |
| 497 | url: 'https://assets.example.test/publish/media/source.jpeg', |
| 498 | metadata: { |
| 499 | type: 'image', |
| 500 | width: 4, |
| 501 | height: 2, |
| 502 | format: 'jpeg', |
| 503 | sizeBytes: expect.any(Number), |
| 504 | }, |
| 505 | }], |
| 506 | }) |
| 507 | expect(assetsService.uploadFromBuffer).toHaveBeenCalledWith( |
| 508 | 'user-1', |
| 509 | expect.any(Buffer), |
| 510 | expect.objectContaining({ |
| 511 | type: 'publishMedia', |
| 512 | mimeType: 'image/jpeg', |
| 513 | }), |
| 514 | ) |
| 515 | }) |
| 516 | |
| 517 | it('rejects target image formats that are not allowed by platform media rules', async () => { |
| 518 | const assetsService = { uploadFromBuffer: vi.fn() } |
| 519 | const service = createService(assetsService) |
| 520 | vi.spyOn(service, 'probeImage').mockResolvedValue({ |
| 521 | width: 800, |
| 522 | height: 600, |
| 523 | format: 'png', |
| 524 | sizeBytes: 512, |
| 525 | }) |
| 526 | const content = { |
| 527 | media: [{ |
| 528 | url: 'https://cdn.example.test/source.png', |
| 529 | options: { adaptation: { imageFormat: PublishMediaAdaptationImageFormat.Webp } }, |
| 530 | }], |
| 531 | } |
| 532 | |
| 533 | const result = await service.preparePublishContentMedia({ |
| 534 | userId: 'user-1', |
| 535 | content, |
| 536 | mediaRules: { imageFormats: ['jpg', 'jpeg', 'png'] }, |
| 537 | }) |
| 538 | |
| 539 | expect(result.content).toBe(content) |
| 540 | expect(result.issues).toEqual([expect.objectContaining({ |
| 541 | code: PublishValidationIssueCode.InvalidOption, |
| 542 | path: ['content', 'media', 0, 'options', 'adaptation', 'imageFormat'], |
| 543 | })]) |
| 544 | expect(assetsService.uploadFromBuffer).not.toHaveBeenCalled() |
| 545 | }) |
| 546 | |
| 547 | it('converts publish images and reuses the uploaded asset for the same source URL', async () => { |
| 548 | const source = await sharp({ |
| 549 | create: { |
| 550 | width: 4, |
| 551 | height: 2, |
| 552 | channels: 3, |
| 553 | background: '#ffffff', |
| 554 | }, |
| 555 | }).png().toBuffer() |
| 556 | const assetsService = { |
| 557 | uploadFromBuffer: vi.fn(async () => ({ |
| 558 | url: 'https://assets.example.test/publish/media/source.jpeg', |
| 559 | })), |
| 560 | } |
| 561 | const service = createService(assetsService) |
| 562 | setHttpAdapter(service, async config => ({ |
| 563 | data: config.method === 'head' ? undefined : source, |
| 564 | status: 200, |
| 565 | statusText: 'OK', |
| 566 | headers: { 'content-length': String(source.length) }, |
| 567 | config, |
| 568 | })) |
| 569 | |
| 570 | const result = await service.preparePublishContentMedia({ |
| 571 | userId: 'user-1', |
| 572 | content: { |
| 573 | media: [{ |
| 574 | url: 'https://cdn.example.test/source.png', |
| 575 | options: { adaptation: { imageFormat: PublishMediaAdaptationImageFormat.Jpeg } }, |
| 576 | }], |
| 577 | cover: { |
| 578 | url: 'https://cdn.example.test/source.png', |
| 579 | options: { adaptation: { imageFormat: PublishMediaAdaptationImageFormat.Jpeg } }, |
| 580 | }, |
| 581 | }, |
| 582 | mediaRules: { imageFormats: ['jpg', 'jpeg'], maxImageSize: 1024 * 1024 }, |
| 583 | }) |
| 584 | |
| 585 | expect(result.issues).toEqual([]) |
| 586 | expect(result.content).toEqual({ |
| 587 | media: [{ |
| 588 | url: 'https://assets.example.test/publish/media/source.jpeg', |
| 589 | metadata: { |
| 590 | type: 'image', |
| 591 | width: 4, |
| 592 | height: 2, |
| 593 | format: 'jpeg', |
| 594 | sizeBytes: expect.any(Number), |
| 595 | }, |
| 596 | }], |
| 597 | cover: { |
| 598 | url: 'https://assets.example.test/publish/media/source.jpeg', |
| 599 | metadata: { |
| 600 | type: 'image', |
| 601 | width: 4, |
| 602 | height: 2, |
| 603 | format: 'jpeg', |
| 604 | sizeBytes: expect.any(Number), |
| 605 | }, |
| 606 | }, |
| 607 | }) |
| 608 | expect(assetsService.uploadFromBuffer).toHaveBeenCalledTimes(1) |
| 609 | expect(assetsService.uploadFromBuffer).toHaveBeenCalledWith( |
| 610 | 'user-1', |
| 611 | expect.any(Buffer), |
| 612 | expect.objectContaining({ |
| 613 | type: 'publishMedia', |
| 614 | mimeType: 'image/jpeg', |
| 615 | metadata: { width: 4, height: 2 }, |
| 616 | }), |
| 617 | ) |
| 618 | }) |
| 619 | |
| 620 | it('rejects oversized conversion sources before uploading them', async () => { |
| 621 | const assetsService = { uploadFromBuffer: vi.fn() } |
| 622 | const service = createService(assetsService) |
| 623 | const source = await sharp({ |
| 624 | create: { |
| 625 | width: 4, |
| 626 | height: 2, |
| 627 | channels: 3, |
| 628 | background: '#ffffff', |
| 629 | }, |
| 630 | }).png().toBuffer() |
| 631 | setHttpAdapter(service, async config => ({ |
| 632 | data: config.method === 'head' ? undefined : source, |
| 633 | status: 200, |
| 634 | statusText: 'OK', |
| 635 | headers: { 'content-length': String(26 * 1024 * 1024) }, |
| 636 | config, |
| 637 | })) |
| 638 | |
| 639 | const result = await service.preparePublishContentMedia({ |
| 640 | userId: 'user-1', |
| 641 | content: { |
| 642 | media: [{ |
| 643 | url: 'https://cdn.example.test/source.png', |
| 644 | options: { adaptation: { imageFormat: PublishMediaAdaptationImageFormat.Jpeg } }, |
| 645 | }], |
| 646 | }, |
| 647 | mediaRules: { imageFormats: ['jpg', 'jpeg'] }, |
| 648 | }) |
| 649 | |
| 650 | expect(result.issues).toEqual([expect.objectContaining({ |
| 651 | code: PublishValidationIssueCode.TooBig, |
| 652 | path: ['content', 'media', 0], |
| 653 | })]) |
| 654 | expect(assetsService.uploadFromBuffer).not.toHaveBeenCalled() |
| 655 | }) |
| 656 | }) |
| 657 | |
| 658 | describe('media service video validation', () => { |
| 659 | it('accepts video formats configured as extensions when probe format is MIME', () => { |
| 660 | const issues = createService().validateVideo({ |
| 661 | width: 1920, |
| 662 | height: 1080, |
| 663 | format: 'video/mp4', |
| 664 | durationSec: 30, |
| 665 | codec: 'unknown', |
| 666 | sizeBytes: 1024, |
| 667 | }, { |
| 668 | videoFormats: ['mp4', 'mov'], |
| 669 | }, ['content', 'media', 0]) |
| 670 | |
| 671 | expect(issues).toEqual([]) |
| 672 | }) |
| 673 | |
| 674 | it('reports unsupported video formats using normalized extension names', () => { |
| 675 | const issues = createService().validateVideo({ |
| 676 | width: 1920, |
| 677 | height: 1080, |
| 678 | format: 'video/x-msvideo', |
| 679 | durationSec: 30, |
| 680 | codec: 'unknown', |
| 681 | sizeBytes: 1024, |
| 682 | }, { |
| 683 | videoFormats: ['mp4', 'mov'], |
| 684 | }, ['content', 'media', 0]) |
| 685 | |
| 686 | expect(issues).toEqual([expect.objectContaining({ |
| 687 | code: PublishValidationIssueCode.UnsupportedFormat, |
| 688 | params: expect.objectContaining({ |
| 689 | format: 'avi', |
| 690 | }), |
| 691 | })]) |
| 692 | }) |
| 693 | |
| 694 | it('reports unknown video formats by default', () => { |
| 695 | const issues = createService().validateVideo({ |
| 696 | width: 1920, |
| 697 | height: 1080, |
| 698 | format: 'unknown', |
| 699 | durationSec: 30, |
| 700 | codec: 'unknown', |
| 701 | sizeBytes: 1024, |
| 702 | }, { |
| 703 | videoFormats: ['mp4', 'mov'], |
| 704 | }, ['content', 'media', 0]) |
| 705 | |
| 706 | expect(issues).toEqual([expect.objectContaining({ |
| 707 | code: PublishValidationIssueCode.UnsupportedFormat, |
| 708 | params: expect.objectContaining({ |
| 709 | format: 'unknown', |
| 710 | }), |
| 711 | })]) |
| 712 | }) |
| 713 | |
| 714 | it('reports video duration limits from media rules', () => { |
| 715 | const service = createService() |
| 716 | |
| 717 | const tooShort = service.validateVideo({ |
| 718 | width: 1920, |
| 719 | height: 1080, |
| 720 | format: 'video/mp4', |
| 721 | durationSec: 2, |
| 722 | codec: 'unknown', |
| 723 | sizeBytes: 1024, |
| 724 | }, { |
| 725 | videoFormats: ['mp4'], |
| 726 | minVideoDuration: 3, |
| 727 | maxVideoDuration: 90, |
| 728 | }, ['content', 'media', 0]) |
| 729 | |
| 730 | const tooLong = service.validateVideo({ |
| 731 | width: 1920, |
| 732 | height: 1080, |
| 733 | format: 'video/mp4', |
| 734 | durationSec: 120, |
| 735 | codec: 'unknown', |
| 736 | sizeBytes: 1024, |
| 737 | }, { |
| 738 | videoFormats: ['mp4'], |
| 739 | minVideoDuration: 3, |
| 740 | maxVideoDuration: 90, |
| 741 | }, ['content', 'media', 0]) |
| 742 | |
| 743 | expect(tooShort).toEqual([expect.objectContaining({ |
| 744 | code: PublishValidationIssueCode.InvalidDuration, |
| 745 | params: expect.objectContaining({ current: 2, minimum: 3, maximum: 90 }), |
| 746 | })]) |
| 747 | expect(tooLong).toEqual([expect.objectContaining({ |
| 748 | code: PublishValidationIssueCode.InvalidDuration, |
| 749 | params: expect.objectContaining({ current: 120, minimum: 3, maximum: 90 }), |
| 750 | })]) |
| 751 | }) |
| 752 | }) |
| 753 |