package tech.easyflow.ai.share;

import org.junit.Assert;
import org.junit.Test;
import tech.easyflow.system.enums.ResourceAction;

import java.util.Date;

/**
 * {@link WorkflowSharePolicy} 测试。
 */
public class WorkflowSharePolicyTest {

    /**
     * 验证相同密钥生成稳定的 SHA-256 摘要。
     */
    @Test
    public void shouldHashShareKeyDeterministically() {
        String first = WorkflowSharePolicy.hashShareKey("share-key");
        String second = WorkflowSharePolicy.hashShareKey("share-key");

        Assert.assertEquals(first, second);
        Assert.assertEquals(64, first.length());
        Assert.assertNotEquals("share-key", first);
    }

    /**
     * 验证默认过期时间为创建时间后 30 分钟。
     */
    @Test
    public void shouldExpireThirtyMinutesAfterCreation() {
        Date createdAt = new Date(1_000L);

        Date expiresAt = WorkflowSharePolicy.defaultExpiresAt(createdAt);

        Assert.assertEquals(30 * 60 * 1_000L, expiresAt.getTime() - createdAt.getTime());
    }

    /**
     * 验证对话分享默认在创建七天后过期。
     */
    @Test
    public void shouldExpireChatShareSevenDaysAfterCreation() {
        Date createdAt = new Date(1_000L);

        Date expiresAt = WorkflowSharePolicy.defaultChatExpiresAt(createdAt);

        Assert.assertEquals(
                7L * 24L * 60L * 60L * 1_000L,
                expiresAt.getTime() - createdAt.getTime()
        );
    }

    /**
     * 验证分享授权仅覆盖编辑、运行和发布所需接口。
     */
    @Test
    public void shouldAllowOnlyCollaborativeWorkflowOperations() {
        Assert.assertTrue(WorkflowSharePolicy.isAllowedRequest(
                "GET",
                "/api/v1/workflow/detail",
                ResourceAction.READ
        ));
        Assert.assertTrue(WorkflowSharePolicy.isAllowedRequest(
                "GET",
                "/api/v1/workflow/designer/childWorkflow",
                ResourceAction.READ
        ));
        Assert.assertTrue(WorkflowSharePolicy.isAllowedRequest(
                "POST",
                "/api/v1/workflow/update",
                ResourceAction.MANAGE
        ));
        Assert.assertTrue(WorkflowSharePolicy.isAllowedRequest(
                "POST",
                "/api/v1/workflow/runAsync",
                ResourceAction.USE
        ));
        Assert.assertTrue(WorkflowSharePolicy.isAllowedRequest(
                "POST",
                "/api/v1/workflow/submitPublishApproval",
                ResourceAction.MANAGE
        ));
    }

    /**
     * 验证删除、下线和再次分享不在协作授权范围内。
     */
    @Test
    public void shouldRejectSensitiveWorkflowOperations() {
        Assert.assertFalse(WorkflowSharePolicy.isAllowedRequest(
                "POST",
                "/api/v1/workflow/submitDeleteApproval",
                ResourceAction.MANAGE
        ));
        Assert.assertFalse(WorkflowSharePolicy.isAllowedRequest(
                "POST",
                "/api/v1/workflow/submitOfflineApproval",
                ResourceAction.MANAGE
        ));
        Assert.assertFalse(WorkflowSharePolicy.isAllowedRequest(
                "POST",
                "/api/v1/workflowShare/url/create",
                ResourceAction.MANAGE
        ));
        Assert.assertFalse(WorkflowSharePolicy.isAllowedRequest(
                "GET",
                "/api/v1/workflow/update",
                ResourceAction.MANAGE
        ));
    }
}
