|
| 1 | +package com.groqandroid |
| 2 | + |
| 3 | +import kotlinx.coroutines.test.runTest |
| 4 | +import okhttp3.mockwebserver.MockResponse |
| 5 | +import okhttp3.mockwebserver.MockWebServer |
| 6 | +import org.junit.After |
| 7 | +import org.junit.Assert.* |
| 8 | +import org.junit.Before |
| 9 | +import org.junit.Test |
| 10 | +import java.io.File |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests for GroqApiClient error handling. |
| 14 | + * Uses MockWebServer to simulate various API responses. |
| 15 | + */ |
| 16 | +class ApiClientTest { |
| 17 | + |
| 18 | + private lateinit var server: MockWebServer |
| 19 | + private lateinit var tempFile: File |
| 20 | + |
| 21 | + @Before |
| 22 | + fun setup() { |
| 23 | + server = MockWebServer() |
| 24 | + server.start() |
| 25 | + |
| 26 | + // Create a minimal valid WAV file for testing |
| 27 | + tempFile = File.createTempFile("test_audio", ".wav") |
| 28 | + tempFile.writeBytes(ByteArray(44 + 100)) |
| 29 | + } |
| 30 | + |
| 31 | + @After |
| 32 | + fun teardown() { |
| 33 | + server.shutdown() |
| 34 | + tempFile.delete() |
| 35 | + } |
| 36 | + |
| 37 | + private fun createClient(): GroqApiClient { |
| 38 | + return GroqApiClient("test-api-key", server.url("/v1/audio/transcriptions").toString()) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `successful transcription returns text`() = runTest { |
| 43 | + server.enqueue(MockResponse() |
| 44 | + .setResponseCode(200) |
| 45 | + .setBody("""{"text": "Hello world"}""")) |
| 46 | + |
| 47 | + val result = createClient().transcribe(tempFile) |
| 48 | + assertEquals("Hello world", result) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `empty transcription returns empty string`() = runTest { |
| 53 | + server.enqueue(MockResponse() |
| 54 | + .setResponseCode(200) |
| 55 | + .setBody("""{"text": ""}""")) |
| 56 | + |
| 57 | + val result = createClient().transcribe(tempFile) |
| 58 | + assertEquals("", result) |
| 59 | + } |
| 60 | + |
| 61 | + @Test(expected = AuthenticationException::class) |
| 62 | + fun `401 throws AuthenticationException`() = runTest { |
| 63 | + server.enqueue(MockResponse() |
| 64 | + .setResponseCode(401) |
| 65 | + .setBody("""{"error": {"message": "Invalid API key"}}""")) |
| 66 | + |
| 67 | + createClient().transcribe(tempFile) |
| 68 | + } |
| 69 | + |
| 70 | + @Test(expected = AuthenticationException::class) |
| 71 | + fun `403 throws AuthenticationException`() = runTest { |
| 72 | + server.enqueue(MockResponse() |
| 73 | + .setResponseCode(403) |
| 74 | + .setBody("""{"error": {"message": "Forbidden"}}""")) |
| 75 | + |
| 76 | + createClient().transcribe(tempFile) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `429 retries with backoff then succeeds`() = runTest { |
| 81 | + server.enqueue(MockResponse().setResponseCode(429).setBody("""{"error": {"message": "Rate limited"}}""")) |
| 82 | + server.enqueue(MockResponse().setResponseCode(200).setBody("""{"text": "After retry"}""")) |
| 83 | + |
| 84 | + val result = createClient().transcribe(tempFile) |
| 85 | + assertEquals("After retry", result) |
| 86 | + assertEquals(2, server.requestCount) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `500 retries with backoff then succeeds`() = runTest { |
| 91 | + server.enqueue(MockResponse().setResponseCode(500).setBody("""{"error": {"message": "Server error"}}""")) |
| 92 | + server.enqueue(MockResponse().setResponseCode(200).setBody("""{"text": "Recovered"}""")) |
| 93 | + |
| 94 | + val result = createClient().transcribe(tempFile) |
| 95 | + assertEquals("Recovered", result) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun `401 does not retry or try fallback models`() = runTest { |
| 100 | + server.enqueue(MockResponse() |
| 101 | + .setResponseCode(401) |
| 102 | + .setBody("""{"error": {"message": "Invalid key"}}""")) |
| 103 | + |
| 104 | + try { |
| 105 | + createClient().transcribe(tempFile) |
| 106 | + fail("Should have thrown AuthenticationException") |
| 107 | + } catch (_: AuthenticationException) { |
| 108 | + assertEquals(1, server.requestCount) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun `response with missing text field returns empty`() = runTest { |
| 114 | + server.enqueue(MockResponse() |
| 115 | + .setResponseCode(200) |
| 116 | + .setBody("""{"duration": 1.5}""")) |
| 117 | + |
| 118 | + val result = createClient().transcribe(tempFile) |
| 119 | + assertEquals("", result) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun `model fallback on non-auth error`() = runTest { |
| 124 | + // First model fails with 400 (non-retryable, goes to next model) |
| 125 | + server.enqueue(MockResponse().setResponseCode(400).setBody("""{"error": {"message": "Bad model"}}""")) |
| 126 | + // Fallback model succeeds |
| 127 | + server.enqueue(MockResponse().setResponseCode(200).setBody("""{"text": "Fallback worked"}""")) |
| 128 | + |
| 129 | + val result = createClient().transcribe(tempFile) |
| 130 | + assertEquals("Fallback worked", result) |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun `authorization header is sent`() = runTest { |
| 135 | + server.enqueue(MockResponse() |
| 136 | + .setResponseCode(200) |
| 137 | + .setBody("""{"text": "test"}""")) |
| 138 | + |
| 139 | + createClient().transcribe(tempFile) |
| 140 | + |
| 141 | + val request = server.takeRequest() |
| 142 | + assertEquals("Bearer test-api-key", request.getHeader("Authorization")) |
| 143 | + } |
| 144 | +} |
0 commit comments