import com.correx.core.context.builder.DefaultContextPackBuilder
import com.correx.core.context.compression.DefaultContextCompressor
import com.correx.core.context.model.ContextEntry
import com.correx.core.context.model.ContextLayer
import com.correx.core.context.model.TokenBudget
import com.correx.core.events.types.ContextEntryId
import com.correx.core.events.types.ContextPackId
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.StageId
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

/**
 * The remaining-delta checklist (stage-termination design 2026-07-11) must survive the budget/dedup
 * passes — its whole point is to outlive the truncation that gives the model amnesia. It is pinned
 * via sourceType "remainingDelta" in [DefaultContextPackBuilder.neverDropSourceTypes].
 */
class RemainingDeltaPinningTest {

    private val builder = DefaultContextPackBuilder(DefaultContextCompressor())
    private val sessionId = SessionId("s1")
    private val stageId = StageId("stage-1")
    private val packId = ContextPackId("pack-1")

    @Test
    fun `remainingDelta entry survives a wildly over-budget build`() = kotlinx.coroutines.runBlocking {
        val checklist = ContextEntry(
            id = ContextEntryId("delta-1"),
            layer = ContextLayer.L1,
            content = "## Remaining to finish this stage\n- [ ] Foo.tsx — exports_default_component",
            sourceType = "remainingDelta",
            sourceId = "remaining-delta",
            tokenEstimate = 500,
        )
        // A large freeform entry that would be dropped to fit the tiny budget.
        val filler = ContextEntry(
            id = ContextEntryId("filler-1"),
            layer = ContextLayer.L2,
            content = "lorem ipsum ".repeat(200),
            sourceType = "toolResult",
            sourceId = "filler-1",
            tokenEstimate = 5000,
        )
        val pack = builder.build(packId, sessionId, stageId, listOf(filler, checklist), TokenBudget(limit = 10))
        val retained = pack.layers.values.flatten()
        assertTrue(
            retained.any { it.sourceType == "remainingDelta" },
            "remainingDelta checklist must be retained even when the budget is blown",
        )
        Unit
    }
}
