package com.correx.core.kernel.orchestration

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

class BriefReferenceExtractorTest {

    @Test
    fun `extracts relative file paths from string arrays`() {
        val json = """
            {
              "summary": "touch the validator",
              "affected_areas": [
                "core/validation/JsonSchemaValidator.kt",
                "the validation pipeline",
                "core:validation module"
              ]
            }
        """.trimIndent()
        assertEquals(listOf("core/validation/JsonSchemaValidator.kt"), BriefReferenceExtractor.fileReferences(json))
    }

    @Test
    fun `strips surrounding prose punctuation`() {
        val json = """{"requirements":["see (core/kernel/Foo.kt), then bar.","ends here: apps/cli/Main.kt."]}"""
        assertEquals(
            listOf("core/kernel/Foo.kt", "apps/cli/Main.kt"),
            BriefReferenceExtractor.fileReferences(json),
        )
    }

    @Test
    fun `skips urls, absolute and parent paths, and bare names`() {
        val json = """
            {
              "notes": [
                "https://example.com/x.html",
                "/etc/passwd",
                "../escape/secret.kt",
                "StageConfig.kt",
                "just prose with no paths"
              ]
            }
        """.trimIndent()
        assertTrue(BriefReferenceExtractor.fileReferences(json).isEmpty())
    }

    @Test
    fun `dedupes and preserves first-seen order`() {
        val json = """{"a":["core/A.kt","docs/x.md"],"b":["core/A.kt"]}"""
        assertEquals(listOf("core/A.kt", "docs/x.md"), BriefReferenceExtractor.fileReferences(json))
    }

    @Test
    fun `keeps nested dotfile-directory paths`() {
        val json = """{"areas":[".github/workflows/ci.yml"]}"""
        assertEquals(listOf(".github/workflows/ci.yml"), BriefReferenceExtractor.fileReferences(json))
    }

    @Test
    fun `malformed json yields no references`() {
        assertTrue(BriefReferenceExtractor.fileReferences("not json at all").isEmpty())
    }
}
