|
| 1 | +import { createClient } from "../../src/index"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Simple serializer that replaces values with type placeholders for non-deterministic testing |
| 5 | + */ |
| 6 | +const structureOnlySerializer = { |
| 7 | + test: (val: any) => val != null && typeof val === "object", |
| 8 | + serialize: (val: any) => { |
| 9 | + const replaceValues = (obj: any): any => { |
| 10 | + if (obj === null) return null; |
| 11 | + if (obj === undefined) return undefined; |
| 12 | + if (typeof obj === "string") return "<string>"; |
| 13 | + if (typeof obj === "number") return "<number>"; |
| 14 | + if (typeof obj === "boolean") return "<boolean>"; |
| 15 | + |
| 16 | + if (Array.isArray(obj)) { |
| 17 | + return obj.length > 0 ? [replaceValues(obj[0])] : []; |
| 18 | + } |
| 19 | + |
| 20 | + if (typeof obj === "object") { |
| 21 | + const result: any = {}; |
| 22 | + for (const key of Object.keys(obj).sort()) { |
| 23 | + result[key] = replaceValues(obj[key]); |
| 24 | + } |
| 25 | + return result; |
| 26 | + } |
| 27 | + |
| 28 | + return obj; |
| 29 | + }; |
| 30 | + |
| 31 | + return JSON.stringify(replaceValues(val), null, 2); |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +describe("transcribeUrl E2E", () => { |
| 36 | + let deepgram: ReturnType<typeof createClient>; |
| 37 | + |
| 38 | + beforeAll(() => { |
| 39 | + // Ensure we have an API key from .env |
| 40 | + if (!process.env.DEEPGRAM_API_KEY) { |
| 41 | + throw new Error("DEEPGRAM_API_KEY must be set in .env file for e2e tests"); |
| 42 | + } |
| 43 | + |
| 44 | + deepgram = createClient(process.env.DEEPGRAM_API_KEY); |
| 45 | + |
| 46 | + // Add our custom serializer |
| 47 | + expect.addSnapshotSerializer(structureOnlySerializer); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should transcribe audio from URL and match expected response structure", async () => { |
| 51 | + const testUrl = "https://dpgr.am/spacewalk.wav"; |
| 52 | + |
| 53 | + const { result, error } = await deepgram.listen.prerecorded.transcribeUrl( |
| 54 | + { url: testUrl }, |
| 55 | + { |
| 56 | + model: "nova-3", |
| 57 | + smart_format: true, |
| 58 | + punctuate: true, |
| 59 | + } |
| 60 | + ); |
| 61 | + |
| 62 | + // Verify no error occurred |
| 63 | + expect(error).toBeNull(); |
| 64 | + expect(result).toBeTruthy(); |
| 65 | + |
| 66 | + // Type guard to ensure result is not null for subsequent operations |
| 67 | + if (!result) { |
| 68 | + throw new Error("Result should not be null after toBeTruthy check"); |
| 69 | + } |
| 70 | + |
| 71 | + // Test the structure with snapshot (using custom serializer for non-deterministic content) |
| 72 | + expect(result).toMatchSnapshot("transcribeUrl-response-structure"); |
| 73 | + |
| 74 | + // Essential structural validation - verify we have the required properties |
| 75 | + expect(result.metadata).toBeDefined(); |
| 76 | + expect(result.results).toBeDefined(); |
| 77 | + expect(Array.isArray(result.results.channels)).toBe(true); |
| 78 | + expect(result.results.channels.length).toBeGreaterThan(0); |
| 79 | + |
| 80 | + // Verify we got actual transcription content |
| 81 | + const transcript = result.results.channels[0]?.alternatives?.[0]?.transcript; |
| 82 | + expect(transcript).toBeTruthy(); |
| 83 | + expect(typeof transcript).toBe("string"); |
| 84 | + expect(transcript.length).toBeGreaterThan(0); |
| 85 | + }, 30000); // 30 second timeout for API call |
| 86 | +}); |
0 commit comments