package com.suno.android.common_data import org.junit.Assert import org.junit.Test class PartialJsonExtractorTest { private val extractor = PartialJsonExtractor() @Test fun `given complete JSON when extracting message field then returns full message`() { val json = """{"message":"Hello World"}""" val result = extractor.extractField(partialJson = json, fieldName = "message") Assert.assertEquals("Hello World", result) } @Test fun `given incomplete JSON when extracting message field then returns partial message`() { val json = """{"message":"Hello""" val result = extractor.extractField(partialJson = json, fieldName = "message") Assert.assertEquals("Hello", result) } @Test fun `given streaming SSE arguments when extracting message then accumulates correctly`() { val fragments = listOf( """{"message":"Hey""", """! What""", """ kind""", """ of sound""", ) val results = mutableListOf() fragments.forEachIndexed { index, _ -> val accumulated = fragments.subList(0, index + 1).joinToString("") val result = extractor.extractField(partialJson = accumulated, fieldName = "message") result?.let { results.add(it) } } Assert.assertEquals( listOf("Hey", "Hey! What", "Hey! What kind", "Hey! What kind of sound"), results, ) } @Test fun `given JSON with escaped quotes when extracting then handles escapes correctly`() { val json = """{"message":"He said \"Hello\""}""" val result = extractor.extractField(partialJson = json, fieldName = "message") Assert.assertEquals("He said \"Hello\"", result) } @Test fun `given JSON without message field when extracting then returns null`() { val json = """{"other":"value"}""" val result = extractor.extractField(partialJson = json, fieldName = "message") Assert.assertNull(result) } @Test fun `given nested JSON when extracting message field then returns correct value`() { val json = """{"choices":["Pop"],"message":"Hello"}""" val result = extractor.extractField(partialJson = json, fieldName = "message") Assert.assertEquals("Hello", result) } @Test fun `given JSON with newlines when extracting then preserves newlines`() { val json = """{"message":"Line 1\nLine 2"}""" val result = extractor.extractField(partialJson = json, fieldName = "message") Assert.assertEquals("Line 1\nLine 2", result) } @Test fun `given incomplete JSON with partial field when extracting then returns what exists`() { val json = """{"mess""" val result = extractor.extractField(partialJson = json, fieldName = "message") Assert.assertNull(result) } @Test fun `given JSON with message field at different positions when extracting then finds it`() { val json = """{"other":"value","message":"Test","more":"data"}""" val result = extractor.extractField(partialJson = json, fieldName = "message") Assert.assertEquals("Test", result) } }