package com.correx.core.validation.artifact

import com.correx.core.artifacts.kind.JsonSchema
import com.correx.core.artifacts.kind.JsonSchemaProperty
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class ExecutionPlanSchemaValidationTest {

    private val schema = JsonSchema(
        type = "object",
        properties = mapOf(
            "goal" to JsonSchemaProperty(type = "string"),
            "stages" to JsonSchemaProperty(type = "array"),
            "edges" to JsonSchemaProperty(type = "array"),
        ),
        required = listOf("goal", "stages", "edges"),
        additionalProperties = true,
    )

    private fun errors(json: String) =
        JsonSchemaValidator.validate(schema, Json.parseToJsonElement(json))

    @Test
    fun `minimal valid execution plan passes`() {
        val plan = """
            {
              "goal": "g",
              "stages": [{"id": "s", "prompt": "p", "produces": "x"}],
              "edges": []
            }
        """.trimIndent()
        assertTrue(errors(plan).isEmpty())
    }

    @Test
    fun `plan missing stages is rejected`() {
        val plan = """{"goal": "g", "edges": []}"""
        assertTrue(errors(plan).any { it.contains("stages") })
    }
}
