import Foundation
import Testing

@testable import NucleicCore

@Suite("Orchestra orchestration mode")
struct OrchestrationModeTests {
    @Test func detectsOrchestraSentinel() {
        #expect(OrchestrationMode.isOrchestra("orchestra"))
        #expect(OrchestrationMode.isOrchestra("  Orchestra  "))  // trimmed, case-insensitive
        #expect(!OrchestrationMode.isOrchestra("xhigh"))
        #expect(!OrchestrationMode.isOrchestra("high"))
        #expect(!OrchestrationMode.isOrchestra(nil))
    }

    @Test func stillRecognizesLegacyUltracodeSentinel() {
        // Orchestra was renamed from "Ultracode"; a session persisted under the old token must
        // still resolve to the mode, not be sent to a backend verbatim.
        #expect(OrchestrationMode.isOrchestra("ultracode"))
        #expect(OrchestrationMode.isOrchestra("  Ultracode  "))
        #expect(OrchestrationMode.resolvedEffort("ultracode") == "xhigh")
    }

    @Test func resolvesOrchestraToXhigh() {
        // The sentinel never reaches a backend verbatim — it maps to its real effort level.
        #expect(OrchestrationMode.resolvedEffort("orchestra") == "xhigh")
        #expect(OrchestrationMode.resolvedEffort("Orchestra") == "xhigh")
    }

    @Test func passesThroughRealEffortLevels() {
        for level in ["low", "medium", "high", "xhigh", "max"] {
            #expect(OrchestrationMode.resolvedEffort(level) == level)
        }
        #expect(OrchestrationMode.resolvedEffort(nil) == nil)
    }

    @Test func appendsConsentOnlyInOrchestra() {
        // Off: nothing to append when there's no environment guidance either.
        #expect(OrchestrationMode.combinedSystemPrompt(environment: nil, effort: "high") == nil)
        // On: the consent text is appended.
        let consentOnly = OrchestrationMode.combinedSystemPrompt(environment: nil, effort: "orchestra")
        #expect(consentOnly == OrchestrationMode.consentPrompt)
    }

    @Test func mergesEnvironmentGuidanceWithConsent() {
        let environment = "Build environment: you are running inside a Linux container."
        // Not orchestra → only the environment guidance survives.
        #expect(
            OrchestrationMode.combinedSystemPrompt(environment: environment, effort: "max") == environment)
        // Orchestra → both, environment first, consent as its own paragraph.
        let merged = OrchestrationMode.combinedSystemPrompt(environment: environment, effort: "orchestra")
        #expect(merged == environment + "\n\n" + OrchestrationMode.consentPrompt)
    }

    @Test func consentMentionsStandingFanOut() {
        // The contract that makes orchestra work: standing consent to fan out to subagents.
        let consent = OrchestrationMode.consentPrompt.lowercased()
        #expect(consent.contains("standing consent"))
        #expect(consent.contains("subagent"))
    }
}
