|
| 1 | +import { Node } from "@dafthunk/types"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { NodeContext } from "../types"; |
| 5 | +import { Gemini25FlashAudioUnderstandingNode } from "./gemini-2-5-flash-audio-understanding-node"; |
| 6 | + |
| 7 | +describe("Gemini25FlashAudioUnderstandingNode", () => { |
| 8 | + vi.mock("@google/genai", () => ({ |
| 9 | + GoogleGenAI: class MockGoogleGenAI { |
| 10 | + models = { |
| 11 | + generateContent: vi.fn().mockResolvedValue({ |
| 12 | + candidates: [ |
| 13 | + { |
| 14 | + content: { |
| 15 | + parts: [ |
| 16 | + { |
| 17 | + text: "This is a transcript of the audio content. The speaker discusses various topics including technology and innovation.", |
| 18 | + }, |
| 19 | + ], |
| 20 | + }, |
| 21 | + finishReason: "STOP", |
| 22 | + }, |
| 23 | + ], |
| 24 | + usageMetadata: { |
| 25 | + promptTokenCount: 150, |
| 26 | + candidatesTokenCount: 25, |
| 27 | + totalTokenCount: 175, |
| 28 | + }, |
| 29 | + }), |
| 30 | + }; |
| 31 | + constructor() {} |
| 32 | + }, |
| 33 | + })); |
| 34 | + |
| 35 | + const nodeId = "gemini-2-5-flash-audio-understanding"; |
| 36 | + const node = new Gemini25FlashAudioUnderstandingNode({ |
| 37 | + nodeId, |
| 38 | + } as unknown as Node); |
| 39 | + |
| 40 | + const createContext = (inputs: Record<string, any>): NodeContext => |
| 41 | + ({ |
| 42 | + nodeId: "test", |
| 43 | + inputs, |
| 44 | + workflowId: "test", |
| 45 | + organizationId: "test-org", |
| 46 | + env: { |
| 47 | + DB: {} as any, |
| 48 | + AI: {} as any, |
| 49 | + AI_OPTIONS: {}, |
| 50 | + RESSOURCES: {} as any, |
| 51 | + DATASETS: {} as any, |
| 52 | + DATASETS_AUTORAG: "", |
| 53 | + EMAIL_DOMAIN: "", |
| 54 | + CLOUDFLARE_ACCOUNT_ID: "", |
| 55 | + CLOUDFLARE_API_TOKEN: "", |
| 56 | + CLOUDFLARE_AI_GATEWAY_ID: "", |
| 57 | + TWILIO_ACCOUNT_SID: "", |
| 58 | + TWILIO_AUTH_TOKEN: "", |
| 59 | + TWILIO_PHONE_NUMBER: "", |
| 60 | + SENDGRID_API_KEY: "", |
| 61 | + SENDGRID_DEFAULT_FROM: "", |
| 62 | + RESEND_API_KEY: "", |
| 63 | + RESEND_DEFAULT_FROM: "", |
| 64 | + AWS_ACCESS_KEY_ID: "", |
| 65 | + AWS_SECRET_ACCESS_KEY: "", |
| 66 | + AWS_REGION: "", |
| 67 | + SES_DEFAULT_FROM: "", |
| 68 | + OPENAI_API_KEY: "", |
| 69 | + ANTHROPIC_API_KEY: "", |
| 70 | + GEMINI_API_KEY: "test", |
| 71 | + }, |
| 72 | + }) as unknown as NodeContext; |
| 73 | + |
| 74 | + const createMockAudio = (mimeType = "audio/wav") => ({ |
| 75 | + data: new Uint8Array([1, 2, 3, 4, 5]), // Mock audio data |
| 76 | + mimeType, |
| 77 | + }); |
| 78 | + |
| 79 | + describe("execute", () => { |
| 80 | + it("should transcribe audio with default prompt", async () => { |
| 81 | + const result = await node.execute( |
| 82 | + createContext({ |
| 83 | + audio: createMockAudio(), |
| 84 | + prompt: "Transcribe this audio", |
| 85 | + }) |
| 86 | + ); |
| 87 | + |
| 88 | + expect(result.status).toBe("completed"); |
| 89 | + expect(result.outputs?.text).toBeDefined(); |
| 90 | + expect(result.outputs?.text).toContain("This is a transcript"); |
| 91 | + expect(result.outputs?.finish_reason).toBe("STOP"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should analyze audio with custom prompt", async () => { |
| 95 | + const result = await node.execute( |
| 96 | + createContext({ |
| 97 | + audio: createMockAudio("audio/mp3"), |
| 98 | + prompt: "Describe what you hear in this audio clip", |
| 99 | + }) |
| 100 | + ); |
| 101 | + |
| 102 | + expect(result.status).toBe("completed"); |
| 103 | + expect(result.outputs?.text).toBeDefined(); |
| 104 | + expect(result.outputs?.text).toContain("speaker discusses"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should handle timestamp-based analysis", async () => { |
| 108 | + const result = await node.execute( |
| 109 | + createContext({ |
| 110 | + audio: createMockAudio(), |
| 111 | + prompt: "Provide a transcript from 02:30 to 03:29", |
| 112 | + }) |
| 113 | + ); |
| 114 | + |
| 115 | + expect(result.status).toBe("completed"); |
| 116 | + expect(result.outputs?.text).toBeDefined(); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should handle thinking budget configuration", async () => { |
| 120 | + const result = await node.execute( |
| 121 | + createContext({ |
| 122 | + audio: createMockAudio(), |
| 123 | + prompt: "Analyze this audio content", |
| 124 | + thinking_budget: 500, |
| 125 | + }) |
| 126 | + ); |
| 127 | + |
| 128 | + expect(result.status).toBe("completed"); |
| 129 | + expect(result.outputs?.text).toBeDefined(); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should return error when audio is missing", async () => { |
| 133 | + const result = await node.execute( |
| 134 | + createContext({ |
| 135 | + prompt: "Transcribe this audio", |
| 136 | + }) |
| 137 | + ); |
| 138 | + |
| 139 | + expect(result.status).toBe("error"); |
| 140 | + expect(result.error).toContain("Audio input is required"); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should return error when prompt is missing", async () => { |
| 144 | + const result = await node.execute( |
| 145 | + createContext({ |
| 146 | + audio: createMockAudio(), |
| 147 | + }) |
| 148 | + ); |
| 149 | + |
| 150 | + expect(result.status).toBe("error"); |
| 151 | + expect(result.error).toContain("Prompt is required"); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should return error when API key is missing", async () => { |
| 155 | + const context = createContext({ |
| 156 | + audio: createMockAudio(), |
| 157 | + prompt: "Transcribe this audio", |
| 158 | + }); |
| 159 | + context.env.GEMINI_API_KEY = ""; |
| 160 | + |
| 161 | + const result = await node.execute(context); |
| 162 | + |
| 163 | + expect(result.status).toBe("error"); |
| 164 | + expect(result.error).toContain("GEMINI_API_KEY is not configured"); |
| 165 | + }); |
| 166 | + |
| 167 | + it("should handle different audio formats", async () => { |
| 168 | + const formats = [ |
| 169 | + "audio/wav", |
| 170 | + "audio/mp3", |
| 171 | + "audio/aiff", |
| 172 | + "audio/aac", |
| 173 | + "audio/ogg", |
| 174 | + "audio/flac", |
| 175 | + ]; |
| 176 | + |
| 177 | + for (const format of formats) { |
| 178 | + const result = await node.execute( |
| 179 | + createContext({ |
| 180 | + audio: createMockAudio(format), |
| 181 | + prompt: "Transcribe this audio", |
| 182 | + }) |
| 183 | + ); |
| 184 | + |
| 185 | + expect(result.status).toBe("completed"); |
| 186 | + expect(result.outputs?.text).toBeDefined(); |
| 187 | + } |
| 188 | + }); |
| 189 | + |
| 190 | + it("should handle large audio files without stack overflow", async () => { |
| 191 | + // Create a larger mock audio file to test the base64 conversion |
| 192 | + const largeAudioData = new Uint8Array(100000); // 100KB of data |
| 193 | + for (let i = 0; i < largeAudioData.length; i++) { |
| 194 | + largeAudioData[i] = Math.floor(Math.random() * 256); |
| 195 | + } |
| 196 | + |
| 197 | + const result = await node.execute( |
| 198 | + createContext({ |
| 199 | + audio: { |
| 200 | + data: largeAudioData, |
| 201 | + mimeType: "audio/wav", |
| 202 | + }, |
| 203 | + prompt: "Transcribe this audio", |
| 204 | + }) |
| 205 | + ); |
| 206 | + |
| 207 | + expect(result.status).toBe("completed"); |
| 208 | + expect(result.outputs?.text).toBeDefined(); |
| 209 | + }); |
| 210 | + }); |
| 211 | +}); |
0 commit comments