Import Instagram archive to a Bluesky account
9
fork

Configure Feed

Select the types of activity you want to include in your feed.

Update media processing tests to handle different media types

- Modify processPost tests to support both video and image media
- Adjust test expectations for video and image media embedding
- Remove unnecessary test parameters
- Add new test case for image media processing
- Improve test coverage for media type handling

+87 -64
+87 -64
tests/media.test.ts
··· 1 - import { getMimeType, processMedia, processPost } from '../src/media'; 2 - import path from 'path'; 3 - import fs from 'fs'; 1 + import { getMimeType, processMedia, processPost } from "../src/media"; 2 + import path from "path"; 3 + import fs from "fs"; 4 4 5 5 // Mock the file system 6 - jest.mock('fs', () => ({ 6 + jest.mock("fs", () => ({ 7 7 readFileSync: jest.fn(), 8 8 })); 9 9 10 10 // Mock the logger to avoid console noise during tests 11 - jest.mock('../src/logger', () => ({ 11 + jest.mock("../src/logger", () => ({ 12 12 logger: { 13 13 error: jest.fn(), 14 14 warn: jest.fn(), ··· 17 17 })); 18 18 19 19 // Mock the video validation 20 - jest.mock('../src/video', () => ({ 20 + jest.mock("../src/video", () => ({ 21 21 validateVideo: jest.fn().mockReturnValue(true), 22 22 getVideoDimensions: jest.fn().mockResolvedValue({ width: 640, height: 480 }), 23 23 })); 24 24 25 - describe('Media Processing', () => { 25 + describe("Media Processing", () => { 26 26 beforeEach(() => { 27 27 // Clear all mocks before each test 28 28 jest.clearAllMocks(); 29 29 // Setup default mock for readFileSync 30 - (fs.readFileSync as jest.Mock).mockReturnValue(Buffer.from('test')); 30 + (fs.readFileSync as jest.Mock).mockReturnValue(Buffer.from("test")); 31 31 }); 32 32 33 - describe('getMimeType', () => { 34 - test('should return correct mime types for supported files', () => { 35 - expect(getMimeType('jpg')).toBe('image/jpeg'); 36 - expect(getMimeType('mp4')).toBe('video/mp4'); 37 - expect(getMimeType('mov')).toBe('video/quicktime'); 33 + describe("getMimeType", () => { 34 + test("should return correct mime types for supported files", () => { 35 + expect(getMimeType("jpg")).toBe("image/jpeg"); 36 + expect(getMimeType("mp4")).toBe("video/mp4"); 37 + expect(getMimeType("mov")).toBe("video/quicktime"); 38 38 }); 39 39 40 - test('should return empty string for unsupported files', () => { 41 - expect(getMimeType('xyz')).toBe(''); 40 + test("should return empty string for unsupported files", () => { 41 + expect(getMimeType("xyz")).toBe(""); 42 42 }); 43 43 }); 44 44 45 - describe('processMedia', () => { 45 + describe("processMedia", () => { 46 46 const testMedia = { 47 - uri: 'test.mp4', 47 + uri: "test.mp4", 48 48 creation_timestamp: Date.now() / 1000, 49 - title: 'Test Media', 49 + title: "Test Media", 50 50 media_metadata: { 51 51 photo_metadata: { 52 - exif_data: [{ 53 - latitude: 45.5, 54 - longitude: -122.5 55 - }] 56 - } 57 - } 52 + exif_data: [ 53 + { 54 + latitude: 45.5, 55 + longitude: -122.5, 56 + }, 57 + ], 58 + }, 59 + }, 58 60 }; 59 61 60 - test('should process video media file correctly', async () => { 62 + test("should process video media file correctly", async () => { 61 63 const result = await processMedia( 62 64 testMedia, 63 - path.join(__dirname, '../transfer/test_videos'), 64 - true 65 + path.join(__dirname, "../transfer/test_videos") 65 66 ); 66 - 67 - expect(result.mimeType).toBe('video/mp4'); 67 + 68 + expect(result.mimeType).toBe("video/mp4"); 68 69 expect(result.isVideo).toBe(true); 69 70 expect(result.mediaBuffer).toBeTruthy(); 70 - expect(result.mediaText).toContain('Test Media'); 71 - expect(result.mediaText).toContain('geo:45.5,-122.5'); 71 + expect(result.mediaText).toContain("Test Media"); 72 + expect(result.mediaText).toContain("geo:45.5,-122.5"); 72 73 }); 73 74 74 - test('should handle missing media file', async () => { 75 + test("should handle missing media file", async () => { 75 76 (fs.readFileSync as jest.Mock).mockImplementation(() => { 76 - throw new Error('File not found'); 77 + throw new Error("File not found"); 77 78 }); 78 79 79 80 const result = await processMedia( 80 81 testMedia, 81 - path.join(__dirname, '../transfer/test_videos'), 82 - true 82 + path.join(__dirname, "../transfer/test_videos") 83 83 ); 84 - 84 + 85 85 expect(result.mimeType).toBeNull(); 86 86 expect(result.mediaBuffer).toBeNull(); 87 87 }); 88 88 }); 89 89 90 - describe('processPost', () => { 90 + describe("processPost", () => { 91 91 const testPost = { 92 92 creation_timestamp: Date.now() / 1000, 93 - title: 'Test Post', 94 - media: [{ 95 - uri: 'test.mp4', 96 - creation_timestamp: Date.now() / 1000, 97 - title: 'Test Media' 98 - }] 93 + title: "Test Post", 94 + media: [ 95 + { 96 + uri: "test.mp4", 97 + creation_timestamp: Date.now() / 1000, 98 + title: "Test Media", 99 + }, 100 + ], 99 101 }; 100 102 101 - test('should process post correctly', async () => { 103 + test("should process post correctly", async () => { 102 104 const result = await processPost( 103 105 testPost, 104 - path.join(__dirname, '../transfer/test_videos'), 105 - true, 106 - true 106 + path.join(__dirname, "../transfer/test_videos") 107 107 ); 108 108 109 109 expect(result.postDate).toBeTruthy(); 110 - expect(result.postText).toBe('Test Post'); 111 - expect(Array.isArray(result.embeddedMedia)).toBe(true); 110 + expect(result.postText).toBe("Test Post"); 111 + // Video media should only be a single embedded object. 112 + expect(Array.isArray(result.embeddedMedia)).toBe(false); 112 113 expect(result.mediaCount).toBe(1); 113 114 }); 114 115 115 - test('should handle post with no media', async () => { 116 + test("should handle post with no media", async () => { 116 117 const emptyPost = { 117 118 creation_timestamp: Date.now() / 1000, 118 - title: 'Empty Post', 119 - media: [] 119 + title: "Empty Post", 120 + media: [], 120 121 }; 121 122 122 123 const result = await processPost( 123 124 emptyPost, 124 - path.join(__dirname, '../transfer/test_videos'), 125 - true, 126 - true 125 + path.join(__dirname, "../transfer/test_videos") 127 126 ); 128 127 129 128 expect(result.postDate).toBeTruthy(); 130 - expect(result.postText).toBe('Empty Post'); 129 + expect(result.postText).toBe("Empty Post"); 131 130 expect(result.embeddedMedia).toHaveLength(0); 132 131 expect(result.mediaCount).toBe(0); 133 132 }); 134 133 135 - test('should truncate long post text', async () => { 134 + test("should truncate long post text", async () => { 136 135 const longPost = { 137 136 creation_timestamp: Date.now() / 1000, 138 - title: 'A'.repeat(400), // Create a string longer than POST_TEXT_LIMIT 139 - media: [] 137 + title: "A".repeat(400), // Create a string longer than POST_TEXT_LIMIT 138 + media: [], 140 139 }; 141 140 142 141 const result = await processPost( 143 142 longPost, 144 - path.join(__dirname, '../transfer/test_videos'), 145 - true, 146 - true 143 + path.join(__dirname, "../transfer/test_videos") 147 144 ); 148 145 149 146 expect(result.postText.length).toBeLessThanOrEqual(300); 150 - expect(result.postText.endsWith('...')).toBe(true); 147 + expect(result.postText.endsWith("...")).toBe(true); 148 + }); 149 + 150 + test("should handle post with jpg media as array", async () => { 151 + const jpgPost = { 152 + creation_timestamp: Date.now() / 1000, 153 + title: "Image Post", 154 + media: [ 155 + { 156 + uri: "test.jpg", 157 + creation_timestamp: Date.now() / 1000, 158 + title: "Test Image", 159 + }, 160 + ], 161 + }; 162 + 163 + const result = await processPost( 164 + jpgPost, 165 + path.join(__dirname, "../transfer/test_videos") 166 + ); 167 + 168 + expect(result.postDate).toBeTruthy(); 169 + expect(result.postText).toBe("Image Post"); 170 + // Image media should be an array 171 + expect(Array.isArray(result.embeddedMedia)).toBe(true); 172 + expect(result.embeddedMedia).toHaveLength(1); 173 + expect(result.mediaCount).toBe(1); 151 174 }); 152 175 }); 153 - }); 176 + });