package com.correx.core.artifacts.kind

import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

class ArtifactKindRegistryTest {
    private val registry = DefaultArtifactKindRegistry()

    @Test
    fun `built-in kinds are registered`() {
        assertNotNull(registry.get("file_written"))
        assertNotNull(registry.get("process_result"))
    }

    @Test
    fun `get returns null for unknown kind`() {
        assertNull(registry.get("nonexistent"))
    }

    @Test
    fun `FileWrittenKind is not llmEmitted`() {
        assertEquals(false, registry.get("file_written")!!.llmEmitted)
    }

    @Test
    fun `ProcessResultKind is not llmEmitted`() {
        assertEquals(false, registry.get("process_result")!!.llmEmitted)
    }

    @Test
    fun `config-declared kind registers and exposes its declared schema`() {
        val schema = JsonSchema(
            type = "object",
            properties = mapOf("x" to JsonSchemaProperty(type = "string")),
            required = listOf("x"),
        )
        registry.register(ConfigArtifactKind(id = "custom", schema = schema, llmEmitted = true))

        val kind = registry.get("custom")
        assertNotNull(kind)
        assertEquals(true, kind!!.llmEmitted)
        assertEquals(schema, kind.deriveJsonSchema())
    }

    @Test
    fun `FileWrittenKind schema has required fields`() {
        val schema = FileWrittenKind.deriveJsonSchema()
        assertEquals("object", schema.type)
        assert("path" in schema.required)
        assert("content" in schema.required)
    }

    @Test
    fun `list returns all registered kinds`() {
        val ids = registry.list().map { it.id }.toSet()
        assert("file_written" in ids) { "expected file_written in $ids" }
        assert("process_result" in ids) { "expected process_result in $ids" }
        assertEquals(2, ids.size)
    }

    @Test
    fun `list reflects newly registered kind`() {
        val schema = JsonSchema(
            type = "object",
            properties = mapOf("x" to JsonSchemaProperty(type = "string")),
            required = listOf("x"),
        )
        registry.register(ConfigArtifactKind(id = "extra", schema = schema, llmEmitted = true))
        val ids = registry.list().map { it.id }.toSet()
        assert("extra" in ids) { "expected extra in $ids" }
        assertEquals(3, ids.size)
    }
}
