feat: format and highlight raw JSON in tool cards (#1189) (#1191)

This commit is contained in:
M57 authored and GitHub committed 2026-09-18 14:32:56 +03:00
1 parent 7290b300ab
commit 89cd598c25
9 files changed
+239 -57

No files matched your search

@@ -0,0 +1,29 @@
package com.m57.hermescontrol.ui.chat.components
import com.m57.hermescontrol.theme.CodeKeyword
import com.m57.hermescontrol.theme.CodeNumber
import com.m57.hermescontrol.theme.CodePunctuation
import com.m57.hermescontrol.theme.CodeString
import org.junit.Assert.assertNotNull
import org.junit.Test
class SyntaxHighlightingTest {
@Test
fun `highlightSyntax highlights json keys as keyword and values as string`() {
val json = """{"status": "running", "code": 0}"""
val result = highlightSyntax(json)
val styles = result.spanStyles
val keySpan = styles.find { it.item.color == CodeKeyword }
assertNotNull("JSON key should be highlighted with CodeKeyword", keySpan)
val stringSpan = styles.find { it.item.color == CodeString }
assertNotNull("String value should be highlighted with CodeString", stringSpan)
val numberSpan = styles.find { it.item.color == CodeNumber }
assertNotNull("Number value should be highlighted with CodeNumber", numberSpan)
val punctSpan = styles.find { it.item.color == CodePunctuation }
assertNotNull("Colon/braces should be highlighted with CodePunctuation", punctSpan)
}
}
@@ -0,0 +1,41 @@
package com.m57.hermescontrol.ui.chat.tool
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
class ToolJsonTest {
@Test
fun `prettyPrintJson formats minified json object line-by-line with indentation`() {
val raw = """{"name":"test","count":42,"active":true}"""
val formatted = ToolJson.prettyPrintJson(raw)
assertTrue(formatted.contains("\n"))
assertTrue(formatted.contains(" \"name\": \"test\""))
assertTrue(formatted.contains(" \"count\": 42"))
assertTrue(formatted.contains(" \"active\": true"))
}
@Test
fun `prettyPrintJson formats json array with indentation`() {
val raw = """[{"id":1},{"id":2}]"""
val formatted = ToolJson.prettyPrintJson(raw)
assertTrue(formatted.contains("\n"))
assertTrue(formatted.contains(" {\n \"id\": 1\n }"))
}
@Test
fun `prettyPrintJson returns non-json content unchanged`() {
val raw = "Execution failed with exit code 1"
val formatted = ToolJson.prettyPrintJson(raw)
assertEquals(raw, formatted)
}
@Test
fun `prettyPrintJson returns empty and blank strings unchanged`() {
assertEquals("", ToolJson.prettyPrintJson(""))
assertEquals(" ", ToolJson.prettyPrintJson(" "))
}
}