<!DOCTYPE html>
<meta charset="utf-8">
<title>Tests for providing `total` as part of `ApplePayModifier`.</title>
<script src="/js-test-resources/ui-helper.js"></script>
<script src="/resources/payment-request.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
setup({ explicit_done: true, explicit_timeout: true });

const immediateLabel = "Immediate Label";
const immediateAmount = "20.00";
const recurringLabel = "Recurring Label";
const recurringAmount = "30.00";
const recurringPaymentStartDate = new Date(0);
const recurringPaymentIntervalUnit = "year";
const recurringPaymentIntervalCount = 10;
const recurringPaymentEndDate = new Date(1);
const deferredLabel = "Deferred Label";
const deferredPaymentDate = new Date(2);
const deferredAmount = "40.00";

function validPaymentDetailsWithModifierThatHasTotal(total) {
    let details = validPaymentDetails();
    if (total)
        details.modifiers = [ { supportedMethods: "https://apple.com/apple-pay", data: { total } } ];
    return details;
}

function testPaymentItemData(description, {initialTotal, expectedTotal}) {
    user_activation_test(async (test) => {
        let request = new PaymentRequest([validPaymentMethod()], validPaymentDetailsWithModifierThatHasTotal(initialTotal));
        request.addEventListener("merchantvalidation", (event) => {
            event.complete({ });
        });
        request.addEventListener("shippingaddresschange", (event) => {
            event.updateWith({ });
            internals.mockPaymentCoordinator.acceptPayment();
        });

        let response = await request.show();

        let actualTotal = internals.mockPaymentCoordinator.total;
        assert_equals(actualTotal.label, expectedTotal.label, "check that the `label` matches for total");
        assert_equals(actualTotal.amount, expectedTotal.amount, "check that the `amount` matches for total");
        assert_equals(actualTotal.paymentTiming, expectedTotal.paymentTiming, "check that the `paymentTiming` matches for total");
        assert_equals(actualTotal.recurringPaymentStartDate?.getTime(), expectedTotal.recurringPaymentStartDate?.getTime(), "check that the `recurringPaymentStartDate` matches for total");
        assert_equals(actualTotal.recurringPaymentIntervalUnit, expectedTotal.recurringPaymentIntervalUnit, "check that the `recurringPaymentIntervalUnit` matches for total");
        assert_equals(actualTotal.recurringPaymentIntervalCount, expectedTotal.recurringPaymentIntervalCount, "check that the `recurringPaymentIntervalCount` matches for total");
        assert_equals(actualTotal.recurringPaymentEndDate?.getTime(), expectedTotal.recurringPaymentEndDate?.getTime(), "check that the `recurringPaymentEndDate` matches for total");
        assert_equals(actualTotal.deferredPaymentDate?.getTime(), expectedTotal.deferredPaymentDate?.getTime(), "check that the `deferredPaymentDate` matches for total");

        await response.complete("success");
    }, description);
}

testPaymentItemData("The default value of `paymentTiming` should be `\"immediate\"`.", {
    initialTotal: undefined,
    expectedTotal: {label: "Total", amount: "10.00", paymentTiming: "immediate", recurringPaymentIntervalUnit: "month", recurringPaymentIntervalCount: 1},
});

testPaymentItemData("Should not have a default value for `recurringPaymentIntervalStartDate` and `recurringPaymentIntervalEndDate` when `paymentTiming` is `\"recurring\"`.", {
    initialTotal: {label: recurringLabel, amount: recurringAmount, paymentTiming: "recurring"},
    expectedTotal: {label: recurringLabel, amount: recurringAmount, paymentTiming: "recurring", recurringPaymentIntervalUnit: "month", recurringPaymentIntervalCount: 1},
});
testPaymentItemData("Should also propagate `recurringPaymentIntervalStartDate` and `recurringPaymentIntervalEndDate` if provided when `paymentTiming` is `\"recurring\"`.", {
    initialTotal: {label: recurringLabel, amount: recurringAmount, paymentTiming: "recurring", recurringPaymentStartDate, recurringPaymentIntervalUnit, recurringPaymentIntervalCount, recurringPaymentEndDate},
    expectedTotal: {label: recurringLabel, amount: recurringAmount, paymentTiming: "recurring", recurringPaymentStartDate, recurringPaymentIntervalUnit, recurringPaymentIntervalCount, recurringPaymentEndDate},
});

testPaymentItemData("Should not have a default value for `deferredPaymentDate` when `paymentTiming` is `\"deferred\"`.", {
    initialTotal: {label: deferredLabel, amount: deferredAmount, paymentTiming: "deferred"},
    expectedTotal: {label: deferredLabel, amount: deferredAmount, paymentTiming: "deferred", recurringPaymentIntervalUnit: "month", recurringPaymentIntervalCount: 1},
});
testPaymentItemData("Should also propagate `deferredPaymentDate` if provided when `paymentTiming` is `\"deferred\"`.", {
    initialTotal: {label: deferredLabel, amount: deferredAmount, paymentTiming: "deferred", deferredPaymentDate},
    expectedTotal: {label: deferredLabel, amount: deferredAmount, paymentTiming: "deferred", recurringPaymentIntervalUnit: "month", recurringPaymentIntervalCount: 1, deferredPaymentDate},
});

done();
</script>

