Skip to content

Commit f730d83

Browse files
committed
feat(test): add transcribeUrl integration test
1 parent 6093bb7 commit f730d83

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`transcribeUrl E2E should transcribe audio from URL and match expected response structure: transcribeUrl-response-structure 1`] = `
4+
{
5+
"metadata": {
6+
"channels": "<number>",
7+
"created": "<string>",
8+
"duration": "<number>",
9+
"model_info": {
10+
"3b3aabe4-608a-46ac-9585-7960a25daf1a": {
11+
"arch": "<string>",
12+
"name": "<string>",
13+
"version": "<string>"
14+
}
15+
},
16+
"models": [
17+
"<string>"
18+
],
19+
"request_id": "<string>",
20+
"sha256": "<string>",
21+
"transaction_key": "<string>"
22+
},
23+
"results": {
24+
"channels": [
25+
{
26+
"alternatives": [
27+
{
28+
"confidence": "<number>",
29+
"paragraphs": {
30+
"paragraphs": [
31+
{
32+
"end": "<number>",
33+
"num_words": "<number>",
34+
"sentences": [
35+
{
36+
"end": "<number>",
37+
"start": "<number>",
38+
"text": "<string>"
39+
}
40+
],
41+
"start": "<number>"
42+
}
43+
],
44+
"transcript": "<string>"
45+
},
46+
"transcript": "<string>",
47+
"words": [
48+
{
49+
"confidence": "<number>",
50+
"end": "<number>",
51+
"punctuated_word": "<string>",
52+
"start": "<number>",
53+
"word": "<string>"
54+
}
55+
]
56+
}
57+
]
58+
}
59+
]
60+
}
61+
}
62+
`;

tests/e2e/transcribe-url.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)