import Testing

@testable import NucleicCore

/// `Decision` → Codex app-server approval reply (ADAPTERS §2.4). The review-decision
/// enum (`accept`/`acceptForSession`/`decline`/`cancel`) is source-confirmed.
@Suite struct CodexDecisionMappingTests {
    @Test func reviewDecisionEnum() {
        #expect(CodexDecisionMapping.reviewDecision(for: .allow()) == "accept")
        #expect(CodexDecisionMapping.reviewDecision(for: .allowAlways(.session)) == "acceptForSession")
        #expect(CodexDecisionMapping.reviewDecision(for: .allowAlways(.toolName)) == "acceptForSession")
        #expect(CodexDecisionMapping.reviewDecision(for: .deny(reason: "no")) == "decline")
        #expect(CodexDecisionMapping.reviewDecision(for: .cancelRun) == "cancel")
    }

    @Test func reviewReplyShape() {
        #expect(CodexDecisionMapping.reviewReply(for: .allow()) == ["decision": "accept"])
        #expect(CodexDecisionMapping.reviewReply(for: .cancelRun) == ["decision": "cancel"])
    }

    @Test func legacyV1ReviewDecisionEnum() {
        // Deprecated execCommandApproval/applyPatchApproval use the snake_case ReviewDecision.
        #expect(CodexDecisionMapping.reviewDecisionV1(for: .allow()) == "approved")
        #expect(CodexDecisionMapping.reviewDecisionV1(for: .allowAlways(.session)) == "approved_for_session")
        #expect(CodexDecisionMapping.reviewDecisionV1(for: .deny(reason: nil)) == "denied")
        #expect(CodexDecisionMapping.reviewDecisionV1(for: .cancelRun) == "abort")
    }

    @Test func permissionsReplyGrantsRequestedOnAllow() {
        let requested: JSONValue = ["fileWrite": true, "network": false]
        let allow = CodexDecisionMapping.permissionsReply(for: .allow(), requestedPermissions: requested)
        #expect(allow["permissions"] == requested)
        #expect(allow["scope"]?.stringValue == "turn")

        let always = CodexDecisionMapping.permissionsReply(
            for: .allowAlways(.session), requestedPermissions: requested)
        #expect(always["scope"]?.stringValue == "session")
    }

    @Test func permissionsReplyGrantsNothingOnDeny() {
        let denied = CodexDecisionMapping.permissionsReply(
            for: .deny(reason: nil), requestedPermissions: ["fileWrite": true])
        #expect(denied["permissions"] == .object([:]))
    }
}
