馃 distributed transcription service thistle.dunkirk.sh
1import { afterAll, describe, expect, test } from "bun:test"; 2import { extractAudioCreationDate } from "./audio-metadata"; 3 4describe("extractAudioCreationDate (integration)", () => { 5 const testAudioPath = "./test-audio-sample.m4a"; 6 7 // Clean up test file after tests 8 afterAll(async () => { 9 try { 10 await Bun.file(testAudioPath).exists().then(async (exists) => { 11 if (exists) { 12 await Bun.$`rm ${testAudioPath}`; 13 } 14 }); 15 } catch { 16 // Ignore cleanup errors 17 } 18 }); 19 20 test("extracts creation date from audio file with metadata", async () => { 21 // Create a test audio file with metadata using ffmpeg 22 // 1 second silent audio with creation_time metadata 23 const creationTime = "2024-01-15T14:30:00.000000Z"; 24 25 // Create the file with metadata 26 await Bun.$`ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t 1 -metadata creation_time=${creationTime} -y ${testAudioPath}`.quiet(); 27 28 const date = await extractAudioCreationDate(testAudioPath); 29 30 expect(date).not.toBeNull(); 31 expect(date).toBeInstanceOf(Date); 32 // JavaScript Date.toISOString() uses 3 decimal places, not 6 like the input 33 expect(date?.toISOString()).toBe("2024-01-15T14:30:00.000Z"); 34 }); 35 36 test("returns null for audio file without creation_time metadata", async () => { 37 // Create audio file without metadata 38 await Bun.$`ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t 1 -y ${testAudioPath}`.quiet(); 39 40 const date = await extractAudioCreationDate(testAudioPath); 41 42 // Should use file modification time as fallback 43 expect(date).not.toBeNull(); 44 expect(date).toBeInstanceOf(Date); 45 // Should be very recent (within last minute) 46 const now = new Date(); 47 const diff = now.getTime() - (date?.getTime() ?? 0); 48 expect(diff).toBeLessThan(60000); // Less than 1 minute 49 }); 50 51 test("returns null for non-existent file", async () => { 52 const date = await extractAudioCreationDate("./non-existent-file.m4a"); 53 expect(date).toBeNull(); 54 }); 55});