image-dimensions.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { describe, expect, it } from 'vitest';
  2. import { imageDimensions } from '../../src/pi-turn-runner/image-dimensions.js';
  3. import {
  4. BOUNDARY_100X7_JPEG,
  5. BOUNDARY_100X8_JPEG,
  6. CORRUPT_TRUNCATED_JPEG,
  7. EXIF_80X60_JPEG,
  8. MALFORMED_SHORT_SOF_JPEG,
  9. NORMAL_40X24_PNG,
  10. NORMAL_64X48_JPEG,
  11. THIN_432X2_JPEG,
  12. UNSUPPORTED_50X50_WEBP,
  13. } from './image-fixtures.js';
  14. describe('imageDimensions', () => {
  15. it('reads a synthetic thin JPEG header', () => {
  16. expect(imageDimensions(THIN_432X2_JPEG, 'image/jpeg')).toEqual({ width: 432, height: 2 });
  17. });
  18. it('reads a PNG header (TS-9)', () => {
  19. expect(imageDimensions(NORMAL_40X24_PNG, 'image/png')).toEqual({ width: 40, height: 24 });
  20. });
  21. it('reads an ordinary JPEG (TS-11)', () => {
  22. expect(imageDimensions(NORMAL_64X48_JPEG, 'image/jpeg')).toEqual({ width: 64, height: 48 });
  23. });
  24. it('walks the segment chain past EXIF instead of assuming a fixed offset (TS-12)', () => {
  25. expect(imageDimensions(EXIF_80X60_JPEG, 'image/jpeg')).toEqual({ width: 80, height: 60 });
  26. });
  27. it('reads the boundary fixtures used by the outbound projection', () => {
  28. expect(imageDimensions(BOUNDARY_100X8_JPEG, 'image/jpeg')).toEqual({ width: 100, height: 8 });
  29. expect(imageDimensions(BOUNDARY_100X7_JPEG, 'image/jpeg')).toEqual({ width: 100, height: 7 });
  30. });
  31. it('resolves images whose declared mimeType does not match their bytes', () => {
  32. // `projectVideoBlocks` turns a `video` block into an `image` block while
  33. // keeping its `video/*` mimeType, so the bytes have to win.
  34. expect(imageDimensions(THIN_432X2_JPEG, 'video/mp4')).toEqual({ width: 432, height: 2 });
  35. expect(imageDimensions(NORMAL_40X24_PNG, 'image/jpeg')).toEqual({ width: 40, height: 24 });
  36. });
  37. it.each([
  38. ['an unsupported format (webp)', UNSUPPORTED_50X50_WEBP, 'image/webp'],
  39. ['a truncated JPEG', CORRUPT_TRUNCATED_JPEG, 'image/jpeg'],
  40. ['an empty string', '', 'image/jpeg'],
  41. ['text that is not base64', '!!!not base64!!!', 'image/jpeg'],
  42. ['plain text bytes', 'aGVsbG8gd29ybGQ=', 'image/png'],
  43. ['a missing mimeType', THIN_432X2_JPEG.slice(0, 8), undefined],
  44. ['a non-string payload', 42, 'image/jpeg'],
  45. ['a null payload', null, 'image/jpeg'],
  46. ['an undefined payload', undefined, 'image/jpeg'],
  47. ])('returns undefined without throwing for %s (TS-13, TS-14)', (_label, data, mimeType) => {
  48. let result: unknown = 'unset';
  49. expect(() => {
  50. result = imageDimensions(data, mimeType);
  51. }).not.toThrow();
  52. expect(result).toBeUndefined();
  53. });
  54. it('refuses an SOF segment too short to hold its own frame header (TS-31)', () => {
  55. // Declared segment length 3; the bytes a naive read would use as the size
  56. // live outside the segment and would fabricate 7x5.
  57. let result: unknown = 'unset';
  58. expect(() => {
  59. result = imageDimensions(MALFORMED_SHORT_SOF_JPEG, 'image/jpeg');
  60. }).not.toThrow();
  61. expect(result).toBeUndefined();
  62. });
  63. it('does not throw on a JPEG truncated at every prefix length (TS-14)', () => {
  64. const bytes = Buffer.from(THIN_432X2_JPEG, 'base64');
  65. for (let length = 0; length <= bytes.length; length += 1) {
  66. const truncated = bytes.subarray(0, length).toString('base64');
  67. let result: ReturnType<typeof imageDimensions>;
  68. expect(() => {
  69. result = imageDimensions(truncated, 'image/jpeg');
  70. }).not.toThrow();
  71. // Either it cannot tell, or it tells the truth. Never a fabricated size.
  72. if (result !== undefined) expect(result).toEqual({ width: 432, height: 2 });
  73. }
  74. });
  75. });