package net.siteed.audiostream.integration import android.os.Bundle import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import org.junit.Test import org.junit.runner.RunWith import org.junit.Assert.* import java.io.File /** * Integration test for Compressed-Only Output (Issue #244) * * This test validates that when primary output is disabled and compressed output * is enabled, the compressed file information is properly returned in the result. */ @RunWith(AndroidJUnit4::class) class CompressedOnlyOutputTest { private val context = InstrumentationRegistry.getInstrumentation().targetContext @Test fun testCompressedOnlyOutput_AAC() { println("๐Ÿงช Test: Compressed-Only Output with AAC") println("---------------------------------------") // Configuration with primary disabled, compressed enabled val config = Bundle().apply { putInt("sampleRate", 44100) putInt("channels", 1) putString("encoding", "pcm_16bit") val outputBundle = Bundle().apply { val primaryBundle = Bundle().apply { putBoolean("enabled", false) } val compressedBundle = Bundle().apply { putBoolean("enabled", true) putString("format", "aac") putInt("bitrate", 128000) } putBundle("primary", primaryBundle) putBundle("compressed", compressedBundle) } putBundle("output", outputBundle) } // Simulate recording result val result = simulateRecording(config) // Verify compression info is present val compressionBundle = result.getBundle("compression") assertNotNull("Compression info should not be null", compressionBundle) // Verify compressed file URI is provided val compressedFileUri = compressionBundle?.getString("compressedFileUri") assertNotNull("Compressed file URI should not be null", compressedFileUri) assertNotEquals("Compressed file URI should not be empty", "", compressedFileUri) // Verify format and bitrate assertEquals("aac", compressionBundle?.getString("format")) assertEquals(128000, compressionBundle?.getInt("bitrate")) println("โœ… Compression info properly returned") println("โœ… Compressed file URI: $compressedFileUri") println("โœ… Format: ${compressionBundle?.getString("format")}") println() } @Test fun testCompressedOnlyOutput_Opus() { println("๐Ÿงช Test: Compressed-Only Output with Opus") println("----------------------------------------") val config = Bundle().apply { putInt("sampleRate", 48000) putInt("channels", 1) putString("encoding", "pcm_16bit") val outputBundle = Bundle().apply { val primaryBundle = Bundle().apply { putBoolean("enabled", false) } val compressedBundle = Bundle().apply { putBoolean("enabled", true) putString("format", "opus") putInt("bitrate", 64000) } putBundle("primary", primaryBundle) putBundle("compressed", compressedBundle) } putBundle("output", outputBundle) } val result = simulateRecording(config) val compressionBundle = result.getBundle("compression") assertNotNull("Compression info should not be null", compressionBundle) assertEquals("opus", compressionBundle?.getString("format")) assertEquals(64000, compressionBundle?.getInt("bitrate")) println("โœ… Opus compression properly configured") println() } @Test fun testFileAccessibility() { println("๐Ÿงช Test: Verify Compressed File Accessibility") println("--------------------------------------------") val config = Bundle().apply { putInt("sampleRate", 44100) putInt("channels", 1) putString("encoding", "pcm_16bit") val outputBundle = Bundle().apply { val primaryBundle = Bundle().apply { putBoolean("enabled", false) } val compressedBundle = Bundle().apply { putBoolean("enabled", true) putString("format", "aac") } putBundle("primary", primaryBundle) putBundle("compressed", compressedBundle) } putBundle("output", outputBundle) } val result = simulateRecording(config) // Check main result structure val fileUri = result.getString("fileUri", "") val filename = result.getString("filename", "") // Check compression structure val compressionBundle = result.getBundle("compression") val compressedUri = compressionBundle?.getString("compressedFileUri") val compressedSize = compressionBundle?.getLong("size", 0L) ?: 0L // When primary is disabled, we should have access to compressed file val hasAccessToCompressed = !compressedUri.isNullOrEmpty() || fileUri.isNotEmpty() assertTrue("Should have access to compressed file", hasAccessToCompressed) assertTrue("Compressed file should have size > 0", compressedSize > 0) println("โœ… Compressed file is accessible") println("โœ… File size reported: $compressedSize bytes") println() } @Test fun testBugScenario() { println("๐Ÿงช Test: Current Bug Scenario") println("-----------------------------") // This test demonstrates the current bug val config = Bundle().apply { putInt("sampleRate", 44100) putInt("channels", 1) putString("encoding", "pcm_16bit") val outputBundle = Bundle().apply { val primaryBundle = Bundle().apply { putBoolean("enabled", false) } val compressedBundle = Bundle().apply { putBoolean("enabled", true) putString("format", "aac") putInt("bitrate", 128000) } putBundle("primary", primaryBundle) putBundle("compressed", compressedBundle) } putBundle("output", outputBundle) } // Current buggy behavior val buggyResult = simulateBuggyRecording(config) // This is what currently happens - compression is null val compressionBundle = buggyResult.getBundle("compression") if (compressionBundle == null) { println("โŒ BUG CONFIRMED: Compression info is null when primary is disabled") println(" This prevents users from accessing the compressed file") } else { println("โœ… Bug appears to be fixed - compression info is present") } println() } /** * Simulates the expected correct behavior after fix */ private fun simulateRecording(config: Bundle): Bundle { val outputConfig = config.getBundle("output") val primaryEnabled = outputConfig?.getBundle("primary")?.getBoolean("enabled", true) ?: true val compressedConfig = outputConfig?.getBundle("compressed") val compressedEnabled = compressedConfig?.getBoolean("enabled", false) ?: false return Bundle().apply { if (!primaryEnabled) { // Expected behavior after fix putString("fileUri", "") putString("filename", "stream-only") putLong("durationMs", 5000) putLong("size", 0) putString("mimeType", "audio/wav") // FIXED: Include compression info when compressed is enabled if (compressedEnabled) { val compressionBundle = Bundle().apply { putString("compressedFileUri", "file:///storage/emulated/0/Android/data/test/files/recording.aac") putString("format", compressedConfig?.getString("format") ?: "aac") putInt("bitrate", compressedConfig?.getInt("bitrate") ?: 128000) putLong("size", 40000) putString("mimeType", "audio/aac") } putBundle("compression", compressionBundle) } } else { // Normal behavior putString("fileUri", "file:///storage/emulated/0/Android/data/test/files/recording.wav") putString("filename", "recording.wav") putLong("durationMs", 5000) putLong("size", 240000) putString("mimeType", "audio/wav") } } } /** * Simulates the current buggy behavior */ private fun simulateBuggyRecording(config: Bundle): Bundle { val outputConfig = config.getBundle("output") val primaryEnabled = outputConfig?.getBundle("primary")?.getBoolean("enabled", true) ?: true return Bundle().apply { if (!primaryEnabled) { // Current buggy behavior putString("fileUri", "") putString("filename", "stream-only") putLong("durationMs", 5000) putLong("size", 0) putString("mimeType", "audio/wav") // BUG: compression is null even when compressed output is enabled putBundle("compression", null) } } } }