package prolog.builtins

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import prolog.ast.arithmetic.Integer
import prolog.ast.lists.List.Cons
import prolog.ast.lists.List.Empty
import prolog.ast.terms.Variable

class MetaOperatorsTests {
    @Test
    fun `ignore of failing goal succeeds`() {
        val goal = Member(Integer(4), Cons(Integer(1), Cons(Integer(2), Cons(Integer(3), Empty))))

        val result = Ignore(goal).satisfy(emptyMap()).toList()

        assertEquals(1, result.size, "Should return one result")
        assertTrue(result[0].isSuccess, "Result should be successful")
        assertTrue(result[0].getOrNull()!!.isEmpty(), "Expected empty substitutions")
    }

    @Test
    fun `ignore of succeeding goal returns first solution`() {
        val goal = Member(Variable("X"), Cons(Integer(1), Cons(Integer(2), Cons(Integer(3), Empty))))

        val result = Ignore(goal).satisfy(emptyMap()).toList()

        assertEquals(1, result.size, "Should return one result")
        assertTrue(result[0].isSuccess, "Result should be successful")
        val subs = result[0].getOrNull()!!
        assertEquals(Integer(1), subs[Variable("X")], "Expected first solution to be 1")
    }
}