package com.correx.core.kernel.orchestration

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class FrameTruncatedToolResultTest {

    @Test
    fun `keeps header, head, tail and a marker naming the retrieval ref`() {
        val body = (1..300).joinToString("\n") { "line-$it" }
        val out = frameTruncatedToolResult("[grep exit=0]", body, "abc123")

        assertTrue(out.startsWith("[grep exit=0]"), out)
        assertTrue(out.contains("line-1\n"), "head lines kept")
        assertTrue(out.contains("line-300"), "tail lines kept")
        assertTrue(out.contains("output truncated (300 lines)"), out)
        assertTrue(out.contains("tool_output(ref=\"abc123\")"), out)
        // Middle is dropped — a line well inside the elided span must be absent.
        assertTrue(!out.contains("line-150"), "middle elided")
    }

    @Test
    fun `char-caps a single pathological long line so the bound is not defeated`() {
        val giant = "x".repeat(1_000_000)
        val out = frameTruncatedToolResult("[shell exit=0]", giant, "ref")
        // One line means head==tail==giant; each is char-capped, so output can't be ~2MB.
        assertTrue(out.length < 20_000, "length was ${out.length}")
        assertTrue(out.contains("tool_output(ref=\"ref\")"), out)
    }
}
