|
| 1 | +package agents_engine.generation |
| 2 | + |
| 3 | +import kotlin.test.Test |
| 4 | +import kotlin.test.assertEquals |
| 5 | +import kotlin.test.assertNotNull |
| 6 | +import kotlin.test.assertTrue |
| 7 | + |
| 8 | +// Tests for #937 — toLlmInput serializes typed agent input as JSON when the |
| 9 | +// type is @Generable; falls back to toString() for plain types; preserves |
| 10 | +// literal String passthrough so free-form prompts don't get JSON-quoted. |
| 11 | + |
| 12 | +@Generable("a single field") |
| 13 | +data class InputSingle(val v: String) |
| 14 | + |
| 15 | +@Generable("multiple primitive fields") |
| 16 | +data class InputMulti(val name: String, val count: Int, val active: Boolean) |
| 17 | + |
| 18 | +@Generable("contains a list") |
| 19 | +data class InputWithList(val tags: List<String>) |
| 20 | + |
| 21 | +@Generable("contains a nested generable") |
| 22 | +data class InputWithNested(val inner: InputSingle, val label: String) |
| 23 | + |
| 24 | +@Generable("sealed root") |
| 25 | +sealed interface Decision937 { |
| 26 | + @Generable("approved") |
| 27 | + data class Approved(val confidence: Double) : Decision937 |
| 28 | + |
| 29 | + @Generable("rejected") |
| 30 | + data class Rejected(val reason: String) : Decision937 |
| 31 | +} |
| 32 | + |
| 33 | +class PlainNonGenerable(val v: String) { |
| 34 | + override fun toString(): String = "PLAIN-$v" |
| 35 | +} |
| 36 | + |
| 37 | +class LlmInputSerializationTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `null serializes as JSON null literal`() { |
| 41 | + assertEquals("null", toLlmInput(null)) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `String passes through unchanged (no JSON quoting)`() { |
| 46 | + // Free-form prompts must not get wrapped in quotes — that would |
| 47 | + // change what the LLM sees from the user message. |
| 48 | + assertEquals("hello world", toLlmInput("hello world")) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `String with quotes and backslashes still passes through unchanged`() { |
| 53 | + // Top-level String is opaque; the agent passed it as-is, the LLM |
| 54 | + // sees it as-is. |
| 55 | + val s = "she said \"hi\" \\ then left" |
| 56 | + assertEquals(s, toLlmInput(s)) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `primitive Number renders as JSON literal`() { |
| 61 | + assertEquals("42", toLlmInput(42)) |
| 62 | + assertEquals("3.14", toLlmInput(3.14)) |
| 63 | + assertEquals("9999999999", toLlmInput(9_999_999_999L)) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `Boolean renders as JSON literal`() { |
| 68 | + assertEquals("true", toLlmInput(true)) |
| 69 | + assertEquals("false", toLlmInput(false)) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `Generable single-field data class serializes as JSON object`() { |
| 74 | + val out = toLlmInput(InputSingle("hello")) |
| 75 | + assertEquals("""{"v":"hello"}""", out) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `Generable multi-field data class serializes with each constructor param`() { |
| 80 | + val out = toLlmInput(InputMulti(name = "alice", count = 3, active = true)) |
| 81 | + // Field order follows constructor order. |
| 82 | + assertEquals("""{"name":"alice","count":3,"active":true}""", out) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `String fields inside Generable are JSON-escaped`() { |
| 87 | + val out = toLlmInput(InputSingle("she said \"hi\" then \\left")) |
| 88 | + assertEquals("""{"v":"she said \"hi\" then \\left"}""", out) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun `Generable with List field renders the list as JSON array`() { |
| 93 | + val out = toLlmInput(InputWithList(listOf("a", "b", "c"))) |
| 94 | + assertEquals("""{"tags":["a","b","c"]}""", out) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `Generable with nested Generable field recurses`() { |
| 99 | + val out = toLlmInput(InputWithNested(inner = InputSingle("inside"), label = "outer")) |
| 100 | + assertEquals("""{"inner":{"v":"inside"},"label":"outer"}""", out) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `Sealed Generable variant gets a type discriminator`() { |
| 105 | + val approved = toLlmInput(Decision937.Approved(0.92)) |
| 106 | + assertTrue( |
| 107 | + approved.contains("\"type\":\"Approved\""), |
| 108 | + "sealed-variant must include type discriminator: $approved", |
| 109 | + ) |
| 110 | + assertTrue(approved.contains("\"confidence\":0.92"), "must include field: $approved") |
| 111 | + |
| 112 | + val rejected = toLlmInput(Decision937.Rejected("not enough data")) |
| 113 | + assertTrue( |
| 114 | + rejected.contains("\"type\":\"Rejected\""), |
| 115 | + "sealed-variant must include type discriminator: $rejected", |
| 116 | + ) |
| 117 | + assertTrue(rejected.contains("\"reason\":\"not enough data\"")) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun `non-Generable plain class falls back to toString`() { |
| 122 | + val out = toLlmInput(PlainNonGenerable("xyz")) |
| 123 | + assertEquals("PLAIN-xyz", out) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun `top-level List of Strings renders as JSON array`() { |
| 128 | + val out = toLlmInput(listOf("a", "b", "c")) |
| 129 | + assertEquals("""["a","b","c"]""", out) |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun `top-level List of Generables renders as JSON array of objects`() { |
| 134 | + val out = toLlmInput(listOf(InputSingle("x"), InputSingle("y"))) |
| 135 | + assertEquals("""[{"v":"x"},{"v":"y"}]""", out) |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun `top-level Map renders as JSON object`() { |
| 140 | + val out = toLlmInput(mapOf("a" to 1, "b" to "two")) |
| 141 | + assertEquals("""{"a":1,"b":"two"}""", out) |
| 142 | + } |
| 143 | + |
| 144 | + // Round-trip: serialized output should re-parse via fromLlmOutput. |
| 145 | + @Test |
| 146 | + fun `round-trip toLlmInput then fromLlmOutput reconstructs the original`() { |
| 147 | + val original = InputMulti(name = "alice", count = 3, active = true) |
| 148 | + val json = toLlmInput(original) |
| 149 | + val reconstructed = InputMulti::class.fromLlmOutput(json) |
| 150 | + assertNotNull(reconstructed) |
| 151 | + assertEquals(original, reconstructed) |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + fun `round-trip works for sealed variant`() { |
| 156 | + val original: Decision937 = Decision937.Rejected("insufficient") |
| 157 | + val json = toLlmInput(original) |
| 158 | + val reconstructed = Decision937::class.fromLlmOutput(json) |
| 159 | + assertEquals(original, reconstructed) |
| 160 | + } |
| 161 | +} |
0 commit comments