package com.correx.core.kernel.orchestration

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

class BriefEchoExtractorTest {

    @Test
    fun `parses a well-formed brief_echo block`() {
        val json = """
            {
              "brief_echo": {
                "referenced_files": ["core/kernel/Foo.kt", "core/kernel/Bar.kt"],
                "symbols": ["Foo"],
                "acceptance_criteria": ["must compile", "must pass tests"]
              }
            }
        """.trimIndent()
        val echo = BriefEchoExtractor.fromEcho(json)!!
        assertEquals(setOf("core/kernel/Foo.kt", "core/kernel/Bar.kt"), echo.referencedFiles)
        assertEquals(setOf("Foo"), echo.symbols)
        assertEquals(setOf("must compile", "must pass tests"), echo.acceptanceCriteria)
    }

    @Test
    fun `returns null when brief_echo key is absent`() {
        assertNull(BriefEchoExtractor.fromEcho("""{"summary": "do the thing"}"""))
    }

    @Test
    fun `tolerates missing dimensions in brief_echo`() {
        val echo = BriefEchoExtractor.fromEcho("""{"brief_echo": {"referenced_files": ["a/b/C.kt"]}}""")!!
        assertEquals(setOf("a/b/C.kt"), echo.referencedFiles)
        assertTrue(echo.symbols.isEmpty())
        assertTrue(echo.acceptanceCriteria.isEmpty())
    }

    @Test
    fun `original-brief extraction pulls referenced files and acceptance criteria`() {
        val json = """
            {
              "summary": "touch core/kernel/Foo.kt to satisfy the gate",
              "acceptance_criteria": ["must compile", "must pass tests"],
              "symbols": ["Foo"]
            }
        """.trimIndent()
        val original = BriefEchoExtractor.fromOriginalBrief(json)
        assertTrue(original.referencedFiles.contains("core/kernel/Foo.kt"))
        assertEquals(setOf("must compile", "must pass tests"), original.acceptanceCriteria)
        assertEquals(setOf("Foo"), original.symbols)
    }

    @Test
    fun `original-brief extraction tolerates missing acceptance_criteria and symbols`() {
        val original = BriefEchoExtractor.fromOriginalBrief("""{"affected": ["core/kernel/Foo.kt"]}""")
        assertTrue(original.referencedFiles.contains("core/kernel/Foo.kt"))
        assertTrue(original.acceptanceCriteria.isEmpty())
        assertTrue(original.symbols.isEmpty())
    }
}
