package com.tianji.aigc.harness;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class ActionPolicyGuardTest {

    private final ActionPolicyGuard policyGuard = new ActionPolicyGuard();

    @Test
    void shouldExposeBusinessActionSchemas() {
        assertThat(ActionSchema.resolve("course.query")).contains(ActionSchema.COURSE_QUERY);
        assertThat(ActionSchema.resolve("course.search")).contains(ActionSchema.COURSE_SEARCH);
        assertThat(ActionSchema.resolve("order.preview")).contains(ActionSchema.ORDER_PREVIEW);
        assertThat(ActionSchema.resolve("knowledgeops.rag_query")).contains(ActionSchema.KNOWLEDGEOPS_RAG_QUERY);
        assertThat(ActionSchema.resolve("mcp.call")).contains(ActionSchema.MCP_CALL);
    }

    @Test
    void shouldAllowOrderPreviewOnlyWhenItDoesNotConfirmOrPay() {
        AgentAction action = new AgentAction(
                "order.preview",
                "BUY",
                "OrderTools.prePlaceOrder",
                "request-1",
                "session-1",
                10001L,
                Map.of("courseIds", List.of(1589905661084430337L))
        );

        ActionPolicyDecision decision = policyGuard.decide(action);

        assertThat(decision.allowed()).isTrue();
        assertThat(decision.reason()).contains("不执行支付");
    }

    @Test
    void shouldBlockPaymentAndAutoConfirmationActions() {
        AgentAction paymentAction = new AgentAction(
                "order.pay",
                "BUY",
                "OrderTools.pay",
                "request-1",
                "session-1",
                10001L,
                Map.of("courseIds", List.of(1L))
        );
        AgentAction autoConfirmAction = new AgentAction(
                "order.preview",
                "BUY",
                "OrderTools.prePlaceOrder",
                "request-1",
                "session-1",
                10001L,
                Map.of("courseIds", List.of(1L), "autoConfirm", true)
        );

        assertThat(policyGuard.decide(paymentAction).allowed()).isFalse();
        assertThat(policyGuard.decide(paymentAction).reason()).contains("只允许 order.preview");
        assertThat(policyGuard.decide(autoConfirmAction).allowed()).isFalse();
        assertThat(policyGuard.decide(autoConfirmAction).reason()).contains("禁止真实支付");
    }
}
