package com.ruoyi.app.utils.linepay;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class LinePaySignerTest {

    private final LinePaySigner signer = new LinePaySigner();

    @Test
    void signsExactUtf8PostBodyIncludingNonAsciiCharacters() {
        String signature = signer.sign("test-secret", "/v4/payments/request",
                "{\"amount\":100,\"currency\":\"TWD\",\"orderId\":\"订单-1\"}", "nonce-123");

        assertEquals("UVkesLuCS7osTAd/T/YMld8uDwCicfkBHoUtcq0xtXY=", signature);
    }

    @Test
    void signsFinalOrderedQueryStringForGet() {
        String signature = signer.sign("test-secret", "/v4/payments",
                "orderId=LP%2D001&fields=ORDER", "nonce-123");

        assertEquals("IYNy7LPc3wdkSMgu13sQNrhKbvmMNedLQ1/lEuo8Elo=", signature);
    }

    @Test
    void changingQueryOrderChangesSignature() {
        String first = signer.sign("test-secret", "/v4/payments", "a=1&b=2", "nonce-123");
        String second = signer.sign("test-secret", "/v4/payments", "b=2&a=1", "nonce-123");

        assertNotEquals(first, second);
    }

    @Test
    void emptyBodyAndFreshNonceArePartOfTheSignature() {
        String first = signer.sign("test-secret", "/v4/payments/request", "", "nonce-1");
        String second = signer.sign("test-secret", "/v4/payments/request", "", "nonce-2");

        assertNotEquals(first, second);
    }
}
