git.iko.soy/opencode-android
on 4cdfbda help sign in
102 lines 3.6 kB history raw
1package soy.iko.opencode
3import org.junit.Assert.assertEquals
4import org.junit.Assert.assertFalse
5import org.junit.Assert.assertTrue
6import org.junit.Test
7import soy.iko.opencode.data.model.Agent
8import soy.iko.opencode.data.model.Command
9import soy.iko.opencode.data.network.OpencodeJson
10import soy.iko.opencode.ui.components.DiffLine
11import soy.iko.opencode.ui.components.looksLikeDiff
12import soy.iko.opencode.ui.components.parseDiff
13import kotlinx.serialization.builtins.ListSerializer
15class M6Test {
17 private fun <T> decode(s: kotlinx.serialization.KSerializer<T>, json: String): T =
18 OpencodeJson.decodeFromString(s, json)
20 // --- Agent / Command decoding ---
22 @Test
23 fun decodesAgentList() {
24 val json = """
25 [
26 { "name": "build", "description": "Build and implement code", "mode": "primary",
27 "builtIn": true, "tools": {}, "options": {} },
28 { "name": "custom", "mode": "subagent", "builtIn": false }
29 ]
30 """.trimIndent()
31 val agents = decode(ListSerializer(Agent.serializer()), json)
32 assertEquals(2, agents.size)
33 assertEquals("build", agents[0].name)
34 assertTrue(agents[0].isPrimary)
35 assertTrue(agents[0].builtIn)
36 assertEquals("Build and implement code", agents[0].displayDescription)
37 assertFalse(agents[1].isPrimary)
38 assertEquals("Custom agent", agents[1].displayDescription)
39 }
41 @Test
42 fun decodesCommandList() {
43 val json = """
44 [
45 { "name": "compact", "description": "Compact the session",
46 "template": "Summarize the conversation so far", "subtask": false },
47 { "name": "test", "agent": "build", "template": "Run the tests" }
48 ]
49 """.trimIndent()
50 val commands = decode(ListSerializer(Command.serializer()), json)
51 assertEquals(2, commands.size)
52 assertEquals("compact", commands[0].name)
53 assertEquals("Compact the session", commands[0].displayDescription)
54 assertEquals("build", commands[1].agent)
55 }
57 // --- Diff parsing ---
59 @Test
60 fun parsesUnifiedDiff() {
61 val diff = """
62 --- a/src/Main.kt
63 +++ b/src/Main.kt
64 @@ -10,3 +10,4 @@
65 fun main() {
66 - println("old")
67 + println("new")
68 + // added
69 }
70 """.trimIndent()
71 val lines = parseDiff(diff)
72 assertTrue(lines[0] is DiffLine.FileHeader)
73 assertTrue(lines[1] is DiffLine.FileHeader)
74 assertTrue(lines[2] is DiffLine.Hunk)
75 assertTrue(lines[3] is DiffLine.Context)
76 assertTrue(lines[4] is DiffLine.Remove)
77 assertTrue(lines[5] is DiffLine.Add)
78 assertTrue(lines[6] is DiffLine.Add)
79 assertTrue(lines[7] is DiffLine.Context)
80 assertEquals(" println(\"old\")", (lines[4] as DiffLine.Remove).text)
81 assertEquals(" println(\"new\")", (lines[5] as DiffLine.Add).text)
82 }
84 @Test
85 fun looksLikeDiffDetectsRealDiffs() {
86 val realDiff = """
87 @@ -1,2 +1,2 @@
88 -old line
89 +new line
90 """.trimIndent()
91 assertTrue(looksLikeDiff(realDiff))
93 val plainText = "This is just a regular message from the assistant."
94 assertFalse(looksLikeDiff(plainText))
95 }
97 @Test
98 fun looksLikeDiffIgnoresStrayMarkers() {
99 assertFalse(looksLikeDiff("not a diff at all"))
100 assertFalse(looksLikeDiff("just some + text - here"))
101 }